1. /* $Id: VariableAttributes.java,v 1.6 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.xml.sax.Attributes;
  19. import java.util.Map;
  20. import java.util.ArrayList;
  21. /**
  22. * <p>Wrapper for an org.xml.sax.Attributes object which expands any
  23. * "variables" referenced in the attribute value via ${foo} or similar.
  24. * This is only done something actually asks for the attribute value,
  25. * thereby imposing no performance penalty if the attribute is not used.</p>
  26. *
  27. * @since 1.6
  28. */
  29. public class VariableAttributes implements Attributes {
  30. // list of mapped attributes.
  31. private ArrayList values = new ArrayList(10);
  32. private Attributes attrs;
  33. private VariableExpander expander;
  34. // ------------------- Public Methods
  35. /**
  36. * Specify which attributes class this object is a proxy for.
  37. */
  38. public void init(Attributes attrs, VariableExpander expander) {
  39. this.attrs = attrs;
  40. this.expander = expander;
  41. // I hope this doesn't release the memory for this array; for
  42. // efficiency, this should just mark the array as being size 0.
  43. values.clear();
  44. }
  45. public String getValue(int index) {
  46. if (index >= values.size()) {
  47. // Expand the values array with null elements, so the later
  48. // call to set(index, s) works ok.
  49. //
  50. // Unfortunately, there is no easy way to set the size of
  51. // an arraylist; we must repeatedly add null elements to it..
  52. values.ensureCapacity(index+1);
  53. for(int i = values.size(); i<= index; ++i) {
  54. values.add(null);
  55. }
  56. }
  57. String s = (String) values.get(index);
  58. if (s == null) {
  59. // we have never been asked for this value before.
  60. // get the real attribute value and perform substitution
  61. // on it.
  62. s = attrs.getValue(index);
  63. if (s != null) {
  64. s = expander.expand(s);
  65. values.set(index, s);
  66. }
  67. }
  68. return s;
  69. }
  70. public String getValue(String qname) {
  71. int index = attrs.getIndex(qname);
  72. if (index == -1) {
  73. return null;
  74. }
  75. return getValue(index);
  76. }
  77. public String getValue(String uri, String localname) {
  78. int index = attrs.getIndex(uri, localname);
  79. if (index == -1) {
  80. return null;
  81. }
  82. return getValue(index);
  83. }
  84. // plain proxy methods follow : nothing interesting :-)
  85. public int getIndex(String qname) {
  86. return attrs.getIndex(qname);
  87. }
  88. public int getIndex(String uri, String localpart) {
  89. return attrs.getIndex(uri, localpart);
  90. }
  91. public int getLength() {
  92. return attrs.getLength();
  93. }
  94. public String getLocalName(int index) {
  95. return attrs.getLocalName(index);
  96. }
  97. public String getQName(int index) {
  98. return attrs.getQName(index);
  99. }
  100. public String getType(int index) {
  101. return attrs.getType(index);
  102. }
  103. public String getType(String qname) {
  104. return attrs.getType(qname);
  105. }
  106. public String getType(String uri, String localname) {
  107. return attrs.getType(uri, localname);
  108. }
  109. public String getURI(int index) {
  110. return attrs.getURI(index);
  111. }
  112. }