1. /* $Id: FinderFromDfltResource.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 org.apache.commons.digester.Digester;
  21. import org.apache.commons.digester.plugins.RuleFinder;
  22. import org.apache.commons.digester.plugins.RuleLoader;
  23. import org.apache.commons.digester.plugins.PluginException;
  24. import org.apache.commons.logging.Log;
  25. /**
  26. * A rule-finding algorithm which looks for a resource file in the classpath
  27. * whose name is derived from the plugin class name plus a specified suffix.
  28. * <p>
  29. * If the resource-file is found, then it is expected to define a set of
  30. * Digester rules in xmlrules format.
  31. *
  32. * @since 1.6
  33. */
  34. public class FinderFromDfltResource extends RuleFinder {
  35. public static String DFLT_RESOURCE_SUFFIX = "RuleInfo.xml";
  36. private String resourceSuffix;
  37. /** See {@link #findLoader}. */
  38. public FinderFromDfltResource() {
  39. this(DFLT_RESOURCE_SUFFIX);
  40. }
  41. /**
  42. * Create a rule-finder which can load an xmlrules file, cache
  43. * the rules away, and later add them as a plugin's custom rules
  44. * when that plugin is referenced.
  45. *
  46. * @param resourceSuffix must be non-null.
  47. */
  48. public FinderFromDfltResource(String resourceSuffix) {
  49. this.resourceSuffix = resourceSuffix;
  50. }
  51. /**
  52. * If there exists a resource file whose name is equal to the plugin
  53. * class name + the suffix specified in the constructor, then
  54. * load that file, run it through the xmlrules module and return an object
  55. * encapsulating those rules.
  56. * <p>
  57. * If there is no such resource file, then just return null.
  58. * <p>
  59. * The returned object (when non-null) will add the selected rules to
  60. * the digester whenever its addRules method is invoked.
  61. */
  62. public RuleLoader findLoader(Digester d, Class pluginClass, Properties p)
  63. throws PluginException {
  64. String resourceName =
  65. pluginClass.getName().replace('.', '/')
  66. + resourceSuffix;
  67. InputStream is =
  68. pluginClass.getClassLoader().getResourceAsStream(
  69. resourceName);
  70. if (is == null) {
  71. // ok, no such resource
  72. return null;
  73. }
  74. return FinderFromResource.loadRules(d, pluginClass, is, resourceName);
  75. }
  76. }