]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/gpsies/GpsiesXmlHandler.java
3d801be5bc845c5e449c486626ef0837d89ba4d6
[GpsPrune.git] / tim / prune / function / gpsies / GpsiesXmlHandler.java
1 package tim.prune.function.gpsies;
2
3 import java.util.ArrayList;
4
5 import org.xml.sax.Attributes;
6 import org.xml.sax.SAXException;
7 import org.xml.sax.helpers.DefaultHandler;
8
9 /**
10  * XML handler for dealing with XML returned from gpsies.com
11  */
12 public class GpsiesXmlHandler extends DefaultHandler
13 {
14         private String _value = null;
15         private ArrayList<GpsiesTrack> _trackList = null;
16         private GpsiesTrack _track = null;
17
18
19         /**
20          * React to the start of an XML tag
21          */
22         public void startElement(String inUri, String inLocalName, String inTagName,
23                 Attributes inAttributes) throws SAXException
24         {
25                 if (inTagName.equals("tracks")) {
26                         _trackList = new ArrayList<GpsiesTrack>();
27                 }
28                 else if (inTagName.equals("track")) {
29                         _track = new GpsiesTrack();
30                 }
31                 _value = null;
32                 super.startElement(inUri, inLocalName, inTagName, inAttributes);
33         }
34
35         /**
36          * React to the end of an XML tag
37          */
38         public void endElement(String inUri, String inLocalName, String inTagName)
39         throws SAXException
40         {
41                 if (inTagName.equals("track")) {
42                         _trackList.add(_track);
43                 }
44                 else if (inTagName.equals("title")) {
45                         _track.setTrackName(_value);
46                 }
47                 else if (inTagName.equals("description")) {
48                         _track.setDescription(_value);
49                 }
50                 else if (inTagName.equals("fileId")) {
51                         _track.setWebUrl("http://gpsies.com/map.do?fileId=" + _value);
52                 }
53                 else if (inTagName.equals("trackLengthM")) {
54                         try {
55                                 _track.setLength(Double.parseDouble(_value));
56                         }
57                         catch (NumberFormatException nfe) {}
58                 }
59                 else if (inTagName.equals("downloadLink")) {
60                         _track.setDownloadLink(_value);
61                 }
62                 super.endElement(inUri, inLocalName, inTagName);
63         }
64
65         /**
66          * React to characters received inside tags
67          */
68         public void characters(char[] inCh, int inStart, int inLength)
69         throws SAXException
70         {
71                 String value = new String(inCh, inStart, inLength);
72                 _value = (_value==null?value:_value+value);
73                 super.characters(inCh, inStart, inLength);
74         }
75
76         /**
77          * @return the list of tracks
78          */
79         public ArrayList<GpsiesTrack> getTrackList()
80         {
81                 return _trackList;
82         }
83 }