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 _name = new GpxTag(), _trackName = new GpxTag();
23 private String _latitude = null, _longitude = null;
24 private GpxTag _elevation = new GpxTag(), _time = new GpxTag();
25 private GpxTag _type = new GpxTag(), _description = new GpxTag();
26 private GpxTag _link = new GpxTag();
27 private GpxTag _currentTag = null;
28 private ArrayList<String[]> _pointList = new ArrayList<String[]>();
29 private ArrayList<String> _linkList = new ArrayList<String>();
30 private TrackNameList _trackNameList = new TrackNameList();
34 * Receive the start of a tag
35 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
37 public void startElement(String uri, String localName, String qName,
38 Attributes attributes) throws SAXException
40 // Read the parameters for waypoints and track points
41 String tag = qName.toLowerCase();
42 if (tag.equals("wpt") || tag.equals("trkpt") || tag.equals("rtept"))
45 _insideWaypoint = tag.equals("wpt");
46 _isTrackPoint = tag.equals("trkpt");
47 final int numAttributes = attributes.getLength();
48 for (int i=0; i<numAttributes; i++)
50 String att = attributes.getQName(i).toLowerCase();
51 if (att.equals("lat")) {_latitude = attributes.getValue(i);}
52 else if (att.equals("lon")) {_longitude = attributes.getValue(i);}
54 _elevation.setValue(null);
59 _description.setValue(null);
61 else if (tag.equals("ele")) {
62 _currentTag = _elevation;
64 else if (tag.equals("name")) {
65 _currentTag = (_insidePoint?_name:_trackName);
67 else if (tag.equals("time")) {
70 else if (tag.equals("type")) {
73 else if (tag.equals("description")) {
74 _currentTag = _description;
76 else if (tag.equals("link")) {
77 _link.setValue(attributes.getValue("href"));
79 else if (tag.equals("trkseg")) {
82 else if (tag.equals("trk"))
85 _trackName.setValue(null);
87 super.startElement(uri, localName, qName, attributes);
93 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
95 public void endElement(String uri, String localName, String qName)
98 String tag = qName.toLowerCase();
99 if (tag.equals("wpt") || tag.equals("trkpt") || tag.equals("rtept"))
102 _insidePoint = false;
107 super.endElement(uri, localName, qName);
112 * Process character text (inside tags or between them)
113 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
115 public void characters(char[] ch, int start, int length)
118 String value = new String(ch, start, length);
119 if (_currentTag != null) {
120 _currentTag.setValue(checkCharacters(_currentTag.getValue(), value));
122 super.characters(ch, start, length);
127 * Check to concatenate partially-received values, if necessary
128 * @param inVariable variable containing characters received until now
129 * @param inValue new value received
130 * @return concatenation
132 private static String checkCharacters(String inVariable, String inValue)
134 if (inVariable == null) {return inValue;}
135 return inVariable + inValue;
140 * Process a point, either a waypoint or track point
142 private void processPoint()
144 // Put the values into a String array matching the order in getFieldArray()
145 String[] values = new String[8];
146 values[0] = _latitude;
147 values[1] = _longitude;
148 values[2] = _elevation.getValue();
149 if (_insideWaypoint) {values[3] = _name.getValue();}
150 values[4] = _time.getValue();
151 if (_startSegment && !_insideWaypoint)
154 _startSegment = false;
156 values[6] = _type.getValue();
157 values[7] = _description.getValue();
158 _pointList.add(values);
159 _trackNameList.addPoint(_trackNum, _trackName.getValue(), _isTrackPoint);
160 _linkList.add(_link.getValue());
165 * @see tim.prune.load.xml.XmlHandler#getFieldArray()
167 public Field[] getFieldArray()
169 final Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE,
170 Field.WAYPT_NAME, Field.TIMESTAMP, Field.NEW_SEGMENT,
171 Field.WAYPT_TYPE, Field.DESCRIPTION};
177 * Return the parsed information as a 2d array
178 * @see tim.prune.load.xml.XmlHandler#getDataArray()
180 public String[][] getDataArray()
182 int numPoints = _pointList.size();
183 // construct data array
184 String[][] result = new String[numPoints][];
185 for (int i=0; i<numPoints; i++)
187 result[i] = _pointList.get(i);
193 * @return array of links, or null if none
195 public String[] getLinkArray()
197 int numPoints = _linkList.size();
198 boolean hasLink = false;
199 String[] result = new String[numPoints];
200 for (int i=0; i<numPoints; i++)
202 result[i] = _linkList.get(i);
203 if (result[i] != null) {hasLink = true;}
205 if (!hasLink) {result = null;}
210 * @return track name list
212 public TrackNameList getTrackNameList() {
213 return _trackNameList;