FileDocCategorySizeDatePackage
RulesBase.javaAPI DocApache Tomcat 6.0.149388Fri Jul 20 04:20:32 BST 2007org.apache.tomcat.util.digester

RulesBase

public class RulesBase extends Object implements Rules

Default 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
cache
The 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
digester
The Digester instance with which this Rules instance is associated.
protected String
namespaceURI
The namespace URI for which subsequently added Rule objects are relevant, or null for matching independent of namespaces.
protected ArrayList
rules
The set of registered Rule instances, in the order that they were originally registered.
Constructors Summary
Methods Summary
public voidadd(java.lang.String pattern, Rule rule)
Register a new Rule instance matching the specified pattern.

param
pattern Nesting pattern to be matched for this Rule
param
rule Rule instance to be registered

        // 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 voidclear()
Clear all existing Rule instance registrations.


        cache.clear();
        rules.clear();

    
public DigestergetDigester()
Return the Digester instance with which this Rules instance is associated.



    // ------------------------------------------------------------- Properties


                    
       

        return (this.digester);

    
public java.lang.StringgetNamespaceURI()
Return the namespace URI that will be applied to all subsequently added Rule objects.


        return (this.namespaceURI);

    
protected java.util.Listlookup(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.

param
namespaceURI Namespace URI to match, or null to select matching rules regardless of namespace URI
param
pattern Pattern to be matched


        // 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.Listmatch(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.

param
pattern Nesting pattern to be matched
deprecated
Call match(namespaceURI,pattern) instead.


        return (match(null, pattern));

    
public java.util.Listmatch(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.

param
namespaceURI Namespace URI for which to select matching rules, or null to match regardless of namespace URI
param
pattern Nesting pattern to be matched


        // 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.Listrules()
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 voidsetDigester(Digester digester)
Set the Digester instance with which this Rules instance is associated.

param
digester The newly associated Digester instance


        this.digester = digester;
        Iterator items = rules.iterator();
        while (items.hasNext()) {
            Rule item = (Rule) items.next();
            item.setDigester(digester);
        }

    
public voidsetNamespaceURI(java.lang.String namespaceURI)
Set the namespace URI that will be applied to all subsequently added Rule objects.

param
namespaceURI Namespace URI that must match on all subsequently added rules, or null for matching regardless of the current namespace URI


        this.namespaceURI = namespaceURI;