KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: FinderFromDfltClass.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 looks for a method with a specific name
28  * on a class whose name is derived from the plugin class name.
29  *
30  * @since 1.6
31  */

32
33 public class FinderFromDfltClass extends RuleFinder {
34     public static String JavaDoc DFLT_RULECLASS_SUFFIX = "RuleInfo";
35     public static String JavaDoc DFLT_METHOD_NAME = "addRules";
36     
37     private String JavaDoc rulesClassSuffix;
38     private String JavaDoc methodName;
39     
40     /** See {@link #findLoader}. */
41     public FinderFromDfltClass() {
42         this(DFLT_RULECLASS_SUFFIX, DFLT_METHOD_NAME);
43     }
44     
45     /**
46      * Create a rule-finder which invokes a method on a class whenever
47      * dynamic rules for a plugin need to be loaded. See the findRules
48      * method for more info.
49      *
50      * @param rulesClassSuffix must be non-null.
51      * @param methodName may be null.
52      */

53      public FinderFromDfltClass(String JavaDoc rulesClassSuffix, String JavaDoc methodName) {
54         this.rulesClassSuffix = rulesClassSuffix;
55         this.methodName = methodName;
56     }
57     
58     /**
59      * If there exists a class whose name is the plugin class name + the
60      * suffix specified to the constructor, then load that class, locate
61      * the appropriate rules-adding method on that class, and return an
62      * object encapsulating that info.
63      * <p>
64      * If there is no such class, then just return null.
65      * <p>
66      * The returned object (when non-null) will invoke the target method
67      * on the selected class whenever its addRules method is invoked. The
68      * target method is expected to have the following prototype:
69      * <code> public static void xxxxx(Digester d, String patternPrefix); </code>
70      */

71     public RuleLoader findLoader(Digester digester, Class JavaDoc pluginClass, Properties JavaDoc p)
72                             throws PluginException {
73
74         String JavaDoc rulesClassName = pluginClass.getName() + rulesClassSuffix;
75
76         Class JavaDoc rulesClass = null;
77         try {
78             rulesClass = digester.getClassLoader().loadClass(rulesClassName);
79         } catch(ClassNotFoundException JavaDoc cnfe) {
80             // ok, ignore
81
}
82
83         if (rulesClass == null) {
84             // nope, no rule-info class in the classpath
85
return null;
86         }
87         
88         if (methodName == null) {
89             methodName = DFLT_METHOD_NAME;
90         }
91         
92         return new LoaderFromClass(rulesClass, methodName);
93     }
94 }
95
96
Popular Tags