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 caller to specify a methodname as a plugin property, where the method
031     * exists on the plugin class.
032     * 
033     * @since 1.6
034     */
035    public class FinderFromMethod
036        extends RuleFinder
037    {
038    
039        /**
040         * Default XML attribute that needs to be present on a plugin declaration in order to specify the method
041         * to load rules from.
042         */
043        private static final String DFLT_METHOD_ATTR = "method";
044    
045        /** See {@link #findLoader}. */
046        private final String methodAttr;
047    
048        /** Constructor. */
049        public FinderFromMethod()
050        {
051            this( DFLT_METHOD_ATTR );
052        }
053    
054        /**
055         * See {@link #findLoader}.
056         *
057         * @param methodAttr The XML attribute that needs to be present on a plugin declaration in order to specify the
058         *        method to load rules from.
059         */
060        public FinderFromMethod( String methodAttr )
061        {
062            this.methodAttr = methodAttr;
063        }
064    
065        /**
066         * If there exists a property with the name matching constructor param methodAttr, then locate the appropriate
067         * Method on the plugin class and return an object encapsulating that info.
068         * <p>
069         * If there is no matching property provided, then just return null.
070         * <p>
071         * The returned object (when non-null) will invoke the target method on the plugin class whenever its addRules
072         * method is invoked. The target method is expected to have the following prototype:
073         * <code> public static void xxxxx(Digester d, String patternPrefix); </code>
074         *
075         * @param d The digester instance where locating plugin classes
076         * @param pluginClass The plugin Java class
077         * @param p The properties object that holds any xml attributes the user may have specified on the plugin
078         *          declaration in order to indicate how to locate the plugin rules.
079         * @return a source of digester rules for the specified plugin class.
080         * @throws PluginException if the algorithm finds a source of rules, but there is something invalid
081         *         about that source.
082         */
083        @Override
084        public RuleLoader findLoader( Digester d, Class<?> pluginClass, Properties p )
085            throws PluginException
086        {
087    
088            String methodName = p.getProperty( methodAttr );
089            if ( methodName == null )
090            {
091                // nope, user hasn't requested dynamic rules to be loaded
092                // from a specific class.
093                return null;
094            }
095    
096            return new LoaderFromClass( pluginClass, methodName );
097        }
098    
099    }