Package org.openstreetmap.josm.io
Class GpxParser
- java.lang.Object
-
- org.xml.sax.helpers.DefaultHandler
-
- org.openstreetmap.josm.io.GpxParser
-
- All Implemented Interfaces:
org.xml.sax.ContentHandler
,org.xml.sax.DTDHandler
,org.xml.sax.EntityResolver
,org.xml.sax.ErrorHandler
class GpxParser extends org.xml.sax.helpers.DefaultHandler
A parser for gpx files
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
GpxParser.State
-
Field Summary
Fields Modifier and Type Field Description private java.lang.StringBuilder
accumulator
private GpxExtensionCollection
currentExtensionCollection
private GpxLink
currentLink
private GpxRoute
currentRoute
private GpxParser.State
currentState
private java.util.Collection<IGpxTrackSegment>
currentTrack
private java.util.Map<java.lang.String,java.lang.Object>
currentTrackAttr
private GpxExtensionCollection
currentTrackExtensionCollection
private java.util.Collection<WayPoint>
currentTrackSeg
private WayPoint
currentWayPoint
private GpxData
data
private java.util.Stack<java.lang.String[]>
elements
private boolean
nokiaSportsTrackerBug
private java.util.Stack<GpxParser.State>
states
private java.lang.String
version
-
Constructor Summary
Constructors Constructor Description GpxParser()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
characters(char[] ch, int start, int length)
private static void
convertUrlToLink(java.util.Map<java.lang.String,java.lang.Object> attr)
convert url/urlname to link element (GPX 1.0 → GPX 1.1).void
endDocument()
void
endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName)
private void
endElementAuthor(java.lang.String localName)
End the author elementprivate void
endElementCopyright(java.lang.String localName)
End the copyright elementprivate void
endElementDefault(java.lang.String localName)
End the default elementprivate void
endElementExt(java.lang.String localName, java.lang.String qName)
End an ext elementprivate void
endElementLink(java.lang.String localName)
End a link elementprivate void
endElementMetadata(java.lang.String localName)
End the metadata elementprivate void
endElementTrk(java.lang.String localName)
End a trk elementprivate void
endElementTrkseg(java.lang.String localName)
End a trkseg elementprivate void
endElementWpt(java.lang.String localName)
End a wpt elementprivate java.util.Optional<java.util.Map<java.lang.String,java.lang.Object>>
getAttr()
Get the current attributes(package private) GpxData
getData()
Get the parsedGpxData
private static double
parseCoordinates(java.lang.String s)
Convert a string coordinate to a doubleprivate static double
parseCoordinates(org.xml.sax.Attributes attributes, java.lang.String key)
Convert the specified key's value to a numberprivate static LatLon
parseLatLon(org.xml.sax.Attributes attributes)
Convert coordinates in attributes to aLatLon
objectvoid
startDocument()
void
startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attributes)
private void
startElementAuthor(java.lang.String localName, org.xml.sax.Attributes attributes)
Start an author elementprivate void
startElementExt(java.lang.String namespaceURI, java.lang.String qName, org.xml.sax.Attributes attributes)
Start an ext elementprivate void
startElementGpx(java.lang.String localName, org.xml.sax.Attributes attributes)
Start the root gpx elementprivate void
startElementInit(org.xml.sax.Attributes attributes)
Start the root elementprivate void
startElementMetadata(java.lang.String localName, org.xml.sax.Attributes attributes)
Start a metadata elementprivate void
startElementRte(java.lang.String localName, org.xml.sax.Attributes attributes)
Start the rte elementprivate void
startElementTrk(java.lang.String localName, org.xml.sax.Attributes attributes)
Start a trk elementprivate void
startElementTrkSeg(java.lang.String localName, org.xml.sax.Attributes attributes)
Start a trkseg elementprivate void
startElementWpt(java.lang.String localName, org.xml.sax.Attributes attributes)
Start the wpt elementvoid
startPrefixMapping(java.lang.String prefix, java.lang.String uri)
(package private) void
tryToFinish()
Attempt to finish parsing
-
-
-
Field Detail
-
version
private java.lang.String version
-
currentTrack
private java.util.Collection<IGpxTrackSegment> currentTrack
-
currentTrackAttr
private java.util.Map<java.lang.String,java.lang.Object> currentTrackAttr
-
currentTrackSeg
private java.util.Collection<WayPoint> currentTrackSeg
-
currentRoute
private GpxRoute currentRoute
-
currentWayPoint
private WayPoint currentWayPoint
-
currentState
private GpxParser.State currentState
-
currentLink
private GpxLink currentLink
-
currentExtensionCollection
private GpxExtensionCollection currentExtensionCollection
-
currentTrackExtensionCollection
private GpxExtensionCollection currentTrackExtensionCollection
-
states
private final java.util.Stack<GpxParser.State> states
-
elements
private final java.util.Stack<java.lang.String[]> elements
-
accumulator
private java.lang.StringBuilder accumulator
-
nokiaSportsTrackerBug
private boolean nokiaSportsTrackerBug
-
-
Constructor Detail
-
GpxParser
GpxParser()
-
-
Method Detail
-
startDocument
public void startDocument()
- Specified by:
startDocument
in interfaceorg.xml.sax.ContentHandler
- Overrides:
startDocument
in classorg.xml.sax.helpers.DefaultHandler
-
startPrefixMapping
public void startPrefixMapping(java.lang.String prefix, java.lang.String uri)
- Specified by:
startPrefixMapping
in interfaceorg.xml.sax.ContentHandler
- Overrides:
startPrefixMapping
in classorg.xml.sax.helpers.DefaultHandler
-
parseCoordinates
private static double parseCoordinates(org.xml.sax.Attributes attributes, java.lang.String key)
Convert the specified key's value to a number- Parameters:
attributes
- The attributes to get the value fromkey
- The key to use- Returns:
- A valid double, or
Double.NaN
-
parseCoordinates
private static double parseCoordinates(java.lang.String s)
Convert a string coordinate to a double- Parameters:
s
- The string to convert to double- Returns:
- A valid double, or
Double.NaN
-
parseLatLon
private static LatLon parseLatLon(org.xml.sax.Attributes attributes)
Convert coordinates in attributes to aLatLon
object- Parameters:
attributes
- The attributes to parse- Returns:
- The
LatLon
, warning: it may be invalid, useLatLon.isValid()
-
startElement
public void startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attributes) throws org.xml.sax.SAXException
- Specified by:
startElement
in interfaceorg.xml.sax.ContentHandler
- Overrides:
startElement
in classorg.xml.sax.helpers.DefaultHandler
- Throws:
org.xml.sax.SAXException
-
startElementInit
private void startElementInit(org.xml.sax.Attributes attributes)
Start the root element- Parameters:
attributes
- The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
-
startElementGpx
private void startElementGpx(java.lang.String localName, org.xml.sax.Attributes attributes)
Start the root gpx element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.attributes
- The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
-
startElementMetadata
private void startElementMetadata(java.lang.String localName, org.xml.sax.Attributes attributes)
Start a metadata element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.attributes
- The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.- See Also:
endElementMetadata(String)
-
startElementAuthor
private void startElementAuthor(java.lang.String localName, org.xml.sax.Attributes attributes)
Start an author element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.attributes
- The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.- See Also:
endElementAuthor(String)
-
startElementTrk
private void startElementTrk(java.lang.String localName, org.xml.sax.Attributes attributes)
Start a trk element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.attributes
- The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.- See Also:
endElementTrk(String)
-
startElementTrkSeg
private void startElementTrkSeg(java.lang.String localName, org.xml.sax.Attributes attributes)
Start a trkseg element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.attributes
- The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.- See Also:
endElementTrkseg(String)
-
startElementWpt
private void startElementWpt(java.lang.String localName, org.xml.sax.Attributes attributes)
Start the wpt element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.attributes
- The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.- See Also:
endElementWpt(String)
-
startElementRte
private void startElementRte(java.lang.String localName, org.xml.sax.Attributes attributes)
Start the rte element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.attributes
- The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
-
startElementExt
private void startElementExt(java.lang.String namespaceURI, java.lang.String qName, org.xml.sax.Attributes attributes)
Start an ext element- Parameters:
namespaceURI
- The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.qName
- The qualified name (with prefix), or the empty string if qualified names are not available.attributes
- The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
-
characters
public void characters(char[] ch, int start, int length)
- Specified by:
characters
in interfaceorg.xml.sax.ContentHandler
- Overrides:
characters
in classorg.xml.sax.helpers.DefaultHandler
-
getAttr
private java.util.Optional<java.util.Map<java.lang.String,java.lang.Object>> getAttr()
Get the current attributes- Returns:
- The current attributes, if available
-
endElement
public void endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName) throws org.xml.sax.SAXException
- Specified by:
endElement
in interfaceorg.xml.sax.ContentHandler
- Overrides:
endElement
in classorg.xml.sax.helpers.DefaultHandler
- Throws:
org.xml.sax.SAXException
-
endElementMetadata
private void endElementMetadata(java.lang.String localName)
End the metadata element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.- See Also:
startElementMetadata(String, Attributes)
-
endElementAuthor
private void endElementAuthor(java.lang.String localName)
End the author element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.- See Also:
startElementAuthor(String, Attributes)
-
endElementCopyright
private void endElementCopyright(java.lang.String localName)
End the copyright element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.
-
endElementLink
private void endElementLink(java.lang.String localName)
End a link element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.
-
endElementWpt
private void endElementWpt(java.lang.String localName) throws org.xml.sax.SAXException
End a wpt element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.- Throws:
org.xml.sax.SAXException
- If a waypoint does not have valid coordinates- See Also:
startElementWpt(String, Attributes)
-
endElementTrkseg
private void endElementTrkseg(java.lang.String localName)
End a trkseg element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.- See Also:
startElementTrkSeg(String, Attributes)
-
endElementTrk
private void endElementTrk(java.lang.String localName)
End a trk element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.- See Also:
startElementTrk(String, Attributes)
-
endElementExt
private void endElementExt(java.lang.String localName, java.lang.String qName)
End an ext element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.qName
- The qualified name (with prefix), or the empty string if qualified names are not available.- See Also:
startElementExt(String, String, Attributes)
-
endElementDefault
private void endElementDefault(java.lang.String localName)
End the default element- Parameters:
localName
- The local name (without prefix), or the empty string if Namespace processing is not being performed.
-
endDocument
public void endDocument() throws org.xml.sax.SAXException
- Specified by:
endDocument
in interfaceorg.xml.sax.ContentHandler
- Overrides:
endDocument
in classorg.xml.sax.helpers.DefaultHandler
- Throws:
org.xml.sax.SAXException
-
convertUrlToLink
private static void convertUrlToLink(java.util.Map<java.lang.String,java.lang.Object> attr)
convert url/urlname to link element (GPX 1.0 → GPX 1.1).- Parameters:
attr
- attributes
-
tryToFinish
void tryToFinish() throws org.xml.sax.SAXException
Attempt to finish parsing- Throws:
org.xml.sax.SAXException
- If there are additional parsing errors
-
-