1. /* $Id: FinderFromDfltMethod.java,v 1.5 2004/05/10 06:34:01 skitching Exp $
  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. package org.apache.commons.digester.plugins.strategies;
  18. import java.io.InputStream;
  19. import java.util.Properties;
  20. import java.lang.reflect.Method;
  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. * A rule-finding algorithm which looks for a method with a specific name
  27. * on the plugin class.
  28. *
  29. * @since 1.6
  30. */
  31. public class FinderFromDfltMethod extends RuleFinder {
  32. public static String DFLT_METHOD_NAME = "addRules";
  33. private String methodName;
  34. /** See {@link #findLoader}. */
  35. public FinderFromDfltMethod() {
  36. this(DFLT_METHOD_NAME);
  37. }
  38. /**
  39. * Create a rule-finder which invokes a specific method on the plugin
  40. * class whenever dynamic rules for a plugin need to be loaded. See the
  41. * findRules method for more info.
  42. *
  43. * @param methodName must be non-null.
  44. */
  45. public FinderFromDfltMethod(String methodName) {
  46. this.methodName = methodName;
  47. }
  48. /**
  49. * If there exists on the plugin class a method with name matching the
  50. * constructor's methodName value then locate the appropriate Method on
  51. * the plugin class and return an object encapsulating that info.
  52. * <p>
  53. * If there is no matching method then just return null.
  54. * <p>
  55. * The returned object (when non-null) will invoke the target method
  56. * on the plugin class whenever its addRules method is invoked. The
  57. * target method is expected to have the following prototype:
  58. * <code> public static void xxxxx(Digester d, String patternPrefix); </code>
  59. */
  60. public RuleLoader findLoader(Digester d, Class pluginClass, Properties p)
  61. throws PluginException {
  62. Method rulesMethod = LoaderFromClass.locateMethod(pluginClass, methodName);
  63. if (rulesMethod == null) {
  64. return null;
  65. }
  66. return new LoaderFromClass(pluginClass, rulesMethod);
  67. }
  68. }