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.gpsies.GpsiesTrack;
12 * XML handler for dealing with XML returned from the geonames api
14 public class GetWikipediaXmlHandler extends DefaultHandler
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;
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<GpsiesTrack>();
32 else if (inTagName.equals("entry")) {
33 _track = new GpsiesTrack();
37 else if (inTagName.equals("status")) {
38 _errorMessage = inAttributes.getValue("message");
41 super.startElement(inUri, inLocalName, inTagName, inAttributes);
45 * React to the end of an XML tag
47 public void endElement(String inUri, String inLocalName, String inTagName)
50 if (inTagName.equals("entry")) {
52 _track.setDownloadLink(_lat + "," + _lon);
53 _trackList.add(_track);
55 else if (inTagName.equals("title")) {
56 _track.setTrackName(_value);
58 else if (inTagName.equals("summary")) {
59 _track.setDescription(_value);
61 else if (inTagName.equals("lat")) {
64 else if (inTagName.equals("lng")) {
67 else if (inTagName.equals("distance")) {
69 _track.setLength(Double.parseDouble(_value) * 1000.0); // convert from km to m
71 catch (NumberFormatException nfe) {}
73 else if (inTagName.equals("wikipediaUrl")) {
74 _track.setWebUrl(_value.replaceFirst("http://", "https://"));
76 super.endElement(inUri, inLocalName, inTagName);
80 * React to characters received inside tags
82 public void characters(char[] inCh, int inStart, int inLength)
85 String value = new String(inCh, inStart, inLength);
86 _value = (_value==null?value:_value+value);
87 super.characters(inCh, inStart, inLength);
91 * @return the list of tracks
93 public ArrayList<GpsiesTrack> getTrackList()
99 * @return error message, if any
101 public String getErrorMessage() {
102 return _errorMessage;