Class GraphBuildingHandler

  • All Implemented Interfaces:
    org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

    public class GraphBuildingHandler
    extends org.xml.sax.helpers.DefaultHandler
    Parses OSM XML files using an XML SAX parser. Used to construct the graph of roads for pathfinding, under some constraints. See OSM documentation on the highway tag, the way XML element, the node XML element, and the java SAX parser tutorial. You may find the CSCourseGraphDB and CSCourseGraphDBHandler examples useful. The idea here is that some external library is going to walk through the XML file, and your override method tells Java what to do every time it gets to the next element in the file. This is a very common but strange-when-you-first-see it pattern. It is similar to the Visitor pattern we discussed for graphs.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String activeState  
      private static java.util.Set<java.lang.String> ALLOWED_HIGHWAY_TYPES
      Only allow for non-service roads; this prevents going on pedestrian streets as much as possible.
      private GraphDB g  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void endElement​(java.lang.String uri, java.lang.String localName, java.lang.String qName)
      Receive notification of the end of an element.
      void startElement​(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attributes)
      Called at the beginning of an element.
      • Methods inherited from class org.xml.sax.helpers.DefaultHandler

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

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

      • ALLOWED_HIGHWAY_TYPES

        private static final java.util.Set<java.lang.String> ALLOWED_HIGHWAY_TYPES
        Only allow for non-service roads; this prevents going on pedestrian streets as much as possible. Note that in Berkeley, many of the campus roads are tagged as motor vehicle roads, but in practice we walk all over them with such impunity that we forget cars can actually drive on them.
      • activeState

        private java.lang.String activeState
    • Constructor Detail

      • GraphBuildingHandler

        public GraphBuildingHandler​(GraphDB g)
        Create a new GraphBuildingHandler.
        Parameters:
        g - The graph to populate with the XML data.
    • Method Detail

      • startElement

        public void startElement​(java.lang.String uri,
                                 java.lang.String localName,
                                 java.lang.String qName,
                                 org.xml.sax.Attributes attributes)
                          throws org.xml.sax.SAXException
        Called at the beginning of an element. Typically, you will want to handle each element in here, and you may want to track the parent element.
        Specified by:
        startElement in interface org.xml.sax.ContentHandler
        Overrides:
        startElement in class org.xml.sax.helpers.DefaultHandler
        Parameters:
        uri - The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
        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. This tells us which element we're looking at.
        attributes - The attributes attached to the element. If there are no attributes, it shall be an empty Attributes object.
        Throws:
        org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.
        See Also:
        Attributes
      • endElement

        public void endElement​(java.lang.String uri,
                               java.lang.String localName,
                               java.lang.String qName)
                        throws org.xml.sax.SAXException
        Receive notification of the end of an element. You may want to take specific terminating actions here, like finalizing vertices or edges found.
        Specified by:
        endElement in interface org.xml.sax.ContentHandler
        Overrides:
        endElement in class org.xml.sax.helpers.DefaultHandler
        Parameters:
        uri - The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
        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.
        Throws:
        org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.