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