Update to Worldwind release 0.4.1
[worldwind-tracker.git] / gov / nasa / worldwind / formats / gpx / GpxReader.java
bloba13c6055c0ce66464d9349125e389d83e16628f8
1 /*
2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
5 All Rights Reserved.
6 */
7 package gov.nasa.worldwind.formats.gpx;
9 import gov.nasa.worldwind.tracks.*;
10 import gov.nasa.worldwind.util.Logging;
12 /**
13 * @author tag
14 * @version $Id: GpxReader.java 2471 2007-07-31 21:50:57Z tgaskins $
16 public class GpxReader // TODO: I18N, proper exception handling, remove stack-trace prints
18 private javax.xml.parsers.SAXParser parser;
19 private java.util.List<Track> tracks = new java.util.ArrayList<Track>();
21 public GpxReader() throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException
23 javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
24 factory.setNamespaceAware(true);
26 this.parser = factory.newSAXParser();
29 /**
30 * @param path
31 * @throws IllegalArgumentException if <code>path</code> is null
32 * @throws java.io.IOException if no file exists at the location specified by <code>path</code>
33 * @throws org.xml.sax.SAXException
35 public void readFile(String path) throws java.io.IOException, org.xml.sax.SAXException
37 if (path == null)
39 String msg = Logging.getMessage("nullValue.PathIsNull");
40 Logging.logger().severe(msg);
41 throw new IllegalArgumentException(msg);
44 java.io.File file = new java.io.File(path);
45 if (!file.exists())
47 String msg = Logging.getMessage("generic.FileNotFound", path);
48 Logging.logger().severe(msg);
49 throw new java.io.FileNotFoundException(path);
52 java.io.FileInputStream fis = new java.io.FileInputStream(file);
53 this.doRead(fis);
56 /**
57 * @param stream
58 * @throws IllegalArgumentException if <code>stream</code> is null
59 * @throws java.io.IOException
60 * @throws org.xml.sax.SAXException
62 public void readStream(java.io.InputStream stream) throws java.io.IOException, org.xml.sax.SAXException
64 if (stream == null)
66 String msg = Logging.getMessage("nullValue.InputStreamIsNull");
67 Logging.logger().severe(msg);
68 throw new IllegalArgumentException(msg);
71 this.doRead(stream);
74 public java.util.List<Track> getTracks()
76 return this.tracks;
79 private void doRead(java.io.InputStream fis) throws java.io.IOException, org.xml.sax.SAXException
81 this.parser.parse(fis, new Handler());
84 private class Handler extends org.xml.sax.helpers.DefaultHandler
86 // this is a private class used solely by the containing class, so no validation occurs in it.
88 private gov.nasa.worldwind.formats.gpx.ElementParser currentElement = null;
90 @Override
91 public void warning(org.xml.sax.SAXParseException saxParseException) throws org.xml.sax.SAXException
93 saxParseException.printStackTrace();
94 super.warning(saxParseException);
97 @Override
98 public void error(org.xml.sax.SAXParseException saxParseException) throws org.xml.sax.SAXException
100 saxParseException.printStackTrace();
101 super.error(saxParseException);
104 @Override
105 public void fatalError(org.xml.sax.SAXParseException saxParseException) throws org.xml.sax.SAXException
107 saxParseException.printStackTrace();
108 super.fatalError(saxParseException);
111 @Override
112 public void startElement(String uri, String lname, String qname, org.xml.sax.Attributes attributes)
113 throws org.xml.sax.SAXException
115 if (this.currentElement != null)
117 this.currentElement.startElement(uri, lname, qname, attributes);
119 else if (lname.equalsIgnoreCase("trk"))
121 GpxTrack track = new GpxTrack(uri, lname, qname, attributes);
122 this.currentElement = track;
123 GpxReader.this.tracks.add(track);
127 @Override
128 public void endElement(String uri, String lname, String qname) throws org.xml.sax.SAXException
130 if (this.currentElement != null)
132 this.currentElement.endElement(uri, lname, qname);
134 if (lname.equalsIgnoreCase(this.currentElement.getElementName()))
135 this.currentElement = null;
139 @Override
140 public void characters(char[] data, int start, int length) throws org.xml.sax.SAXException
142 if (this.currentElement != null)
143 this.currentElement.characters(data, start, length);
147 public static void main(String[] args)
151 gov.nasa.worldwind.formats.gpx.GpxReader reader = new gov.nasa.worldwind.formats.gpx.GpxReader();
152 reader.readFile("src/worldwinddemo/track data/20061126.gpx");
154 System.out.printf("%d tracks\n", reader.getTracks().size());
155 for (Track track : reader.getTracks())
157 System.out.printf("GpxTrack %d segments\n", track.getSegments().size());
158 int i = 0;
159 for (TrackSegment segment : track.getSegments())
161 System.out.printf("\tSegment %d, %d points\n", i++, segment.getPoints().size());
162 int j = 0;
163 for (TrackPoint point : segment.getPoints())
165 System.out.printf("\t\t%4d: %s\n", j++, point);
170 catch (javax.xml.parsers.ParserConfigurationException e)
172 e.printStackTrace();
174 catch (org.xml.sax.SAXException e)
176 e.printStackTrace();
178 catch (java.io.IOException e)
180 e.printStackTrace();