Class Reader
java.lang.Object
org.apache.sis.internal.storage.xml.stream.StaxStreamIO
org.apache.sis.internal.storage.xml.stream.StaxStreamReader
org.apache.sis.internal.storage.gpx.Reader
- All Implemented Interfaces:
AutoCloseable
,Runnable
,Spliterator<AbstractFeature>
,XMLStreamConstants
Reader for GPX 1.0 and 1.1 files.
This reader is itself a spliterator over all features found in the XML file.
Usage:
- Since:
- 0.8
- Version:
- 1.0
-
Nested Class Summary
Nested classes/interfaces inherited from interface java.util.Spliterator
Spliterator.OfDouble, Spliterator.OfInt, Spliterator.OfLong, Spliterator.OfPrimitive<T extends Object,
T_CONS extends Object, T_SPLITR extends Spliterator.OfPrimitive<T, T_CONS, T_SPLITR>> -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Metadata
The metadata (ISO 19115 compatible), ornull
if none.private String
The namespace, which should be eitherTags.NAMESPACE_V10
orTags.NAMESPACE_V11
.private int
Identifier of the last "route" feature instance created.private int
Identifier of the last "track" feature instance created.private int
Identifier of the last "way point" feature instance created.Fields inherited from class org.apache.sis.internal.storage.xml.stream.StaxStreamReader
reader
Fields inherited from class org.apache.sis.internal.storage.xml.stream.StaxStreamIO
owner
Fields inherited from interface java.util.Spliterator
CONCURRENT, DISTINCT, IMMUTABLE, NONNULL, ORDERED, SIZED, SORTED, SUBSIZED
Fields inherited from interface javax.xml.stream.XMLStreamConstants
ATTRIBUTE, CDATA, CHARACTERS, COMMENT, DTD, END_DOCUMENT, END_ELEMENT, ENTITY_DECLARATION, ENTITY_REFERENCE, NAMESPACE, NOTATION_DECLARATION, PROCESSING_INSTRUCTION, SPACE, START_DOCUMENT, START_ELEMENT
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate Person
author()
Returns theMetadata.author
field, creating all necessary objects if needed.void
forEachRemaining
(Consumer<? super AbstractFeature> action) Performs the given action for each remaining element until all elements have been processed.Returns the metadata (ISO 19115 compatible), ornull
if none.initialize
(boolean readMetadata) Reads the metadata.private boolean
isEndGPX()
Returnstrue
if the current position of the given reader is the closing</gpx>
tag.private boolean
isGPX()
Returnstrue
if the current element can be considered as in the GPX namespace.private static boolean
Returnstrue
if the given namespace is a GPX namespace or is null.private Link
link()
Returns the first element of theMetadata.links
field, creating all necessary objects if needed.private Metadata
metadata()
Returns themetadata
field, creating it if needed.private boolean
parse
(Consumer<? super AbstractFeature> action, boolean all) Implementation oftryAdvance(Consumer)
andforEachRemaining(Consumer)
.private AbstractFeature
parseRoute
(int index) Parses a<rte>
element.private AbstractFeature
parseTrack
(int index) Parses a<trk>
element.private AbstractFeature
parseTrackSegment
(int index) Parses a<trkseg>
element.private AbstractFeature
parseWayPoint
(int index) Parses a<wpt>
,<rtept>
or<trkpt>
element.boolean
tryAdvance
(Consumer<? super AbstractFeature> action) Performs the given action on the next feature instance, or returnsnull
if there are no more features to parse.Methods inherited from class org.apache.sis.internal.storage.xml.stream.StaxStreamReader
canNotParseFile, characteristics, close, endOfFile, estimateSize, getElementAsDate, getElementAsDouble, getElementAsInteger, getElementAsList, getElementAsTemporal, getElementAsURI, getElementText, getSubReader, moveToRootElement, nestedElement, next, parseBoolean, parseDouble, run, skipUntilEnd, trySplit, unmarshal
Methods inherited from class org.apache.sis.internal.storage.xml.stream.StaxStreamIO
errors
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Spliterator
getComparator, getExactSizeIfKnown, hasCharacteristics
-
Field Details
-
namespace
The namespace, which should be eitherTags.NAMESPACE_V10
orTags.NAMESPACE_V11
. We store this information for identifying the closing<gpx>
tag. -
metadata
The metadata (ISO 19115 compatible), ornull
if none. -
wayPointId
private int wayPointIdIdentifier of the last "way point" feature instance created. We use sequential numbers starting from 1. -
routeId
private int routeIdIdentifier of the last "route" feature instance created. We use sequential numbers starting from 1. -
trackId
private int trackIdIdentifier of the last "track" feature instance created. We use sequential numbers starting from 1.
-
-
Constructor Details
-
Reader
Creates a new GPX reader for the given data store. Theinitialize(boolean)
method must be invoked after this constructor.- Parameters:
owner
- the data store for which this reader is created.- Throws:
DataStoreException
- if the input type is not recognized.XMLStreamException
- if an error occurred while opening the XML file.IOException
- if an error occurred while preparing the input stream.Exception
- if another kind of error occurred while closing a previous stream.
-
-
Method Details
-
isGPX
Returnstrue
if the given namespace is a GPX namespace or is null. -
isGPX
private boolean isGPX()Returnstrue
if the current element can be considered as in the GPX namespace. Strictly speaking we should require the namespace URI to be exactlynamespace
, but this method is a little bit more lenient. -
isEndGPX
private boolean isEndGPX()Returnstrue
if the current position of the given reader is the closing</gpx>
tag. The reader event should beXMLStreamConstants.END_ELEMENT
before to invoke this method. -
initialize
public Version initialize(boolean readMetadata) throws DataStoreException, XMLStreamException, JAXBException, URISyntaxException, EOFException Reads the metadata. This method should be invoked exactly once after construction. This work is performed outside the constructor for allowingStaxStreamReader.close()
method invocation no matter if thisinitialize(boolean)
method fails.- Parameters:
readMetadata
- iffalse
, skip the reading of metadata elements.- Returns:
- the GPX file version, or
null
if no version information was found. - Throws:
DataStoreException
- if the root element is not the expected one.XMLStreamException
- if an error occurred while reading the XML file.JAXBException
- if an error occurred while parsing GPX 1.1 metadata.ClassCastException
- if an object unmarshalled by JAXB was not of the expected type.URISyntaxException
- if an error occurred while parsing URI in GPX 1.0 metadata.DateTimeParseException
- if a text cannot be parsed as a date.EOFException
- if the file seems to be truncated.
-
metadata
Returns themetadata
field, creating it if needed. This is a convenience method for GPX 1.0 metadata parsing. This is not for returning the metadata result after parsing.- See Also:
-
author
Returns theMetadata.author
field, creating all necessary objects if needed. This is a convenience method for GPX 1.0 metadata parsing. -
link
Returns the first element of theMetadata.links
field, creating all necessary objects if needed. This is a convenience method for GPX 1.0 metadata parsing. -
getMetadata
Returns the metadata (ISO 19115 compatible), ornull
if none. This method can return a non-null value only ifinitialize(true)
has been invoked before this method.- Returns:
- the metadata, or
null
if none. - See Also:
-
tryAdvance
Performs the given action on the next feature instance, or returnsnull
if there are no more features to parse.- Specified by:
tryAdvance
in interfaceSpliterator<AbstractFeature>
- Specified by:
tryAdvance
in classStaxStreamReader
- Parameters:
action
- the action to perform on the next feature instances.- Returns:
true
if a feature has been found, orfalse
if we reached the end of GPX file.- Throws:
BackingStoreException
- if an error occurred while parsing the next feature instance. The cause may beDataStoreException
,IOException
,URISyntaxException
or variousRuntimeException
.
-
forEachRemaining
Performs the given action for each remaining element until all elements have been processed.- Parameters:
action
- the action to perform on the remaining feature instances.- Throws:
BackingStoreException
- if an error occurred while parsing the next feature instance. The cause may beDataStoreException
,IOException
,URISyntaxException
or variousRuntimeException
.
-
parse
Implementation oftryAdvance(Consumer)
andforEachRemaining(Consumer)
.- Parameters:
action
- the action to perform on the remaining feature instances.all
- whether to perform the action on all remaining instances or only the next one.- Returns:
false
if this method as detected the end of<gpx>
element or the end of document.- Throws:
DataStoreException
- if the file contains invalid elements.XMLStreamException
- if an error occurred while reading the XML file.URISyntaxException
- if an error occurred while parsing GPX 1.0 URI.JAXBException
- if an error occurred while parsing GPX 1.1 link.ClassCastException
- if an object unmarshalled by JAXB was not of the expected type.NumberFormatException
- if a text cannot be parsed as an integer or a floating point number.DateTimeParseException
- if a text cannot be parsed as a date.EOFException
- if the file seems to be truncated.Exception
-
parseWayPoint
Parses a<wpt>
,<rtept>
or<trkpt>
element. The STAX reader current event must be aXMLStreamConstants.START_ELEMENT
. After this method invocation, the reader will be onXMLStreamConstants.END_ELEMENT
.- Throws:
Exception
- see the list of exceptions documented inparse(Consumer, boolean)
.
-
parseRoute
Parses a<rte>
element. The STAX reader current event must be aXMLStreamConstants.START_ELEMENT
and the name of that start element must beTags.ROUTES
.- Throws:
Exception
- see the list of exceptions documented inparse(Consumer, boolean)
.
-
parseTrackSegment
Parses a<trkseg>
element. The STAX reader current event must be aXMLStreamConstants.START_ELEMENT
and the name of that start element must beTags.TRACK_SEGMENTS
.- Throws:
Exception
- see the list of exceptions documented inparse(Consumer, boolean)
.
-
parseTrack
Parses a<trk>
element. The STAX reader current event must be aXMLStreamConstants.START_ELEMENT
and the name of that start element must beTags.TRACKS
.- Throws:
Exception
- see the list of exceptions documented inparse(Consumer, boolean)
.
-