1 package tim.prune.load.xml;
3 import java.util.ArrayList;
4 import org.xml.sax.Attributes;
5 import org.xml.sax.SAXException;
7 import tim.prune.data.Field;
11 * Class for handling specifics of parsing Kml files
13 public class KmlHandler extends XmlHandler
15 private boolean _insidePlacemark = false;
16 private boolean _insideName = false;
17 private boolean _insideCoordinates = false;
18 private String _name = null;
19 private StringBuffer _coordinates = null;
20 private ArrayList<String[]> _pointList = new ArrayList<String[]>();
24 * Receive the start of a tag
25 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
27 public void startElement(String uri, String localName, String qName,
28 Attributes attributes) throws SAXException
30 String tagName = localName;
31 if (tagName == null || tagName.equals("")) {tagName = qName;}
32 if (tagName.equalsIgnoreCase("Placemark")) _insidePlacemark = true;
33 else if (tagName.equalsIgnoreCase("coordinates")) {_insideCoordinates = true; _coordinates = null;}
34 else if (tagName.equalsIgnoreCase("name")) {_insideName = true; _name = null;}
35 super.startElement(uri, localName, qName, attributes);
41 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
43 public void endElement(String uri, String localName, String qName)
46 String tagName = localName;
47 if (tagName == null || tagName.equals("")) {tagName = qName;}
48 if (tagName.equalsIgnoreCase("Placemark"))
51 _insidePlacemark = false;
53 else if (tagName.equalsIgnoreCase("coordinates")) _insideCoordinates = false;
54 else if (tagName.equalsIgnoreCase("name")) _insideName = false;
55 super.endElement(uri, localName, qName);
60 * Process character text (inside tags or between them)
61 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
63 public void characters(char[] ch, int start, int length)
66 if (_insidePlacemark && (_insideName || _insideCoordinates))
68 String value = new String(ch, start, length);
69 if (_insideName) {_name = value;}
70 else if (_insideCoordinates)
72 if (_coordinates == null)
74 _coordinates = new StringBuffer();
76 _coordinates.append(value);
79 super.characters(ch, start, length);
84 * Process a placemark entry, either a single waypoint or a whole track
86 private void processPlacemark()
88 if (_coordinates == null) return;
89 String allCoords = _coordinates.toString().trim();
90 String[] coordArray = allCoords.split("[ \n]");
91 int numPoints = coordArray.length;
94 // Add single waypoint to list
95 _pointList.add(makeStringArray(allCoords, _name));
97 else if (numPoints > 1)
99 // Add each of the unnamed track points to list
100 boolean firstPoint = true;
101 for (int p=0; p<numPoints; p++)
103 if (coordArray[p] != null && coordArray[p].trim().length()>3)
105 String[] pointArray = makeStringArray(coordArray[p], null);
106 if (firstPoint) {pointArray[4] = "1";} // start of segment flag
108 _pointList.add(pointArray);
116 * Construct the String array for the given coordinates and name
117 * @param inCoordinates coordinate string in Kml format
118 * @param inName name of waypoint, or null if track point
119 * @return String array for point
121 private static String[] makeStringArray(String inCoordinates, String inName)
123 String[] result = new String[5];
124 String[] values = inCoordinates.split(",");
125 if (values.length == 3) {System.arraycopy(values, 0, result, 0, 3);}
132 * @see tim.prune.load.xml.XmlHandler#getFieldArray()
134 public Field[] getFieldArray()
136 final Field[] fields = {Field.LONGITUDE, Field.LATITUDE, Field.ALTITUDE, Field.WAYPT_NAME, Field.NEW_SEGMENT};
142 * Return the parsed information as a 2d array
143 * @see tim.prune.load.xml.XmlHandler#getDataArray()
145 public String[][] getDataArray()
147 int numPoints = _pointList.size();
148 // construct data array
149 String[][] result = new String[numPoints][];
150 for (int i=0; i<numPoints; i++)
152 result[i] = _pointList.get(i);