1 package tim.prune.load.xml;
3 import java.util.ArrayList;
5 import org.xml.sax.Attributes;
6 import org.xml.sax.SAXException;
8 import tim.prune.data.Field;
9 import tim.prune.load.TrackNameList;
13 * Class for handling specifics of parsing Gpx files
15 public class GpxHandler extends XmlHandler
17 private boolean _insidePoint = false;
18 private boolean _insideWaypoint = false;
19 private boolean _startSegment = true;
20 private boolean _isTrackPoint = false;
21 private int _trackNum = -1;
22 private GpxTag _fileTitle = new GpxTag();
23 private GpxTag _pointName = new GpxTag(), _trackName = new GpxTag();
24 private String _latitude = null, _longitude = null;
25 private GpxTag _elevation = new GpxTag(), _time = new GpxTag();
26 private GpxTag _type = new GpxTag(), _description = new GpxTag();
27 private GpxTag _link = new GpxTag();
28 private GpxTag _currentTag = null;
29 private ArrayList<String[]> _pointList = new ArrayList<String[]>();
30 private ArrayList<String> _linkList = new ArrayList<String>();
31 private TrackNameList _trackNameList = new TrackNameList();
35 * Receive the start of a tag
36 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
38 public void startElement(String uri, String localName, String qName,
39 Attributes attributes) throws SAXException
41 // Read the parameters for waypoints and track points
42 String tag = qName.toLowerCase();
43 if (tag.equals("wpt") || tag.equals("trkpt") || tag.equals("rtept"))
46 _insideWaypoint = tag.equals("wpt");
47 _isTrackPoint = tag.equals("trkpt");
48 final int numAttributes = attributes.getLength();
49 for (int i=0; i<numAttributes; i++)
51 String att = attributes.getQName(i).toLowerCase();
52 if (att.equals("lat")) {_latitude = attributes.getValue(i);}
53 else if (att.equals("lon")) {_longitude = attributes.getValue(i);}
55 _elevation.setValue(null);
56 _pointName.setValue(null);
60 _description.setValue(null);
62 else if (tag.equals("ele")) {
63 _currentTag = _elevation;
65 else if (tag.equals("name"))
68 _currentTag = _pointName;
70 else if (_trackNum < 0)
72 _currentTag = _fileTitle;
75 _currentTag = _trackName;
78 else if (tag.equals("time")) {
81 else if (tag.equals("type")) {
84 else if (tag.equals("description") || tag.equals("desc")) {
85 _currentTag = _description;
87 else if (tag.equals("link")) {
88 _link.setValue(attributes.getValue("href"));
90 else if (tag.equals("trkseg")) {
93 else if (tag.equals("trk"))
96 _trackName.setValue(null);
98 super.startElement(uri, localName, qName, attributes);
104 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
106 public void endElement(String uri, String localName, String qName)
109 String tag = qName.toLowerCase();
110 if (tag.equals("wpt") || tag.equals("trkpt") || tag.equals("rtept"))
113 _insidePoint = false;
118 super.endElement(uri, localName, qName);
123 * Process character text (inside tags or between them)
124 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
126 public void characters(char[] ch, int start, int length)
129 String value = new String(ch, start, length);
130 if (_currentTag != null) {
131 _currentTag.setValue(checkCharacters(_currentTag.getValue(), value));
133 super.characters(ch, start, length);
138 * Check to concatenate partially-received values, if necessary
139 * @param inVariable variable containing characters received until now
140 * @param inValue new value received
141 * @return concatenation
143 private static String checkCharacters(String inVariable, String inValue)
145 if (inVariable == null) {return inValue;}
146 return inVariable + inValue;
151 * Process a point, either a waypoint or track point
153 private void processPoint()
155 // Put the values into a String array matching the order in getFieldArray()
156 String[] values = new String[8];
157 values[0] = _latitude;
158 values[1] = _longitude;
159 values[2] = _elevation.getValue();
160 if (_insideWaypoint) {values[3] = _pointName.getValue();}
161 values[4] = _time.getValue();
162 if (_startSegment && !_insideWaypoint)
165 _startSegment = false;
167 values[6] = _type.getValue();
168 values[7] = _description.getValue();
169 _pointList.add(values);
170 _trackNameList.addPoint(_trackNum, _trackName.getValue(), _isTrackPoint);
171 _linkList.add(_link.getValue());
176 * @see tim.prune.load.xml.XmlHandler#getFieldArray()
178 public Field[] getFieldArray()
180 final Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE,
181 Field.WAYPT_NAME, Field.TIMESTAMP, Field.NEW_SEGMENT,
182 Field.WAYPT_TYPE, Field.DESCRIPTION};
188 * Return the parsed information as a 2d array
189 * @see tim.prune.load.xml.XmlHandler#getDataArray()
191 public String[][] getDataArray()
193 int numPoints = _pointList.size();
194 // construct data array
195 String[][] result = new String[numPoints][];
196 for (int i=0; i<numPoints; i++)
198 result[i] = _pointList.get(i);
204 * @return array of links, or null if none
206 public String[] getLinkArray()
208 int numPoints = _linkList.size();
209 boolean hasLink = false;
210 String[] result = new String[numPoints];
211 for (int i=0; i<numPoints; i++)
213 result[i] = _linkList.get(i);
214 if (result[i] != null) {hasLink = true;}
216 if (!hasLink) {result = null;}
221 * @return track name list
223 public TrackNameList getTrackNameList() {
224 return _trackNameList;
230 public String getFileTitle() {
231 return _fileTitle.getValue();