1. /* $Id: FinderSetProperties.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 whether
  28. * "automatic property setting" is desired. If this class discovers that
  29. * this is in fact the case for a declaration, then a RuleLoader is returned
  30. * which, when invoked, adds a single SetPropertiesRule instance to the
  31. * digester.
  32. * <p>
  33. * This allows ordinary JavaBean classes to be used as plugins, and have
  34. * xml attributes be mapped to bean properties of the same name, without
  35. * any custom plugin rules being created for them.
  36. * <p>
  37. * This RuleFinder is typically used as the <i>last</i> RuleFinder, so that
  38. * automatic property setting only occurs if there is no other source of
  39. * custom rules available.
  40. *
  41. * @since 1.6
  42. */
  43. public class FinderSetProperties extends RuleFinder {
  44. public static String DFLT_PROPS_ATTR = "setprops";
  45. public static String DFLT_FALSEVAL = "false";
  46. private String propsAttr;
  47. private String falseval;
  48. /** See {@link #findLoader}. */
  49. public FinderSetProperties() {
  50. this(DFLT_PROPS_ATTR, DFLT_FALSEVAL);
  51. }
  52. /**
  53. * Create a rule-finder which will arrange for a SetPropertiesRule to
  54. * be defined for each instance of a plugin, so that xml attributes
  55. * map to bean properties.
  56. * <p>
  57. * Param falseval will commonly be the string "false" for config files
  58. * written in English.
  59. *
  60. * @param propsAttr must be non-null.
  61. * @param falseval must be non-null.
  62. */
  63. public FinderSetProperties(String propsAttr, String falseval) {
  64. this.propsAttr = propsAttr;
  65. this.falseval = falseval;
  66. }
  67. /**
  68. * Returns a RuleLoader <i>unless</i> the properties contain an entry
  69. * with the name matching constructor param propsAttr, and the value
  70. * matching what is in falseval.
  71. * <p>
  72. * If no custom source of rules for a plugin is found, then the user
  73. * almost always wants xml attributes to map to java bean properties,
  74. * so this is the default behaviour unless the user explicitly indicates
  75. * that they do <i>not</i> want a SetPropertiesRule to be provided for
  76. * the plugged-in class.
  77. * <p>
  78. * The returned object (when non-null) will add a SetPropertiesRule to
  79. * the digester whenever its addRules method is invoked.
  80. */
  81. public RuleLoader findLoader(Digester d, Class pluginClass, Properties p) {
  82. String state = p.getProperty(propsAttr);
  83. if ((state != null) && state.equals(falseval)) {
  84. // user has explicitly disabled automatic setting of properties.
  85. // this is not expected to be common, but allowed.
  86. return null;
  87. }
  88. return new LoaderSetProperties();
  89. }
  90. }