public abstract class Rule extends Object
Writing a custom Rule is considered perfectly normal when using Digester, and is encouraged whenever the default set of Rule classes don't meet your requirements; the digester framework can help process xml even when the built-in rules aren't quite what is needed. Creating a custom Rule is just as easy as subclassing javax.servlet.http.HttpServlet for webapps, or javax.swing.Action for GUI applications.
If a rule wishes to manipulate a digester stack (the default object stack, a named stack, or the parameter stack) then it should only ever push objects in the rule's begin method and always pop exactly the same number of objects off the stack during the rule's end method. Of course peeking at the objects on the stacks can be done from anywhere.
Rule objects should limit their state data to the digester object stack and named stacks. Storing state in instance fields (other than digester) during the parsing process will cause problems if invoked in a "nested" manner; this can happen if the same instance is added to digester multiple times or if a wildcard pattern is used which can match both an element and a child of the same element.
Rule objects are not thread-safe when each thread creates a new digester, as is commonly the case. In a multithreaded context you should create new Rule instances for every digester or synchronize read/write access to the digester within the Rule.
Constructor and Description |
---|
Rule() |
Modifier and Type | Method and Description |
---|---|
void |
begin(String namespace,
String name,
Attributes attributes)
This method is called when the beginning of a matching XML element is encountered.
|
void |
body(String namespace,
String name,
String text)
This method is called when the body of a matching XML element is encountered.
|
void |
end(String namespace,
String name)
This method is called when the end of a matching XML element is encountered.
|
void |
finish()
This method is called after all parsing methods have been called, to allow Rules to remove temporary data.
|
Digester |
getDigester()
Return the Digester with which this Rule is associated.
|
String |
getNamespaceURI()
Return the namespace URI for which this Rule is relevant, if any.
|
void |
setDigester(Digester digester)
Set the
Digester with which this Rule is associated. |
void |
setNamespaceURI(String namespaceURI)
Set the namespace URI for which this Rule is relevant, if any.
|
public Rule()
public Digester getDigester()
public void setDigester(Digester digester)
Digester
with which this Rule
is associated.digester
- the Digester
with which this Rule
is associatedpublic String getNamespaceURI()
public void setNamespaceURI(String namespaceURI)
namespaceURI
- Namespace URI for which this Rule is relevant, or null
to match independent of
namespace.public void begin(String namespace, String name, Attributes attributes) throws Exception
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 otherwiseattributes
- The attribute list of this elementException
- if any error occurspublic void body(String namespace, String name, String text) throws Exception
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 otherwisetext
- The text of the body of this elementException
- if any error occurspublic void end(String namespace, String name) throws Exception
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 otherwiseException
- if any error occursCopyright © 2001-2013 The Apache Software Foundation. All Rights Reserved.