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 lookup table that stores a list of strings, the even
  61. * number strings being keys, and the odd number strings being values.
  62. */
  63. public class StringToStringTable
  64. {
  65. /** Size of blocks to allocate */
  66. private int m_blocksize;
  67. /** Array of strings this contains */
  68. private String m_map[];
  69. /** Number of strings this contains */
  70. private int m_firstFree = 0;
  71. /** Size of this table */
  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 StringToStringTable()
  78. {
  79. m_blocksize = 16;
  80. m_mapSize = m_blocksize;
  81. m_map = new String[m_blocksize];
  82. }
  83. /**
  84. * Construct a StringToStringTable, using the given block size.
  85. *
  86. * @param blocksize Size of blocks to allocate
  87. */
  88. public StringToStringTable(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 final int getLength()
  100. {
  101. return m_firstFree;
  102. }
  103. /**
  104. * Append a string onto the vector.
  105. * The strings go to the even locations in the array
  106. * and the values in the odd.
  107. *
  108. * @param key String to add to the list
  109. * @param value Value of the string
  110. */
  111. public final void put(String key, String value)
  112. {
  113. if ((m_firstFree + 2) >= m_mapSize)
  114. {
  115. m_mapSize += m_blocksize;
  116. String newMap[] = new String[m_mapSize];
  117. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  118. m_map = newMap;
  119. }
  120. m_map[m_firstFree] = key;
  121. m_firstFree++;
  122. m_map[m_firstFree] = value;
  123. m_firstFree++;
  124. }
  125. /**
  126. * Tell if the table contains the given string.
  127. *
  128. * @param key String to look up
  129. *
  130. * @return return the value of the string or null if not found.
  131. */
  132. public final String get(String key)
  133. {
  134. for (int i = 0; i < m_firstFree; i += 2)
  135. {
  136. if (m_map[i].equals(key))
  137. return m_map[i + 1];
  138. }
  139. return null;
  140. }
  141. /**
  142. * Remove the given string and its value from this table.
  143. *
  144. * @param key String to remove from the table
  145. */
  146. public final void remove(String key)
  147. {
  148. for (int i = 0; i < m_firstFree; i += 2)
  149. {
  150. if (m_map[i].equals(key))
  151. {
  152. if ((i + 2) < m_firstFree)
  153. System.arraycopy(m_map, i + 2, m_map, i, m_firstFree - (i + 2));
  154. m_firstFree -= 2;
  155. m_map[m_firstFree] = null;
  156. m_map[m_firstFree + 1] = null;
  157. break;
  158. }
  159. }
  160. }
  161. /**
  162. * Tell if the table contains the given string. Ignore case
  163. *
  164. * @param key String to look up
  165. *
  166. * @return The value of the string or null if not found
  167. */
  168. public final String getIgnoreCase(String key)
  169. {
  170. if (null == key)
  171. return null;
  172. for (int i = 0; i < m_firstFree; i += 2)
  173. {
  174. if (m_map[i].equalsIgnoreCase(key))
  175. return m_map[i + 1];
  176. }
  177. return null;
  178. }
  179. /**
  180. * Tell if the table contains the given string in the value.
  181. *
  182. * @param val Value of the string to look up
  183. *
  184. * @return the string associated with the given value or null if not found
  185. */
  186. public final String getByValue(String val)
  187. {
  188. for (int i = 1; i < m_firstFree; i += 2)
  189. {
  190. if (m_map[i].equals(val))
  191. return m_map[i - 1];
  192. }
  193. return null;
  194. }
  195. /**
  196. * Get the nth element.
  197. *
  198. * @param i index of the string to look up.
  199. *
  200. * @return The string at the given index.
  201. */
  202. public final String elementAt(int i)
  203. {
  204. return m_map[i];
  205. }
  206. /**
  207. * Tell if the table contains the given string.
  208. *
  209. * @param key String to look up
  210. *
  211. * @return True if the given string is in this table
  212. */
  213. public final boolean contains(String key)
  214. {
  215. for (int i = 0; i < m_firstFree; i += 2)
  216. {
  217. if (m_map[i].equals(key))
  218. return true;
  219. }
  220. return false;
  221. }
  222. /**
  223. * Tell if the table contains the given string.
  224. *
  225. * @param val value to look up
  226. *
  227. * @return True if the given value is in the table.
  228. */
  229. public final boolean containsValue(String val)
  230. {
  231. for (int i = 1; i < m_firstFree; i += 2)
  232. {
  233. if (m_map[i].equals(val))
  234. return true;
  235. }
  236. return false;
  237. }
  238. }