(no commit message)
[utils] / gps / src / main / java / org / wamblee / gpx / GpxParser.java
index 043794146a12335665a232414417d4ce31300910..686c8d0843684aa461c69a4358bd81c19d48734c 100644 (file)
@@ -23,7 +23,10 @@ import java.util.List;
 import org.dom4j.Document;
 import org.dom4j.Element;
 import org.wamblee.gps.track.Track;
+import org.wamblee.gps.track.TrackImpl;
 import org.wamblee.gps.track.TrackPoint;
+import org.wamblee.gps.track.TrackSegment;
+import org.wamblee.gps.track.TrackSegmentImpl;
 import org.wamblee.xml.DomUtils;
 import org.wamblee.xml.XMLException;
 
@@ -34,49 +37,74 @@ import org.wamblee.xml.XMLException;
  */
 public class GpxParser {
 
-       private static final String SCHEMA_RESOURCE = "gpx.xsd";
+    private static final String SCHEMA_RESOURCE = "gpx.xsd";
 
-       public GpxParser() {
-               // Empty.
-       }
+    public GpxParser() {
+        // Empty.
+    }
 
-       public Track parse(InputStream aIs) throws XMLException {
-               Document doc = DomUtils.convert(DomUtils.read(aIs));
-               return parse(doc);
-       }
+    public Track parse(String aDescription, InputStream aIs) throws XMLException {
+        Document doc = DomUtils.convert(DomUtils.read(aIs));
+        return parse(aDescription, doc);
+    }
 
-       /**
-        * @param doc
-        */
-       public Track parse(Document doc) {
-               Track track = new Track();
-               List<Element> segments = doc.getRootElement().element("trk").elements(
-                               "trkseg");
+    /**
+     * @param doc
+     */
+    public Track parse(String aDescription, Document doc) {
+        TrackImpl result = new TrackImpl(aDescription);
+        List<Element> tracks = doc.getRootElement().elements("trk");
+        if ( tracks != null ) { 
+            for (Element track: tracks) {
+                String trackName = track.elementText("name");
+                TrackSegmentImpl trackSegment = new TrackSegmentImpl(trackName);
+                result.addSegment(trackSegment);
+                List<Element> segments = track.elements("trkseg");
+                if ( segments != null ) { 
+                    for (Element segment: segments) {           
+                        parseTrackPoints(trackSegment, segment.elementIterator("trkpt"));
+                    }
+                }
+            }
+        }
 
-               for (Element segment : segments) {
-                       for (Iterator i = segment.elementIterator("trkpt"); i.hasNext();) {
-                               Element trkpt = (Element) i.next();
-                               track.addPoint(parseTrackPoint(trkpt));
-                       }
-               }
-               return track;
-       }
+        List<Element> routes = doc.getRootElement().elements("rte");
+        if ( routes != null ) {
+            for (Element route: routes) {
+                String routeName = route.elementText("desc");
+                TrackSegmentImpl trackSegment = new TrackSegmentImpl(routeName);
+                result.addSegment(trackSegment);
+                parseTrackPoints(trackSegment, route.elementIterator("rtept"));
+            }
+        }
+        
+        return result;
+    }
+    
+    
+    private void parseTrackPoints(TrackSegmentImpl aTrack, Iterator<Element> aPoints) { 
+        while (aPoints.hasNext()) { 
+            Element point = aPoints.next();
+            aTrack.addPoint(parseTrackPoint(point));
+        }
+    }
+    
 
-       /**
-        * @param trkpt
-        */
-       private TrackPoint parseTrackPoint(Element trkpt) {
-               // System.out.println(trkpt.asXML() + "|\n");
-               double latitude = new Double(trkpt.attributeValue("lat"));
-               double longitude = new Double(trkpt.attributeValue("lon"));
-               Element ele = trkpt.element("ele");
-               double elevation = 0.0;
-               if (ele != null) {
-                       elevation = new Double(ele.getText());
-               }
-               // System.out.println(" lat = " + lat + " lon = " + lon + " ele = " +
-               // ele);
-               return new TrackPoint(latitude, longitude, elevation);
-       }
+    /**
+     * @param trkpt
+     */
+    private TrackPoint parseTrackPoint(Element trkpt) {
+        // System.out.println(trkpt.asXML() + "|\n");
+        double latitude = new Double(trkpt.attributeValue("lat"));
+        double longitude = new Double(trkpt.attributeValue("lon"));
+        Element ele = trkpt.element("ele");
+        double elevation = 0.0;
+        if (ele != null) {
+            elevation = new Double(ele.getText());
+        }
+        // System.out.println(" lat = " + lat + " lon = " + lon + " ele = " +
+        // ele);
+        return new TrackPoint(latitude, longitude, elevation);
+    }
 
 }