1. /*
  2. * Copyright 1999-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: StringToStringTable.java,v 1.5 2004/02/17 04:21:14 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. /**
  21. * A very simple lookup table that stores a list of strings, the even
  22. * number strings being keys, and the odd number strings being values.
  23. * @xsl.usage internal
  24. */
  25. public class StringToStringTable
  26. {
  27. /** Size of blocks to allocate */
  28. private int m_blocksize;
  29. /** Array of strings this contains */
  30. private String m_map[];
  31. /** Number of strings this contains */
  32. private int m_firstFree = 0;
  33. /** Size of this table */
  34. private int m_mapSize;
  35. /**
  36. * Default constructor. Note that the default
  37. * block size is very small, for small lists.
  38. */
  39. public StringToStringTable()
  40. {
  41. m_blocksize = 16;
  42. m_mapSize = m_blocksize;
  43. m_map = new String[m_blocksize];
  44. }
  45. /**
  46. * Construct a StringToStringTable, using the given block size.
  47. *
  48. * @param blocksize Size of blocks to allocate
  49. */
  50. public StringToStringTable(int blocksize)
  51. {
  52. m_blocksize = blocksize;
  53. m_mapSize = blocksize;
  54. m_map = new String[blocksize];
  55. }
  56. /**
  57. * Get the length of the list.
  58. *
  59. * @return Number of strings in the list
  60. */
  61. public final int getLength()
  62. {
  63. return m_firstFree;
  64. }
  65. /**
  66. * Append a string onto the vector.
  67. * The strings go to the even locations in the array
  68. * and the values in the odd.
  69. *
  70. * @param key String to add to the list
  71. * @param value Value of the string
  72. */
  73. public final void put(String key, String value)
  74. {
  75. if ((m_firstFree + 2) >= m_mapSize)
  76. {
  77. m_mapSize += m_blocksize;
  78. String newMap[] = new String[m_mapSize];
  79. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  80. m_map = newMap;
  81. }
  82. m_map[m_firstFree] = key;
  83. m_firstFree++;
  84. m_map[m_firstFree] = value;
  85. m_firstFree++;
  86. }
  87. /**
  88. * Tell if the table contains the given string.
  89. *
  90. * @param key String to look up
  91. *
  92. * @return return the value of the string or null if not found.
  93. */
  94. public final String get(String key)
  95. {
  96. for (int i = 0; i < m_firstFree; i += 2)
  97. {
  98. if (m_map[i].equals(key))
  99. return m_map[i + 1];
  100. }
  101. return null;
  102. }
  103. /**
  104. * Remove the given string and its value from this table.
  105. *
  106. * @param key String to remove from the table
  107. */
  108. public final void remove(String key)
  109. {
  110. for (int i = 0; i < m_firstFree; i += 2)
  111. {
  112. if (m_map[i].equals(key))
  113. {
  114. if ((i + 2) < m_firstFree)
  115. System.arraycopy(m_map, i + 2, m_map, i, m_firstFree - (i + 2));
  116. m_firstFree -= 2;
  117. m_map[m_firstFree] = null;
  118. m_map[m_firstFree + 1] = null;
  119. break;
  120. }
  121. }
  122. }
  123. /**
  124. * Tell if the table contains the given string. Ignore case
  125. *
  126. * @param key String to look up
  127. *
  128. * @return The value of the string or null if not found
  129. */
  130. public final String getIgnoreCase(String key)
  131. {
  132. if (null == key)
  133. return null;
  134. for (int i = 0; i < m_firstFree; i += 2)
  135. {
  136. if (m_map[i].equalsIgnoreCase(key))
  137. return m_map[i + 1];
  138. }
  139. return null;
  140. }
  141. /**
  142. * Tell if the table contains the given string in the value.
  143. *
  144. * @param val Value of the string to look up
  145. *
  146. * @return the string associated with the given value or null if not found
  147. */
  148. public final String getByValue(String val)
  149. {
  150. for (int i = 1; i < m_firstFree; i += 2)
  151. {
  152. if (m_map[i].equals(val))
  153. return m_map[i - 1];
  154. }
  155. return null;
  156. }
  157. /**
  158. * Get the nth element.
  159. *
  160. * @param i index of the string to look up.
  161. *
  162. * @return The string at the given index.
  163. */
  164. public final String elementAt(int i)
  165. {
  166. return m_map[i];
  167. }
  168. /**
  169. * Tell if the table contains the given string.
  170. *
  171. * @param key String to look up
  172. *
  173. * @return True if the given string is in this table
  174. */
  175. public final boolean contains(String key)
  176. {
  177. for (int i = 0; i < m_firstFree; i += 2)
  178. {
  179. if (m_map[i].equals(key))
  180. return true;
  181. }
  182. return false;
  183. }
  184. /**
  185. * Tell if the table contains the given string.
  186. *
  187. * @param val value to look up
  188. *
  189. * @return True if the given value is in the table.
  190. */
  191. public final boolean containsValue(String val)
  192. {
  193. for (int i = 1; i < m_firstFree; i += 2)
  194. {
  195. if (m_map[i].equals(val))
  196. return true;
  197. }
  198. return false;
  199. }
  200. }