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(), _comment = 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);
61 _comment.setValue(null);
63 else if (tag.equals("ele")) {
64 _currentTag = _elevation;
66 else if (tag.equals("name"))
69 _currentTag = _pointName;
71 else if (_trackNum < 0)
73 _currentTag = _fileTitle;
76 _currentTag = _trackName;
79 else if (tag.equals("time")) {
82 else if (tag.equals("type")) {
85 else if (tag.equals("description") || tag.equals("desc")) {
86 _currentTag = _description;
88 else if (tag.equals("cmt")) {
89 _currentTag = _comment;
91 else if (tag.equals("link")) {
92 _link.setValue(attributes.getValue("href"));
94 else if (tag.equals("trkseg")) {
97 else if (tag.equals("trk"))
100 _trackName.setValue(null);
102 super.startElement(uri, localName, qName, attributes);
108 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
110 public void endElement(String uri, String localName, String qName)
113 String tag = qName.toLowerCase();
114 if (tag.equals("wpt") || tag.equals("trkpt") || tag.equals("rtept"))
117 _insidePoint = false;
122 super.endElement(uri, localName, qName);
127 * Process character text (inside tags or between them)
128 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
130 public void characters(char[] ch, int start, int length)
133 String value = new String(ch, start, length);
134 if (_currentTag != null) {
135 _currentTag.setValue(checkCharacters(_currentTag.getValue(), value));
137 super.characters(ch, start, length);
142 * Check to concatenate partially-received values, if necessary
143 * @param inVariable variable containing characters received until now
144 * @param inValue new value received
145 * @return concatenation
147 private static String checkCharacters(String inVariable, String inValue)
149 if (inVariable == null) {return inValue;}
150 return inVariable + inValue;
155 * Process a point, either a waypoint or track point
157 private void processPoint()
159 // Put the values into a String array matching the order in getFieldArray()
160 String[] values = new String[9];
161 values[0] = _latitude;
162 values[1] = _longitude;
163 values[2] = _elevation.getValue();
164 if (_insideWaypoint) {values[3] = _pointName.getValue();}
165 values[4] = _time.getValue();
166 if (_startSegment && !_insideWaypoint)
169 _startSegment = false;
171 values[6] = _type.getValue();
172 values[7] = _description.getValue();
173 values[8] = _comment.getValue();
174 _pointList.add(values);
175 _trackNameList.addPoint(_trackNum, _trackName.getValue(), _isTrackPoint);
176 _linkList.add(_link.getValue());
181 * @see tim.prune.load.xml.XmlHandler#getFieldArray()
183 public Field[] getFieldArray()
185 final Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE,
186 Field.WAYPT_NAME, Field.TIMESTAMP, Field.NEW_SEGMENT,
187 Field.WAYPT_TYPE, Field.DESCRIPTION, Field.COMMENT};
193 * Return the parsed information as a 2d array
194 * @see tim.prune.load.xml.XmlHandler#getDataArray()
196 public String[][] getDataArray()
198 int numPoints = _pointList.size();
199 // construct data array
200 String[][] result = new String[numPoints][];
201 for (int i=0; i<numPoints; i++)
203 result[i] = _pointList.get(i);
209 * @return array of links, or null if none
211 public String[] getLinkArray()
213 int numPoints = _linkList.size();
214 boolean hasLink = false;
215 String[] result = new String[numPoints];
216 for (int i=0; i<numPoints; i++)
218 result[i] = _linkList.get(i);
219 if (result[i] != null) {hasLink = true;}
221 if (!hasLink) {result = null;}
226 * @return track name list
228 public TrackNameList getTrackNameList() {
229 return _trackNameList;
235 public String getFileTitle() {
236 return _fileTitle.getValue();