1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xml.utils;
  58. import org.apache.xml.utils.StringVector;
  59. import org.xml.sax.Attributes;
  60. import java.io.Serializable;
  61. import org.xml.sax.helpers.AttributesImpl;
  62. /**
  63. * <meta name="usage" content="advanced"/>
  64. * Mutable version of AttributesImpl.
  65. */
  66. public class MutableAttrListImpl extends AttributesImpl
  67. implements Serializable
  68. {
  69. /**
  70. * Construct a new, empty AttributesImpl object.
  71. */
  72. public MutableAttrListImpl()
  73. {
  74. super();
  75. }
  76. /**
  77. * Copy an existing Attributes object.
  78. *
  79. * <p>This constructor is especially useful inside a start
  80. * element event.</p>
  81. *
  82. * @param atts The existing Attributes object.
  83. */
  84. public MutableAttrListImpl(Attributes atts)
  85. {
  86. super(atts);
  87. }
  88. /**
  89. * Add an attribute to the end of the list.
  90. *
  91. * <p>For the sake of speed, this method does no checking
  92. * to see if the attribute is already in the list: that is
  93. * the responsibility of the application.</p>
  94. *
  95. * @param uri The Namespace URI, or the empty string if
  96. * none is available or Namespace processing is not
  97. * being performed.
  98. * @param localName The local name, or the empty string if
  99. * Namespace processing is not being performed.
  100. * @param qName The qualified (prefixed) name, or the empty string
  101. * if qualified names are not available.
  102. * @param type The attribute type as a string.
  103. * @param value The attribute value.
  104. */
  105. public void addAttribute(String uri, String localName, String qName,
  106. String type, String value)
  107. {
  108. if (null == uri)
  109. uri = "";
  110. // getIndex(qName) seems to be more reliable than getIndex(uri, localName),
  111. // in the case of the xmlns attribute anyway.
  112. int index = this.getIndex(qName);
  113. // int index = this.getIndex(uri, localName);
  114. // System.out.println("MutableAttrListImpl#addAttribute: "+uri+":"+localName+", "+index+", "+qName+", "+this);
  115. if (index >= 0)
  116. this.setAttribute(index, uri, localName, qName, type, value);
  117. else
  118. super.addAttribute(uri, localName, qName, type, value);
  119. }
  120. /**
  121. * Add the contents of the attribute list to this list.
  122. *
  123. * @param atts List of attributes to add to this list
  124. */
  125. public void addAttributes(Attributes atts)
  126. {
  127. int nAtts = atts.getLength();
  128. for (int i = 0; i < nAtts; i++)
  129. {
  130. String uri = atts.getURI(i);
  131. if (null == uri)
  132. uri = "";
  133. String localName = atts.getLocalName(i);
  134. String qname = atts.getQName(i);
  135. int index = this.getIndex(uri, localName);
  136. // System.out.println("MutableAttrListImpl#addAttributes: "+uri+":"+localName+", "+index+", "+atts.getQName(i)+", "+this);
  137. if (index >= 0)
  138. this.setAttribute(index, uri, localName, qname, atts.getType(i),
  139. atts.getValue(i));
  140. else
  141. addAttribute(uri, localName, qname, atts.getType(i),
  142. atts.getValue(i));
  143. }
  144. }
  145. /**
  146. * Return true if list contains the given (raw) attribute name.
  147. *
  148. * @param name Raw name of attribute to look for
  149. *
  150. * @return true if an attribute is found with this name
  151. */
  152. public boolean contains(String name)
  153. {
  154. return getValue(name) != null;
  155. }
  156. }
  157. // end of MutableAttrListImpl.java