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