01: /* $Id: FinderFromMethod.java 471661 2006-11-06 08:09:25Z skitching $
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package org.apache.commons.digester.plugins.strategies;
20:
21: import java.util.Properties;
22: import org.apache.commons.digester.Digester;
23: import org.apache.commons.digester.plugins.RuleFinder;
24: import org.apache.commons.digester.plugins.RuleLoader;
25: import org.apache.commons.digester.plugins.PluginException;
26:
27: /**
28: * A rule-finding algorithm which expects the caller to specify a methodname
29: * as a plugin property, where the method exists on the plugin class.
30: *
31: * @since 1.6
32: */
33:
34: public class FinderFromMethod extends RuleFinder {
35: /**
36: * Xml attribute that needs to be present on a plugin declaration
37: * in order to specify the method to load rules from.
38: */
39: public static String DFLT_METHOD_ATTR = "method";
40:
41: /** See {@link #findLoader}. */
42: private String methodAttr;
43:
44: /** Constructor. */
45: public FinderFromMethod() {
46: this (DFLT_METHOD_ATTR);
47: }
48:
49: /** See {@link #findLoader}. */
50: public FinderFromMethod(String methodAttr) {
51: this .methodAttr = methodAttr;
52: }
53:
54: /**
55: * If there exists a property with the name matching constructor param
56: * methodAttr, then locate the appropriate Method on the plugin class
57: * and return an object encapsulating that info.
58: * <p>
59: * If there is no matching property provided, then just return null.
60: * <p>
61: * The returned object (when non-null) will invoke the target method
62: * on the plugin class whenever its addRules method is invoked. The
63: * target method is expected to have the following prototype:
64: * <code> public static void xxxxx(Digester d, String patternPrefix); </code>
65: */
66: public RuleLoader findLoader(Digester d, Class pluginClass,
67: Properties p) throws PluginException {
68:
69: String methodName = p.getProperty(methodAttr);
70: if (methodName == null) {
71: // nope, user hasn't requested dynamic rules to be loaded
72: // from a specific class.
73: return null;
74: }
75:
76: return new LoaderFromClass(pluginClass, methodName);
77: }
78: }
|