FileDocCategorySizeDatePackage
WithDefaultsRulesWrapper.javaAPI DocApache Tomcat 6.0.145720Fri Jul 20 04:20:36 BST 2007org.apache.tomcat.util.digester

WithDefaultsRulesWrapper

public class WithDefaultsRulesWrapper extends Object implements Rules

Rules Decorator that returns default rules when no matches are returned by the wrapped implementation.

This allows default Rule instances to be added to any existing Rules implementation. These default Rule instances will be returned for any match for which the wrapped implementation does not return any matches.

For example,

Rule alpha;
...
WithDefaultsRulesWrapper rules = new WithDefaultsRulesWrapper(new BaseRules());
rules.addDefault(alpha);
...
digester.setRules(rules);
...
when a pattern does not match any other rule, then rule alpha will be called.

WithDefaultsRulesWrapper follows the Decorator pattern.

since
1.6

Fields Summary
private Rules
wrappedRules
The Rules implementation that this class wraps.
private List
defaultRules
Rules to be fired when the wrapped implementations returns none.
private List
allRules
All rules (preserves order in which they were originally added)
Constructors Summary
public WithDefaultsRulesWrapper(Rules wrappedRules)
Base constructor.

param
wrappedRules the wrapped Rules implementation, not null
throws
IllegalArgumentException when wrappedRules is null

    
    // --------------------------------------------------------- Constructor
    
                          
       
        if (wrappedRules == null) {
            throw new IllegalArgumentException("Wrapped rules must not be null");
        }
        this.wrappedRules = wrappedRules;
    
Methods Summary
public voidadd(java.lang.String pattern, Rule rule)
Adds a Rule to be fired on given pattern. Pattern matching is delegated to wrapped implementation.

        wrappedRules.add(pattern, rule);
        allRules.add(rule);
    
public voidaddDefault(Rule rule)
Adds a rule to be fired when wrapped implementation returns no matches

        // set up rule
        if (wrappedRules.getDigester() != null) {
            rule.setDigester(wrappedRules.getDigester());
        }
        
        if (wrappedRules.getNamespaceURI() != null) {
            rule.setNamespaceURI(wrappedRules.getNamespaceURI());
        }
        
        defaultRules.add(rule);
        allRules.add(rule);
    
public voidclear()
Clears all Rule's

        wrappedRules.clear();
        allRules.clear();
        defaultRules.clear();
    
public java.util.ListgetDefaults()
Gets Rule's which will be fired when the wrapped implementation returns no matches

        return defaultRules;
    
public DigestergetDigester()
Gets digester using these Rules

        return wrappedRules.getDigester();
    
public java.lang.StringgetNamespaceURI()
Gets namespace to apply to Rule's added

        return wrappedRules.getNamespaceURI();
    
public java.util.Listmatch(java.lang.String pattern)

        return match("", pattern);
    
public java.util.Listmatch(java.lang.String namespaceURI, java.lang.String pattern)
Return list of rules matching given pattern. If wrapped implementation returns any matches return those. Otherwise, return default matches.

        List matches = wrappedRules.match(namespaceURI, pattern);
        if (matches ==  null || matches.isEmpty()) {	
            // a little bit of defensive programming
            return new ArrayList(defaultRules);
        }
        // otherwise
        return matches;
    
public java.util.Listrules()
Gets all rules

        return allRules;
    
public voidsetDigester(Digester digester)
Sets digeseter using these Rules

        wrappedRules.setDigester(digester);
        Iterator it = defaultRules.iterator();
        while (it.hasNext()) {
            Rule rule = (Rule) it.next();
            rule.setDigester(digester);
        }
    
public voidsetNamespaceURI(java.lang.String namespaceURI)
Sets namespace to apply to Rule's added subsequently

        wrappedRules.setNamespaceURI(namespaceURI);