KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > WithDefaultsRulesWrapper


1 /* $Id: WithDefaultsRulesWrapper.java 179716 2005-06-03 04:06:00Z skitching $
2  *
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.commons.digester;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 /**
25  * <p><code>Rules</code> <em>Decorator</em> that returns default rules
26  * when no matches are returned by the wrapped implementation.</p>
27  *
28  * <p>This allows default <code>Rule</code> instances to be added to any
29  * existing <code>Rules</code> implementation. These default <code>Rule</code>
30  * instances will be returned for any match for which the wrapped
31  * implementation does not return any matches.</p>
32  * <p> For example,
33  * <pre>
34  * Rule alpha;
35  * ...
36  * WithDefaultsRulesWrapper rules = new WithDefaultsRulesWrapper(new BaseRules());
37  * rules.addDefault(alpha);
38  * ...
39  * digester.setRules(rules);
40  * ...
41  * </pre>
42  * when a pattern does not match any other rule, then rule alpha will be called.
43  * </p>
44  * <p><code>WithDefaultsRulesWrapper</code> follows the <em>Decorator</em> pattern.</p>
45  *
46  * @since 1.6
47  */

48
49 public class WithDefaultsRulesWrapper implements Rules {
50
51     // --------------------------------------------------------- Fields
52

53     /** The Rules implementation that this class wraps. */
54     private Rules wrappedRules;
55     /** Rules to be fired when the wrapped implementations returns none. */
56     private List JavaDoc defaultRules = new ArrayList JavaDoc();
57     /** All rules (preserves order in which they were originally added) */
58     private List JavaDoc allRules = new ArrayList JavaDoc();
59     
60     // --------------------------------------------------------- Constructor
61

62     /**
63      * Base constructor.
64      *
65      * @param wrappedRules the wrapped <code>Rules</code> implementation, not null
66      * @throws IllegalArgumentException when <code>wrappedRules</code> is null
67      */

68     public WithDefaultsRulesWrapper(Rules wrappedRules) {
69         if (wrappedRules == null) {
70             throw new IllegalArgumentException JavaDoc("Wrapped rules must not be null");
71         }
72         this.wrappedRules = wrappedRules;
73     }
74
75     // --------------------------------------------------------- Properties
76

77     /** Gets digester using these Rules */
78     public Digester getDigester() {
79         return wrappedRules.getDigester();
80     }
81     
82     /** Sets digeseter using these Rules */
83     public void setDigester(Digester digester) {
84         wrappedRules.setDigester(digester);
85         Iterator JavaDoc it = defaultRules.iterator();
86         while (it.hasNext()) {
87             Rule rule = (Rule) it.next();
88             rule.setDigester(digester);
89         }
90     }
91     
92     /** Gets namespace to apply to Rule's added */
93     public String JavaDoc getNamespaceURI() {
94         return wrappedRules.getNamespaceURI();
95     }
96     
97     /** Sets namespace to apply to Rule's added subsequently */
98     public void setNamespaceURI(String JavaDoc namespaceURI) {
99         wrappedRules.setNamespaceURI(namespaceURI);
100     }
101     
102     /** Gets Rule's which will be fired when the wrapped implementation returns no matches */
103     public List JavaDoc getDefaults() {
104         return defaultRules;
105     }
106     
107     // --------------------------------------------------------- Public Methods
108

109     public List JavaDoc match(String JavaDoc pattern) {
110         return match("", pattern);
111     }
112     
113     /**
114      * Return list of rules matching given pattern.
115      * If wrapped implementation returns any matches return those.
116      * Otherwise, return default matches.
117      */

118     public List JavaDoc match(String JavaDoc namespaceURI, String JavaDoc pattern) {
119         List JavaDoc matches = wrappedRules.match(namespaceURI, pattern);
120         if (matches == null || matches.isEmpty()) {
121             // a little bit of defensive programming
122
return new ArrayList JavaDoc(defaultRules);
123         }
124         // otherwise
125
return matches;
126     }
127     
128     /** Adds a rule to be fired when wrapped implementation returns no matches */
129     public void addDefault(Rule rule) {
130         // set up rule
131
if (wrappedRules.getDigester() != null) {
132             rule.setDigester(wrappedRules.getDigester());
133         }
134         
135         if (wrappedRules.getNamespaceURI() != null) {
136             rule.setNamespaceURI(wrappedRules.getNamespaceURI());
137         }
138         
139         defaultRules.add(rule);
140         allRules.add(rule);
141     }
142     
143     /** Gets all rules */
144     public List JavaDoc rules() {
145         return allRules;
146     }
147     
148     /** Clears all Rule's */
149     public void clear() {
150         wrappedRules.clear();
151         allRules.clear();
152         defaultRules.clear();
153     }
154     
155     /**
156      * Adds a Rule to be fired on given pattern.
157      * Pattern matching is delegated to wrapped implementation.
158      */

159     public void add(String JavaDoc pattern, Rule rule) {
160         wrappedRules.add(pattern, rule);
161         allRules.add(rule);
162     }
163 }
164
Popular Tags