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