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 strings, optimized
  61. * for small lists.
  62. */
  63. public class StringVector implements java.io.Serializable
  64. {
  65. /** @serial Size of blocks to allocate */
  66. protected int m_blocksize;
  67. /** @serial Array of strings this contains */
  68. protected String m_map[];
  69. /** @serial Number of strings this contains */
  70. protected int m_firstFree = 0;
  71. /** @serial Size of the array */
  72. protected int m_mapSize;
  73. /**
  74. * Default constructor. Note that the default
  75. * block size is very small, for small lists.
  76. */
  77. public StringVector()
  78. {
  79. m_blocksize = 8;
  80. m_mapSize = m_blocksize;
  81. m_map = new String[m_blocksize];
  82. }
  83. /**
  84. * Construct a StringVector, using the given block size.
  85. *
  86. * @param blocksize Size of the blocks to allocate
  87. */
  88. public StringVector(int blocksize)
  89. {
  90. m_blocksize = blocksize;
  91. m_mapSize = blocksize;
  92. m_map = new String[blocksize];
  93. }
  94. /**
  95. * Get the length of the list.
  96. *
  97. * @return Number of strings in the list
  98. */
  99. public int getLength()
  100. {
  101. return m_firstFree;
  102. }
  103. /**
  104. * Get the length of the list.
  105. *
  106. * @return Number of strings in the list
  107. */
  108. public final int size()
  109. {
  110. return m_firstFree;
  111. }
  112. /**
  113. * Append a string onto the vector.
  114. *
  115. * @param value Sting to add to the vector
  116. */
  117. public final void addElement(String value)
  118. {
  119. if ((m_firstFree + 1) >= m_mapSize)
  120. {
  121. m_mapSize += m_blocksize;
  122. String newMap[] = new String[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. * Get the nth element.
  131. *
  132. * @param i Index of string to find
  133. *
  134. * @return String at given index
  135. */
  136. public final String elementAt(int i)
  137. {
  138. return m_map[i];
  139. }
  140. /**
  141. * Tell if the table contains the given string.
  142. *
  143. * @param s String to look for
  144. *
  145. * @return True if the string is in this table
  146. */
  147. public final boolean contains(String s)
  148. {
  149. if (null == s)
  150. return false;
  151. for (int i = 0; i < m_firstFree; i++)
  152. {
  153. if (m_map[i].equals(s))
  154. return true;
  155. }
  156. return false;
  157. }
  158. /**
  159. * Tell if the table contains the given string. Ignore case.
  160. *
  161. * @param s String to find
  162. *
  163. * @return True if the String is in this vector
  164. */
  165. public final boolean containsIgnoreCase(String s)
  166. {
  167. if (null == s)
  168. return false;
  169. for (int i = 0; i < m_firstFree; i++)
  170. {
  171. if (m_map[i].equalsIgnoreCase(s))
  172. return true;
  173. }
  174. return false;
  175. }
  176. /**
  177. * Tell if the table contains the given string.
  178. *
  179. * @param s String to push into the vector
  180. */
  181. public final void push(String s)
  182. {
  183. if ((m_firstFree + 1) >= m_mapSize)
  184. {
  185. m_mapSize += m_blocksize;
  186. String newMap[] = new String[m_mapSize];
  187. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  188. m_map = newMap;
  189. }
  190. m_map[m_firstFree] = s;
  191. m_firstFree++;
  192. }
  193. /**
  194. * Pop the tail of this vector.
  195. *
  196. * @return The String last added to this vector or null not found.
  197. * The string is removed from the vector.
  198. */
  199. public final String pop()
  200. {
  201. if (m_firstFree <= 0)
  202. return null;
  203. m_firstFree--;
  204. String s = m_map[m_firstFree];
  205. m_map[m_firstFree] = null;
  206. return s;
  207. }
  208. /**
  209. * Get the string at the tail of this vector without popping.
  210. *
  211. * @return The string at the tail of this vector.
  212. */
  213. public final String peek()
  214. {
  215. return (m_firstFree <= 0) ? null : m_map[m_firstFree - 1];
  216. }
  217. }