001/* $Id: FinderSetProperties.java 471661 2006-11-06 08:09:25Z skitching $
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 
019package org.apache.commons.digester.plugins.strategies;
020
021import java.util.Properties;
022
023import org.apache.commons.digester.Digester;
024import org.apache.commons.digester.plugins.RuleFinder;
025import org.apache.commons.digester.plugins.RuleLoader;
026
027/**
028 * A rule-finding algorithm which expects the user to specify whether
029 * "automatic property setting" is desired. If this class discovers that
030 * this is in fact the case for a declaration, then a RuleLoader is returned
031 * which, when invoked, adds a single SetPropertiesRule instance to the
032 * digester.
033 * <p>
034 * This allows ordinary JavaBean classes to be used as plugins, and have
035 * xml attributes be mapped to bean properties of the same name, without
036 * any custom plugin rules being created for them.
037 * <p>
038 * This RuleFinder is typically used as the <i>last</i> RuleFinder, so that
039 * automatic property setting only occurs if there is no other source of
040 * custom rules available.
041 *
042 * @since 1.6
043 */
044
045public class FinderSetProperties extends RuleFinder {
046    public static String DFLT_PROPS_ATTR = "setprops";
047    public static String DFLT_FALSEVAL = "false";
048
049    private String propsAttr;
050    private String falseval;
051    
052    /** See {@link #findLoader}. */
053    public FinderSetProperties() {
054        this(DFLT_PROPS_ATTR, DFLT_FALSEVAL);
055    }
056    
057    /**
058     * Create a rule-finder which will arrange for a SetPropertiesRule to
059     * be defined for each instance of a plugin, so that xml attributes
060     * map to bean properties.
061     * <p>
062     * Param falseval will commonly be the string "false" for config files 
063     * written in English.
064     *
065     * @param propsAttr must be non-null.
066     * @param falseval must be non-null.
067     */
068    public FinderSetProperties(String propsAttr, String falseval) { 
069        this.propsAttr = propsAttr;
070        this.falseval = falseval;
071    }
072    
073    /**
074     * Returns a RuleLoader <i>unless</i> the properties contain an entry
075     * with the name matching constructor param propsAttr, and the value 
076     * matching what is in falseval.
077     * <p>
078     * If no custom source of rules for a plugin is found, then the user
079     * almost always wants xml attributes to map to java bean properties,
080     * so this is the default behaviour unless the user explicitly indicates
081     * that they do <i>not</i> want a SetPropertiesRule to be provided for
082     * the plugged-in class.
083     * <p>
084     * The returned object (when non-null) will add a SetPropertiesRule to
085     * the digester whenever its addRules method is invoked.
086     */
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