1. /* $Id: FinderFromFile.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 java.io.FileInputStream;
  21. import java.io.IOException;
  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. * A rule-finding algorithm which expects the user to specify an absolute
  28. * or relative path in the plugin declaration.
  29. * <p>
  30. * The file is expected to contain Digester rules in xmlrules format.
  31. *
  32. * @since 1.6
  33. */
  34. public class FinderFromFile extends RuleFinder {
  35. /**
  36. * Xml attribute that needs to be present on a plugin declaration
  37. * in order to specify the file to load rules from.
  38. */
  39. public static String DFLT_FILENAME_ATTR = "file";
  40. /** See {@link #findLoader}. */
  41. private String filenameAttr;
  42. /** See {@link #findLoader}. */
  43. public FinderFromFile() {
  44. this(DFLT_FILENAME_ATTR);
  45. }
  46. /** See {@link #findLoader}. */
  47. public FinderFromFile(String filenameAttr) {
  48. this.filenameAttr = filenameAttr;
  49. }
  50. /**
  51. * If there exists a property with the name specified in the constructor,
  52. * then load that file, run it through the xmlrules module and return an
  53. * object encapsulating those rules.
  54. * <p>
  55. * If there is no matching property provided, then just return null.
  56. * <p>
  57. * The returned object (when non-null) will add the selected rules to
  58. * the digester whenever its addRules method is invoked.
  59. */
  60. public RuleLoader findLoader(Digester d, Class pluginClass, Properties p)
  61. throws PluginException {
  62. String rulesFileName = p.getProperty(filenameAttr);
  63. if (rulesFileName == null) {
  64. // nope, user hasn't requested dynamic rules to be loaded
  65. // from a specific file.
  66. return null;
  67. }
  68. InputStream is = null;
  69. try {
  70. is = new FileInputStream(rulesFileName);
  71. } catch(IOException ioe) {
  72. throw new PluginException(
  73. "Unable to process file [" + rulesFileName + "]", ioe);
  74. }
  75. try {
  76. RuleLoader loader = new LoaderFromStream(is);
  77. return loader;
  78. } catch(Exception e) {
  79. throw new PluginException(
  80. "Unable to load xmlrules from file [" +
  81. rulesFileName + "]", e);
  82. } finally {
  83. try {
  84. is.close();
  85. } catch(java.io.IOException ioe) {
  86. throw new PluginException(
  87. "Unable to close stream for file [" +
  88. rulesFileName + "]", ioe);
  89. }
  90. }
  91. }
  92. }