View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  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  package org.apache.commons.chain.config;
18  
19  
20  import org.apache.commons.digester.Digester;
21  import org.apache.commons.digester.RuleSetBase;
22  
23  
24  /**
25   * <p>Digester <code>RuleSet</code> for configuring <em>Chain of
26   * Responsibility</em> command chains, and adding them to an appropriate
27   * {@link org.apache.commons.chain.Catalog}.  The following properties
28   * may be configured prior to executing the <code>addRuleInstance()</code>
29   * method in order to influence the rules that get added, with default
30   * values in square brackets:</p>
31   * <ul>
32   * <li><strong>catalogClass</strong> -- Fully qualified name of the
33   *     implementation class used to create new
34   *     {@link org.apache.commons.chain.Catalog} instances.
35   *     If not specified, the default value is
36   *     <code>org.apache.commons.chain.impl.CatalogBsae</code>.</li>
37   * <li><strong>catalogElement</strong> -- Name of the XML element representing
38   *     the addition of a {@link org.apache.commons.chain.Catalog}.
39   *     Any such catalog that is created will be registered with the
40   *     {@link org.apache.commons.chain.CatalogFactory} instance for our
41   *     application, under the name specified by the <code>nameAttribute</code>
42   *     attribute (if present), or as the default {@link org.apache.commons.chain.Catalog}.
43   *     If not specified, the default value is <code>catalog</code>.</li>
44   * <li><strong>chainClass</strong> -- Fully qualified name of the implementation
45   *     class used to create new {@link org.apache.commons.chain.Chain} instances.
46   *     If not specified, the default value is
47   *     <code>org.apache.commons.chain.impl.ChainBase</code>.
48   *     </li>
49   * <li><strong>chainElement</strong> -- Name of the XML element representing
50   *     the addition of a {@link org.apache.commons.chain.Chain}.  A chain
51   *     element has the same functionality as a command element, except that
52   *     it defaults the implementation class to
53   *     <code>org.apache.commons.chain.impl.ChainBase</code>.  [chain]</li>
54   * <li><strong>classAttribute</strong> -- Attribute on a chain (optional) or
55   *     command (required) element that specifies the fully qualified class
56   *     name of the implementation class that should be instantiated.
57   *     [className]</li>
58   * <li><strong>commandElement</strong> -- Name of the XML element
59   *     representing the addition of a {@link org.apache.commons.chain.Command}.
60   *     An implementation class name must be provided on the attribute named by the
61   *     <code>classAttribute</code> property.  [command]</li>
62   * <li><strong>defineElement</strong> -- Name of the XML element
63   *     that associates the element specified by the <code>nameAttribute</code>
64   *     attributes with a {@link org.apache.commons.chain.Command} or
65   *     {@link org.apache.commons.chain.Chain} implementation class
66   *     named by the <code>classAttribute</code> attribute.  [define]</li>
67   * <li><strong>nameAttribute</strong> -- Attribute on an outermost chain or
68   *     command element that will be used to register this command with the
69   *     associated {@link org.apache.commons.chain.Catalog} instance on the stack.
70   *     [name]</li>
71   * <li><strong>namespaceURI</strong> -- The XML namespace URI with which these
72   *     rules will be associated, or <code>null</code> for no namespace.
73   *     [null]</li>
74   * </ul>
75   *
76   * @author Craig R. McClanahan
77   * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
78   */
79  
80  public class ConfigRuleSet extends RuleSetBase {
81  
82  
83      // ----------------------------------------------------- Instance Variables
84  
85  
86      private String catalogClass = "org.apache.commons.chain.impl.CatalogBase";
87      private String catalogElement = "catalog";
88      private String chainClass = "org.apache.commons.chain.impl.ChainBase";
89      private String chainElement = "chain";
90      private String classAttribute = "className";
91      private String commandElement = "command";
92      private String defineElement = "define";
93      private String nameAttribute = "name";
94  
95  
96      // ------------------------------------------------------------- Properties
97  
98  
99      /**
100      * <p>Return the fully qualified {@link org.apache.commons.chain.Catalog}
101      *  implementation class.</p>
102      * @return The Catalog's class name.
103      */
104     public String getCatalogClass() {
105         return (this.catalogClass);
106     }
107 
108 
109     /**
110      * <p>Set the fully qualified {@link org.apache.commons.chain.Catalog}
111      * implementation class.</p>
112      *
113      * @param catalogClass The new {@link org.apache.commons.chain.Catalog}
114      *  implementation class
115      */
116     public void setCatalogClass(String catalogClass) {
117         this.catalogClass = catalogClass;
118     }
119 
120 
121     /**
122      * <p>Return the element name of a catalog element.</p>
123      * @return The element name of a catalog element.
124      */
125     public String getCatalogElement() {
126         return (this.catalogElement);
127     }
128 
129 
130     /**
131      * <p>Set the element name of a catalog element.</p>
132      *
133      * @param catalogElement The new element name
134      */
135     public void setCatalogElement(String catalogElement) {
136         this.catalogElement = catalogElement;
137     }
138 
139 
140     /**
141      * <p>Return the fully qualified {@link org.apache.commons.chain.Chain}
142      * implementation class.</p>
143      * @return The Chain's class name.
144      */
145     public String getChainClass() {
146         return (this.chainClass);
147     }
148 
149 
150     /**
151      * <p>Set the fully qualified {@link org.apache.commons.chain.Chain}
152      * implementation class.</p>
153      *
154      * @param chainClass The new {@link org.apache.commons.chain.Chain}
155      * implementation class
156      */
157     public void setChainClass(String chainClass) {
158         this.chainClass = chainClass;
159     }
160 
161 
162     /**
163      * <p>Return the element name of a chain element.</p>
164      * @return The element name of a catalog element.
165      */
166     public String getChainElement() {
167         return (this.chainElement);
168     }
169 
170 
171     /**
172      * <p>Set the element name of a chain element.</p>
173      *
174      * @param chainElement The new element name
175      */
176     public void setChainElement(String chainElement) {
177         this.chainElement = chainElement;
178     }
179 
180 
181     /**
182      * <p>Return the attribute name of a class attribute.</p>
183      * @return The attribute name of a class attribute.
184      */
185     public String getClassAttribute() {
186         return (this.classAttribute);
187     }
188 
189 
190     /**
191      * <p>Set the attribute name of a class attribute.</p>
192      *
193      * @param classAttribute The new attribute name
194      */
195     public void setClassAttribute(String classAttribute) {
196         this.classAttribute = classAttribute;
197     }
198 
199 
200     /**
201      * <p>Return the element name of a command element.</p>
202      * @return The element name of a command element.
203      */
204     public String getCommandElement() {
205         return (this.commandElement);
206     }
207 
208 
209     /**
210      * <p>Set the element name of a command element.</p>
211      *
212      * @param commandElement The new element name
213      */
214     public void setCommandElement(String commandElement) {
215         this.commandElement = commandElement;
216     }
217 
218 
219     /**
220      * <p>Return the element name of a define element.</p>
221      * @return The element name of a define element.
222      */
223     public String getDefineElement() {
224         return (this.defineElement);
225     }
226 
227 
228     /**
229      * <p>Set the element name of a define element.</p>
230      *
231      * @param defineElement The new element name
232      */
233     public void setDefineElement(String defineElement) {
234         this.defineElement = defineElement;
235     }
236 
237 
238     /**
239      * <p>Return the attribute name of a name attribute.</p>
240      * @return The attribute name of an attribute element.
241      */
242     public String getNameAttribute() {
243         return (this.nameAttribute);
244     }
245 
246 
247     /**
248      * <p>Set the attribute name of a name attribute.</p>
249      *
250      * @param nameAttribute The new attribute name
251      */
252     public void setNameAttribute(String nameAttribute) {
253         this.nameAttribute = nameAttribute;
254     }
255 
256 
257     // --------------------------------------------------------- Public Methods
258 
259 
260     /**
261      * <p>Add the set of Rule instances defined in this RuleSet to the
262      * specified <code>Digester</code> instance, associating them with
263      * our namespace URI (if any).  This method should only be called
264      * by a Digester instance.</p>
265      *
266      * @param digester Digester instance to which the new Rule instances
267      *  should be added.
268      */
269     public void addRuleInstances(Digester digester) {
270 
271         // Add rules for a catalog element
272         digester.addRule("*/" + getCatalogElement(),
273                          new ConfigCatalogRule(nameAttribute, catalogClass));
274         digester.addSetProperties("*/" + getCatalogElement());
275 
276         // Add rules for a chain element
277         digester.addObjectCreate("*/" + getChainElement(),
278                                  getChainClass(),
279                                  getClassAttribute());
280         digester.addSetProperties("*/" + getChainElement());
281         digester.addRule("*/" + getChainElement(),
282                          new ConfigRegisterRule(nameAttribute));
283 
284         // Add rules for a command element
285         digester.addObjectCreate("*/" + getCommandElement(),
286                                  null,
287                                  getClassAttribute());
288         digester.addSetProperties("*/" + getCommandElement());
289         digester.addRule("*/" + getCommandElement(),
290                          new ConfigRegisterRule(nameAttribute));
291 
292         // Add rules for a define element
293         digester.addRule("*/" + getDefineElement(),
294                          new ConfigDefineRule(getNameAttribute(),
295                                               getClassAttribute()));
296 
297     }
298 
299 
300 }