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