1. /* $Id: FinderFromResource.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 java.io.InputStream;
  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. /**
  25. * A rule-finding algorithm which expects the user to specify a resource
  26. * name (ie a file in the classpath). The file is expected to contain Digester
  27. * rules in xmlrules format.
  28. *
  29. * @since 1.6
  30. */
  31. public class FinderFromResource extends RuleFinder {
  32. /**
  33. * Name of xml attribute on the plugin declaration which is used
  34. * to configure rule-loading for that declaration.
  35. */
  36. public static String DFLT_RESOURCE_ATTR = "resource";
  37. /** See {@link #findLoader}. */
  38. private String resourceAttr;
  39. /** Constructor. */
  40. public FinderFromResource() {
  41. this(DFLT_RESOURCE_ATTR);
  42. }
  43. /** See {@link #findLoader}. */
  44. public FinderFromResource(String resourceAttr) {
  45. this.resourceAttr = resourceAttr;
  46. }
  47. /**
  48. * If there exists a property with the name matching constructor param
  49. * resourceAttr, then load that file, run it through the xmlrules
  50. * module and return an object encapsulating those rules.
  51. * <p>
  52. * If there is no matching property provided, then just return null.
  53. * <p>
  54. * The returned object (when non-null) will add the selected rules to
  55. * the digester whenever its addRules method is invoked.
  56. */
  57. public RuleLoader findLoader(Digester d, Class pluginClass, Properties p)
  58. throws PluginException {
  59. String resourceName = p.getProperty(resourceAttr);
  60. if (resourceName == null) {
  61. // nope, user hasn't requested dynamic rules to be loaded
  62. // from a specific file.
  63. return null;
  64. }
  65. InputStream is =
  66. pluginClass.getClassLoader().getResourceAsStream(
  67. resourceName);
  68. if (is == null) {
  69. throw new PluginException(
  70. "Resource " + resourceName + " not found.");
  71. }
  72. return loadRules(d, pluginClass, is, resourceName);
  73. }
  74. /**
  75. * Open the specified resource file (ie a file in the classpath,
  76. * including being within a jar in the classpath), run it through
  77. * the xmlrules module and return an object encapsulating those rules.
  78. *
  79. * @param d is the digester into which rules will eventually be loaded.
  80. * @param pluginClass is the class whose xml params the rules are parsing.
  81. * @param is is where the xmlrules will be read from, and must be non-null.
  82. * @param resourceName is a string describing the source of the xmlrules,
  83. * for use in generating error messages.
  84. */
  85. public static RuleLoader loadRules(Digester d, Class pluginClass,
  86. InputStream is, String resourceName)
  87. throws PluginException {
  88. try {
  89. RuleLoader loader = new LoaderFromStream(is);
  90. return loader;
  91. } catch(Exception e) {
  92. throw new PluginException(
  93. "Unable to load xmlrules from resource [" +
  94. resourceName + "]", e);
  95. } finally {
  96. try {
  97. is.close();
  98. } catch(java.io.IOException ioe) {
  99. throw new PluginException(
  100. "Unable to close stream for resource [" +
  101. resourceName + "]", ioe);
  102. }
  103. }
  104. }
  105. }