Class WithDefaultsRulesWrapper

  • All Implemented Interfaces:
    Rules

    public class WithDefaultsRulesWrapper
    extends java.lang.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
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.List<Rule> allRules
      All rules (preserves order in which they were originally added)
      private java.util.List<Rule> defaultRules
      Rules to be fired when the wrapped implementations returns none.
      private Rules wrappedRules
      The Rules implementation that this class wraps.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(java.lang.String pattern, Rule rule)
      Register a new Rule instance matching the specified pattern.
      void addDefault​(Rule rule)
      Adds a rule to be fired when wrapped implementation returns no matches
      void clear()
      Clear all existing Rule instance registrations.
      java.util.List<Rule> getDefaults()
      Gets Rule's which will be fired when the wrapped implementation returns no matches
      Digester getDigester()
      Return the Digester instance with which this Rules instance is associated.
      java.lang.String getNamespaceURI()
      Return the namespace URI that will be applied to all subsequently added Rule objects.
      java.util.List<Rule> match​(java.lang.String namespaceURI, java.lang.String pattern, java.lang.String name, org.xml.sax.Attributes attributes)
      Return a List of all registered Rule instances that match the specified nesting pattern, or a zero-length List if there are no matches.
      java.util.List<Rule> rules()
      Return a List of all registered Rule instances, or a zero-length List if there are no registered Rule instances.
      void setDigester​(Digester digester)
      Set the Digester instance with which this Rules instance is associated.
      void setNamespaceURI​(java.lang.String namespaceURI)
      Set the namespace URI that will be applied to all subsequently added Rule objects.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • wrappedRules

        private Rules wrappedRules
        The Rules implementation that this class wraps.
      • defaultRules

        private java.util.List<Rule> defaultRules
        Rules to be fired when the wrapped implementations returns none.
      • allRules

        private java.util.List<Rule> allRules
        All rules (preserves order in which they were originally added)
    • Constructor Detail

      • WithDefaultsRulesWrapper

        public WithDefaultsRulesWrapper​(Rules wrappedRules)
        Base constructor.
        Parameters:
        wrappedRules - the wrapped Rules implementation, not null
    • Method Detail

      • getDigester

        public Digester getDigester()
        Return the Digester instance with which this Rules instance is associated.
        Specified by:
        getDigester in interface Rules
        Returns:
        the Digester instance with which this Rules instance is associated
      • setDigester

        public void setDigester​(Digester digester)
        Set the Digester instance with which this Rules instance is associated.
        Specified by:
        setDigester in interface Rules
        Parameters:
        digester - The newly associated Digester instance
      • getNamespaceURI

        public java.lang.String getNamespaceURI()
        Return the namespace URI that will be applied to all subsequently added Rule objects.
        Specified by:
        getNamespaceURI in interface Rules
        Returns:
        the namespace URI that will be applied to all subsequently added Rule objects.
      • setNamespaceURI

        public void setNamespaceURI​(java.lang.String namespaceURI)
        Set the namespace URI that will be applied to all subsequently added Rule objects.
        Specified by:
        setNamespaceURI in interface Rules
        Parameters:
        namespaceURI - Namespace URI that must match on all subsequently added rules, or null for matching regardless of the current namespace URI
      • getDefaults

        public java.util.List<Rule> getDefaults()
        Gets Rule's which will be fired when the wrapped implementation returns no matches
        Returns:
        Rule's which will be fired when the wrapped implementation returns no matches
      • match

        public java.util.List<Rule> match​(java.lang.String namespaceURI,
                                          java.lang.String pattern,
                                          java.lang.String name,
                                          org.xml.sax.Attributes attributes)
        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.
        Specified by:
        match in interface Rules
        Parameters:
        namespaceURI - Namespace URI for which to select matching rules, or null to match regardless of namespace URI
        pattern - Nesting pattern to be matched
        name - the local name if the parser is namespace aware, or just the element name otherwise
        attributes - The attribute list of the current matching element
        Returns:
        a List of all registered Rule instances that match the specified nesting pattern
      • addDefault

        public void addDefault​(Rule rule)
        Adds a rule to be fired when wrapped implementation returns no matches
        Parameters:
        rule - a Rule to be fired when wrapped implementation returns no matches
      • rules

        public java.util.List<Rule> 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.
        Specified by:
        rules in interface Rules
        Returns:
        a List of all registered Rule instances
      • clear

        public void clear()
        Clear all existing Rule instance registrations.
        Specified by:
        clear in interface Rules
      • add

        public void add​(java.lang.String pattern,
                        Rule rule)
        Register a new Rule instance matching the specified pattern.
        Specified by:
        add in interface Rules
        Parameters:
        pattern - Nesting pattern to be matched for this Rule
        rule - Rule instance to be registered