/projects/tomcat-7.0.2/java/org/apache/jasper/xmlparser/SymbolTable.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 302 lines · 107 code · 44 blank · 151 comment · 30 complexity · 8c054f0becb759c0117d5145afb821aa MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. * ====================================================================
  17. *
  18. * This software consists of voluntary contributions made by many
  19. * individuals on behalf of the Apache Software Foundation and was
  20. * originally based on software copyright (c) 1999, International
  21. * Business Machines, Inc., http://www.apache.org. For more
  22. * information on the Apache Software Foundation, please see
  23. * <http://www.apache.org/>.
  24. */
  25. package org.apache.jasper.xmlparser;
  26. /**
  27. * This class is a symbol table implementation that guarantees that
  28. * strings used as identifiers are unique references. Multiple calls
  29. * to <code>addSymbol</code> will always return the same string
  30. * reference.
  31. * <p>
  32. * The symbol table performs the same task as <code>String.intern()</code>
  33. * with the following differences:
  34. * <ul>
  35. * <li>
  36. * A new string object does not need to be created in order to
  37. * retrieve a unique reference. Symbols can be added by using
  38. * a series of characters in a character array.
  39. * </li>
  40. * <li>
  41. * Users of the symbol table can provide their own symbol hashing
  42. * implementation. For example, a simple string hashing algorithm
  43. * may fail to produce a balanced set of hashcodes for symbols
  44. * that are <em>mostly</em> unique. Strings with similar leading
  45. * characters are especially prone to this poor hashing behavior.
  46. * </li>
  47. * </ul>
  48. *
  49. * @author Andy Clark
  50. * @version $Id: SymbolTable.java 467222 2006-10-24 03:17:11Z markt $
  51. */
  52. public class SymbolTable {
  53. //
  54. // Constants
  55. //
  56. /** Default table size. */
  57. protected static final int TABLE_SIZE = 101;
  58. //
  59. // Data
  60. //
  61. /** Buckets. */
  62. protected Entry[] fBuckets = null;
  63. // actual table size
  64. protected int fTableSize;
  65. //
  66. // Constructors
  67. //
  68. /** Constructs a symbol table with a default number of buckets. */
  69. public SymbolTable() {
  70. this(TABLE_SIZE);
  71. }
  72. /** Constructs a symbol table with a specified number of buckets. */
  73. public SymbolTable(int tableSize) {
  74. fTableSize = tableSize;
  75. fBuckets = new Entry[fTableSize];
  76. }
  77. //
  78. // Public methods
  79. //
  80. /**
  81. * Adds the specified symbol to the symbol table and returns a
  82. * reference to the unique symbol. If the symbol already exists,
  83. * the previous symbol reference is returned instead, in order
  84. * guarantee that symbol references remain unique.
  85. *
  86. * @param symbol The new symbol.
  87. */
  88. public String addSymbol(String symbol) {
  89. // search for identical symbol
  90. int bucket = hash(symbol) % fTableSize;
  91. int length = symbol.length();
  92. OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
  93. if (length == entry.characters.length) {
  94. for (int i = 0; i < length; i++) {
  95. if (symbol.charAt(i) != entry.characters[i]) {
  96. continue OUTER;
  97. }
  98. }
  99. return entry.symbol;
  100. }
  101. }
  102. // create new entry
  103. Entry entry = new Entry(symbol, fBuckets[bucket]);
  104. fBuckets[bucket] = entry;
  105. return entry.symbol;
  106. } // addSymbol(String):String
  107. /**
  108. * Adds the specified symbol to the symbol table and returns a
  109. * reference to the unique symbol. If the symbol already exists,
  110. * the previous symbol reference is returned instead, in order
  111. * guarantee that symbol references remain unique.
  112. *
  113. * @param buffer The buffer containing the new symbol.
  114. * @param offset The offset into the buffer of the new symbol.
  115. * @param length The length of the new symbol in the buffer.
  116. */
  117. public String addSymbol(char[] buffer, int offset, int length) {
  118. // search for identical symbol
  119. int bucket = hash(buffer, offset, length) % fTableSize;
  120. OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
  121. if (length == entry.characters.length) {
  122. for (int i = 0; i < length; i++) {
  123. if (buffer[offset + i] != entry.characters[i]) {
  124. continue OUTER;
  125. }
  126. }
  127. return entry.symbol;
  128. }
  129. }
  130. // add new entry
  131. Entry entry = new Entry(buffer, offset, length, fBuckets[bucket]);
  132. fBuckets[bucket] = entry;
  133. return entry.symbol;
  134. } // addSymbol(char[],int,int):String
  135. /**
  136. * Returns a hashcode value for the specified symbol. The value
  137. * returned by this method must be identical to the value returned
  138. * by the <code>hash(char[],int,int)</code> method when called
  139. * with the character array that comprises the symbol string.
  140. *
  141. * @param symbol The symbol to hash.
  142. */
  143. public int hash(String symbol) {
  144. int code = 0;
  145. int length = symbol.length();
  146. for (int i = 0; i < length; i++) {
  147. code = code * 37 + symbol.charAt(i);
  148. }
  149. return code & 0x7FFFFFF;
  150. } // hash(String):int
  151. /**
  152. * Returns a hashcode value for the specified symbol information.
  153. * The value returned by this method must be identical to the value
  154. * returned by the <code>hash(String)</code> method when called
  155. * with the string object created from the symbol information.
  156. *
  157. * @param buffer The character buffer containing the symbol.
  158. * @param offset The offset into the character buffer of the start
  159. * of the symbol.
  160. * @param length The length of the symbol.
  161. */
  162. public int hash(char[] buffer, int offset, int length) {
  163. int code = 0;
  164. for (int i = 0; i < length; i++) {
  165. code = code * 37 + buffer[offset + i];
  166. }
  167. return code & 0x7FFFFFF;
  168. } // hash(char[],int,int):int
  169. /**
  170. * Returns true if the symbol table already contains the specified
  171. * symbol.
  172. *
  173. * @param symbol The symbol to look for.
  174. */
  175. public boolean containsSymbol(String symbol) {
  176. // search for identical symbol
  177. int bucket = hash(symbol) % fTableSize;
  178. int length = symbol.length();
  179. OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
  180. if (length == entry.characters.length) {
  181. for (int i = 0; i < length; i++) {
  182. if (symbol.charAt(i) != entry.characters[i]) {
  183. continue OUTER;
  184. }
  185. }
  186. return true;
  187. }
  188. }
  189. return false;
  190. } // containsSymbol(String):boolean
  191. /**
  192. * Returns true if the symbol table already contains the specified
  193. * symbol.
  194. *
  195. * @param buffer The buffer containing the symbol to look for.
  196. * @param offset The offset into the buffer.
  197. * @param length The length of the symbol in the buffer.
  198. */
  199. public boolean containsSymbol(char[] buffer, int offset, int length) {
  200. // search for identical symbol
  201. int bucket = hash(buffer, offset, length) % fTableSize;
  202. OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
  203. if (length == entry.characters.length) {
  204. for (int i = 0; i < length; i++) {
  205. if (buffer[offset + i] != entry.characters[i]) {
  206. continue OUTER;
  207. }
  208. }
  209. return true;
  210. }
  211. }
  212. return false;
  213. } // containsSymbol(char[],int,int):boolean
  214. //
  215. // Classes
  216. //
  217. /**
  218. * This class is a symbol table entry. Each entry acts as a node
  219. * in a linked list.
  220. */
  221. protected static final class Entry {
  222. //
  223. // Data
  224. //
  225. /** Symbol. */
  226. public String symbol;
  227. /**
  228. * Symbol characters. This information is duplicated here for
  229. * comparison performance.
  230. */
  231. public char[] characters;
  232. /** The next entry. */
  233. public Entry next;
  234. //
  235. // Constructors
  236. //
  237. /**
  238. * Constructs a new entry from the specified symbol and next entry
  239. * reference.
  240. */
  241. public Entry(String symbol, Entry next) {
  242. this.symbol = symbol.intern();
  243. characters = new char[symbol.length()];
  244. symbol.getChars(0, characters.length, characters, 0);
  245. this.next = next;
  246. }
  247. /**
  248. * Constructs a new entry from the specified symbol information and
  249. * next entry reference.
  250. */
  251. public Entry(char[] ch, int offset, int length, Entry next) {
  252. characters = new char[length];
  253. System.arraycopy(ch, offset, characters, 0, length);
  254. symbol = new String(characters).intern();
  255. this.next = next;
  256. }
  257. } // class Entry
  258. } // class SymbolTable