us.zuercher.gpx2map.data
Class GpxContentHandler

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by us.zuercher.gpx2map.data.GpxContentHandler
All Implemented Interfaces:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler

public class GpxContentHandler
extends DefaultHandler
implements ContentHandler

GpxContentHandler is a ContentHandler that understands the GPX XSD, version 1.1 and earlier. It endeavors to ignore all portions of the GPX file that it is not capable of processing and makes as few assumptions as possible about the structure of the GPX file.

Author:
Stephan Zuercher

Field Summary
private  StringBuffer characters
          Characters seen since the last call to startElement(String, String, String, Attributes).
private  Route currentRoute
          The route currently being read.
private  RoutePoint currentRoutePoint
          The route point currently being read.
private  Track currentTrack
          The track currently being read.
private  TrackPoint currentTrackPoint
          The track point currently being read.
private  TrackSegment currentTrackSegment
          The track segment currently being read.
private  WayPoint currentWayPoint
          The way point currently being read.
private static SimpleDateFormat dateFormatter
          Date formatter.
private  List<Throwable> errors
          List of errors encountered durig processing.
static String GPX_DATE_FORMAT
          Date format string.
private  Locator locator
          Current location in the file.
private  List<Route> routes
          Routes found in the file.
private  List<Track> tracks
          Tracks found in the file.
private  List<WayPoint> wayPoints
          WayPoints found in the file.
 
Constructor Summary
GpxContentHandler(List<Throwable> errors)
          Constructs a new GpxContentHandler that stores errors in the given list.
 
Method Summary
 void characters(char[] ch, int start, int length)
          Character data event.
 void endElement(String uri, String localName, String qName)
          Handle element end event.
 void error(SAXParseException e)
          Override error processing.
 void fatalError(SAXParseException e)
          Override error processing.
private  LatLon getLocation(String type, Attributes attributes)
          Convert element attributes into a LatLon.
(package private)  List<Route> getRoutes()
          Return the loaded routes.
(package private)  List<Track> getTracks()
          Return the loaded tracks.
(package private)  List<WayPoint> getWayPoints()
          Return the loaded way-points.
 void setDocumentLocator(Locator locator)
          Mutator to configure the document locator object.
private  void setProperty(AbstractGpxEntity entity, String name, String value)
          Set the given property on the given entity.
 void startElement(String uri, String localName, String qName, Attributes attributes)
          Handle element start.
 void warning(SAXParseException e)
          Override warning processing.
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endDocument, endPrefixMapping, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.xml.sax.ContentHandler
endDocument, endPrefixMapping, ignorableWhitespace, processingInstruction, skippedEntity, startDocument, startPrefixMapping
 

Field Detail

GPX_DATE_FORMAT

public static final String GPX_DATE_FORMAT
Date format string.

See Also:
Constant Field Values

dateFormatter

private static final SimpleDateFormat dateFormatter
Date formatter.


characters

private StringBuffer characters
Characters seen since the last call to startElement(String, String, String, Attributes).


locator

private Locator locator
Current location in the file.


tracks

private List<Track> tracks
Tracks found in the file.


currentTrack

private Track currentTrack
The track currently being read.


currentTrackSegment

private TrackSegment currentTrackSegment
The track segment currently being read.


currentTrackPoint

private TrackPoint currentTrackPoint
The track point currently being read.


wayPoints

private List<WayPoint> wayPoints
WayPoints found in the file.


currentWayPoint

private WayPoint currentWayPoint
The way point currently being read.


routes

private List<Route> routes
Routes found in the file.


currentRoute

private Route currentRoute
The route currently being read.


currentRoutePoint

private RoutePoint currentRoutePoint
The route point currently being read.


errors

private List<Throwable> errors
List of errors encountered durig processing.

Constructor Detail

GpxContentHandler

GpxContentHandler(List<Throwable> errors)
Constructs a new GpxContentHandler that stores errors in the given list.

Parameters:
errors - modifiable list where parsing errors will be stored.
Method Detail

getTracks

List<Track> getTracks()
Return the loaded tracks.

Returns:
the loaded tracks.

getWayPoints

List<WayPoint> getWayPoints()
Return the loaded way-points.

Returns:
the loaded way-points.

getRoutes

List<Route> getRoutes()
Return the loaded routes.

Returns:
the loaded routes.

startElement

public void startElement(String uri,
                         String localName,
                         String qName,
                         Attributes attributes)
                  throws SAXException
Handle element start.

Specified by:
startElement in interface ContentHandler
Overrides:
startElement in class DefaultHandler
Parameters:
uri - namespace URI
localName - element's local name
qName - element's qualified name
attributes - element attributes
Throws:
SAXException - if there's a parsing error related to ordering or elements or element parent-child relationships

getLocation

private LatLon getLocation(String type,
                           Attributes attributes)
                    throws SAXParseException
Convert element attributes into a LatLon.

Parameters:
type - element type, used for exceptions
attributes - the element attributes to search for "lat" and "lon"
Returns:
a LatLon based on the "lat" and "lon" attributes
Throws:
SAXParseException - if the attributes cannot be found or parsed

endElement

public void endElement(String uri,
                       String localName,
                       String qName)
                throws SAXException
Handle element end event.

Specified by:
endElement in interface ContentHandler
Overrides:
endElement in class DefaultHandler
Parameters:
uri - element namespace uri
localName - element's local name
qName - element's qualified name
Throws:
SAXException - if there's an error converting element character data (see characters) into a non-String property value.

setProperty

private void setProperty(AbstractGpxEntity entity,
                         String name,
                         String value)
                  throws SAXParseException
Set the given property on the given entity. Uses the entity's methods to determine the type of the property and then converts the string value into the correct type. If the entity does not support the property, the method returns.

Parameters:
entity - entity to set the property on
name - property name
value - property value
Throws:
SAXParseException - if the value cannot be converted to the required type

characters

public void characters(char[] ch,
                       int start,
                       int length)
Character data event.

Specified by:
characters in interface ContentHandler
Overrides:
characters in class DefaultHandler
Parameters:
ch - character buffer
start - data start
length - number of characters to process

setDocumentLocator

public void setDocumentLocator(Locator locator)
Mutator to configure the document locator object.

Specified by:
setDocumentLocator in interface ContentHandler
Overrides:
setDocumentLocator in class DefaultHandler
Parameters:
locator - a document locator

error

public void error(SAXParseException e)
           throws SAXException
Override error processing.

Specified by:
error in interface ErrorHandler
Overrides:
error in class DefaultHandler
Parameters:
e - parsing exception to handle
Throws:
SAXException - always

fatalError

public void fatalError(SAXParseException e)
                throws SAXException
Override error processing.

Specified by:
fatalError in interface ErrorHandler
Overrides:
fatalError in class DefaultHandler
Parameters:
e - parsing exception to handle
Throws:
SAXException - always

warning

public void warning(SAXParseException e)
Override warning processing. Stores warnings for later retrieval.

Specified by:
warning in interface ErrorHandler
Overrides:
warning in class DefaultHandler
Parameters:
e - parsing exception to handle