001 /* $Id: FinderSetProperties.java 992060 2010-09-02 19:09:47Z simonetripodi $
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.commons.digester.plugins.strategies;
019
020 import java.util.Properties;
021
022 import org.apache.commons.digester.Digester;
023 import org.apache.commons.digester.plugins.RuleFinder;
024 import org.apache.commons.digester.plugins.RuleLoader;
025
026 /**
027 * A rule-finding algorithm which expects the user to specify whether
028 * "automatic property setting" is desired. If this class discovers that
029 * this is in fact the case for a declaration, then a RuleLoader is returned
030 * which, when invoked, adds a single SetPropertiesRule instance to the
031 * digester.
032 * <p>
033 * This allows ordinary JavaBean classes to be used as plugins, and have
034 * xml attributes be mapped to bean properties of the same name, without
035 * any custom plugin rules being created for them.
036 * <p>
037 * This RuleFinder is typically used as the <i>last</i> RuleFinder, so that
038 * automatic property setting only occurs if there is no other source of
039 * custom rules available.
040 *
041 * @since 1.6
042 */
043
044 public class FinderSetProperties extends RuleFinder {
045 public static String DFLT_PROPS_ATTR = "setprops";
046 public static String DFLT_FALSEVAL = "false";
047
048 private String propsAttr;
049 private String falseval;
050
051 /** See {@link #findLoader}. */
052 public FinderSetProperties() {
053 this(DFLT_PROPS_ATTR, DFLT_FALSEVAL);
054 }
055
056 /**
057 * Create a rule-finder which will arrange for a SetPropertiesRule to
058 * be defined for each instance of a plugin, so that xml attributes
059 * map to bean properties.
060 * <p>
061 * Param falseval will commonly be the string "false" for config files
062 * written in English.
063 *
064 * @param propsAttr must be non-null.
065 * @param falseval must be non-null.
066 */
067 public FinderSetProperties(String propsAttr, String falseval) {
068 this.propsAttr = propsAttr;
069 this.falseval = falseval;
070 }
071
072 /**
073 * Returns a RuleLoader <i>unless</i> the properties contain an entry
074 * with the name matching constructor param propsAttr, and the value
075 * matching what is in falseval.
076 * <p>
077 * If no custom source of rules for a plugin is found, then the user
078 * almost always wants xml attributes to map to java bean properties,
079 * so this is the default behaviour unless the user explicitly indicates
080 * that they do <i>not</i> want a SetPropertiesRule to be provided for
081 * the plugged-in class.
082 * <p>
083 * The returned object (when non-null) will add a SetPropertiesRule to
084 * the digester whenever its addRules method is invoked.
085 */
086 @Override
087 public RuleLoader findLoader(Digester d, Class<?> pluginClass, Properties p) {
088 String state = p.getProperty(propsAttr);
089 if ((state != null) && state.equals(falseval)) {
090 // user has explicitly disabled automatic setting of properties.
091 // this is not expected to be common, but allowed.
092 return null;
093 }
094
095 return new LoaderSetProperties();
096 }
097 }
098