KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > plugins > strategies > FinderSetProperties


1 /* $Id: FinderSetProperties.java 179714 2005-06-03 03:53:39Z skitching $
2  *
3  * Copyright 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.plugins.strategies;
19
20 import java.util.Properties JavaDoc;
21
22 import org.apache.commons.digester.Digester;
23 import org.apache.commons.digester.plugins.RuleFinder;
24 import org.apache.commons.digester.plugins.RuleLoader;
25
26 /**
27  * A rule-finding algorithm which expects the user to specify whether
28  * "automatic property setting" is desired. If this class discovers that
29  * this is in fact the case for a declaration, then a RuleLoader is returned
30  * which, when invoked, adds a single SetPropertiesRule instance to the
31  * digester.
32  * <p>
33  * This allows ordinary JavaBean classes to be used as plugins, and have
34  * xml attributes be mapped to bean properties of the same name, without
35  * any custom plugin rules being created for them.
36  * <p>
37  * This RuleFinder is typically used as the <i>last</i> RuleFinder, so that
38  * automatic property setting only occurs if there is no other source of
39  * custom rules available.
40  *
41  * @since 1.6
42  */

43
44 public class FinderSetProperties extends RuleFinder {
45     public static String JavaDoc DFLT_PROPS_ATTR = "setprops";
46     public static String JavaDoc DFLT_FALSEVAL = "false";
47
48     private String JavaDoc propsAttr;
49     private String JavaDoc falseval;
50     
51     /** See {@link #findLoader}. */
52     public FinderSetProperties() {
53         this(DFLT_PROPS_ATTR, DFLT_FALSEVAL);
54     }
55     
56     /**
57      * Create a rule-finder which will arrange for a SetPropertiesRule to
58      * be defined for each instance of a plugin, so that xml attributes
59      * map to bean properties.
60      * <p>
61      * Param falseval will commonly be the string "false" for config files
62      * written in English.
63      *
64      * @param propsAttr must be non-null.
65      * @param falseval must be non-null.
66      */

67     public FinderSetProperties(String JavaDoc propsAttr, String JavaDoc falseval) {
68         this.propsAttr = propsAttr;
69         this.falseval = falseval;
70     }
71     
72     /**
73      * Returns a RuleLoader <i>unless</i> the properties contain an entry
74      * with the name matching constructor param propsAttr, and the value
75      * matching what is in falseval.
76      * <p>
77      * If no custom source of rules for a plugin is found, then the user
78      * almost always wants xml attributes to map to java bean properties,
79      * so this is the default behaviour unless the user explicitly indicates
80      * that they do <i>not</i> want a SetPropertiesRule to be provided for
81      * the plugged-in class.
82      * <p>
83      * The returned object (when non-null) will add a SetPropertiesRule to
84      * the digester whenever its addRules method is invoked.
85      */

86     public RuleLoader findLoader(Digester d, Class JavaDoc pluginClass, Properties JavaDoc p) {
87         String JavaDoc state = p.getProperty(propsAttr);
88         if ((state != null) && state.equals(falseval)) {
89             // user has explicitly disabled automatic setting of properties.
90
// this is not expected to be common, but allowed.
91
return null;
92         }
93         
94         return new LoaderSetProperties();
95     }
96 }
97
98
Popular Tags