public class SAXContentHandler extends DefaultHandler implements LexicalHandler, DeclHandler, DTDHandler
SAXHandler
builds a DOM4J tree via SAX events.
Modifier and Type | Field and Description |
---|---|
private Map |
availableNamespaceMap
namespaces that are available for use
|
private Element |
currentElement
The current element we are on
|
private int |
declaredNamespaceIndex
The number of namespaces that are declared in the current scope
|
private List |
declaredNamespaceList
declared namespaces that are not yet available for use
|
private Document |
document
The document that is being built
|
private DocumentFactory |
documentFactory
The factory used to create new
Document instances |
private ElementHandler |
elementHandler
the
ElementHandler called as the elements are complete |
private ElementStack |
elementStack
stack of
Element objects |
private String |
entity
The name of the current entity
|
private int |
entityLevel
The number of levels deep we are inside a startEntity / endEntity call
|
private EntityResolver |
entityResolver
The entity resolver
|
private List |
externalDTDDeclarations
external DTD declarations
|
private boolean |
includeExternalDTDDeclarations
Should external DTD declarations be expanded into a List in the DTD
|
private boolean |
includeInternalDTDDeclarations
Should internal DTD declarations be expanded into a List in the DTD
|
private InputSource |
inputSource |
private boolean |
insideCDATASection
Flag used to indicate that we are inside a CDATA section
|
private boolean |
insideDTDSection
Flag used to indicate that we are inside a DTD section
|
private List |
internalDTDDeclarations
internal DTD declarations
|
private boolean |
internalDTDsubset
Are we in an internal DTD subset?
|
private boolean |
mergeAdjacentText
Whether adjacent text nodes should be merged
|
private NamespaceStack |
namespaceStack
stack of
Namespace and QName objects |
private boolean |
stripWhitespaceText
Holds value of property stripWhitespaceText.
|
private StringBuffer |
textBuffer
Buffer used to concatenate text together
|
private boolean |
textInTextBuffer
Have we added text to the buffer
|
Constructor and Description |
---|
SAXContentHandler() |
SAXContentHandler(DocumentFactory documentFactory) |
SAXContentHandler(DocumentFactory documentFactory,
ElementHandler elementHandler) |
SAXContentHandler(DocumentFactory documentFactory,
ElementHandler elementHandler,
ElementStack elementStack) |
Modifier and Type | Method and Description |
---|---|
protected void |
addAttributes(Element element,
Attributes attributes)
Add all the attributes to the given elements
|
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
|
protected void |
addDTDDeclaration(Object declaration)
Adds an internal DTD declaration to the list of declarations
|
protected void |
addExternalDTDDeclaration(Object declaration)
Adds an external DTD declaration to the list of declarations
|
void |
attributeDecl(String eName,
String aName,
String type,
String valueDefault,
String value)
Report an attribute type declaration.
|
void |
characters(char[] ch,
int start,
int end)
Receive notification of character data inside an element.
|
void |
comment(char[] ch,
int start,
int end)
Report an XML comment anywhere in the document.
|
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
|
protected Document |
createDocument() |
protected ElementStack |
createElementStack() |
void |
elementDecl(String name,
String model)
Report an element type declaration.
|
void |
endCDATA()
Report the end of a CDATA section.
|
void |
endDocument()
Receive notification of the end of the document.
|
void |
endDTD()
Report the end of DTD declarations.
|
void |
endElement(String namespaceURI,
String localName,
String qName)
Receive notification of the end of an element.
|
void |
endEntity(String name)
Report the end of an entity.
|
void |
endPrefixMapping(String prefix)
Receive notification of the end of a Namespace mapping.
|
void |
error(SAXParseException exception)
This method is called when an error is detected during parsing
such as a validation error.
|
void |
externalEntityDecl(String name,
String publicID,
String systemID)
Report a parsed external entity declaration.
|
void |
fatalError(SAXParseException exception)
This method is called when a fatal error occurs during parsing.
|
Document |
getDocument() |
ElementStack |
getElementStack() |
EntityResolver |
getEntityResolver() |
InputSource |
getInputSource() |
void |
internalEntityDecl(String name,
String value)
Report an internal entity declaration.
|
protected boolean |
isIgnorableEntity(String name)
a Strategy Method to determine if a given entity name is ignorable
|
boolean |
isIncludeExternalDTDDeclarations() |
boolean |
isIncludeInternalDTDDeclarations() |
boolean |
isMergeAdjacentText()
Returns whether adjacent text nodes should be merged together.
|
boolean |
isStripWhitespaceText()
Sets whether whitespace between element start and end tags should be ignored
|
void |
notationDecl(String name,
String publicId,
String systemId)
Receive notification of a notation declaration event.
|
void |
processingInstruction(String target,
String data)
Receive notification of a processing instruction.
|
void |
setElementStack(ElementStack elementStack) |
void |
setEntityResolver(EntityResolver entityResolver) |
void |
setIncludeExternalDTDDeclarations(boolean includeExternalDTDDeclarations)
Sets whether DTD external declarations should be expanded into the DocumentType
object or not.
|
void |
setIncludeInternalDTDDeclarations(boolean includeInternalDTDDeclarations)
Sets whether internal DTD declarations should be expanded into the DocumentType
object or not.
|
void |
setInputSource(InputSource inputSource) |
void |
setMergeAdjacentText(boolean mergeAdjacentText)
Sets whether or not adjacent text nodes should be merged
together when parsing.
|
void |
setStripWhitespaceText(boolean stripWhitespaceText)
Sets whether whitespace between element start and end tags should be ignored.
|
void |
startCDATA()
Report the start of a CDATA section.
|
void |
startDocument()
Receive notification of the beginning of the document.
|
void |
startDTD(String name,
String publicId,
String systemId)
Report the start of DTD declarations, if any.
|
void |
startElement(String namespaceURI,
String localName,
String qualifiedName,
Attributes attributes)
Receive notification of the start of an element.
|
void |
startEntity(String name)
Report the beginning of some internal and external XML entities.
|
void |
startPrefixMapping(String prefix,
String uri)
Receive notification of the start of a Namespace mapping.
|
void |
unparsedEntityDecl(String name,
String publicId,
String systemId,
String notationName)
Receive notification of an unparsed entity declaration event.
|
void |
warning(SAXParseException exception)
This method is called when a warning occurs during the parsing
of the document.
|
ignorableWhitespace, resolveEntity, setDocumentLocator, skippedEntity
private DocumentFactory documentFactory
Document
instancesprivate ElementStack elementStack
Element
objectsprivate NamespaceStack namespaceStack
Namespace
and QName
objectsprivate ElementHandler elementHandler
ElementHandler
called as the elements are completeprivate boolean insideDTDSection
private boolean insideCDATASection
private Map availableNamespaceMap
private List declaredNamespaceList
private List internalDTDDeclarations
private List externalDTDDeclarations
private int declaredNamespaceIndex
private EntityResolver entityResolver
private InputSource inputSource
private Element currentElement
private boolean includeInternalDTDDeclarations
private boolean includeExternalDTDDeclarations
private int entityLevel
private boolean internalDTDsubset
private boolean mergeAdjacentText
private boolean textInTextBuffer
private StringBuffer textBuffer
private boolean stripWhitespaceText
public SAXContentHandler()
public SAXContentHandler(DocumentFactory documentFactory)
public SAXContentHandler(DocumentFactory documentFactory, ElementHandler elementHandler)
public SAXContentHandler(DocumentFactory documentFactory, ElementHandler elementHandler, ElementStack elementStack)
public Document getDocument()
public void processingInstruction(String target, String data) throws SAXException
DefaultHandler
By default, do nothing. Application writers may override this method in a subclass to take specific actions for each processing instruction, such as setting status variables or invoking other methods.
processingInstruction
in interface ContentHandler
processingInstruction
in class DefaultHandler
target
- The processing instruction target.data
- The processing instruction data, or null if
none is supplied.SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.processingInstruction(java.lang.String, java.lang.String)
public void startPrefixMapping(String prefix, String uri) throws SAXException
DefaultHandler
By default, do nothing. Application writers may override this method in a subclass to take specific actions at the start of each Namespace prefix scope (such as storing the prefix mapping).
startPrefixMapping
in interface ContentHandler
startPrefixMapping
in class DefaultHandler
prefix
- The Namespace prefix being declared.uri
- The Namespace URI mapped to the prefix.SAXException
- The client may throw
an exception during processing.ContentHandler.startPrefixMapping(java.lang.String, java.lang.String)
public void endPrefixMapping(String prefix) throws SAXException
DefaultHandler
By default, do nothing. Application writers may override this method in a subclass to take specific actions at the end of each prefix mapping.
endPrefixMapping
in interface ContentHandler
endPrefixMapping
in class DefaultHandler
prefix
- The Namespace prefix being declared.SAXException
- The client may throw
an exception during processing.ContentHandler.endPrefixMapping(java.lang.String)
public void startDocument() throws SAXException
DefaultHandler
By default, do nothing. Application writers may override this method in a subclass to take specific actions at the beginning of a document (such as allocating the root node of a tree or creating an output file).
startDocument
in interface ContentHandler
startDocument
in class DefaultHandler
SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.startDocument()
public void endDocument() throws SAXException
DefaultHandler
By default, do nothing. Application writers may override this method in a subclass to take specific actions at the end of a document (such as finalising a tree or closing an output file).
endDocument
in interface ContentHandler
endDocument
in class DefaultHandler
SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.endDocument()
public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes attributes) throws SAXException
DefaultHandler
By default, do nothing. Application writers may override this method in a subclass to take specific actions at the start of each element (such as allocating a new tree node or writing output to a file).
startElement
in interface ContentHandler
startElement
in class DefaultHandler
localName
- The local name (without prefix), or the
empty string if Namespace processing is not being
performed.qualifiedName
- The qualified name (with prefix), or the
empty string if qualified names are not available.attributes
- The specified or defaulted attributes.SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
public void endElement(String namespaceURI, String localName, String qName)
DefaultHandler
By default, do nothing. Application writers may override this method in a subclass to take specific actions at the end of each element (such as finalising a tree node or writing output to a file).
endElement
in interface ContentHandler
endElement
in class DefaultHandler
localName
- The local name (without prefix), or the
empty string if Namespace processing is not being
performed.qName
- The qualified XML 1.0 name (with prefix), or the
empty string if qualified names are not available.ContentHandler.endElement(java.lang.String, java.lang.String, java.lang.String)
public void characters(char[] ch, int start, int end) throws SAXException
DefaultHandler
By default, do nothing. Application writers may override this method to take specific actions for each chunk of character data (such as adding the data to a node or buffer, or printing it to a file).
characters
in interface ContentHandler
characters
in class DefaultHandler
ch
- The characters.start
- The start position in the character array.end
- The number of characters to use from the
character array.SAXException
- Any SAX exception, possibly
wrapping another exception.ContentHandler.characters(char[], int, int)
public void warning(SAXParseException exception) throws SAXException
warning
in interface ErrorHandler
warning
in class DefaultHandler
exception
- The warning information encoded as an exception.SAXException
- Any SAX exception, possibly
wrapping another exception.ErrorHandler.warning(org.xml.sax.SAXParseException)
,
SAXParseException
public void error(SAXParseException exception) throws SAXException
error
in interface ErrorHandler
error
in class DefaultHandler
exception
- The warning information encoded as an exception.SAXException
- Any SAX exception, possibly
wrapping another exception.ErrorHandler.warning(org.xml.sax.SAXParseException)
,
SAXParseException
public void fatalError(SAXParseException exception) throws SAXException
fatalError
in interface ErrorHandler
fatalError
in class DefaultHandler
exception
- The error information encoded as an exception.SAXException
- Any SAX exception, possibly
wrapping another exception.ErrorHandler.fatalError(org.xml.sax.SAXParseException)
,
SAXParseException
public void startDTD(String name, String publicId, String systemId) throws SAXException
LexicalHandler
This method is intended to report the beginning of the DOCTYPE declaration; if the document has no DOCTYPE declaration, this method will not be invoked.
All declarations reported through
DTDHandler
or
DeclHandler
events must appear
between the startDTD and endDTD
events.
Declarations are assumed to belong to the internal DTD subset
unless they appear between startEntity
and endEntity
events. Comments and
processing instructions from the DTD should also be reported
between the startDTD and endDTD events.
Note that the start/endDTD events will appear within
the start/endDocument events from ContentHandler and
before the first
startElement
event.
startDTD
in interface LexicalHandler
name
- The document type name.publicId
- The declared public identifier for the
external DTD subset, or null if none was declared.systemId
- The declared system identifier for the
external DTD subset, or null if none was declared.SAXException
- The application may raise an
exception.LexicalHandler.endDTD()
,
LexicalHandler.startEntity(java.lang.String)
public void endDTD() throws SAXException
LexicalHandler
This method is intended to report the end of the DOCTYPE declaration; if the document has no DOCTYPE declaration, this method will not be invoked.
endDTD
in interface LexicalHandler
SAXException
- The application may raise an exception.LexicalHandler.startDTD(java.lang.String, java.lang.String, java.lang.String)
public void startEntity(String name) throws SAXException
LexicalHandler
The reporting of parameter entities (including
the external DTD subset) is optional, and SAX2 drivers that
support LexicalHandler may not support it; you can use the
http://xml.org/sax/features/lexical-handler/parameter-entities
feature to query or control the reporting of parameter entities.
General entities are reported with their regular names, parameter entities have '%' prepended to their names, and the external DTD subset has the pseudo-entity name "[dtd]".
When a SAX2 driver is providing these events, all other
events must be properly nested within start/end entity
events. There is no additional requirement that events from
DeclHandler
or
DTDHandler
be properly ordered.
Note that skipped entities will be reported through the
skippedEntity
event, which is part of the ContentHandler interface.
Because of the streaming event model that SAX uses, some entity boundaries cannot be reported under any circumstances:
These will be silently expanded, with no indication of where the original entity boundaries were.
Note also that the boundaries of character references (which are not really entities anyway) are not reported.
All start/endEntity events must be properly nested.
startEntity
in interface LexicalHandler
name
- The name of the entity. If it is a parameter
entity, the name will begin with '%', and if it is the
external DTD subset, it will be "[dtd]".SAXException
- The application may raise an exception.LexicalHandler.endEntity(java.lang.String)
,
DeclHandler.internalEntityDecl(java.lang.String, java.lang.String)
,
DeclHandler.externalEntityDecl(java.lang.String, java.lang.String, java.lang.String)
public void endEntity(String name) throws SAXException
LexicalHandler
endEntity
in interface LexicalHandler
name
- The name of the entity that is ending.SAXException
- The application may raise an exception.LexicalHandler.startEntity(java.lang.String)
public void startCDATA() throws SAXException
LexicalHandler
The contents of the CDATA section will be reported through
the regular characters
event; this event is intended only to report
the boundary.
startCDATA
in interface LexicalHandler
SAXException
- The application may raise an exception.LexicalHandler.endCDATA()
public void endCDATA() throws SAXException
LexicalHandler
endCDATA
in interface LexicalHandler
SAXException
- The application may raise an exception.LexicalHandler.startCDATA()
public void comment(char[] ch, int start, int end) throws SAXException
LexicalHandler
This callback will be used for comments inside or outside the document element, including comments in the external DTD subset (if read). Comments in the DTD must be properly nested inside start/endDTD and start/endEntity events (if used).
comment
in interface LexicalHandler
ch
- An array holding the characters in the comment.start
- The starting position in the array.end
- The number of characters to use from the array.SAXException
- The application may raise an exception.public void elementDecl(String name, String model) throws SAXException
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.
elementDecl
in interface DeclHandler
name
- The element type name.model
- The content model as a normalized string.SAXException
- The application may raise an exception.public void attributeDecl(String eName, String aName, String type, String valueDefault, String value) throws SAXException
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.
attributeDecl
in interface DeclHandler
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.value
- A string representing the attribute's default value,
or null if there is none.SAXException
- The application may raise an exception.public void internalEntityDecl(String name, String value) throws SAXException
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.
internalEntityDecl
in interface DeclHandler
name
- The name of the entity. If it is a parameter
entity, the name will begin with '%'.value
- The replacement text of the entity.SAXException
- The application may raise an exception.externalEntityDecl(java.lang.String, java.lang.String, java.lang.String)
,
DTDHandler.unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
public void externalEntityDecl(String name, String publicID, String systemID) throws SAXException
Only the effective (first) declaration for each entity will be reported.
externalEntityDecl
in interface DeclHandler
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.systemId
- The declared system identifier of the entity.SAXException
- The application may raise an exception.internalEntityDecl(java.lang.String, java.lang.String)
,
DTDHandler.unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
public void notationDecl(String name, String publicId, String systemId) throws SAXException
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.
notationDecl
in interface DTDHandler
notationDecl
in class DefaultHandler
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.SAXException
- Any SAX exception, possibly
wrapping another exception.unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
,
AttributeList
public void unparsedEntityDecl(String name, String publicId, String systemId, String notationName) throws SAXException
Note that the notation name corresponds to a notation
reported by the notationDecl
event.
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.
unparsedEntityDecl
in interface DTDHandler
unparsedEntityDecl
in class DefaultHandler
name
- The unparsed entity's name.publicId
- The entity's public identifier, or null if none
was given.systemId
- The entity's system identifier.notation
- name The name of the associated notation.SAXException
- Any SAX exception, possibly
wrapping another exception.notationDecl(java.lang.String, java.lang.String, java.lang.String)
,
AttributeList
public ElementStack getElementStack()
public void setElementStack(ElementStack elementStack)
public EntityResolver getEntityResolver()
public void setEntityResolver(EntityResolver entityResolver)
public InputSource getInputSource()
public void setInputSource(InputSource inputSource)
public boolean isIncludeInternalDTDDeclarations()
public void setIncludeInternalDTDDeclarations(boolean includeInternalDTDDeclarations)
includeInternalDTDDeclarations
- whether or not DTD declarations should be expanded
and included into the DocumentType object.public boolean isIncludeExternalDTDDeclarations()
public void setIncludeExternalDTDDeclarations(boolean includeExternalDTDDeclarations)
includeInternalDTDDeclarations
- whether or not DTD declarations should be expanded
and included into the DocumentType object.public boolean isMergeAdjacentText()
public void setMergeAdjacentText(boolean mergeAdjacentText)
mergeAdjacentText
- New value of property mergeAdjacentText.public boolean isStripWhitespaceText()
public void setStripWhitespaceText(boolean stripWhitespaceText)
stripWhitespaceText
- New value of property stripWhitespaceText.protected void completeCurrentTextNode()
protected Document createDocument()
protected boolean isIgnorableEntity(String name)
protected void addDeclaredNamespaces(Element element)
protected void addAttributes(Element element, Attributes attributes)
protected void addDTDDeclaration(Object declaration)
protected void addExternalDTDDeclaration(Object declaration)
protected ElementStack createElementStack()
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses