1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: MutableAttrListImpl.java,v 1.7 2004/02/17 04:21:14 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. import java.io.Serializable;
  21. import org.xml.sax.Attributes;
  22. import org.xml.sax.helpers.AttributesImpl;
  23. /**
  24. * Mutable version of AttributesImpl.
  25. * @xsl.usage advanced
  26. */
  27. public class MutableAttrListImpl extends AttributesImpl
  28. implements Serializable
  29. {
  30. /**
  31. * Construct a new, empty AttributesImpl object.
  32. */
  33. public MutableAttrListImpl()
  34. {
  35. super();
  36. }
  37. /**
  38. * Copy an existing Attributes object.
  39. *
  40. * <p>This constructor is especially useful inside a start
  41. * element event.</p>
  42. *
  43. * @param atts The existing Attributes object.
  44. */
  45. public MutableAttrListImpl(Attributes atts)
  46. {
  47. super(atts);
  48. }
  49. /**
  50. * Add an attribute to the end of the list.
  51. *
  52. * <p>For the sake of speed, this method does no checking
  53. * to see if the attribute is already in the list: that is
  54. * the responsibility of the application.</p>
  55. *
  56. * @param uri The Namespace URI, or the empty string if
  57. * none is available or Namespace processing is not
  58. * being performed.
  59. * @param localName The local name, or the empty string if
  60. * Namespace processing is not being performed.
  61. * @param qName The qualified (prefixed) name, or the empty string
  62. * if qualified names are not available.
  63. * @param type The attribute type as a string.
  64. * @param value The attribute value.
  65. */
  66. public void addAttribute(String uri, String localName, String qName,
  67. String type, String value)
  68. {
  69. if (null == uri)
  70. uri = "";
  71. // getIndex(qName) seems to be more reliable than getIndex(uri, localName),
  72. // in the case of the xmlns attribute anyway.
  73. int index = this.getIndex(qName);
  74. // int index = this.getIndex(uri, localName);
  75. // System.out.println("MutableAttrListImpl#addAttribute: "+uri+":"+localName+", "+index+", "+qName+", "+this);
  76. if (index >= 0)
  77. this.setAttribute(index, uri, localName, qName, type, value);
  78. else
  79. super.addAttribute(uri, localName, qName, type, value);
  80. }
  81. /**
  82. * Add the contents of the attribute list to this list.
  83. *
  84. * @param atts List of attributes to add to this list
  85. */
  86. public void addAttributes(Attributes atts)
  87. {
  88. int nAtts = atts.getLength();
  89. for (int i = 0; i < nAtts; i++)
  90. {
  91. String uri = atts.getURI(i);
  92. if (null == uri)
  93. uri = "";
  94. String localName = atts.getLocalName(i);
  95. String qname = atts.getQName(i);
  96. int index = this.getIndex(uri, localName);
  97. // System.out.println("MutableAttrListImpl#addAttributes: "+uri+":"+localName+", "+index+", "+atts.getQName(i)+", "+this);
  98. if (index >= 0)
  99. this.setAttribute(index, uri, localName, qname, atts.getType(i),
  100. atts.getValue(i));
  101. else
  102. addAttribute(uri, localName, qname, atts.getType(i),
  103. atts.getValue(i));
  104. }
  105. }
  106. /**
  107. * Return true if list contains the given (raw) attribute name.
  108. *
  109. * @param name Raw name of attribute to look for
  110. *
  111. * @return true if an attribute is found with this name
  112. */
  113. public boolean contains(String name)
  114. {
  115. return getValue(name) != null;
  116. }
  117. }
  118. // end of MutableAttrListImpl.java