]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/xml/GpxHandler.java
Version 13, August 2011
[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 _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();
31
32
33         /**
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)
36          */
37         public void startElement(String uri, String localName, String qName,
38                 Attributes attributes) throws SAXException
39         {
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"))
43                 {
44                         _insidePoint = true;
45                         _insideWaypoint = tag.equals("wpt");
46                         _isTrackPoint = tag.equals("trkpt");
47                         final int numAttributes = attributes.getLength();
48                         for (int i=0; i<numAttributes; i++)
49                         {
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);}
53                         }
54                         _elevation.setValue(null);
55                         _name.setValue(null);
56                         _time.setValue(null);
57                         _type.setValue(null);
58                         _link.setValue(null);
59                         _description.setValue(null);
60                 }
61                 else if (tag.equals("ele")) {
62                         _currentTag = _elevation;
63                 }
64                 else if (tag.equals("name")) {
65                         _currentTag = (_insidePoint?_name:_trackName);
66                 }
67                 else if (tag.equals("time")) {
68                         _currentTag = _time;
69                 }
70                 else if (tag.equals("type")) {
71                         _currentTag = _type;
72                 }
73                 else if (tag.equals("description")) {
74                         _currentTag = _description;
75                 }
76                 else if (tag.equals("link")) {
77                         _link.setValue(attributes.getValue("href"));
78                 }
79                 else if (tag.equals("trkseg")) {
80                         _startSegment = true;
81                 }
82                 else if (tag.equals("trk"))
83                 {
84                         _trackNum++;
85                         _trackName.setValue(null);
86                 }
87                 super.startElement(uri, localName, qName, attributes);
88         }
89
90
91         /**
92          * Process end tag
93          * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
94          */
95         public void endElement(String uri, String localName, String qName)
96                 throws SAXException
97         {
98                 String tag = qName.toLowerCase();
99                 if (tag.equals("wpt") || tag.equals("trkpt") || tag.equals("rtept"))
100                 {
101                         processPoint();
102                         _insidePoint = false;
103                 }
104                 else {
105                         _currentTag = null;
106                 }
107                 super.endElement(uri, localName, qName);
108         }
109
110
111         /**
112          * Process character text (inside tags or between them)
113          * @see org.xml.sax.ContentHandler#characters(char[], int, int)
114          */
115         public void characters(char[] ch, int start, int length)
116                 throws SAXException
117         {
118                 String value = new String(ch, start, length);
119                 if (_currentTag != null) {
120                         _currentTag.setValue(checkCharacters(_currentTag.getValue(), value));
121                 }
122                 super.characters(ch, start, length);
123         }
124
125
126         /**
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
131          */
132         private static String checkCharacters(String inVariable, String inValue)
133         {
134                 if (inVariable == null) {return inValue;}
135                 return inVariable + inValue;
136         }
137
138
139         /**
140          * Process a point, either a waypoint or track point
141          */
142         private void processPoint()
143         {
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)
152                 {
153                         values[5] = "1";
154                         _startSegment = false;
155                 }
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());
161         }
162
163
164         /**
165          * @see tim.prune.load.xml.XmlHandler#getFieldArray()
166          */
167         public Field[] getFieldArray()
168         {
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};
172                 return fields;
173         }
174
175
176         /**
177          * Return the parsed information as a 2d array
178          * @see tim.prune.load.xml.XmlHandler#getDataArray()
179          */
180         public String[][] getDataArray()
181         {
182                 int numPoints = _pointList.size();
183                 // construct data array
184                 String[][] result = new String[numPoints][];
185                 for (int i=0; i<numPoints; i++)
186                 {
187                         result[i] = _pointList.get(i);
188                 }
189                 return result;
190         }
191
192         /**
193          * @return array of links, or null if none
194          */
195         public String[] getLinkArray()
196         {
197                 int numPoints = _linkList.size();
198                 boolean hasLink = false;
199                 String[] result = new String[numPoints];
200                 for (int i=0; i<numPoints; i++)
201                 {
202                         result[i] = _linkList.get(i);
203                         if (result[i] != null) {hasLink = true;}
204                 }
205                 if (!hasLink) {result = null;}
206                 return result;
207         }
208
209         /**
210          * @return track name list
211          */
212         public TrackNameList getTrackNameList() {
213                 return _trackNameList;
214         }
215 }