public abstract class GrammarRule
extends java.lang.Object
An XML document is validated in one of two ways. Clients may invoke the
top ElementRule's method accept(root)
on the root Element
of an XML syntax tree. This returns true, if the tree is valid with
respect to the rules, or false if the tree is invalid. Alternatively,
clients may invoke validate(root)
on the root Element, which
succeeds silently if the tree is valid, or raises a SemanticError
exception giving the errorReason for the mismatch, if the tree is invalid.
Modifier and Type | Field and Description |
---|---|
private static Content |
errorContext
Stores the last context where mismatch occurred.
|
private static java.lang.String |
errorReason
Stores the last global reason for the mismatch.
|
protected java.lang.String |
identifier
The identifying label of this GrammarRule.
|
protected GrammarRule |
parent
The parent GrammarRule for this GrammarRule.
|
Modifier | Constructor and Description |
---|---|
protected |
GrammarRule(java.lang.String identifier)
Creates a GrammarRule, with the given identifier.
|
Modifier and Type | Method and Description |
---|---|
boolean |
accept(Element node)
Reports whether this GrammarRule accepts the given XML Element node.
|
void |
addChildRule(GrammarRule rule)
Adds a child rule to this GrammarRule.
|
protected void |
clearError()
Secret method to clear the latest mismatch error.
|
protected int |
getChildIndex()
Returns the child index at which the next GrammarRule starts matching.
|
private java.lang.String |
getContext()
Returns the XML errorContext in which a semantic error is detected.
|
java.lang.String |
getIdentifier()
Returns the identifying label of this GrammarRule.
|
java.util.List<GrammarRule> |
getRuleChildren()
Returns the immediate GrammarRule children of this GrammarRule.
|
boolean |
isAttributeRule()
Reports whether this GrammarRule is an attribute rule.
|
boolean |
isElementRule()
Reports whether this GrammarRule is an element rule.
|
boolean |
isNestingRule()
Reports whether this GrammarRule is allowed to be nested inside other
CompoundRule rules.
|
void |
restrict(Filter filter)
Adds a value, range or type restriction to this GrammarRule.
|
protected void |
setChildIndex(int index)
Sets the child index at which the next GrammarRule starts matching.
|
protected void |
setError(java.lang.String reason,
Content context)
Secret method to record the latest mismatch error.
|
protected void |
setParent(GrammarRule rule)
Secret method to install the parent GrammarRule of this GrammarRule.
|
void |
validate(Element root)
Validates a given XML document tree starting at an Element root node.
|
private static Content errorContext
private static java.lang.String errorReason
protected java.lang.String identifier
protected GrammarRule parent
protected GrammarRule(java.lang.String identifier)
identifier
- the identifier of this GrammarRule.private java.lang.String getContext()
protected void setError(java.lang.String reason, Content context)
reason
- a reason for the mismatch.context
- the node at which matching failed.protected void clearError()
protected int getChildIndex()
protected void setChildIndex(int index)
index
- the child index.protected void setParent(GrammarRule rule)
Conceptually, all the subsidiary GrammarRules in a production form a unique tree under their dominating ElementRule. ElementRule rules break this principle, since they may be added to more than one compound rule, and so are shared in a graph. However, it is of no consequence that ElementRule retains only the first of its parents, since its back-reference is never used (the matching algorithm only tracks back to the nearest dominating ElementRule).
rule
- the parent GrammarRule of this rule.public boolean accept(Element node)
node
- an XML Element node, possibly the root of an XML tree.public void validate(Element root) throws SemanticError
accept(node)
and terminates silently
if validation succeeds. Otherwise, it raises a SemanticError with a
message describing the nature and errorContext of the mismatch.root
- the root Element of a Document.SemanticError
- if validation fails.public void addChildRule(GrammarRule rule)
rule
- the child rule to add.public java.util.List<GrammarRule> getRuleChildren()
public java.lang.String getIdentifier()
public boolean isAttributeRule()
public boolean isElementRule()
public boolean isNestingRule()
public void restrict(Filter filter)
filter
- the restriction to add.