Package org.dom4j.io

Class SAXContentHandler

  • All Implemented Interfaces:
    org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler, org.xml.sax.ext.DeclHandler, org.xml.sax.ext.LexicalHandler
    Direct Known Subclasses:
    SAXModifyContentHandler

    public class SAXContentHandler
    extends org.xml.sax.helpers.DefaultHandler
    implements org.xml.sax.ext.LexicalHandler, org.xml.sax.ext.DeclHandler, org.xml.sax.DTDHandler

    SAXContentHandler builds a dom4j tree via SAX events.

    Version:
    $Revision: 1.61 $
    • Field Detail

      • documentFactory

        private DocumentFactory documentFactory
        The factory used to create new Document instances
      • document

        private Document document
        The document that is being built
      • elementStack

        private ElementStack elementStack
        stack of Element objects
      • namespaceStack

        private NamespaceStack namespaceStack
        stack of Namespace and QName objects
      • elementHandler

        private ElementHandler elementHandler
        the ElementHandler called as the elements are complete
      • locator

        private org.xml.sax.Locator locator
        the Locator
      • entity

        private java.lang.String entity
        The name of the current entity
      • insideDTDSection

        private boolean insideDTDSection
        Flag used to indicate that we are inside a DTD section
      • insideCDATASection

        private boolean insideCDATASection
        Flag used to indicate that we are inside a CDATA section
      • cdataText

        private java.lang.StringBuffer cdataText
        buffer to hold contents of cdata section across multiple characters events
      • internalDTDDeclarations

        private java.util.List<Decl> internalDTDDeclarations
        internal DTD declarations
      • externalDTDDeclarations

        private java.util.List<Decl> externalDTDDeclarations
        external DTD declarations
      • declaredNamespaceIndex

        private int declaredNamespaceIndex
        The number of namespaces that are declared in the current scope
      • entityResolver

        private org.xml.sax.EntityResolver entityResolver
        The entity resolver
      • inputSource

        private org.xml.sax.InputSource inputSource
      • currentElement

        private Element currentElement
        The current element we are on
      • includeInternalDTDDeclarations

        private boolean includeInternalDTDDeclarations
        Should internal DTD declarations be expanded into a List in the DTD
      • includeExternalDTDDeclarations

        private boolean includeExternalDTDDeclarations
        Should external DTD declarations be expanded into a List in the DTD
      • entityLevel

        private int entityLevel
        The number of levels deep we are inside a startEntity/endEntity call
      • internalDTDsubset

        private boolean internalDTDsubset
        Are we in an internal DTD subset?
      • mergeAdjacentText

        private boolean mergeAdjacentText
        Whether adjacent text nodes should be merged
      • textInTextBuffer

        private boolean textInTextBuffer
        Have we added text to the buffer
      • ignoreComments

        private boolean ignoreComments
        Should we ignore comments
      • textBuffer

        private java.lang.StringBuffer textBuffer
        Buffer used to concatenate text together
      • stripWhitespaceText

        private boolean stripWhitespaceText
        Holds value of property stripWhitespaceText.
    • Method Detail

      • getDocument

        public Document getDocument()
        DOCUMENT ME!
        Returns:
        the document that has been or is being built
      • setDocumentLocator

        public void setDocumentLocator​(org.xml.sax.Locator documentLocator)
        Specified by:
        setDocumentLocator in interface org.xml.sax.ContentHandler
        Overrides:
        setDocumentLocator in class org.xml.sax.helpers.DefaultHandler
      • processingInstruction

        public void processingInstruction​(java.lang.String target,
                                          java.lang.String data)
                                   throws org.xml.sax.SAXException
        Specified by:
        processingInstruction in interface org.xml.sax.ContentHandler
        Overrides:
        processingInstruction in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • startPrefixMapping

        public void startPrefixMapping​(java.lang.String prefix,
                                       java.lang.String uri)
                                throws org.xml.sax.SAXException
        Specified by:
        startPrefixMapping in interface org.xml.sax.ContentHandler
        Overrides:
        startPrefixMapping in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • endPrefixMapping

        public void endPrefixMapping​(java.lang.String prefix)
                              throws org.xml.sax.SAXException
        Specified by:
        endPrefixMapping in interface org.xml.sax.ContentHandler
        Overrides:
        endPrefixMapping in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • startDocument

        public void startDocument()
                           throws org.xml.sax.SAXException
        Specified by:
        startDocument in interface org.xml.sax.ContentHandler
        Overrides:
        startDocument in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • endDocument

        public void endDocument()
                         throws org.xml.sax.SAXException
        Specified by:
        endDocument in interface org.xml.sax.ContentHandler
        Overrides:
        endDocument in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • startElement

        public void startElement​(java.lang.String namespaceURI,
                                 java.lang.String localName,
                                 java.lang.String qualifiedName,
                                 org.xml.sax.Attributes attributes)
                          throws org.xml.sax.SAXException
        Specified by:
        startElement in interface org.xml.sax.ContentHandler
        Overrides:
        startElement in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • endElement

        public void endElement​(java.lang.String namespaceURI,
                               java.lang.String localName,
                               java.lang.String qName)
                        throws org.xml.sax.SAXException
        Specified by:
        endElement in interface org.xml.sax.ContentHandler
        Overrides:
        endElement in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • characters

        public void characters​(char[] ch,
                               int start,
                               int end)
                        throws org.xml.sax.SAXException
        Specified by:
        characters in interface org.xml.sax.ContentHandler
        Overrides:
        characters in class org.xml.sax.helpers.DefaultHandler
        Throws:
        org.xml.sax.SAXException
      • warning

        public void warning​(org.xml.sax.SAXParseException exception)
                     throws org.xml.sax.SAXException
        This method is called when a warning occurs during the parsing of the document. This method does nothing.
        Specified by:
        warning in interface org.xml.sax.ErrorHandler
        Overrides:
        warning in class org.xml.sax.helpers.DefaultHandler
        Parameters:
        exception - DOCUMENT ME!
        Throws:
        org.xml.sax.SAXException - DOCUMENT ME!
      • error

        public void error​(org.xml.sax.SAXParseException exception)
                   throws org.xml.sax.SAXException
        This method is called when an error is detected during parsing such as a validation error. This method rethrows the exception
        Specified by:
        error in interface org.xml.sax.ErrorHandler
        Overrides:
        error in class org.xml.sax.helpers.DefaultHandler
        Parameters:
        exception - DOCUMENT ME!
        Throws:
        org.xml.sax.SAXException - DOCUMENT ME!
      • fatalError

        public void fatalError​(org.xml.sax.SAXParseException exception)
                        throws org.xml.sax.SAXException
        This method is called when a fatal error occurs during parsing. This method rethrows the exception
        Specified by:
        fatalError in interface org.xml.sax.ErrorHandler
        Overrides:
        fatalError in class org.xml.sax.helpers.DefaultHandler
        Parameters:
        exception - DOCUMENT ME!
        Throws:
        org.xml.sax.SAXException - DOCUMENT ME!
      • startDTD

        public void startDTD​(java.lang.String name,
                             java.lang.String publicId,
                             java.lang.String systemId)
                      throws org.xml.sax.SAXException
        Specified by:
        startDTD in interface org.xml.sax.ext.LexicalHandler
        Throws:
        org.xml.sax.SAXException
      • endDTD

        public void endDTD()
                    throws org.xml.sax.SAXException
        Specified by:
        endDTD in interface org.xml.sax.ext.LexicalHandler
        Throws:
        org.xml.sax.SAXException
      • startEntity

        public void startEntity​(java.lang.String name)
                         throws org.xml.sax.SAXException
        Specified by:
        startEntity in interface org.xml.sax.ext.LexicalHandler
        Throws:
        org.xml.sax.SAXException
      • endEntity

        public void endEntity​(java.lang.String name)
                       throws org.xml.sax.SAXException
        Specified by:
        endEntity in interface org.xml.sax.ext.LexicalHandler
        Throws:
        org.xml.sax.SAXException
      • startCDATA

        public void startCDATA()
                        throws org.xml.sax.SAXException
        Specified by:
        startCDATA in interface org.xml.sax.ext.LexicalHandler
        Throws:
        org.xml.sax.SAXException
      • endCDATA

        public void endCDATA()
                      throws org.xml.sax.SAXException
        Specified by:
        endCDATA in interface org.xml.sax.ext.LexicalHandler
        Throws:
        org.xml.sax.SAXException
      • comment

        public void comment​(char[] ch,
                            int start,
                            int end)
                     throws org.xml.sax.SAXException
        Specified by:
        comment in interface org.xml.sax.ext.LexicalHandler
        Throws:
        org.xml.sax.SAXException
      • elementDecl

        public void elementDecl​(java.lang.String name,
                                java.lang.String model)
                         throws org.xml.sax.SAXException
        Report an element type declaration.

        The content model will consist of the string "EMPTY", the string "ANY", or a parenthesised group, optionally followed by an occurrence indicator. The model will be normalized so that all parameter entities are fully resolved and all whitespace is removed,and will include the enclosing parentheses. Other normalization (such as removing redundant parentheses or simplifying occurrence indicators) is at the discretion of the parser.

        Specified by:
        elementDecl in interface org.xml.sax.ext.DeclHandler
        Parameters:
        name - The element type name.
        model - The content model as a normalized string.
        Throws:
        org.xml.sax.SAXException - The application may raise an exception.
      • attributeDecl

        public void attributeDecl​(java.lang.String eName,
                                  java.lang.String aName,
                                  java.lang.String type,
                                  java.lang.String valueDefault,
                                  java.lang.String val)
                           throws org.xml.sax.SAXException
        Report an attribute type declaration.

        Only the effective (first) declaration for an attribute will be reported. The type will be one of the strings "CDATA", "ID", "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY", "ENTITIES", a parenthesized token group with the separator "|" and all whitespace removed, or the word "NOTATION" followed by a space followed by a parenthesized token group with all whitespace removed.

        Any parameter entities in the attribute value will be expanded, but general entities will not.

        Specified by:
        attributeDecl in interface org.xml.sax.ext.DeclHandler
        Parameters:
        eName - The name of the associated element.
        aName - The name of the attribute.
        type - A string representing the attribute type.
        valueDefault - A string representing the attribute default ("#IMPLIED", "#REQUIRED", or "#FIXED") or null if none of these applies.
        val - A string representing the attribute's default value, or null if there is none.
        Throws:
        org.xml.sax.SAXException - The application may raise an exception.
      • internalEntityDecl

        public void internalEntityDecl​(java.lang.String name,
                                       java.lang.String value)
                                throws org.xml.sax.SAXException
        Report an internal entity declaration.

        Only the effective (first) declaration for each entity will be reported. All parameter entities in the value will be expanded, but general entities will not.

        Specified by:
        internalEntityDecl in interface org.xml.sax.ext.DeclHandler
        Parameters:
        name - The name of the entity. If it is a parameter entity, the name will begin with '%'.
        value - The replacement text of the entity.
        Throws:
        org.xml.sax.SAXException - The application may raise an exception.
        See Also:
        externalEntityDecl(java.lang.String, java.lang.String, java.lang.String), DTDHandler.unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
      • externalEntityDecl

        public void externalEntityDecl​(java.lang.String name,
                                       java.lang.String publicId,
                                       java.lang.String sysId)
                                throws org.xml.sax.SAXException
        Report a parsed external entity declaration.

        Only the effective (first) declaration for each entity will be reported.

        Specified by:
        externalEntityDecl in interface org.xml.sax.ext.DeclHandler
        Parameters:
        name - The name of the entity. If it is a parameter entity, the name will begin with '%'.
        publicId - The declared public identifier of the entity, or null if none was declared.
        sysId - The declared system identifier of the entity.
        Throws:
        org.xml.sax.SAXException - The application may raise an exception.
        See Also:
        internalEntityDecl(java.lang.String, java.lang.String), DTDHandler.unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
      • notationDecl

        public void notationDecl​(java.lang.String name,
                                 java.lang.String publicId,
                                 java.lang.String systemId)
                          throws org.xml.sax.SAXException
        Receive notification of a notation declaration event.

        It is up to the application to record the notation for later reference, if necessary.

        At least one of publicId and systemId must be non-null. If a system identifier is present, and it is a URL, the SAX parser must resolve it fully before passing it to the application through this event.

        There is no guarantee that the notation declaration will be reported before any unparsed entities that use it.

        Specified by:
        notationDecl in interface org.xml.sax.DTDHandler
        Overrides:
        notationDecl in class org.xml.sax.helpers.DefaultHandler
        Parameters:
        name - The notation name.
        publicId - The notation's public identifier, or null if none was given.
        systemId - The notation's system identifier, or null if none was given.
        Throws:
        org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.
        See Also:
        unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String), AttributeList
      • unparsedEntityDecl

        public void unparsedEntityDecl​(java.lang.String name,
                                       java.lang.String publicId,
                                       java.lang.String systemId,
                                       java.lang.String notationName)
                                throws org.xml.sax.SAXException
        Receive notification of an unparsed entity declaration event.

        Note that the notation name corresponds to a notation reported by the notationDeclevent. It is up to the application to record the entity for later reference, if necessary.

        If the system identifier is a URL, the parser must resolve it fully before passing it to the application.

        Specified by:
        unparsedEntityDecl in interface org.xml.sax.DTDHandler
        Overrides:
        unparsedEntityDecl in class org.xml.sax.helpers.DefaultHandler
        Parameters:
        name - The unparsed entity's name.
        publicId - The entity's public identifier, or null if none was given.
        systemId - The entity's system identifier.
        notationName - The name of the associated notation.
        Throws:
        org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.
        See Also:
        notationDecl(java.lang.String, java.lang.String, java.lang.String), AttributeList
      • setElementStack

        public void setElementStack​(ElementStack elementStack)
      • getEntityResolver

        public org.xml.sax.EntityResolver getEntityResolver()
      • setEntityResolver

        public void setEntityResolver​(org.xml.sax.EntityResolver entityResolver)
      • getInputSource

        public org.xml.sax.InputSource getInputSource()
      • setInputSource

        public void setInputSource​(org.xml.sax.InputSource inputSource)
      • isIncludeInternalDTDDeclarations

        public boolean isIncludeInternalDTDDeclarations()
        DOCUMENT ME!
        Returns:
        whether internal DTD declarations should be expanded into the DocumentType object or not.
      • setIncludeInternalDTDDeclarations

        public void setIncludeInternalDTDDeclarations​(boolean include)
        Sets whether internal DTD declarations should be expanded into the DocumentType object or not.
        Parameters:
        include - whether or not DTD declarations should be expanded and included into the DocumentType object.
      • isIncludeExternalDTDDeclarations

        public boolean isIncludeExternalDTDDeclarations()
        DOCUMENT ME!
        Returns:
        whether external DTD declarations should be expanded into the DocumentType object or not.
      • setIncludeExternalDTDDeclarations

        public void setIncludeExternalDTDDeclarations​(boolean include)
        Sets whether DTD external declarations should be expanded into the DocumentType object or not.
        Parameters:
        include - whether or not DTD declarations should be expanded and included into the DocumentType object.
      • isMergeAdjacentText

        public boolean isMergeAdjacentText()
        Returns whether adjacent text nodes should be merged together.
        Returns:
        Value of property mergeAdjacentText.
      • setMergeAdjacentText

        public void setMergeAdjacentText​(boolean mergeAdjacentText)
        Sets whether or not adjacent text nodes should be merged together when parsing.
        Parameters:
        mergeAdjacentText - New value of property mergeAdjacentText.
      • isStripWhitespaceText

        public boolean isStripWhitespaceText()
        Sets whether whitespace between element start and end tags should be ignored
        Returns:
        Value of property stripWhitespaceText.
      • setStripWhitespaceText

        public void setStripWhitespaceText​(boolean stripWhitespaceText)
        Sets whether whitespace between element start and end tags should be ignored.
        Parameters:
        stripWhitespaceText - New value of property stripWhitespaceText.
      • isIgnoreComments

        public boolean isIgnoreComments()
        Returns whether we should ignore comments or not.
        Returns:
        boolean
      • setIgnoreComments

        public void setIgnoreComments​(boolean ignoreComments)
        Sets whether we should ignore comments or not.
        Parameters:
        ignoreComments - whether we should ignore comments or not.
      • completeCurrentTextNode

        protected void completeCurrentTextNode()
        If the current text buffer contains any text then create a new text node with it and add it to the current element
      • createDocument

        protected Document createDocument()
        DOCUMENT ME!
        Returns:
        the current document
      • getEncoding

        private java.lang.String getEncoding()
      • isIgnorableEntity

        protected boolean isIgnorableEntity​(java.lang.String name)
        a Strategy Method to determine if a given entity name is ignorable
        Parameters:
        name - DOCUMENT ME!
        Returns:
        DOCUMENT ME!
      • addDeclaredNamespaces

        protected void addDeclaredNamespaces​(Element element)
        Add all namespaces declared before the startElement() SAX event to the current element so that they are available to child elements and attributes
        Parameters:
        element - DOCUMENT ME!
      • addAttributes

        protected void addAttributes​(Element element,
                                     org.xml.sax.Attributes attributes)
        Add all the attributes to the given elements
        Parameters:
        element - DOCUMENT ME!
        attributes - DOCUMENT ME!
      • addDTDDeclaration

        protected void addDTDDeclaration​(Decl declaration)
        Adds an internal DTD declaration to the list of declarations
        Parameters:
        declaration - DOCUMENT ME!
      • addExternalDTDDeclaration

        protected void addExternalDTDDeclaration​(Decl declaration)
        Adds an external DTD declaration to the list of declarations
        Parameters:
        declaration - DOCUMENT ME!
      • createElementStack

        protected ElementStack createElementStack()