001    /* $Id: FinderFromMethod.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    import org.apache.commons.digester.Digester;
022    import org.apache.commons.digester.plugins.RuleFinder;
023    import org.apache.commons.digester.plugins.RuleLoader;
024    import org.apache.commons.digester.plugins.PluginException;
025    
026    /**
027     * A rule-finding algorithm which expects the caller to specify a methodname
028     * as a plugin property, where the method exists on the plugin class.
029     *
030     * @since 1.6
031     */
032    
033    public class FinderFromMethod extends RuleFinder {
034        /**
035         * Xml attribute that needs to be present on a plugin declaration
036         * in order to specify the method to load rules from.
037         */
038        public static String DFLT_METHOD_ATTR = "method";
039        
040        /** See {@link #findLoader}. */
041        private String methodAttr;
042        
043        /** Constructor. */
044        public FinderFromMethod() {
045            this(DFLT_METHOD_ATTR);
046        }
047    
048        /** See {@link #findLoader}. */
049        public FinderFromMethod(String methodAttr) { 
050            this.methodAttr = methodAttr;
051        }
052        
053        /**
054         * If there exists a property with the name matching constructor param
055         * methodAttr, then locate the appropriate Method on the plugin class 
056         * and return an object encapsulating that info.
057         * <p>
058         * If there is no matching property provided, then just return null.
059         * <p>
060         * The returned object (when non-null) will invoke the target method
061         * on the plugin class whenever its addRules method is invoked. The
062         * target method is expected to have the following prototype:
063         * <code> public static void xxxxx(Digester d, String patternPrefix); </code>
064         */
065        @Override
066        public RuleLoader findLoader(Digester d, Class<?> pluginClass, Properties p)
067                            throws PluginException {
068    
069            String methodName = p.getProperty(methodAttr);
070            if (methodName == null) {
071                // nope, user hasn't requested dynamic rules to be loaded
072                // from a specific class.
073                return null;
074            }
075            
076            return new LoaderFromClass(pluginClass, methodName);
077        }
078    }
079