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