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