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