Class XmlProductSource

java.lang.Object
org.xml.sax.helpers.DefaultHandler
gov.usgs.earthquake.product.io.XmlProductSource
All Implemented Interfaces:
ProductSource, ContentHandler, DTDHandler, EntityResolver, ErrorHandler
Direct Known Subclasses:
SearchResponseXmlProductSource

public class XmlProductSource extends DefaultHandler implements ProductSource
Load a product from an InputStream containing XML.
  • Constructor Details

    • XmlProductSource

      public XmlProductSource(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 Details

    • streamTo

      public void streamTo(ProductHandler out) throws Exception
      Begin reading the input stream, sending events to out.
      Specified by:
      streamTo in interface ProductSource
      Parameters:
      out - the receiving ProductOutput.
      Throws:
      Exception - if error occurs
    • startElement

      public 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 interface ContentHandler
      Overrides:
      startElement in class DefaultHandler
      Parameters:
      uri - element uri.
      localName - element localName.
      qName - element qName.
      attributes - element attributes.
      Throws:
      SAXException - if onStartElement throws a SAXException.
    • endElement

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

      public void characters(char[] ch, int start, int length) throws SAXException
      Override DefaultHandler characters. Appends content to current element buffer, or skips if before first element.
      Specified by:
      characters in interface ContentHandler
      Overrides:
      characters in class DefaultHandler
      Parameters:
      ch - content.
      start - position in content to read.
      length - lenth of content to read.
      Throws:
      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:
      close in interface ProductSource
    • openContentStream

      public InputStream openContentStream(boolean encoded) throws IOException
      Set 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 in closeContent(). 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:
      IOException - if io error occurs
    • closeContent

      public void closeContent()
      Closes an open output stream