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