KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > digester > WithDefaultsRulesWrapper


1 /* $Id: WithDefaultsRulesWrapper.java 467222 2006-10-24 03:17:11Z markt $
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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

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

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

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

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

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

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

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

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