2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
7 package gov
.nasa
.worldwind
.formats
.gpx
;
9 import gov
.nasa
.worldwind
.tracks
.*;
10 import gov
.nasa
.worldwind
.util
.Logging
;
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();
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
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
);
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
);
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
66 String msg
= Logging
.getMessage("nullValue.InputStreamIsNull");
67 Logging
.logger().severe(msg
);
68 throw new IllegalArgumentException(msg
);
74 public java
.util
.List
<Track
> getTracks()
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;
91 public void warning(org
.xml
.sax
.SAXParseException saxParseException
) throws org
.xml
.sax
.SAXException
93 saxParseException
.printStackTrace();
94 super.warning(saxParseException
);
98 public void error(org
.xml
.sax
.SAXParseException saxParseException
) throws org
.xml
.sax
.SAXException
100 saxParseException
.printStackTrace();
101 super.error(saxParseException
);
105 public void fatalError(org
.xml
.sax
.SAXParseException saxParseException
) throws org
.xml
.sax
.SAXException
107 saxParseException
.printStackTrace();
108 super.fatalError(saxParseException
);
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
);
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;
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());
159 for (TrackSegment segment
: track
.getSegments())
161 System
.out
.printf("\tSegment %d, %d points\n", i
++, segment
.getPoints().size());
163 for (TrackPoint point
: segment
.getPoints())
165 System
.out
.printf("\t\t%4d: %s\n", j
++, point
);
170 catch (javax
.xml
.parsers
.ParserConfigurationException e
)
174 catch (org
.xml
.sax
.SAXException e
)
178 catch (java
.io
.IOException e
)