]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/SearchOsmPoisXmlHandler.java
5683ece37da8f6aeec4dcf9ed250a2da2a3d2c42
[GpsPrune.git] / src / tim / prune / function / SearchOsmPoisXmlHandler.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 OSM Overpass api,
13  * specially for the OSM Poi service
14  */
15 public class SearchOsmPoisXmlHandler extends DefaultHandler
16 {
17         private ArrayList<SearchResult> _pointList = null;
18         private SearchResult _currPoint = 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("osm")) {
29                         _pointList = new ArrayList<SearchResult>();
30                 }
31                 else if (inTagName.equals("node"))
32                 {
33                         _currPoint = new SearchResult();
34                         _currPoint.setLatitude(inAttributes.getValue("lat"));
35                         _currPoint.setLongitude(inAttributes.getValue("lon"));
36                 }
37                 else if (inTagName.equals("tag") && _currPoint != null) {
38                         processTag(inAttributes);
39                 }
40                 super.startElement(inUri, inLocalName, inTagName, inAttributes);
41         }
42
43         /**
44          * @param inAttributes attributes to process
45          */
46         private void processTag(Attributes inAttributes)
47         {
48                 String key = inAttributes.getValue("k");
49                 if (key != null)
50                 {
51                         String value = inAttributes.getValue("v");
52                         if (key.equals("name"))
53                         {
54                                 _currPoint.setTrackName(value);
55                         }
56                         else if (key.equals("amenity") || key.equals("highway") || key.equals("railway"))
57                         {
58                                 _currPoint.setPointType(value);
59                         }
60                 }
61         }
62
63         /**
64          * React to the end of an XML tag
65          */
66         public void endElement(String inUri, String inLocalName, String inTagName)
67         throws SAXException
68         {
69                 if (inTagName.equals("node"))
70                 {
71                         // end of the entry
72                         if (_currPoint.getTrackName() != null && !_currPoint.getTrackName().equals(""))
73                         _pointList.add(_currPoint);
74                 }
75                 super.endElement(inUri, inLocalName, inTagName);
76         }
77
78         /**
79          * @return the list of points
80          */
81         public ArrayList<SearchResult> getPointList()
82         {
83                 return _pointList;
84         }
85
86         /**
87          * @return error message, if any
88          */
89         public String getErrorMessage() {
90                 return _errorMessage;
91         }
92 }