1. /* $Id: AbstractRulesImpl.java,v 1.7 2004/05/10 06:30:06 skitching Exp $
  2. *
  3. * Copyright 2001-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;
  18. import java.util.List;
  19. /**
  20. * <p><code>AbstractRuleImpl</code> provides basic services for <code>Rules</code> implementations.
  21. * Extending this class should make it easier to create a <code>Rules</code> implementation.</p>
  22. *
  23. * <p><code>AbstractRuleImpl</code> manages the <code>Digester</code>
  24. * and <code>namespaceUri</code> properties.
  25. * If the subclass overrides {@link #registerRule} (rather than {@link #add}),
  26. * then the <code>Digester</code> and <code>namespaceURI</code> of the <code>Rule</code>
  27. * will be set correctly before it is passed to <code>registerRule</code>.
  28. * The subclass can then perform whatever it needs to do to register the rule.</p>
  29. *
  30. * @since 1.5
  31. */
  32. abstract public class AbstractRulesImpl implements Rules {
  33. // ------------------------------------------------------------- Fields
  34. /** Digester using this <code>Rules</code> implementation */
  35. private Digester digester;
  36. /** Namespace uri to assoicate with subsequent <code>Rule</code>'s */
  37. private String namespaceURI;
  38. // ------------------------------------------------------------- Properties
  39. /**
  40. * Return the Digester instance with which this Rules instance is
  41. * associated.
  42. */
  43. public Digester getDigester() {
  44. return digester;
  45. }
  46. /**
  47. * Set the Digester instance with which this Rules instance is associated.
  48. *
  49. * @param digester The newly associated Digester instance
  50. */
  51. public void setDigester(Digester digester) {
  52. this.digester = digester;
  53. }
  54. /**
  55. * Return the namespace URI that will be applied to all subsequently
  56. * added <code>Rule</code> objects.
  57. */
  58. public String getNamespaceURI() {
  59. return namespaceURI;
  60. }
  61. /**
  62. * Set the namespace URI that will be applied to all subsequently
  63. * added <code>Rule</code> objects.
  64. *
  65. * @param namespaceURI Namespace URI that must match on all
  66. * subsequently added rules, or <code>null</code> for matching
  67. * regardless of the current namespace URI
  68. */
  69. public void setNamespaceURI(String namespaceURI) {
  70. this.namespaceURI = namespaceURI;
  71. }
  72. // --------------------------------------------------------- Public Methods
  73. /**
  74. * Registers a new Rule instance matching the specified pattern.
  75. * This implementation sets the <code>Digester</code> and the
  76. * <code>namespaceURI</code> on the <code>Rule</code> before calling {@link #registerRule}.
  77. *
  78. * @param pattern Nesting pattern to be matched for this Rule
  79. * @param rule Rule instance to be registered
  80. */
  81. public void add(String pattern, Rule rule) {
  82. // set up rule
  83. if (this.digester != null) {
  84. rule.setDigester(this.digester);
  85. }
  86. if (this.namespaceURI != null) {
  87. rule.setNamespaceURI(this.namespaceURI);
  88. }
  89. registerRule(pattern, rule);
  90. }
  91. /**
  92. * Register rule at given pattern.
  93. * The the Digester and namespaceURI properties of the given <code>Rule</code>
  94. * can be assumed to have been set properly before this method is called.
  95. *
  96. * @param pattern Nesting pattern to be matched for this Rule
  97. * @param rule Rule instance to be registered
  98. */
  99. abstract protected void registerRule(String pattern, Rule rule);
  100. /**
  101. * Clear all existing Rule instance registrations.
  102. */
  103. abstract public void clear();
  104. /**
  105. * Return a List of all registered Rule instances that match the specified
  106. * nesting pattern, or a zero-length List if there are no matches. If more
  107. * than one Rule instance matches, they <strong>must</strong> be returned
  108. * in the order originally registered through the <code>add()</code>
  109. * method.
  110. *
  111. * @param pattern Nesting pattern to be matched
  112. *
  113. * @deprecated Call match(namespaceURI,pattern) instead.
  114. */
  115. public List match(String pattern) {
  116. return match(namespaceURI, pattern);
  117. }
  118. /**
  119. * Return a List of all registered Rule instances that match the specified
  120. * nesting pattern, or a zero-length List if there are no matches. If more
  121. * than one Rule instance matches, they <strong>must</strong> be returned
  122. * in the order originally registered through the <code>add()</code>
  123. * method.
  124. *
  125. * @param namespaceURI Namespace URI for which to select matching rules,
  126. * or <code>null</code> to match regardless of namespace URI
  127. * @param pattern Nesting pattern to be matched
  128. */
  129. abstract public List match(String namespaceURI, String pattern);
  130. /**
  131. * Return a List of all registered Rule instances, or a zero-length List
  132. * if there are no registered Rule instances. If more than one Rule
  133. * instance has been registered, they <strong>must</strong> be returned
  134. * in the order originally registered through the <code>add()</code>
  135. * method.
  136. */
  137. abstract public List rules();
  138. }