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 StringToIntTable
  64. {
  65. public static final int INVALID_KEY = -10000;
  66. /** Block size to allocate */
  67. private int m_blocksize;
  68. /** Array of strings this table points to. Associated with ints
  69. * in m_values */
  70. private String m_map[];
  71. /** Array of ints this table points. Associated with strings from
  72. * m_map. */
  73. private int m_values[];
  74. /** Number of ints in the table */
  75. private int m_firstFree = 0;
  76. /** Size of this table */
  77. private int m_mapSize;
  78. /**
  79. * Default constructor. Note that the default
  80. * block size is very small, for small lists.
  81. */
  82. public StringToIntTable()
  83. {
  84. m_blocksize = 8;
  85. m_mapSize = m_blocksize;
  86. m_map = new String[m_blocksize];
  87. m_values = new int[m_blocksize];
  88. }
  89. /**
  90. * Construct a StringToIntTable, using the given block size.
  91. *
  92. * @param blocksize Size of block to allocate
  93. */
  94. public StringToIntTable(int blocksize)
  95. {
  96. m_blocksize = blocksize;
  97. m_mapSize = blocksize;
  98. m_map = new String[blocksize];
  99. m_values = new int[m_blocksize];
  100. }
  101. /**
  102. * Get the length of the list.
  103. *
  104. * @return the length of the list
  105. */
  106. public final int getLength()
  107. {
  108. return m_firstFree;
  109. }
  110. /**
  111. * Append a string onto the vector.
  112. *
  113. * @param key String to append
  114. * @param value The int value of the string
  115. */
  116. public final void put(String key, int value)
  117. {
  118. if ((m_firstFree + 1) >= m_mapSize)
  119. {
  120. m_mapSize += m_blocksize;
  121. String newMap[] = new String[m_mapSize];
  122. System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
  123. m_map = newMap;
  124. int newValues[] = new int[m_mapSize];
  125. System.arraycopy(m_values, 0, newValues, 0, m_firstFree + 1);
  126. m_values = newValues;
  127. }
  128. m_map[m_firstFree] = key;
  129. m_values[m_firstFree] = value;
  130. m_firstFree++;
  131. }
  132. /**
  133. * Tell if the table contains the given string.
  134. *
  135. * @param key String to look for
  136. *
  137. * @return The String's int value
  138. *
  139. */
  140. public final int get(String key)
  141. {
  142. for (int i = 0; i < m_firstFree; i++)
  143. {
  144. if (m_map[i].equals(key))
  145. return m_values[i];
  146. }
  147. return INVALID_KEY;
  148. }
  149. /**
  150. * Tell if the table contains the given string. Ignore case.
  151. *
  152. * @param key String to look for
  153. *
  154. * @return The string's int value
  155. */
  156. public final int getIgnoreCase(String key)
  157. {
  158. if (null == key)
  159. return INVALID_KEY;
  160. for (int i = 0; i < m_firstFree; i++)
  161. {
  162. if (m_map[i].equalsIgnoreCase(key))
  163. return m_values[i];
  164. }
  165. return INVALID_KEY;
  166. }
  167. /**
  168. * Tell if the table contains the given string.
  169. *
  170. * @param key String to look for
  171. *
  172. * @return True if the string is in the table
  173. */
  174. public final boolean contains(String key)
  175. {
  176. for (int i = 0; i < m_firstFree; i++)
  177. {
  178. if (m_map[i].equals(key))
  179. return true;
  180. }
  181. return false;
  182. }
  183. /**
  184. * Return array of keys in the table.
  185. *
  186. * @return Array of strings
  187. */
  188. public final String[] keys()
  189. {
  190. String [] keysArr = new String[m_firstFree];
  191. for (int i = 0; i < m_firstFree; i++)
  192. {
  193. keysArr[i] = m_map[i];
  194. }
  195. return keysArr;
  196. }
  197. }