1. /* $Id: PluginDeclarationRule.java,v 1.16 2004/05/10 06:44:13 skitching Exp $
  2. *
  3. * Copyright 2003-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;
  18. import java.util.Properties;
  19. import org.apache.commons.digester.Rule;
  20. import org.apache.commons.digester.Digester;
  21. import org.apache.commons.beanutils.MethodUtils;
  22. import org.apache.commons.logging.Log;
  23. /**
  24. * A Digester rule which allows the user to pre-declare a class which is to
  25. * be referenced later at a plugin point by a PluginCreateRule.
  26. * <p>
  27. * Normally, a PluginDeclarationRule is added to a Digester instance with
  28. * the pattern "{root}/plugin" or "* /plugin" where {root} is the name of
  29. * the root tag in the input document.
  30. *
  31. * @since 1.6
  32. */
  33. public class PluginDeclarationRule extends Rule {
  34. //------------------- constructors ---------------------------------------
  35. /** constructor */
  36. public PluginDeclarationRule() {
  37. super();
  38. }
  39. //------------------- methods --------------------------------------------
  40. /**
  41. * Invoked upon reading a tag defining a plugin declaration. The tag
  42. * must have the following mandatory attributes:
  43. * <ul>
  44. * <li> id </li>
  45. * <li> class </li>
  46. * </ul>
  47. *
  48. *@param namespace The xml namespace in which the xml element which
  49. * triggered this rule resides.
  50. *@param name The name of the xml element which triggered this rule.
  51. *@param attributes The set of attributes on the xml element which
  52. * triggered this rule.
  53. *@exception java.lang.Exception
  54. */
  55. public void begin(String namespace, String name,
  56. org.xml.sax.Attributes attributes)
  57. throws java.lang.Exception {
  58. int nAttrs = attributes.getLength();
  59. Properties props = new Properties();
  60. for(int i=0; i<nAttrs; ++i) {
  61. String key = attributes.getLocalName(i);
  62. if ((key == null) || (key.length() == 0)) {
  63. key = attributes.getQName(i);
  64. }
  65. String value = attributes.getValue(i);
  66. props.setProperty(key, value);
  67. }
  68. try {
  69. declarePlugin(digester, props);
  70. } catch(PluginInvalidInputException ex) {
  71. throw new PluginInvalidInputException(
  72. "Error on element [" + digester.getMatch() +
  73. "]: " + ex.getMessage());
  74. }
  75. }
  76. public static void declarePlugin(Digester digester, Properties props)
  77. throws PluginException {
  78. Log log = digester.getLogger();
  79. boolean debug = log.isDebugEnabled();
  80. String id = props.getProperty("id");
  81. String pluginClassName = props.getProperty("class");
  82. if (id == null) {
  83. throw new PluginInvalidInputException(
  84. "mandatory attribute id not present on plugin declaration");
  85. }
  86. if (pluginClassName == null) {
  87. throw new PluginInvalidInputException(
  88. "mandatory attribute class not present on plugin declaration");
  89. }
  90. Declaration newDecl = new Declaration(pluginClassName);
  91. newDecl.setId(id);
  92. newDecl.setProperties(props);
  93. PluginRules rc = (PluginRules) digester.getRules();
  94. PluginManager pm = rc.getPluginManager();
  95. newDecl.init(digester, pm);
  96. pm.addDeclaration(newDecl);
  97. // Note that it is perfectly safe to redeclare a plugin, because
  98. // the declaration doesn't add any rules to digester; all it does
  99. // is create a RuleLoader instance whch is *capable* of adding the
  100. // rules to the digester.
  101. }
  102. }