Package gov.usgs.earthquake.product.io
Class XmlProductSource
- java.lang.Object
-
- org.xml.sax.helpers.DefaultHandler
-
- gov.usgs.earthquake.product.io.XmlProductSource
-
- All Implemented Interfaces:
ProductSource,org.xml.sax.ContentHandler,org.xml.sax.DTDHandler,org.xml.sax.EntityResolver,org.xml.sax.ErrorHandler
- Direct Known Subclasses:
SearchResponseXmlProductSource
public class XmlProductSource extends org.xml.sax.helpers.DefaultHandler implements ProductSource
Load a product from an InputStream containing XML.
-
-
Constructor Summary
Constructors Constructor Description XmlProductSource(ProductHandler out)Create a new XmlProductSource for embedding in another default handler.XmlProductSource(java.io.InputStream in)Create a new XmlProductSource.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcharacters(char[] ch, int start, int length)Override DefaultHandler characters.voidclose()Free any resources associated with this handler.voidcloseContent()Closes an open output streamvoidendElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)Override DefaultHandler endElement.protected ProductHandlergetHandler()java.io.InputStreamopenContentStream(boolean encoded)Set up a piped output stream used during parsing.protected voidsetHandler(ProductHandler out)voidstartElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attributes)Override DefaultHandler startElement.voidstreamTo(ProductHandler out)Begin reading the input stream, sending events to out.
-
-
-
Constructor Detail
-
XmlProductSource
public XmlProductSource(java.io.InputStream in)
Create a new XmlProductSource.- Parameters:
in- the input stream where xml is read.
-
XmlProductSource
public XmlProductSource(ProductHandler out)
Create a new XmlProductSource for embedding in another default handler.- Parameters:
out- the ProductHandler to receive product events.
-
-
Method Detail
-
streamTo
public void streamTo(ProductHandler out) throws java.lang.Exception
Begin reading the input stream, sending events to out.- Specified by:
streamToin interfaceProductSource- Parameters:
out- the receiving ProductOutput.- Throws:
java.lang.Exception- if error occurs
-
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.SAXExceptionOverride DefaultHandler startElement. Adds a new element content buffer and calls onStartElement.- Specified by:
startElementin interfaceorg.xml.sax.ContentHandler- Overrides:
startElementin classorg.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 void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName) throws org.xml.sax.SAXExceptionOverride DefaultHandler endElement. Retrieves element content buffer and passes it to onEndElement.- Specified by:
endElementin interfaceorg.xml.sax.ContentHandler- Overrides:
endElementin classorg.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 void characters(char[] ch, int start, int length) throws org.xml.sax.SAXExceptionOverride DefaultHandler characters. Appends content to current element buffer, or skips if before first element.- Specified by:
charactersin interfaceorg.xml.sax.ContentHandler- Overrides:
charactersin classorg.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.
-
getHandler
protected ProductHandler getHandler()
- Returns:
- ProductHandler
-
setHandler
protected void setHandler(ProductHandler out)
- Parameters:
out- ProductHandler to set
-
close
public void close()
Free any resources associated with this handler.- Specified by:
closein interfaceProductSource
-
openContentStream
public java.io.InputStream openContentStream(boolean encoded) throws java.io.IOExceptionSet up a piped output stream used during parsing. The XmlProductSource parsing thread starts a background thread to deliver content to the handler, then continues parsing XML and delivers parsed content via the piped streams. If xml parsing completes as expected, the parsing thread will close the connection incloseContent(). If errors occur, the objects handling the product source object call closeContent to ensure the resource is closed.- Parameters:
encoded- if it needs to decode base 64 content- Returns:
- a input stream of the Piped output stream
- Throws:
java.io.IOException- if io error occurs
-
closeContent
public void closeContent()
Closes an open output stream
-
-