1. /* $Id: VariableSubstitutor.java,v 1.8 2004/05/10 06:46:31 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.substitution;
  18. import org.apache.commons.digester.Substitutor;
  19. import org.xml.sax.Attributes;
  20. /**
  21. * Substitutor implementation that support variable replacement
  22. * for both attributes and body text.
  23. * The actual expansion of variables into text is delegated to {@link VariableExpander}
  24. * implementations.
  25. * Supports setting an expander just for body text or just for attributes.
  26. * Also supported is setting no expanders for body text and for attributes.
  27. *
  28. * @since 1.6
  29. */
  30. public class VariableSubstitutor extends Substitutor {
  31. /**
  32. * The expander to be used to expand variables in the attributes.
  33. * Null when no expansion should be performed.
  34. */
  35. private VariableExpander attributesExpander;
  36. /**
  37. * Attributes implementation that (lazily) performs variable substitution.
  38. * Will be lazily created when needed then reused.
  39. */
  40. private VariableAttributes variableAttributes;
  41. /**
  42. * The expander to be used to expand variables in the body text.
  43. * Null when no expansion should be performed.
  44. */
  45. private VariableExpander bodyTextExpander;
  46. /**
  47. * Constructs a Substitutor which uses the same VariableExpander for both
  48. * body text and attibutes.
  49. * @param expander VariableExpander implementation,
  50. * null if no substitutions are to be performed
  51. */
  52. public VariableSubstitutor(VariableExpander expander) {
  53. this(expander, expander);
  54. }
  55. /**
  56. * Constructs a Substitutor.
  57. * @param attributesExpander VariableExpander implementation to be used for attributes,
  58. * null if no attribute substitutions are to be performed
  59. * @param bodyTextExpander VariableExpander implementation to be used for bodyTextExpander,
  60. * null if no attribute substitutions are to be performed
  61. */
  62. public VariableSubstitutor(VariableExpander attributesExpander, VariableExpander bodyTextExpander) {
  63. this.attributesExpander = attributesExpander;
  64. this.bodyTextExpander = bodyTextExpander;
  65. variableAttributes = new VariableAttributes();
  66. }
  67. /**
  68. * Substitutes the attributes (before they are passed to the
  69. * <code>Rule</code> implementations's)
  70. */
  71. public Attributes substitute(Attributes attributes) {
  72. Attributes results = attributes;
  73. if (attributesExpander != null) {
  74. variableAttributes.init(attributes, attributesExpander);
  75. results = variableAttributes;
  76. }
  77. return results;
  78. }
  79. /**
  80. * Substitutes for the body text.
  81. * This method may substitute values into the body text of the
  82. * elements that Digester parses.
  83. *
  84. * @param bodyText the body text (as passed to <code>Digester</code>)
  85. * @return the body text to be passed to the <code>Rule</code> implementations
  86. */
  87. public String substitute(String bodyText) {
  88. String result = bodyText;
  89. if (bodyTextExpander != null) {
  90. result = bodyTextExpander.expand(bodyText);
  91. }
  92. return result;
  93. }
  94. }