RulesBasepublic class RulesBase extends Object implements RulesDefault implementation of the Rules interface that supports
the standard rule matching behavior. This class can also be used as a
base class for specialized Rules implementations.
The matching policies implemented by this class support two different
types of pattern matching rules:
- Exact Match - A pattern "a/b/c" exactly matches a
<c> element, nested inside a <b>
element, which is nested inside an <a> element.
- Tail Match - A pattern "*/a/b" matches a
<b> element, nested inside an <a>
element, no matter how deeply the pair is nested.
|
Fields Summary |
---|
protected HashMap | cacheThe set of registered Rule instances, keyed by the matching pattern.
Each value is a List containing the Rules for that pattern, in the
order that they were orginally registered. | protected Digester | digesterThe Digester instance with which this Rules instance is associated. | protected String | namespaceURIThe namespace URI for which subsequently added Rule
objects are relevant, or null for matching independent
of namespaces. | protected ArrayList | rulesThe set of registered Rule instances, in the order that they were
originally registered. |
Methods Summary |
---|
public void | add(java.lang.String pattern, Rule rule)Register a new Rule instance matching the specified pattern.
// to help users who accidently add '/' to the end of their patterns
int patternLength = pattern.length();
if (patternLength>1 && pattern.endsWith("/")) {
pattern = pattern.substring(0, patternLength-1);
}
List list = (List) cache.get(pattern);
if (list == null) {
list = new ArrayList();
cache.put(pattern, list);
}
list.add(rule);
rules.add(rule);
if (this.digester != null) {
rule.setDigester(this.digester);
}
if (this.namespaceURI != null) {
rule.setNamespaceURI(this.namespaceURI);
}
| public void | clear()Clear all existing Rule instance registrations.
cache.clear();
rules.clear();
| public Digester | getDigester()Return the Digester instance with which this Rules instance is
associated.
// ------------------------------------------------------------- Properties
return (this.digester);
| public java.lang.String | getNamespaceURI()Return the namespace URI that will be applied to all subsequently
added Rule objects.
return (this.namespaceURI);
| protected java.util.List | lookup(java.lang.String namespaceURI, java.lang.String pattern)Return a List of Rule instances for the specified pattern that also
match the specified namespace URI (if any). If there are no such
rules, return null .
// Optimize when no namespace URI is specified
List list = (List) this.cache.get(pattern);
if (list == null) {
return (null);
}
if ((namespaceURI == null) || (namespaceURI.length() == 0)) {
return (list);
}
// Select only Rules that match on the specified namespace URI
ArrayList results = new ArrayList();
Iterator items = list.iterator();
while (items.hasNext()) {
Rule item = (Rule) items.next();
if ((namespaceURI.equals(item.getNamespaceURI())) ||
(item.getNamespaceURI() == null)) {
results.add(item);
}
}
return (results);
| public java.util.List | match(java.lang.String pattern)Return a List of all registered Rule instances that match the specified
nesting pattern, or a zero-length List if there are no matches. If more
than one Rule instance matches, they must be returned
in the order originally registered through the add()
method.
return (match(null, pattern));
| public java.util.List | match(java.lang.String namespaceURI, java.lang.String pattern)Return a List of all registered Rule instances that match the specified
nesting pattern, or a zero-length List if there are no matches. If more
than one Rule instance matches, they must be returned
in the order originally registered through the add()
method.
// List rulesList = (List) this.cache.get(pattern);
List rulesList = lookup(namespaceURI, pattern);
if ((rulesList == null) || (rulesList.size() < 1)) {
// Find the longest key, ie more discriminant
String longKey = "";
Iterator keys = this.cache.keySet().iterator();
while (keys.hasNext()) {
String key = (String) keys.next();
if (key.startsWith("*/")) {
if (pattern.equals(key.substring(2)) ||
pattern.endsWith(key.substring(1))) {
if (key.length() > longKey.length()) {
// rulesList = (List) this.cache.get(key);
rulesList = lookup(namespaceURI, key);
longKey = key;
}
}
}
}
}
if (rulesList == null) {
rulesList = new ArrayList();
}
return (rulesList);
| public java.util.List | rules()Return a List of all registered Rule instances, or a zero-length List
if there are no registered Rule instances. If more than one Rule
instance has been registered, they must be returned
in the order originally registered through the add()
method.
return (this.rules);
| public void | setDigester(Digester digester)Set the Digester instance with which this Rules instance is associated.
this.digester = digester;
Iterator items = rules.iterator();
while (items.hasNext()) {
Rule item = (Rule) items.next();
item.setDigester(digester);
}
| public void | setNamespaceURI(java.lang.String namespaceURI)Set the namespace URI that will be applied to all subsequently
added Rule objects.
this.namespaceURI = namespaceURI;
|
|