]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/GetWikipediaXmlHandler.java
b5cb3f920f7533de8474dbf7e4466c1a78b4ba82
[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.search.SearchResult;
10
11 /**
12  * XML handler for dealing with XML returned from the geonames api,
13  * both from the search by name and search by location
14  */
15 public class GetWikipediaXmlHandler extends DefaultHandler
16 {
17         private String _value = null;
18         private ArrayList<SearchResult> _trackList = null;
19         private SearchResult _track = 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<SearchResult>();
31                 }
32                 else if (inTagName.equals("entry")) {
33                         _track = new SearchResult();
34                 }
35                 else if (inTagName.equals("status")) {
36                         _errorMessage = inAttributes.getValue("message");
37                 }
38                 else _value = null;
39                 super.startElement(inUri, inLocalName, inTagName, inAttributes);
40         }
41
42         /**
43          * React to the end of an XML tag
44          */
45         public void endElement(String inUri, String inLocalName, String inTagName)
46         throws SAXException
47         {
48                 if (inTagName.equals("entry")) {
49                         // end of the entry
50                         _trackList.add(_track);
51                 }
52                 else if (inTagName.equals("title")) {
53                         _track.setTrackName(_value);
54                 }
55                 else if (inTagName.equals("summary")) {
56                         _track.setDescription(_value);
57                 }
58                 else if (inTagName.equals("lat")) {
59                         _track.setLatitude(_value);
60                 }
61                 else if (inTagName.equals("lng")) {
62                         _track.setLongitude(_value);
63                 }
64                 else if (inTagName.equals("distance")) {
65                         try {
66                                 _track.setLength(Double.parseDouble(_value) * 1000.0); // convert from km to m
67                         }
68                         catch (NumberFormatException nfe) {}
69                 }
70                 else if (inTagName.equals("wikipediaUrl")) {
71                         _track.setWebUrl(_value.replaceFirst("http://", "https://"));
72                 }
73                 super.endElement(inUri, inLocalName, inTagName);
74         }
75
76         /**
77          * React to characters received inside tags
78          */
79         public void characters(char[] inCh, int inStart, int inLength)
80         throws SAXException
81         {
82                 String value = new String(inCh, inStart, inLength);
83                 _value = (_value==null?value:_value+value);
84                 super.characters(inCh, inStart, inLength);
85         }
86
87         /**
88          * @return the list of tracks
89          */
90         public ArrayList<SearchResult> getTrackList()
91         {
92                 return _trackList;
93         }
94
95         /**
96          * @return error message, if any
97          */
98         public String getErrorMessage() {
99                 return _errorMessage;
100         }
101 }