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 opencaching.de api
14 public class OpenCachingDeXmlHandler extends DefaultHandler
16 private String _value = null;
17 private ArrayList<SearchResult> _trackList = null;
18 private SearchResult _track = null;
19 private String _errorMessage = null;
23 * React to the start of an XML tag
25 public void startElement(String inUri, String inLocalName, String inTagName,
26 Attributes inAttributes) throws SAXException
28 if (inTagName.equals("result")) {
29 _trackList = new ArrayList<SearchResult>();
31 else if (inTagName.equals("cache"))
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("cache"))
51 _trackList.add(_track);
53 else if (inTagName.equals("name")) {
54 _track.setTrackName(_value);
56 else if (inTagName.equals("desc")) {
57 _track.setDescription(_value);
59 else if (inTagName.equals("lat")) {
60 _track.setLatitude(_value);
62 else if (inTagName.equals("lon")) {
63 _track.setLongitude(_value);
65 else if (inTagName.equals("distance")) {
67 _track.setLength(Double.parseDouble(_value) * 1000.0); // convert from km to m
69 catch (NumberFormatException nfe) {}
71 else if (inTagName.equals("link")) {
72 _track.setWebUrl(_value);
74 super.endElement(inUri, inLocalName, inTagName);
78 * React to characters received inside tags
80 public void characters(char[] inCh, int inStart, int inLength)
83 String value = new String(inCh, inStart, inLength);
84 _value = (_value==null?value:_value+value);
85 super.characters(inCh, inStart, inLength);
89 * @return the list of tracks
91 public ArrayList<SearchResult> getTrackList()
97 * @return error message, if any
99 public String getErrorMessage() {
100 return _errorMessage;