1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
package tim.prune.function;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import tim.prune.function.search.SearchResult;
/**
* XML handler for dealing with XML returned from the OSM Overpass api,
* specially for the OSM Poi service
*/
public class SearchOsmPoisXmlHandler extends DefaultHandler
{
private ArrayList<SearchResult> _pointList = null;
private SearchResult _currPoint = null;
private String _errorMessage = null;
/**
* React to the start of an XML tag
*/
public void startElement(String inUri, String inLocalName, String inTagName,
Attributes inAttributes) throws SAXException
{
if (inTagName.equals("osm")) {
_pointList = new ArrayList<SearchResult>();
}
else if (inTagName.equals("node"))
{
_currPoint = new SearchResult();
_currPoint.setLatitude(inAttributes.getValue("lat"));
_currPoint.setLongitude(inAttributes.getValue("lon"));
}
else if (inTagName.equals("tag") && _currPoint != null) {
processTag(inAttributes);
}
super.startElement(inUri, inLocalName, inTagName, inAttributes);
}
/**
* @param inAttributes attributes to process
*/
private void processTag(Attributes inAttributes)
{
String key = inAttributes.getValue("k");
if (key != null)
{
String value = inAttributes.getValue("v");
if (key.equals("name"))
{
_currPoint.setTrackName(value);
}
else if (key.equals("amenity") || key.equals("highway") || key.equals("railway"))
{
_currPoint.setPointType(value);
}
}
}
/**
* React to the end of an XML tag
*/
public void endElement(String inUri, String inLocalName, String inTagName)
throws SAXException
{
if (inTagName.equals("node"))
{
// end of the entry
if (_currPoint.getTrackName() != null && !_currPoint.getTrackName().equals(""))
_pointList.add(_currPoint);
}
super.endElement(inUri, inLocalName, inTagName);
}
/**
* @return the list of points
*/
public ArrayList<SearchResult> getPointList()
{
return _pointList;
}
/**
* @return error message, if any
*/
public String getErrorMessage() {
return _errorMessage;
}
}
|