Package gov.usgs.util

Class SAXAdapter

  • All Implemented Interfaces:
    org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler
    Direct Known Subclasses:
    EventAddonParser, ProductTrackerParser, RegionsHandler, URLNotificationParser

    public class SAXAdapter
    extends org.xml.sax.helpers.DefaultHandler
    SAXAdapter is a sax handler that accumulates element content, which is a common sax handler task. Users should be cautious because this in some ways removes efficiency gained by handling streaming events, because element content is being buffered. One buffer for each element nesting is maintained, so this works best for shallow documents, whose elements contain little content.
    • Constructor Summary

      Constructors 
      Constructor Description
      SAXAdapter()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void characters​(char[] ch, int start, int length)
      Override DefaultHandler characters.
      void endElement​(java.lang.String uri, java.lang.String localName, java.lang.String qName)
      Override DefaultHandler endElement.
      void onEndElement​(java.lang.String uri, java.lang.String localName, java.lang.String qName, java.lang.String content)
      SAXAdapter end element handler.
      void onStartElement​(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attributes)
      SAXAdapter start element handler.
      java.lang.Exception parse​(java.io.InputStream xml)
      Use this handler to parse an input stream.
      java.lang.Exception parse​(java.lang.String xml)
      Use this handler to parse a string.
      void startElement​(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attributes)
      Override DefaultHandler startElement.
      • Methods inherited from class org.xml.sax.helpers.DefaultHandler

        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
    • Constructor Detail

      • SAXAdapter

        public SAXAdapter()
    • Method Detail

      • onStartElement

        public void onStartElement​(java.lang.String uri,
                                   java.lang.String localName,
                                   java.lang.String qName,
                                   org.xml.sax.Attributes attributes)
                            throws org.xml.sax.SAXException
        SAXAdapter start element handler.
        Parameters:
        uri - element uri.
        localName - element localName.
        qName - element qName.
        attributes - element attributes.
        Throws:
        org.xml.sax.SAXException - if there is an error.
      • onEndElement

        public void onEndElement​(java.lang.String uri,
                                 java.lang.String localName,
                                 java.lang.String qName,
                                 java.lang.String content)
                          throws org.xml.sax.SAXException
        SAXAdapter end element handler. Content only includes characters that were read from this element, NOT any characters from child elements.
        Parameters:
        uri - element uri.
        localName - element localName.
        qName - element qName.
        content - element content.
        Throws:
        org.xml.sax.SAXException - if onEndElement throws a SAXException.
      • startElement

        public final void startElement​(java.lang.String uri,
                                       java.lang.String localName,
                                       java.lang.String qName,
                                       org.xml.sax.Attributes attributes)
                                throws org.xml.sax.SAXException
        Override DefaultHandler startElement. Adds a new element content buffer and calls onStartElement.
        Specified by:
        startElement in interface org.xml.sax.ContentHandler
        Overrides:
        startElement in class org.xml.sax.helpers.DefaultHandler
        Parameters:
        uri - element uri.
        localName - element localName.
        qName - element qName.
        attributes - element attributes.
        Throws:
        org.xml.sax.SAXException - if onStartElement throws a SAXException.
      • endElement

        public final void endElement​(java.lang.String uri,
                                     java.lang.String localName,
                                     java.lang.String qName)
                              throws org.xml.sax.SAXException
        Override DefaultHandler endElement. Retrieves element content buffer and passes it to onEndElement.
        Specified by:
        endElement in interface org.xml.sax.ContentHandler
        Overrides:
        endElement in class org.xml.sax.helpers.DefaultHandler
        Parameters:
        uri - element uri.
        localName - element localName.
        qName - element qName.
        Throws:
        org.xml.sax.SAXException - if onEndElement throws a SAXException.
      • characters

        public final void characters​(char[] ch,
                                     int start,
                                     int length)
                              throws org.xml.sax.SAXException
        Override DefaultHandler characters. Appends content to current element buffer, or skips if before first element.
        Specified by:
        characters in interface org.xml.sax.ContentHandler
        Overrides:
        characters in class org.xml.sax.helpers.DefaultHandler
        Parameters:
        ch - content.
        start - position in content to read.
        length - lenth of content to read.
        Throws:
        org.xml.sax.SAXException - never.
      • parse

        public final java.lang.Exception parse​(java.lang.String xml)
        Use this handler to parse a string. Wraps string bytes in a ByteArrayInputStream.
        Parameters:
        xml - string containing xml to parse.
        Returns:
        any exception that occurs while parsing, or null if no exceptions occur.
      • parse

        public final java.lang.Exception parse​(java.io.InputStream xml)
        Use this handler to parse an input stream. Uses self as a content and error handler in an XMLReader. If an error occurs, use getException to retrieve the error.
        Parameters:
        xml - input stream of xml to parse.
        Returns:
        any exception that occurs while parsing, or null if no exceptions occur.