Package org.apache.commons.digester3
Class NodeCreateRule
- java.lang.Object
-
- org.apache.commons.digester3.Rule
-
- org.apache.commons.digester3.NodeCreateRule
-
public class NodeCreateRule extends Rule
A rule implementation that creates a DOMNodecontaining 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
Elementnode. 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
Nodewill be pushed on Digester's object stack when done. To use it in the context of another DOMDocument, it must be imported first, using the Document methodimportNode().Important Note: This is implemented by replacing the SAX
ContentHandlerin 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 aNodeCreateRulewill 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
- the default is to create an
-
-
Constructor Summary
Constructors Constructor Description NodeCreateRule()Default constructor.NodeCreateRule(int nodeType)Constructor.NodeCreateRule(int nodeType, javax.xml.parsers.DocumentBuilder documentBuilder)Constructor.NodeCreateRule(javax.xml.parsers.DocumentBuilder documentBuilder)Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbegin(java.lang.String namespaceURI, java.lang.String name, org.xml.sax.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.voidend(java.lang.String namespace, java.lang.String name)This method is called when the end of a matching XML element is encountered.-
Methods inherited from class org.apache.commons.digester3.Rule
body, finish, getDigester, getNamespaceURI, setDigester, setNamespaceURI
-
-
-
-
Constructor Detail
-
NodeCreateRule
public NodeCreateRule() throws javax.xml.parsers.ParserConfigurationException
Default constructor. Creates an instance of this rule that will create a DOMElement.- Throws:
javax.xml.parsers.ParserConfigurationException- if a DocumentBuilder cannot be created which satisfies the configuration requested.- See Also:
DocumentBuilderFactory.newDocumentBuilder()
-
NodeCreateRule
public NodeCreateRule(javax.xml.parsers.DocumentBuilder documentBuilder)
Constructor. Creates an instance of this rule that will create a DOMElement, but lets you specify the JAXPDocumentBuilderthat should be used when constructing the node tree.- Parameters:
documentBuilder- the JAXPDocumentBuilderto use
-
NodeCreateRule
public NodeCreateRule(int nodeType) throws javax.xml.parsers.ParserConfigurationException
Constructor. Creates an instance of this rule that will create either a DOMElementor a DOMDocumentFragment, depending on the value of thenodeTypeparameter.- Parameters:
nodeType- the type of node to create, which can be eitherNode.ELEMENT_NODEorNode.DOCUMENT_FRAGMENT_NODE- Throws:
javax.xml.parsers.ParserConfigurationException- if a DocumentBuilder cannot be created which satisfies the configuration requested.- See Also:
DocumentBuilderFactory.newDocumentBuilder()
-
NodeCreateRule
public NodeCreateRule(int nodeType, javax.xml.parsers.DocumentBuilder documentBuilder)
Constructor. Creates an instance of this rule that will create either a DOMElementor a DOMDocumentFragment, depending on the value of thenodeTypeparameter. This constructor lets you specify the JAXPDocumentBuilderthat should be used when constructing the node tree.- Parameters:
nodeType- the type of node to create, which can be eitherNode.ELEMENT_NODEorNode.DOCUMENT_FRAGMENT_NODEdocumentBuilder- the JAXPDocumentBuilderto use
-
-
Method Detail
-
begin
public void begin(java.lang.String namespaceURI, java.lang.String name, org.xml.sax.Attributes attributes) throws java.lang.Exception
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:
beginin 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:
java.lang.Exception- indicates a JAXP configuration problem
-
end
public void end(java.lang.String namespace, java.lang.String name) throws java.lang.Exception
This method is called when the end of a matching XML element is encountered.- Overrides:
endin classRule- Parameters:
namespace- 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 otherwise- Throws:
java.lang.Exception- if any error occurs
-
-