KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > config > ConfigRuleSet


1 /*
2  * $Id: ConfigRuleSet.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 2000-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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
20 package org.apache.struts.config;
21
22
23 import org.apache.commons.digester.AbstractObjectCreationFactory;
24 import org.apache.commons.digester.Digester;
25 import org.apache.commons.digester.Rule;
26 import org.apache.commons.digester.RuleSetBase;
27 import org.apache.struts.util.RequestUtils;
28 import org.xml.sax.Attributes JavaDoc;
29
30
31 /**
32  * <p>The set of Digester rules required to parse a Struts
33  * configuration file (<code>struts-config.xml</code>).</p>
34  *
35  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
36  * @since Struts 1.1
37  */

38
39 public class ConfigRuleSet extends RuleSetBase {
40
41
42     // --------------------------------------------------------- Public Methods
43

44
45     /**
46      * <p>Add the set of Rule instances defined in this RuleSet to the
47      * specified <code>Digester</code> instance, associating them with
48      * our namespace URI (if any). This method should only be called
49      * by a Digester instance. These rules assume that an instance of
50      * <code>org.apache.struts.config.ModuleConfig</code> is pushed
51      * onto the evaluation stack before parsing begins.</p>
52      *
53      * @param digester Digester instance to which the new Rule instances
54      * should be added.
55      */

56     public void addRuleInstances(Digester digester) {
57
58         digester.addObjectCreate
59             ("struts-config/data-sources/data-source",
60              "org.apache.struts.config.DataSourceConfig",
61              "className");
62         digester.addSetProperties
63             ("struts-config/data-sources/data-source");
64         digester.addSetNext
65             ("struts-config/data-sources/data-source",
66              "addDataSourceConfig",
67              "org.apache.struts.config.DataSourceConfig");
68
69         digester.addRule
70             ("struts-config/data-sources/data-source/set-property",
71              new AddDataSourcePropertyRule());
72
73         digester.addRule
74             ("struts-config/action-mappings",
75              new SetActionMappingClassRule());
76
77         digester.addFactoryCreate
78             ("struts-config/action-mappings/action",
79              new ActionMappingFactory());
80         digester.addSetProperties
81             ("struts-config/action-mappings/action");
82         digester.addSetNext
83             ("struts-config/action-mappings/action",
84              "addActionConfig",
85              "org.apache.struts.config.ActionConfig");
86
87         digester.addSetProperty
88             ("struts-config/action-mappings/action/set-property",
89              "property", "value");
90
91         digester.addObjectCreate
92             ("struts-config/action-mappings/action/exception",
93              "org.apache.struts.config.ExceptionConfig",
94              "className");
95         digester.addSetProperties
96             ("struts-config/action-mappings/action/exception");
97         digester.addSetNext
98             ("struts-config/action-mappings/action/exception",
99              "addExceptionConfig",
100              "org.apache.struts.config.ExceptionConfig");
101
102         digester.addSetProperty
103             ("struts-config/action-mappings/action/exception/set-property",
104              "property", "value");
105
106         digester.addFactoryCreate
107             ("struts-config/action-mappings/action/forward",
108              new ActionForwardFactory());
109         digester.addSetProperties
110             ("struts-config/action-mappings/action/forward");
111         digester.addSetNext
112             ("struts-config/action-mappings/action/forward",
113              "addForwardConfig",
114              "org.apache.struts.config.ForwardConfig");
115
116         digester.addSetProperty
117             ("struts-config/action-mappings/action/forward/set-property",
118              "property", "value");
119
120         digester.addObjectCreate
121             ("struts-config/controller",
122              "org.apache.struts.config.ControllerConfig",
123              "className");
124         digester.addSetProperties
125             ("struts-config/controller");
126         digester.addSetNext
127             ("struts-config/controller",
128              "setControllerConfig",
129              "org.apache.struts.config.ControllerConfig");
130
131         digester.addSetProperty
132             ("struts-config/controller/set-property",
133              "property", "value");
134
135         digester.addRule
136             ("struts-config/form-beans",
137              new SetActionFormBeanClassRule());
138
139         digester.addFactoryCreate
140             ("struts-config/form-beans/form-bean",
141              new ActionFormBeanFactory());
142         digester.addSetProperties
143             ("struts-config/form-beans/form-bean");
144         digester.addSetNext
145             ("struts-config/form-beans/form-bean",
146              "addFormBeanConfig",
147              "org.apache.struts.config.FormBeanConfig");
148
149         digester.addObjectCreate
150             ("struts-config/form-beans/form-bean/form-property",
151              "org.apache.struts.config.FormPropertyConfig",
152              "className");
153         digester.addSetProperties
154             ("struts-config/form-beans/form-bean/form-property");
155         digester.addSetNext
156             ("struts-config/form-beans/form-bean/form-property",
157              "addFormPropertyConfig",
158              "org.apache.struts.config.FormPropertyConfig");
159
160         digester.addSetProperty
161             ("struts-config/form-beans/form-bean/form-property/set-property",
162              "property", "value");
163
164         digester.addSetProperty
165             ("struts-config/form-beans/form-bean/set-property",
166              "property", "value");
167
168         digester.addObjectCreate
169             ("struts-config/global-exceptions/exception",
170              "org.apache.struts.config.ExceptionConfig",
171              "className");
172         digester.addSetProperties
173             ("struts-config/global-exceptions/exception");
174         digester.addSetNext
175             ("struts-config/global-exceptions/exception",
176              "addExceptionConfig",
177              "org.apache.struts.config.ExceptionConfig");
178
179         digester.addSetProperty
180             ("struts-config/global-exceptions/exception/set-property",
181              "property", "value");
182
183         digester.addRule
184             ("struts-config/global-forwards",
185              new SetActionForwardClassRule());
186
187         digester.addFactoryCreate
188             ("struts-config/global-forwards/forward",
189              new GlobalForwardFactory());
190         digester.addSetProperties
191             ("struts-config/global-forwards/forward");
192         digester.addSetNext
193             ("struts-config/global-forwards/forward",
194              "addForwardConfig",
195              "org.apache.struts.config.ForwardConfig");
196
197         digester.addSetProperty
198             ("struts-config/global-forwards/forward/set-property",
199              "property", "value");
200
201         digester.addObjectCreate
202             ("struts-config/message-resources",
203              "org.apache.struts.config.MessageResourcesConfig",
204              "className");
205         digester.addSetProperties
206             ("struts-config/message-resources");
207         digester.addSetNext
208             ("struts-config/message-resources",
209              "addMessageResourcesConfig",
210              "org.apache.struts.config.MessageResourcesConfig");
211
212         digester.addSetProperty
213             ("struts-config/message-resources/set-property",
214              "property", "value");
215
216         digester.addObjectCreate
217             ("struts-config/plug-in",
218              "org.apache.struts.config.PlugInConfig");
219         digester.addSetProperties
220             ("struts-config/plug-in");
221         digester.addSetNext
222             ("struts-config/plug-in",
223              "addPlugInConfig",
224              "org.apache.struts.config.PlugInConfig");
225
226         digester.addRule
227             ("struts-config/plug-in/set-property",
228              new PlugInSetPropertyRule());
229
230     }
231
232 }
233
234
235 /**
236  * Class that calls <code>addProperty()</code> for the top object
237  * on the stack, which must be a
238  * <code>org.apache.struts.config.DataSourceConfig</code>.
239  */

240
241 final class AddDataSourcePropertyRule extends Rule {
242
243     public AddDataSourcePropertyRule() {
244         super();
245     }
246
247     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes) throws Exception JavaDoc {
248         DataSourceConfig dsc = (DataSourceConfig) digester.peek();
249         dsc.addProperty(attributes.getValue("property"),
250                         attributes.getValue("value"));
251     }
252
253 }
254
255
256 /**
257  * Class that records the name and value of a configuration property to be
258  * used in configuring a <code>PlugIn</code> instance when instantiated.
259  */

260
261 final class PlugInSetPropertyRule extends Rule {
262
263     public PlugInSetPropertyRule() {
264         super();
265     }
266
267     public void begin(String JavaDoc namespace, String JavaDoc names, Attributes JavaDoc attributes) throws Exception JavaDoc {
268         PlugInConfig plugInConfig = (PlugInConfig) digester.peek();
269         plugInConfig.addProperty(attributes.getValue("property"),
270                                  attributes.getValue("value"));
271     }
272
273 }
274
275
276 /**
277  * Class that sets the name of the class to use when creating action form bean
278  * instances. The value is set on the object on the top of the stack, which
279  * must be a <code>org.apache.struts.config.ModuleConfig</code>.
280  */

281 final class SetActionFormBeanClassRule extends Rule {
282
283     public SetActionFormBeanClassRule() {
284         super();
285     }
286
287     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes) throws Exception JavaDoc {
288         String JavaDoc className = attributes.getValue("type");
289         if (className != null) {
290             ModuleConfig mc = (ModuleConfig) digester.peek();
291             mc.setActionFormBeanClass(className);
292         }
293     }
294
295 }
296
297
298 /**
299  * An object creation factory which creates action form bean instances, taking
300  * into account the default class name, which may have been specified on the
301  * parent element and which is made available through the object on the top
302  * of the stack, which must be a
303  * <code>org.apache.struts.config.ModuleConfig</code>.
304  */

305 final class ActionFormBeanFactory extends AbstractObjectCreationFactory {
306
307     public Object JavaDoc createObject(Attributes JavaDoc attributes) {
308
309         // Identify the name of the class to instantiate
310
String JavaDoc className = attributes.getValue("className");
311         if (className == null) {
312             ModuleConfig mc = (ModuleConfig) digester.peek();
313             className = mc.getActionFormBeanClass();
314         }
315
316         // Instantiate the new object and return it
317
Object JavaDoc actionFormBean = null;
318         try {
319             actionFormBean =
320                 RequestUtils.applicationInstance(className);
321         } catch (Exception JavaDoc e) {
322             digester.getLogger().error(
323                     "ActionFormBeanFactory.createObject: ", e);
324         }
325
326         return actionFormBean;
327     }
328
329 }
330
331
332 /**
333  * Class that sets the name of the class to use when creating action mapping
334  * instances. The value is set on the object on the top of the stack, which
335  * must be a <code>org.apache.struts.config.ModuleConfig</code>.
336  */

337 final class SetActionMappingClassRule extends Rule {
338
339     public SetActionMappingClassRule() {
340         super();
341     }
342
343     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes) throws Exception JavaDoc {
344         String JavaDoc className = attributes.getValue("type");
345         if (className != null) {
346             ModuleConfig mc = (ModuleConfig) digester.peek();
347             mc.setActionMappingClass(className);
348         }
349     }
350
351 }
352
353
354 /**
355  * An object creation factory which creates action mapping instances, taking
356  * into account the default class name, which may have been specified on the
357  * parent element and which is made available through the object on the top
358  * of the stack, which must be a
359  * <code>org.apache.struts.config.ModuleConfig</code>.
360  */

361 final class ActionMappingFactory extends AbstractObjectCreationFactory {
362
363     public Object JavaDoc createObject(Attributes JavaDoc attributes) {
364
365         // Identify the name of the class to instantiate
366
String JavaDoc className = attributes.getValue("className");
367         if (className == null) {
368             ModuleConfig mc = (ModuleConfig) digester.peek();
369             className = mc.getActionMappingClass();
370         }
371
372         // Instantiate the new object and return it
373
Object JavaDoc actionMapping = null;
374         try {
375             actionMapping =
376                 RequestUtils.applicationInstance(className);
377         } catch (Exception JavaDoc e) {
378             digester.getLogger().error(
379                     "ActionMappingFactory.createObject: ", e);
380         }
381
382         return actionMapping;
383     }
384
385 }
386
387
388 /**
389  * Class that sets the name of the class to use when creating global forward
390  * instances. The value is set on the object on the top of the stack, which
391  * must be a <code>org.apache.struts.config.ModuleConfig</code>.
392  */

393 final class SetActionForwardClassRule extends Rule {
394
395     public SetActionForwardClassRule() {
396         super();
397     }
398
399     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes) throws Exception JavaDoc {
400         String JavaDoc className = attributes.getValue("type");
401         if (className != null) {
402             ModuleConfig mc = (ModuleConfig) digester.peek();
403             mc.setActionForwardClass(className);
404         }
405     }
406
407 }
408
409
410 /**
411  * An object creation factory which creates global forward instances, taking
412  * into account the default class name, which may have been specified on the
413  * parent element and which is made available through the object on the top
414  * of the stack, which must be a
415  * <code>org.apache.struts.config.ModuleConfig</code>.
416  */

417 final class GlobalForwardFactory extends AbstractObjectCreationFactory {
418
419     public Object JavaDoc createObject(Attributes JavaDoc attributes) {
420
421         // Identify the name of the class to instantiate
422
String JavaDoc className = attributes.getValue("className");
423         if (className == null) {
424             ModuleConfig mc = (ModuleConfig) digester.peek();
425             className = mc.getActionForwardClass();
426         }
427
428         // Instantiate the new object and return it
429
Object JavaDoc globalForward = null;
430         try {
431             globalForward =
432                 RequestUtils.applicationInstance(className);
433         } catch (Exception JavaDoc e) {
434             digester.getLogger().error(
435                     "GlobalForwardFactory.createObject: ", e);
436         }
437
438         return globalForward;
439     }
440
441 }
442
443
444 /**
445  * An object creation factory which creates action forward instances, taking
446  * into account the default class name, which may have been specified on the
447  * parent element and which is made available through the object on the top
448  * of the stack, which must be a
449  * <code>org.apache.struts.config.ModuleConfig</code>.
450  */

451 final class ActionForwardFactory extends AbstractObjectCreationFactory {
452
453     public Object JavaDoc createObject(Attributes JavaDoc attributes) {
454
455         // Identify the name of the class to instantiate
456
String JavaDoc className = attributes.getValue("className");
457         if (className == null) {
458             ModuleConfig mc = (ModuleConfig) digester.peek(1);
459             className = mc.getActionForwardClass();
460         }
461
462         // Instantiate the new object and return it
463
Object JavaDoc actionForward = null;
464         try {
465             actionForward =
466                 RequestUtils.applicationInstance(className);
467         } catch (Exception JavaDoc e) {
468             digester.getLogger().error(
469                     "ActionForwardFactory.createObject: ", e);
470         }
471
472         return actionForward;
473     }
474
475 }
476
Popular Tags