package tim.prune.load.xml; import java.util.ArrayList; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import tim.prune.data.Field; /** * Class for handling specifics of parsing Kml files */ public class KmlHandler extends XmlHandler { private boolean _insidePlacemark = false; private boolean _insideName = false; private boolean _insideCoordinates = false; private String _name = null; private StringBuffer _coordinates = null; private ArrayList _pointList = new ArrayList(); /** * Receive the start of a tag * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) */ public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName; if (tagName == null || tagName.equals("")) {tagName = qName;} if (tagName.equalsIgnoreCase("Placemark")) _insidePlacemark = true; else if (tagName.equalsIgnoreCase("coordinates")) {_insideCoordinates = true; _coordinates = null;} else if (tagName.equalsIgnoreCase("name")) {_insideName = true; _name = null;} super.startElement(uri, localName, qName, attributes); } /** * Process end tag * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String) */ public void endElement(String uri, String localName, String qName) throws SAXException { String tagName = localName; if (tagName == null || tagName.equals("")) {tagName = qName;} if (tagName.equalsIgnoreCase("Placemark")) { processPlacemark(); _insidePlacemark = false; } else if (tagName.equalsIgnoreCase("coordinates")) _insideCoordinates = false; else if (tagName.equalsIgnoreCase("name")) _insideName = false; super.endElement(uri, localName, qName); } /** * Process character text (inside tags or between them) * @see org.xml.sax.ContentHandler#characters(char[], int, int) */ public void characters(char[] ch, int start, int length) throws SAXException { if (_insidePlacemark && (_insideName || _insideCoordinates)) { String value = new String(ch, start, length); if (_insideName) {_name = value;} else if (_insideCoordinates) { if (_coordinates == null) { _coordinates = new StringBuffer(); } _coordinates.append(value); } } super.characters(ch, start, length); } /** * Process a placemark entry, either a single waypoint or a whole track */ private void processPlacemark() { if (_coordinates == null) return; String allCoords = _coordinates.toString(); String[] coordArray = allCoords.split("[ \n]"); int numPoints = coordArray.length; if (numPoints == 1) { // Add single waypoint to list _pointList.add(makeStringArray(allCoords, _name)); } else if (numPoints > 1) { // Add each of the unnamed track points to list for (int p=0; p