]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/GetWikipediaXmlHandler.java
f70e5f710da6f8cb5e7304af7c3d8edfe3f1f571
[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 the geonames api
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         private String _errorMessage = null;
21
22
23         /**
24          * React to the start of an XML tag
25          */
26         public void startElement(String inUri, String inLocalName, String inTagName,
27                 Attributes inAttributes) throws SAXException
28         {
29                 if (inTagName.equals("geonames")) {
30                         _trackList = new ArrayList<GpsiesTrack>();
31                 }
32                 else if (inTagName.equals("entry")) {
33                         _track = new GpsiesTrack();
34                         _lat = null;
35                         _lon = null;
36                 }
37                 else if (inTagName.equals("status")) {
38                         _errorMessage = inAttributes.getValue("message");
39                 }
40                 else _value = null;
41                 super.startElement(inUri, inLocalName, inTagName, inAttributes);
42         }
43
44         /**
45          * React to the end of an XML tag
46          */
47         public void endElement(String inUri, String inLocalName, String inTagName)
48         throws SAXException
49         {
50                 if (inTagName.equals("entry")) {
51                         // end of the entry
52                         _track.setDownloadLink(_lat + "," + _lon);
53                         _trackList.add(_track);
54                 }
55                 else if (inTagName.equals("title")) {
56                         _track.setTrackName(_value);
57                 }
58                 else if (inTagName.equals("summary")) {
59                         _track.setDescription(_value);
60                 }
61                 else if (inTagName.equals("lat")) {
62                         _lat = _value;
63                 }
64                 else if (inTagName.equals("lng")) {
65                         _lon = _value;
66                 }
67                 else if (inTagName.equals("distance")) {
68                         try {
69                                 _track.setLength(Double.parseDouble(_value) * 1000.0); // convert from km to m
70                         }
71                         catch (NumberFormatException nfe) {}
72                 }
73                 else if (inTagName.equals("wikipediaUrl")) {
74                         _track.setWebUrl(_value.replaceFirst("http://", "https://"));
75                 }
76                 super.endElement(inUri, inLocalName, inTagName);
77         }
78
79         /**
80          * React to characters received inside tags
81          */
82         public void characters(char[] inCh, int inStart, int inLength)
83         throws SAXException
84         {
85                 String value = new String(inCh, inStart, inLength);
86                 _value = (_value==null?value:_value+value);
87                 super.characters(inCh, inStart, inLength);
88         }
89
90         /**
91          * @return the list of tracks
92          */
93         public ArrayList<GpsiesTrack> getTrackList()
94         {
95                 return _trackList;
96         }
97
98         /**
99          * @return error message, if any
100          */
101         public String getErrorMessage() {
102                 return _errorMessage;
103         }
104 }