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. /**
  59. * <meta name="usage" content="internal"/>
  60. * A very simple table that stores a list of StringToStringTables, optimized
  61. * for small lists.
  62. */
  63. public class StringToStringTableVector
  64. {
  65. /** Size of blocks to allocate */
  66. private int m_blocksize;
  67. /** Array of StringToStringTable objects */
  68. private StringToStringTable m_map[];
  69. /** Number of StringToStringTable objects in this array */
  70. private int m_firstFree = 0;
  71. /** Size of this array */
  72. private int m_mapSize;
  73. /**
  74. * Default constructor. Note that the default
  75. * block size is very small, for small lists.
  76. */
  77. public StringToStringTableVector()
  78. {
  79. m_blocksize = 8;
  80. m_mapSize = m_blocksize;
  81. m_map = new StringToStringTable[m_blocksize];
  82. }
  83. /**
  84. * Construct a StringToStringTableVector, using the given block size.
  85. *
  86. * @param blocksize Size of blocks to allocate
  87. */
  88. public StringToStringTableVector(int blocksize)
  89. {
  90. m_blocksize = blocksize;
  91. m_mapSize = blocksize;
  92. m_map = new StringToStringTable[blocksize];
  93. }
  94. /**
  95. * Get the length of the list.
  96. *
  97. * @return Number of StringToStringTable objects in the list
  98. */
  99. public final int getLength()
  100. {
  101. return m_firstFree;
  102. }
  103. /**
  104. * Get the length of the list.
  105. *
  106. * @return Number of StringToStringTable objects in the list
  107. */
  108. public final int size()
  109. {
  110. return m_firstFree;
  111. }
  112. /**
  113. * Append a StringToStringTable object onto the vector.
  114. *
  115. * @param value StringToStringTable object to add
  116. */
  117. public final void addElement(StringToStringTable value)
  118. {
  119. if ((m_firstFree + 1) >= m_mapSize)
  120. {
  121. m_mapSize += m_blocksize;
  122. StringToStringTable newMap[] = new StringToStringTable[m_mapSize];
  123. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  124. m_map = newMap;
  125. }
  126. m_map[m_firstFree] = value;
  127. m_firstFree++;
  128. }
  129. /**
  130. * Given a string, find the last added occurance value
  131. * that matches the key.
  132. *
  133. * @param key String to look up
  134. *
  135. * @return the last added occurance value that matches the key
  136. * or null if not found.
  137. */
  138. public final String get(String key)
  139. {
  140. for (int i = m_firstFree - 1; i >= 0; --i)
  141. {
  142. String nsuri = m_map[i].get(key);
  143. if (nsuri != null)
  144. return nsuri;
  145. }
  146. return null;
  147. }
  148. /**
  149. * Given a string, find out if there is a value in this table
  150. * that matches the key.
  151. *
  152. * @param key String to look for
  153. *
  154. * @return True if the string was found in table, null if not
  155. */
  156. public final boolean containsKey(String key)
  157. {
  158. for (int i = m_firstFree - 1; i >= 0; --i)
  159. {
  160. if (m_map[i].get(key) != null)
  161. return true;
  162. }
  163. return false;
  164. }
  165. /**
  166. * Remove the last element.
  167. */
  168. public final void removeLastElem()
  169. {
  170. if (m_firstFree > 0)
  171. {
  172. m_map[m_firstFree] = null;
  173. m_firstFree--;
  174. }
  175. }
  176. /**
  177. * Get the nth element.
  178. *
  179. * @param i Index of element to find
  180. *
  181. * @return The StringToStringTable object at the given index
  182. */
  183. public final StringToStringTable elementAt(int i)
  184. {
  185. return m_map[i];
  186. }
  187. /**
  188. * Tell if the table contains the given StringToStringTable.
  189. *
  190. * @param s The StringToStringTable to find
  191. *
  192. * @return True if the StringToStringTable is found
  193. */
  194. public final boolean contains(StringToStringTable s)
  195. {
  196. for (int i = 0; i < m_firstFree; i++)
  197. {
  198. if (m_map[i].equals(s))
  199. return true;
  200. }
  201. return false;
  202. }
  203. }