Package org.apache.commons.digester
Class NodeCreateRule
java.lang.Object
org.apache.commons.digester.Rule
org.apache.commons.digester.NodeCreateRule
A rule implementation that creates a DOM
Node
containing the XML at the element that matched
the rule. Two concrete types of nodes can be created by this rule:
- the default is to create an
Element
node. The created element will correspond to the element that matched the rule, containing all XML content underneath that element. - alternatively, this rule can create nodes of type
DocumentFragment
, which will contain only the XML content under the element the rule was trigged on.
The created Node
will be pushed on Digester's object stack
when done. To use it in the context of another DOM
Document
, it must be imported first, using the
Document method
importNode()
.
Important Note: This is implemented by replacing the SAX
ContentHandler
in the parser used by
Digester, and resetting it when the matched element is closed. As a side
effect, rules that would match XML nodes under the element that matches
a NodeCreateRule
will never be triggered by Digester, which
usually is the behavior one would expect.
Note that the current implementation does not set the namespace prefixes in the exported nodes. The (usually more important) namespace URIs are set, of course.
- Since:
- Digester 1.4
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate class
The SAX content handler that does all the actual work of assembling the DOM node tree from the SAX events. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate DocumentBuilder
The JAXPDocumentBuilder
to use.private int
The type of the node that should be created.Fields inherited from class org.apache.commons.digester.Rule
digester, namespaceURI
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.NodeCreateRule
(int nodeType) Constructor.NodeCreateRule
(int nodeType, DocumentBuilder documentBuilder) Constructor.NodeCreateRule
(DocumentBuilder documentBuilder) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
begin
(String namespaceURI, String name, Attributes attributes) When this method fires, the digester is told to forward all SAX ContentHandler events to the builder object, resulting in a DOM being built instead of normal digester rule-handling occurring.void
end()
Pop the Node off the top of the stack.Methods inherited from class org.apache.commons.digester.Rule
begin, body, body, end, finish, getDigester, getNamespaceURI, setDigester, setNamespaceURI
-
Field Details
-
documentBuilder
The JAXPDocumentBuilder
to use. -
nodeType
private int nodeTypeThe type of the node that should be created. Must be one of the constants defined inNode
, but currently onlyNode.ELEMENT_NODE
andNode.DOCUMENT_FRAGMENT_NODE
are allowed values.
-
-
Constructor Details
-
NodeCreateRule
Default constructor. Creates an instance of this rule that will create a DOMElement
.- Throws:
ParserConfigurationException
-
NodeCreateRule
Constructor. Creates an instance of this rule that will create a DOMElement
, but lets you specify the JAXPDocumentBuilder
that should be used when constructing the node tree.- Parameters:
documentBuilder
- the JAXPDocumentBuilder
to use
-
NodeCreateRule
Constructor. Creates an instance of this rule that will create either a DOMElement
or a DOMDocumentFragment
, depending on the value of thenodeType
parameter.- Parameters:
nodeType
- the type of node to create, which can be eitherNode.ELEMENT_NODE
orNode.DOCUMENT_FRAGMENT_NODE
- Throws:
IllegalArgumentException
- if the node type is not supportedParserConfigurationException
-
NodeCreateRule
Constructor. Creates an instance of this rule that will create either a DOMElement
or a DOMDocumentFragment
, depending on the value of thenodeType
parameter. This constructor lets you specify the JAXPDocumentBuilder
that should be used when constructing the node tree.- Parameters:
nodeType
- the type of node to create, which can be eitherNode.ELEMENT_NODE
orNode.DOCUMENT_FRAGMENT_NODE
documentBuilder
- the JAXPDocumentBuilder
to use- Throws:
IllegalArgumentException
- if the node type is not supported
-
-
Method Details
-
begin
When this method fires, the digester is told to forward all SAX ContentHandler events to the builder object, resulting in a DOM being built instead of normal digester rule-handling occurring. When the end of the current xml element is encountered, the original content handler is restored (expected to be NULL, allowing normal Digester operations to continue).- Overrides:
begin
in classRule
- Parameters:
namespaceURI
- the namespace URI of the matching element, or an empty string if the parser is not namespace aware or the element has no namespacename
- the local name if the parser is namespace aware, or just the element name otherwiseattributes
- The attribute list of this element- Throws:
Exception
- indicates a JAXP configuration problem
-
end
Pop the Node off the top of the stack.
-