1 package org.apache.commons.digester3.plugins.strategies;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.Properties;
23 import java.lang.reflect.Method;
24
25 import org.apache.commons.digester3.Digester;
26 import org.apache.commons.digester3.plugins.PluginException;
27 import org.apache.commons.digester3.plugins.RuleFinder;
28 import org.apache.commons.digester3.plugins.RuleLoader;
29
30 /**
31 * A rule-finding algorithm which looks for a method with a specific name on the plugin class.
32 *
33 * @since 1.6
34 */
35 public class FinderFromDfltMethod
36 extends RuleFinder
37 {
38 private static final String DFLT_METHOD_NAME = "addRules";
39
40 private final String methodName;
41
42 /** See {@link #findLoader}. */
43 public FinderFromDfltMethod()
44 {
45 this( DFLT_METHOD_NAME );
46 }
47
48 /**
49 * Create a rule-finder which invokes a specific method on the plugin class whenever dynamic rules for a plugin need
50 * to be loaded. See the findRules method for more info.
51 *
52 * @param methodName must be non-null.
53 */
54 public FinderFromDfltMethod( String methodName )
55 {
56 this.methodName = methodName;
57 }
58
59 /**
60 * If there exists on the plugin class a method with name matching the constructor's methodName value then locate
61 * the appropriate Method on the plugin class and return an object encapsulating that info.
62 * <p>
63 * If there is no matching method then just return null.
64 * <p>
65 * The returned object (when non-null) will invoke the target method on the plugin class whenever its addRules
66 * method is invoked. The target method is expected to have the following prototype:
67 * <code> public static void xxxxx(Digester d, String patternPrefix); </code>
68 *
69 * @param d The digester instance where locating plugin classes
70 * @param pluginClass The plugin Java class
71 * @param p The properties object that holds any xml attributes the user may have specified on the plugin
72 * declaration in order to indicate how to locate the plugin rules.
73 * @return a source of digester rules for the specified plugin class.
74 * @throws PluginException if the algorithm finds a source of rules, but there is something invalid
75 * about that source.
76 */
77 @Override
78 public RuleLoader findLoader( Digester d, Class<?> pluginClass, Properties p )
79 throws PluginException
80 {
81
82 Method rulesMethod = LoaderFromClass.locateMethod( pluginClass, methodName );
83 if ( rulesMethod == null )
84 {
85 return null;
86 }
87
88 return new LoaderFromClass( pluginClass, rulesMethod );
89 }
90
91 }