1. /*
  2. * Copyright 2003-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: AttributesImplSerializer.java,v 1.4 2004/02/17 04:18:19 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.serializer;
  20. import java.util.Hashtable;
  21. import org.xml.sax.Attributes;
  22. import org.xml.sax.helpers.AttributesImpl;
  23. /**
  24. * This class extends org.xml.sax.helpers.AttributesImpl which implements org.
  25. * xml.sax.Attributes. But for optimization this class adds a Hashtable for
  26. * faster lookup of an index by qName, which is commonly done in the stream
  27. * serializer.
  28. *
  29. * @see org.xml.sax.Attributes
  30. */
  31. public class AttributesImplSerializer extends AttributesImpl
  32. {
  33. /**
  34. * Hash table of qName/index values to quickly lookup the index
  35. * of an attributes qName. qNames are in uppercase in the hash table
  36. * to make the search case insensitive.
  37. */
  38. private Hashtable m_indexFromQName = new Hashtable();
  39. /**
  40. * This is the number of attributes before switching to the hash table,
  41. * and can be tuned, but 12 seems good for now - bjm
  42. */
  43. public static final int MAX = 12;
  44. /**
  45. * One less than the number of attributes before switching to
  46. * the Hashtable.
  47. */
  48. private static final int MAXMinus1 = MAX - 1;
  49. /**
  50. * This method gets the index of an attribute given its qName.
  51. * @param qname the qualified name of the attribute, e.g. "prefix1:locName1"
  52. * @return the integer index of the attribute.
  53. * @see org.xml.sax.Attributes#getIndex(String)
  54. */
  55. public int getIndex(String qname)
  56. {
  57. int index;
  58. if (super.getLength() < MAX)
  59. {
  60. // if we haven't got too many attributes let the
  61. // super class look it up
  62. index = super.getIndex(qname);
  63. return index;
  64. }
  65. // we have too many attributes and the super class is slow
  66. // so find it quickly using our Hashtable.
  67. Integer i = (Integer)m_indexFromQName.get(qname);
  68. if (i == null)
  69. index = -1;
  70. else
  71. index = i.intValue();
  72. return index;
  73. }
  74. /**
  75. * This method adds the attribute, but also records its qName/index pair in
  76. * the hashtable for fast lookup by getIndex(qName).
  77. * @param uri the URI of the attribute
  78. * @param local the local name of the attribute
  79. * @param qname the qualified name of the attribute
  80. * @param type the type of the attribute
  81. * @param val the value of the attribute
  82. *
  83. * @see org.xml.sax.helpers.AttributesImpl#addAttribute(String, String, String, String, String)
  84. * @see #getIndex(String)
  85. */
  86. public void addAttribute(
  87. String uri,
  88. String local,
  89. String qname,
  90. String type,
  91. String val)
  92. {
  93. int index = super.getLength();
  94. super.addAttribute(uri, local, qname, type, val);
  95. // (index + 1) is now the number of attributes
  96. // so either compare (index+1) to MAX, or compare index to (MAX-1)
  97. if (index < MAXMinus1)
  98. {
  99. return;
  100. }
  101. else if (index == MAXMinus1)
  102. {
  103. switchOverToHash(MAX);
  104. }
  105. else
  106. {
  107. /* we have just added the attibute, its index is the old length */
  108. Integer i = new Integer(index);
  109. m_indexFromQName.put(qname, i);
  110. }
  111. return;
  112. }
  113. /**
  114. * We are switching over to having a hash table for quick look
  115. * up of attributes, but up until now we haven't kept any
  116. * information in the Hashtable, so we now update the Hashtable.
  117. * Future additional attributes will update the Hashtable as
  118. * they are added.
  119. * @param numAtts
  120. */
  121. private void switchOverToHash(int numAtts)
  122. {
  123. for (int index = 0; index < numAtts; index++)
  124. {
  125. String qName = super.getQName(index);
  126. Integer i = new Integer(index);
  127. m_indexFromQName.put(qName, i);
  128. }
  129. }
  130. /**
  131. * This method clears the accumulated attributes.
  132. *
  133. * @see org.xml.sax.helpers.AttributesImpl#clear()
  134. */
  135. public void clear()
  136. {
  137. int len = super.getLength();
  138. super.clear();
  139. if (MAX <= len)
  140. {
  141. // if we have had enough attributes and are
  142. // using the Hashtable, then clear the Hashtable too.
  143. m_indexFromQName.clear();
  144. }
  145. }
  146. /**
  147. * This method sets the attributes, previous attributes are cleared,
  148. * it also keeps the hashtable up to date for quick lookup via
  149. * getIndex(qName).
  150. * @param atts the attributes to copy into these attributes.
  151. * @see org.xml.sax.helpers.AttributesImpl#setAttributes(Attributes)
  152. * @see #getIndex(String)
  153. */
  154. public void setAttributes(Attributes atts)
  155. {
  156. super.setAttributes(atts);
  157. // we've let the super class add the attributes, but
  158. // we need to keep the hash table up to date ourselves for the
  159. // potentially new qName/index pairs for quick lookup.
  160. int numAtts = atts.getLength();
  161. if (MAX <= numAtts)
  162. switchOverToHash(numAtts);
  163. }
  164. }