01: /* $Id: FinderSetProperties.java 471661 2006-11-06 08:09:25Z skitching $
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
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.commons.digester.plugins.strategies;
20:
21: import java.util.Properties;
22:
23: import org.apache.commons.digester.Digester;
24: import org.apache.commons.digester.plugins.RuleFinder;
25: import org.apache.commons.digester.plugins.RuleLoader;
26:
27: /**
28: * A rule-finding algorithm which expects the user to specify whether
29: * "automatic property setting" is desired. If this class discovers that
30: * this is in fact the case for a declaration, then a RuleLoader is returned
31: * which, when invoked, adds a single SetPropertiesRule instance to the
32: * digester.
33: * <p>
34: * This allows ordinary JavaBean classes to be used as plugins, and have
35: * xml attributes be mapped to bean properties of the same name, without
36: * any custom plugin rules being created for them.
37: * <p>
38: * This RuleFinder is typically used as the <i>last</i> RuleFinder, so that
39: * automatic property setting only occurs if there is no other source of
40: * custom rules available.
41: *
42: * @since 1.6
43: */
44:
45: public class FinderSetProperties extends RuleFinder {
46: public static String DFLT_PROPS_ATTR = "setprops";
47: public static String DFLT_FALSEVAL = "false";
48:
49: private String propsAttr;
50: private String falseval;
51:
52: /** See {@link #findLoader}. */
53: public FinderSetProperties() {
54: this (DFLT_PROPS_ATTR, DFLT_FALSEVAL);
55: }
56:
57: /**
58: * Create a rule-finder which will arrange for a SetPropertiesRule to
59: * be defined for each instance of a plugin, so that xml attributes
60: * map to bean properties.
61: * <p>
62: * Param falseval will commonly be the string "false" for config files
63: * written in English.
64: *
65: * @param propsAttr must be non-null.
66: * @param falseval must be non-null.
67: */
68: public FinderSetProperties(String propsAttr, String falseval) {
69: this .propsAttr = propsAttr;
70: this .falseval = falseval;
71: }
72:
73: /**
74: * Returns a RuleLoader <i>unless</i> the properties contain an entry
75: * with the name matching constructor param propsAttr, and the value
76: * matching what is in falseval.
77: * <p>
78: * If no custom source of rules for a plugin is found, then the user
79: * almost always wants xml attributes to map to java bean properties,
80: * so this is the default behaviour unless the user explicitly indicates
81: * that they do <i>not</i> want a SetPropertiesRule to be provided for
82: * the plugged-in class.
83: * <p>
84: * The returned object (when non-null) will add a SetPropertiesRule to
85: * the digester whenever its addRules method is invoked.
86: */
87: public RuleLoader findLoader(Digester d, Class pluginClass,
88: Properties p) {
89: String state = p.getProperty(propsAttr);
90: if ((state != null) && state.equals(falseval)) {
91: // user has explicitly disabled automatic setting of properties.
92: // this is not expected to be common, but allowed.
93: return null;
94: }
95:
96: return new LoaderSetProperties();
97: }
98: }
|