Package gov.usgs.util
Class SAXAdapter
java.lang.Object
org.xml.sax.helpers.DefaultHandler
gov.usgs.util.SAXAdapter
- All Implemented Interfaces:
ContentHandler
,DTDHandler
,EntityResolver
,ErrorHandler
- Direct Known Subclasses:
EventAddonParser
,RegionsHandler
,URLNotificationParser
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 -
Method Summary
Modifier and TypeMethodDescriptionfinal void
characters
(char[] ch, int start, int length) Override DefaultHandler characters.final void
endElement
(String uri, String localName, String qName) Override DefaultHandler endElement.void
onEndElement
(String uri, String localName, String qName, String content) SAXAdapter end element handler.void
onStartElement
(String uri, String localName, String qName, Attributes attributes) SAXAdapter start element handler.final Exception
parse
(InputStream xml) Use this handler to parse an input stream.final Exception
Use this handler to parse a string.final void
startElement
(String uri, String localName, String qName, 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
Methods inherited from interface org.xml.sax.ContentHandler
declaration
-
Constructor Details
-
SAXAdapter
public SAXAdapter()
-
-
Method Details
-
onStartElement
public void onStartElement(String uri, String localName, String qName, Attributes attributes) throws SAXException SAXAdapter start element handler.- Parameters:
uri
- element uri.localName
- element localName.qName
- element qName.attributes
- element attributes.- Throws:
SAXException
- if there is an error.
-
onEndElement
public void onEndElement(String uri, String localName, String qName, String content) throws 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:
SAXException
- if onEndElement throws a SAXException.
-
startElement
public final void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException Override DefaultHandler startElement. Adds a new element content buffer and calls onStartElement.- Specified by:
startElement
in interfaceContentHandler
- Overrides:
startElement
in classDefaultHandler
- Parameters:
uri
- element uri.localName
- element localName.qName
- element qName.attributes
- element attributes.- Throws:
SAXException
- if onStartElement throws a SAXException.
-
endElement
Override DefaultHandler endElement. Retrieves element content buffer and passes it to onEndElement.- Specified by:
endElement
in interfaceContentHandler
- Overrides:
endElement
in classDefaultHandler
- Parameters:
uri
- element uri.localName
- element localName.qName
- element qName.- Throws:
SAXException
- if onEndElement throws a SAXException.
-
characters
Override DefaultHandler characters. Appends content to current element buffer, or skips if before first element.- Specified by:
characters
in interfaceContentHandler
- Overrides:
characters
in classDefaultHandler
- Parameters:
ch
- content.start
- position in content to read.length
- lenth of content to read.- Throws:
SAXException
- never.
-
parse
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
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.
-