]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/xml/GpxHandler.java
Version 11, August 2010
[GpsPrune.git] / tim / prune / load / xml / GpxHandler.java
1 package tim.prune.load.xml;
2
3 import java.util.ArrayList;
4
5 import org.xml.sax.Attributes;
6 import org.xml.sax.SAXException;
7
8 import tim.prune.data.Field;
9 import tim.prune.load.TrackNameList;
10
11
12 /**
13  * Class for handling specifics of parsing Gpx files
14  */
15 public class GpxHandler extends XmlHandler
16 {
17         private boolean _insidePoint = false;
18         private boolean _insideWaypoint = false;
19         private boolean _insideName = false;
20         private boolean _insideElevation = false;
21         private boolean _insideTime = false;
22         private boolean _insideType = false;
23         private boolean _startSegment = true;
24         private boolean _isTrackPoint = false;
25         private int _trackNum = -1;
26         private String _trackName = null;
27         private String _name = null, _latitude = null, _longitude = null;
28         private String _elevation = null;
29         private String _time = null;
30         private String _type = null;
31         private ArrayList<String[]> _pointList = new ArrayList<String[]>();
32         private TrackNameList _trackNameList = new TrackNameList();
33
34
35         /**
36          * Receive the start of a tag
37          * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
38          */
39         public void startElement(String uri, String localName, String qName,
40                 Attributes attributes) throws SAXException
41         {
42                 // Read the parameters for waypoints and track points
43                 if (qName.equalsIgnoreCase("wpt") || qName.equalsIgnoreCase("trkpt") || qName.equalsIgnoreCase("rtept"))
44                 {
45                         _insidePoint = true;
46                         _insideWaypoint = qName.equalsIgnoreCase("wpt");
47                         _isTrackPoint = qName.equalsIgnoreCase("trkpt");
48                         int numAttributes = attributes.getLength();
49                         for (int i=0; i<numAttributes; i++)
50                         {
51                                 String att = attributes.getQName(i);
52                                 if (att.equals("lat")) {_latitude = attributes.getValue(i);}
53                                 else if (att.equals("lon")) {_longitude = attributes.getValue(i);}
54                         }
55                         _elevation = null;
56                         _name = null;
57                         _time = null;
58                         _type = null;
59                 }
60                 else if (qName.equalsIgnoreCase("ele"))
61                 {
62                         _insideElevation = true;
63                 }
64                 else if (qName.equalsIgnoreCase("name"))
65                 {
66                         _name = null;
67                         _insideName = true;
68                 }
69                 else if (qName.equalsIgnoreCase("time"))
70                 {
71                         _insideTime = true;
72                 }
73                 else if (qName.equalsIgnoreCase("type"))
74                 {
75                         _insideType = true;
76                 }
77                 else if (qName.equalsIgnoreCase("trkseg"))
78                 {
79                         _startSegment = true;
80                 }
81                 else if (qName.equalsIgnoreCase("trk"))
82                 {
83                         _trackNum++;
84                         _trackName = null;
85                 }
86                 super.startElement(uri, localName, qName, attributes);
87         }
88
89
90         /**
91          * Process end tag
92          * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
93          */
94         public void endElement(String uri, String localName, String qName)
95                 throws SAXException
96         {
97                 if (qName.equalsIgnoreCase("wpt") || qName.equalsIgnoreCase("trkpt") || qName.equalsIgnoreCase("rtept"))
98                 {
99                         processPoint();
100                         _insidePoint = false;
101                 }
102                 else if (qName.equalsIgnoreCase("ele"))
103                 {
104                         _insideElevation = false;
105                 }
106                 else if (qName.equalsIgnoreCase("name"))
107                 {
108                         _insideName = false;
109                 }
110                 else if (qName.equalsIgnoreCase("time"))
111                 {
112                         _insideTime = false;
113                 }
114                 else if (qName.equalsIgnoreCase("type"))
115                 {
116                         _insideType = false;
117                 }
118                 super.endElement(uri, localName, qName);
119         }
120
121
122         /**
123          * Process character text (inside tags or between them)
124          * @see org.xml.sax.ContentHandler#characters(char[], int, int)
125          */
126         public void characters(char[] ch, int start, int length)
127                 throws SAXException
128         {
129                 String value = new String(ch, start, length);
130                 if (_insideName && _insideWaypoint) {_name = checkCharacters(_name, value);}
131                 if (_insideName && !_insidePoint) {_trackName = checkCharacters(_trackName, value);}
132                 else if (_insideElevation) {_elevation = checkCharacters(_elevation, value);}
133                 else if (_insideTime) {_time = checkCharacters(_time, value);}
134                 else if (_insideType) {_type = checkCharacters(_type, value);}
135                 super.characters(ch, start, length);
136         }
137
138
139         /**
140          * Check to concatenate partially-received values, if necessary
141          * @param inVariable variable containing characters received until now
142          * @param inValue new value received
143          * @return concatenation
144          */
145         private static String checkCharacters(String inVariable, String inValue)
146         {
147                 if (inVariable == null) {return inValue;}
148                 return inVariable + inValue;
149         }
150
151
152         /**
153          * Process a point, either a waypoint or track point
154          */
155         private void processPoint()
156         {
157                 // Put the values into a String array matching the order in getFieldArray()
158                 String[] values = new String[7];
159                 values[0] = _latitude; values[1] = _longitude;
160                 values[2] = _elevation; values[3] = _name;
161                 values[4] = _time;
162                 if (_startSegment && !_insideWaypoint) {
163                         values[5] = "1";
164                         _startSegment = false;
165                 }
166                 values[6] = _type;
167                 _pointList.add(values);
168                 _trackNameList.addPoint(_trackNum, _trackName, _isTrackPoint);
169         }
170
171
172         /**
173          * @see tim.prune.load.xml.XmlHandler#getFieldArray()
174          */
175         public Field[] getFieldArray()
176         {
177                 final Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE,
178                         Field.WAYPT_NAME, Field.TIMESTAMP, Field.NEW_SEGMENT, Field.WAYPT_TYPE};
179                 return fields;
180         }
181
182
183         /**
184          * Return the parsed information as a 2d array
185          * @see tim.prune.load.xml.XmlHandler#getDataArray()
186          */
187         public String[][] getDataArray()
188         {
189                 int numPoints = _pointList.size();
190                 // construct data array
191                 String[][] result = new String[numPoints][];
192                 for (int i=0; i<numPoints; i++)
193                 {
194                         result[i] = _pointList.get(i);
195                 }
196                 return result;
197         }
198
199
200         /**
201          * @return track name list
202          */
203         public TrackNameList getTrackNameList() {
204                 return _trackNameList;
205         }
206 }