1. /* $Id: FinderFromMethod.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.util.Properties;
  19. import org.apache.commons.digester.Digester;
  20. import org.apache.commons.digester.plugins.RuleFinder;
  21. import org.apache.commons.digester.plugins.RuleLoader;
  22. import org.apache.commons.digester.plugins.PluginException;
  23. /**
  24. * A rule-finding algorithm which expects the caller to specify a methodname
  25. * as a plugin property, where the method exists on the plugin class.
  26. *
  27. * @since 1.6
  28. */
  29. public class FinderFromMethod extends RuleFinder {
  30. /**
  31. * Xml attribute that needs to be present on a plugin declaration
  32. * in order to specify the method to load rules from.
  33. */
  34. public static String DFLT_METHOD_ATTR = "method";
  35. /** See {@link #findLoader}. */
  36. private String methodAttr;
  37. /** Constructor. */
  38. public FinderFromMethod() {
  39. this(DFLT_METHOD_ATTR);
  40. }
  41. /** See {@link #findLoader}. */
  42. public FinderFromMethod(String methodAttr) {
  43. this.methodAttr = methodAttr;
  44. }
  45. /**
  46. * If there exists a property with the name matching constructor param
  47. * methodAttr, then locate the appropriate Method on the plugin class
  48. * and return an object encapsulating that info.
  49. * <p>
  50. * If there is no matching property provided, then just return null.
  51. * <p>
  52. * The returned object (when non-null) will invoke the target method
  53. * on the plugin class whenever its addRules method is invoked. The
  54. * target method is expected to have the following prototype:
  55. * <code> public static void xxxxx(Digester d, String patternPrefix); </code>
  56. */
  57. public RuleLoader findLoader(Digester d, Class pluginClass, Properties p)
  58. throws PluginException {
  59. String methodName = p.getProperty(methodAttr);
  60. if (methodName == null) {
  61. // nope, user hasn't requested dynamic rules to be loaded
  62. // from a specific class.
  63. return null;
  64. }
  65. return new LoaderFromClass(pluginClass, methodName);
  66. }
  67. }