Class 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  
    • 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 element
      private void endElementCopyright​(java.lang.String localName)
      End the copyright element
      private void endElementDefault​(java.lang.String localName)
      End the default element
      private void endElementExt​(java.lang.String localName, java.lang.String qName)
      End an ext element
      private void endElementLink​(java.lang.String localName)
      End a link element
      private void endElementMetadata​(java.lang.String localName)
      End the metadata element
      private void endElementTrk​(java.lang.String localName)
      End a trk element
      private void endElementTrkseg​(java.lang.String localName)
      End a trkseg element
      private void endElementWpt​(java.lang.String localName)
      End a wpt element
      private java.util.Optional<java.util.Map<java.lang.String,​java.lang.Object>> getAttr()
      Get the current attributes
      (package private) GpxData getData()
      Get the parsed GpxData
      private static double parseCoordinates​(java.lang.String s)
      Convert a string coordinate to a double
      private static double parseCoordinates​(org.xml.sax.Attributes attributes, java.lang.String key)
      Convert the specified key's value to a number
      private static LatLon parseLatLon​(org.xml.sax.Attributes attributes)
      Convert coordinates in attributes to a LatLon object
      void 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 element
      private void startElementExt​(java.lang.String namespaceURI, java.lang.String qName, org.xml.sax.Attributes attributes)
      Start an ext element
      private void startElementGpx​(java.lang.String localName, org.xml.sax.Attributes attributes)
      Start the root gpx element
      private void startElementInit​(org.xml.sax.Attributes attributes)
      Start the root element
      private void startElementMetadata​(java.lang.String localName, org.xml.sax.Attributes attributes)
      Start a metadata element
      private void startElementRte​(java.lang.String localName, org.xml.sax.Attributes attributes)
      Start the rte element
      private void startElementTrk​(java.lang.String localName, org.xml.sax.Attributes attributes)
      Start a trk element
      private void startElementTrkSeg​(java.lang.String localName, org.xml.sax.Attributes attributes)
      Start a trkseg element
      private void startElementWpt​(java.lang.String localName, org.xml.sax.Attributes attributes)
      Start the wpt element
      void startPrefixMapping​(java.lang.String prefix, java.lang.String uri)  
      (package private) void tryToFinish()
      Attempt to finish parsing
      • Methods inherited from class org.xml.sax.helpers.DefaultHandler

        endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, unparsedEntityDecl, warning
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • startDocument

        public void startDocument()
        Specified by:
        startDocument in interface org.xml.sax.ContentHandler
        Overrides:
        startDocument in class org.xml.sax.helpers.DefaultHandler
      • startPrefixMapping

        public void startPrefixMapping​(java.lang.String prefix,
                                       java.lang.String uri)
        Specified by:
        startPrefixMapping in interface org.xml.sax.ContentHandler
        Overrides:
        startPrefixMapping in class org.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 from
        key - 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 a LatLon object
        Parameters:
        attributes - The attributes to parse
        Returns:
        The LatLon, warning: it may be invalid, use LatLon.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 interface org.xml.sax.ContentHandler
        Overrides:
        startElement in class org.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 interface org.xml.sax.ContentHandler
        Overrides:
        characters in class org.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 interface org.xml.sax.ContentHandler
        Overrides:
        endElement in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • 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 interface org.xml.sax.ContentHandler
        Overrides:
        endDocument in class org.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