001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.chain.config;
018
019
020 import org.apache.commons.digester.Digester;
021 import org.apache.commons.digester.RuleSetBase;
022
023
024 /**
025 * <p>Digester <code>RuleSet</code> for configuring <em>Chain of
026 * Responsibility</em> command chains, and adding them to an appropriate
027 * {@link org.apache.commons.chain.Catalog}. The following properties
028 * may be configured prior to executing the <code>addRuleInstance()</code>
029 * method in order to influence the rules that get added, with default
030 * values in square brackets:</p>
031 * <ul>
032 * <li><strong>catalogClass</strong> -- Fully qualified name of the
033 * implementation class used to create new
034 * {@link org.apache.commons.chain.Catalog} instances.
035 * If not specified, the default value is
036 * <code>org.apache.commons.chain.impl.CatalogBsae</code>.</li>
037 * <li><strong>catalogElement</strong> -- Name of the XML element representing
038 * the addition of a {@link org.apache.commons.chain.Catalog}.
039 * Any such catalog that is created will be registered with the
040 * {@link org.apache.commons.chain.CatalogFactory} instance for our
041 * application, under the name specified by the <code>nameAttribute</code>
042 * attribute (if present), or as the default {@link org.apache.commons.chain.Catalog}.
043 * If not specified, the default value is <code>catalog</code>.</li>
044 * <li><strong>chainClass</strong> -- Fully qualified name of the implementation
045 * class used to create new {@link org.apache.commons.chain.Chain} instances.
046 * If not specified, the default value is
047 * <code>org.apache.commons.chain.impl.ChainBase</code>.
048 * </li>
049 * <li><strong>chainElement</strong> -- Name of the XML element representing
050 * the addition of a {@link org.apache.commons.chain.Chain}. A chain
051 * element has the same functionality as a command element, except that
052 * it defaults the implementation class to
053 * <code>org.apache.commons.chain.impl.ChainBase</code>. [chain]</li>
054 * <li><strong>classAttribute</strong> -- Attribute on a chain (optional) or
055 * command (required) element that specifies the fully qualified class
056 * name of the implementation class that should be instantiated.
057 * [className]</li>
058 * <li><strong>commandElement</strong> -- Name of the XML element
059 * representing the addition of a {@link org.apache.commons.chain.Command}.
060 * An implementation class name must be provided on the attribute named by the
061 * <code>classAttribute</code> property. [command]</li>
062 * <li><strong>defineElement</strong> -- Name of the XML element
063 * that associates the element specified by the <code>nameAttribute</code>
064 * attributes with a {@link org.apache.commons.chain.Command} or
065 * {@link org.apache.commons.chain.Chain} implementation class
066 * named by the <code>classAttribute</code> attribute. [define]</li>
067 * <li><strong>nameAttribute</strong> -- Attribute on an outermost chain or
068 * command element that will be used to register this command with the
069 * associated {@link org.apache.commons.chain.Catalog} instance on the stack.
070 * [name]</li>
071 * <li><strong>namespaceURI</strong> -- The XML namespace URI with which these
072 * rules will be associated, or <code>null</code> for no namespace.
073 * [null]</li>
074 * </ul>
075 *
076 * @author Craig R. McClanahan
077 * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
078 */
079
080 public class ConfigRuleSet extends RuleSetBase {
081
082
083 // ----------------------------------------------------- Instance Variables
084
085
086 private String catalogClass = "org.apache.commons.chain.impl.CatalogBase";
087 private String catalogElement = "catalog";
088 private String chainClass = "org.apache.commons.chain.impl.ChainBase";
089 private String chainElement = "chain";
090 private String classAttribute = "className";
091 private String commandElement = "command";
092 private String defineElement = "define";
093 private String nameAttribute = "name";
094
095
096 // ------------------------------------------------------------- Properties
097
098
099 /**
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 }