]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/OpenCachingDeXmlHandler.java
b4b28d56bb4dd176b9784f7a673227a3dd7f8d1e
[GpsPrune.git] / src / tim / prune / function / OpenCachingDeXmlHandler.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 opencaching.de api
13  */
14 public class OpenCachingDeXmlHandler extends DefaultHandler
15 {
16         private String _value = null;
17         private ArrayList<SearchResult> _trackList = null;
18         private SearchResult _track = null;
19         private String _errorMessage = null;
20
21
22         /**
23          * React to the start of an XML tag
24          */
25         public void startElement(String inUri, String inLocalName, String inTagName,
26                 Attributes inAttributes) throws SAXException
27         {
28                 if (inTagName.equals("result")) {
29                         _trackList = new ArrayList<SearchResult>();
30                 }
31                 else if (inTagName.equals("cache"))
32                 {
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("cache"))
49                 {
50                         // end of the entry
51                         _trackList.add(_track);
52                 }
53                 else if (inTagName.equals("name")) {
54                         _track.setTrackName(_value);
55                 }
56                 else if (inTagName.equals("desc")) {
57                         _track.setDescription(_value);
58                 }
59                 else if (inTagName.equals("lat")) {
60                         _track.setLatitude(_value);
61                 }
62                 else if (inTagName.equals("lon")) {
63                         _track.setLongitude(_value);
64                 }
65                 else if (inTagName.equals("distance")) {
66                         try {
67                                 _track.setLength(Double.parseDouble(_value) * 1000.0); // convert from km to m
68                         }
69                         catch (NumberFormatException nfe) {}
70                 }
71                 else if (inTagName.equals("link")) {
72                         _track.setWebUrl(_value);
73                 }
74                 super.endElement(inUri, inLocalName, inTagName);
75         }
76
77         /**
78          * React to characters received inside tags
79          */
80         public void characters(char[] inCh, int inStart, int inLength)
81         throws SAXException
82         {
83                 String value = new String(inCh, inStart, inLength);
84                 _value = (_value==null?value:_value+value);
85                 super.characters(inCh, inStart, inLength);
86         }
87
88         /**
89          * @return the list of tracks
90          */
91         public ArrayList<SearchResult> getTrackList()
92         {
93                 return _trackList;
94         }
95
96         /**
97          * @return error message, if any
98          */
99         public String getErrorMessage() {
100                 return _errorMessage;
101         }
102 }