Class DocumentFragmentHandler

java.lang.Object
org.xml.sax.helpers.DefaultHandler
fi.iki.jmtilli.javaxmlfrag.DocumentFragmentHandler
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

public abstract class DocumentFragmentHandler
extends org.xml.sax.helpers.DefaultHandler
Handler that is able to collect document fragments into DocumentFragment. Like DefaultHandler in SAX, but with the ability to collect parts of the document to a tree during parsing.
  • Constructor Summary

    Constructors 
    Constructor Description
    DocumentFragmentHandler()  
  • Method Summary

    Modifier and Type Method Description
    void characters​(char[] ch, int start, int length)
    Handler for character data.
    void endElement​(java.lang.String uri, java.lang.String localName, java.lang.String qName)
    Handler for element end.
    abstract void endXMLElement​(java.lang.String uri, java.lang.String localName, java.lang.String qName, DocumentFragment f)
    Handler for element end.
    boolean is​(java.lang.String... tags)
    Test the parsing context.
    static DocumentFragment parseWhole​(java.io.File f)
    Parse a whole document.
    static DocumentFragment parseWhole​(java.io.InputStream is)
    Parse a whole document.
    static DocumentFragment parseWhole​(java.lang.String uri)
    Parse a whole document.
    static DocumentFragment parseWhole​(javax.xml.parsers.SAXParserFactory fact, java.io.File f)
    Parse a whole document.
    static DocumentFragment parseWhole​(javax.xml.parsers.SAXParserFactory fact, java.io.InputStream is)
    Parse a whole document.
    static DocumentFragment parseWhole​(javax.xml.parsers.SAXParserFactory fact, java.lang.String uri)
    Parse a whole document.
    static DocumentFragment parseWhole​(javax.xml.parsers.SAXParserFactory fact, org.xml.sax.InputSource is)
    Parse a whole document.
    static DocumentFragment parseWhole​(org.xml.sax.InputSource is)
    Parse a whole document.
    void startElement​(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attributes)
    Handler for element start.
    void startFragmentCollection()
    Start fragment collection.
    abstract void startXMLElement​(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attributes)
    Handler for element start.
    void xmlCharacters​(char[] ch, int start, int length)
    Handler for character data.

    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

  • Method Details

    • is

      public final boolean is​(java.lang.String... tags)
      Test the parsing context. For example, for a document <doc><elt></elt></doc> the context is {"doc", "elt"} when parsing the inner "elt" element.
      Parameters:
      tags - The list of qualified tag names.
      Returns:
      Whether the parsing context is the tested one
    • startFragmentCollection

      public final void startFragmentCollection()
      Start fragment collection. Changes from sequential event-based parsing mode to a tree-based parsing mode. During fragment collection, the startXMLElement, endXMLElement and xmlCharacters calls are skipped until the whole fragment of the element the user requested has been collected, after which endXMLElement is called with the collected fragment as an argument. Can only be called from within startXMLElement.
    • startXMLElement

      public abstract void startXMLElement​(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attributes)
      Handler for element start. The derived class should implement this. This is not called during fragment collection. The user can start fragment collection by calling startFragmentCollection() from this method. Then the rest of the startXMLElement, endXMLElement and xmlCharacters are skipped until the whole fragment of the element the user requested has been collected, after which endXMLElement is called with the collected fragment as an argument.
      Parameters:
      uri - The namespace URI of the tag
      localName - The local name of the tag
      qName - The qualified name of the tag
      attributes - The attributes of the element
    • xmlCharacters

      public void xmlCharacters​(char[] ch, int start, int length)
      Handler for character data. The derived class may choose to implement this. This is not called during fragment collection.
      Parameters:
      ch - The character array
      start - Start position of the encountered characters witihin ch.
      length - The number of characters encountered.
    • endXMLElement

      public abstract void endXMLElement​(java.lang.String uri, java.lang.String localName, java.lang.String qName, DocumentFragment f)
      Handler for element end. The derived class should implement this. This is not called during fragment collection. After the fragment has been collected, this method is called with a non-null f argument. When fragment collection is not active, f is null.
      Parameters:
      uri - The namespace URI of the tag
      localName - The local name of the tag
      qName - The qualified name of the tag
      f - The document fragment the user requested or null
    • startElement

      public final void startElement​(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attributes)
      Handler for element start. May not be overridden by the derived class. The derived class should instead implement startXMLElement.
      Specified by:
      startElement in interface org.xml.sax.ContentHandler
      Overrides:
      startElement in class org.xml.sax.helpers.DefaultHandler
      Parameters:
      uri - The namespace URI of the tag
      localName - The local name of the tag
      qName - The qualified name of the tag
      attributes - The attributes of the element
    • characters

      public final void characters​(char[] ch, int start, int length)
      Handler for character data. May not be overridden by the derived class. The derived class should instead implement xmlCharacters.
      Specified by:
      characters in interface org.xml.sax.ContentHandler
      Overrides:
      characters in class org.xml.sax.helpers.DefaultHandler
      Parameters:
      ch - The character array
      start - Start position of the encountered characters witihin ch.
      length - The number of characters encountered.
    • endElement

      public void endElement​(java.lang.String uri, java.lang.String localName, java.lang.String qName)
      Handler for element end. May not be overridden by the derived class. The derived class should instead implement endXMLElement.
      Specified by:
      endElement in interface org.xml.sax.ContentHandler
      Overrides:
      endElement in class org.xml.sax.helpers.DefaultHandler
      Parameters:
      uri - The namespace URI of the tag
      localName - The local name of the tag
      qName - The qualified name of the tag
    • parseWhole

      public static DocumentFragment parseWhole​(javax.xml.parsers.SAXParserFactory fact, java.io.File f) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException
      Parse a whole document.
      Parameters:
      fact - A parser factory
      f - File of the document
      Returns:
      A parse tree of the document
      Throws:
      javax.xml.parsers.ParserConfigurationException
      org.xml.sax.SAXException
      java.io.IOException
    • parseWhole

      public static DocumentFragment parseWhole​(java.io.File f) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException
      Parse a whole document.
      Parameters:
      f - File of the document
      Returns:
      A parse tree of the document
      Throws:
      javax.xml.parsers.ParserConfigurationException
      org.xml.sax.SAXException
      java.io.IOException
    • parseWhole

      public static DocumentFragment parseWhole​(javax.xml.parsers.SAXParserFactory fact, java.io.InputStream is) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException
      Parse a whole document.
      Parameters:
      fact - A parser factory
      is - InputStream of the document
      Returns:
      A parse tree of the document
      Throws:
      javax.xml.parsers.ParserConfigurationException
      org.xml.sax.SAXException
      java.io.IOException
    • parseWhole

      public static DocumentFragment parseWhole​(java.io.InputStream is) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException
      Parse a whole document.
      Parameters:
      is - InputStream of the document
      Returns:
      A parse tree of the document
      Throws:
      javax.xml.parsers.ParserConfigurationException
      org.xml.sax.SAXException
      java.io.IOException
    • parseWhole

      public static DocumentFragment parseWhole​(javax.xml.parsers.SAXParserFactory fact, org.xml.sax.InputSource is) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException
      Parse a whole document.
      Parameters:
      fact - A parser factory
      is - InputSource of the document
      Returns:
      A parse tree of the document
      Throws:
      javax.xml.parsers.ParserConfigurationException
      org.xml.sax.SAXException
      java.io.IOException
    • parseWhole

      public static DocumentFragment parseWhole​(org.xml.sax.InputSource is) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException
      Parse a whole document.
      Parameters:
      is - InputSource of the document
      Returns:
      A parse tree of the document
      Throws:
      javax.xml.parsers.ParserConfigurationException
      org.xml.sax.SAXException
      java.io.IOException
    • parseWhole

      public static DocumentFragment parseWhole​(javax.xml.parsers.SAXParserFactory fact, java.lang.String uri) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException
      Parse a whole document.
      Parameters:
      fact - A parser factory
      uri - URI of the document
      Returns:
      A parse tree of the document
      Throws:
      javax.xml.parsers.ParserConfigurationException
      org.xml.sax.SAXException
      java.io.IOException
    • parseWhole

      public static DocumentFragment parseWhole​(java.lang.String uri) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException
      Parse a whole document.
      Parameters:
      uri - URI of the document
      Returns:
      A parse tree of the document
      Throws:
      javax.xml.parsers.ParserConfigurationException
      org.xml.sax.SAXException
      java.io.IOException