KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > plugins > strategies > FinderFromMethod


1 /* $Id: FinderFromMethod.java 155412 2005-02-26 12:58:36Z dirkv $
2  *
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17  
18 package org.apache.commons.digester.plugins.strategies;
19
20 import java.util.Properties JavaDoc;
21 import org.apache.commons.digester.Digester;
22 import org.apache.commons.digester.plugins.RuleFinder;
23 import org.apache.commons.digester.plugins.RuleLoader;
24 import org.apache.commons.digester.plugins.PluginException;
25
26 /**
27  * A rule-finding algorithm which expects the caller to specify a methodname
28  * as a plugin property, where the method exists on the plugin class.
29  *
30  * @since 1.6
31  */

32
33 public class FinderFromMethod extends RuleFinder {
34     /**
35      * Xml attribute that needs to be present on a plugin declaration
36      * in order to specify the method to load rules from.
37      */

38     public static String JavaDoc DFLT_METHOD_ATTR = "method";
39     
40     /** See {@link #findLoader}. */
41     private String JavaDoc methodAttr;
42     
43     /** Constructor. */
44     public FinderFromMethod() {
45         this(DFLT_METHOD_ATTR);
46     }
47
48     /** See {@link #findLoader}. */
49     public FinderFromMethod(String JavaDoc methodAttr) {
50         this.methodAttr = methodAttr;
51     }
52     
53     /**
54      * If there exists a property with the name matching constructor param
55      * methodAttr, then locate the appropriate Method on the plugin class
56      * and return an object encapsulating that info.
57      * <p>
58      * If there is no matching property provided, then just return null.
59      * <p>
60      * The returned object (when non-null) will invoke the target method
61      * on the plugin class whenever its addRules method is invoked. The
62      * target method is expected to have the following prototype:
63      * <code> public static void xxxxx(Digester d, String patternPrefix); </code>
64      */

65     public RuleLoader findLoader(Digester d, Class JavaDoc pluginClass, Properties JavaDoc p)
66                         throws PluginException {
67
68         String JavaDoc methodName = p.getProperty(methodAttr);
69         if (methodName == null) {
70             // nope, user hasn't requested dynamic rules to be loaded
71
// from a specific class.
72
return null;
73         }
74         
75         return new LoaderFromClass(pluginClass, methodName);
76     }
77 }
78
79
Popular Tags