1 package tim.prune.function;
3 import java.util.ArrayList;
5 import org.xml.sax.Attributes;
6 import org.xml.sax.SAXException;
7 import org.xml.sax.helpers.DefaultHandler;
9 import tim.prune.function.search.SearchResult;
12 * XML handler for dealing with XML returned from the geonames api,
13 * both from the search by name and search by location
15 public class GetWikipediaXmlHandler extends DefaultHandler
17 private String _value = null;
18 private ArrayList<SearchResult> _trackList = null;
19 private SearchResult _track = null;
20 private String _errorMessage = null;
24 * React to the start of an XML tag
26 public void startElement(String inUri, String inLocalName, String inTagName,
27 Attributes inAttributes) throws SAXException
29 if (inTagName.equals("geonames")) {
30 _trackList = new ArrayList<SearchResult>();
32 else if (inTagName.equals("entry")) {
33 _track = new SearchResult();
35 else if (inTagName.equals("status")) {
36 _errorMessage = inAttributes.getValue("message");
39 super.startElement(inUri, inLocalName, inTagName, inAttributes);
43 * React to the end of an XML tag
45 public void endElement(String inUri, String inLocalName, String inTagName)
48 if (inTagName.equals("entry")) {
50 _trackList.add(_track);
52 else if (inTagName.equals("title")) {
53 _track.setTrackName(_value);
55 else if (inTagName.equals("summary")) {
56 _track.setDescription(_value);
58 else if (inTagName.equals("lat")) {
59 _track.setLatitude(_value);
61 else if (inTagName.equals("lng")) {
62 _track.setLongitude(_value);
64 else if (inTagName.equals("distance")) {
66 _track.setLength(Double.parseDouble(_value) * 1000.0); // convert from km to m
68 catch (NumberFormatException nfe) {}
70 else if (inTagName.equals("wikipediaUrl")) {
71 _track.setWebUrl(_value.replaceFirst("http://", "https://"));
73 super.endElement(inUri, inLocalName, inTagName);
77 * React to characters received inside tags
79 public void characters(char[] inCh, int inStart, int inLength)
82 String value = new String(inCh, inStart, inLength);
83 _value = (_value==null?value:_value+value);
84 super.characters(inCh, inStart, inLength);
88 * @return the list of tracks
90 public ArrayList<SearchResult> getTrackList()
96 * @return error message, if any
98 public String getErrorMessage() {