1 25 26 package org.apache.jasper.xmlparser; 27 28 54 public class SymbolTable { 55 56 60 61 protected static final int TABLE_SIZE = 101; 62 63 67 68 protected Entry[] fBuckets = null; 69 70 protected int fTableSize; 72 73 77 78 public SymbolTable() { 79 this(TABLE_SIZE); 80 } 81 82 83 public SymbolTable(int tableSize) { 84 fTableSize = tableSize; 85 fBuckets = new Entry[fTableSize]; 86 } 87 88 92 100 public String addSymbol(String symbol) { 101 102 int bucket = hash(symbol) % fTableSize; 104 int length = symbol.length(); 105 OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) { 106 if (length == entry.characters.length) { 107 for (int i = 0; i < length; i++) { 108 if (symbol.charAt(i) != entry.characters[i]) { 109 continue OUTER; 110 } 111 } 112 return entry.symbol; 113 } 114 } 115 116 Entry entry = new Entry(symbol, fBuckets[bucket]); 118 fBuckets[bucket] = entry; 119 return entry.symbol; 120 121 } 123 133 public String addSymbol(char[] buffer, int offset, int length) { 134 135 int bucket = hash(buffer, offset, length) % fTableSize; 137 OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) { 138 if (length == entry.characters.length) { 139 for (int i = 0; i < length; i++) { 140 if (buffer[offset + i] != entry.characters[i]) { 141 continue OUTER; 142 } 143 } 144 return entry.symbol; 145 } 146 } 147 148 Entry entry = new Entry(buffer, offset, length, fBuckets[bucket]); 150 fBuckets[bucket] = entry; 151 return entry.symbol; 152 153 } 155 163 public int hash(String symbol) { 164 165 int code = 0; 166 int length = symbol.length(); 167 for (int i = 0; i < length; i++) { 168 code = code * 37 + symbol.charAt(i); 169 } 170 return code & 0x7FFFFFF; 171 172 } 174 185 public int hash(char[] buffer, int offset, int length) { 186 187 int code = 0; 188 for (int i = 0; i < length; i++) { 189 code = code * 37 + buffer[offset + i]; 190 } 191 return code & 0x7FFFFFF; 192 193 } 195 201 public boolean containsSymbol(String symbol) { 202 203 int bucket = hash(symbol) % fTableSize; 205 int length = symbol.length(); 206 OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) { 207 if (length == entry.characters.length) { 208 for (int i = 0; i < length; i++) { 209 if (symbol.charAt(i) != entry.characters[i]) { 210 continue OUTER; 211 } 212 } 213 return true; 214 } 215 } 216 217 return false; 218 219 } 221 229 public boolean containsSymbol(char[] buffer, int offset, int length) { 230 231 int bucket = hash(buffer, offset, length) % fTableSize; 233 OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) { 234 if (length == entry.characters.length) { 235 for (int i = 0; i < length; i++) { 236 if (buffer[offset + i] != entry.characters[i]) { 237 continue OUTER; 238 } 239 } 240 return true; 241 } 242 } 243 244 return false; 245 246 } 248 252 256 protected static final class Entry { 257 258 262 263 public String symbol; 264 265 269 public char[] characters; 270 271 272 public Entry next; 273 274 278 282 public Entry(String symbol, Entry next) { 283 this.symbol = symbol.intern(); 284 characters = new char[symbol.length()]; 285 symbol.getChars(0, characters.length, characters, 0); 286 this.next = next; 287 } 288 289 293 public Entry(char[] ch, int offset, int length, Entry next) { 294 characters = new char[length]; 295 System.arraycopy(ch, offset, characters, 0, length); 296 symbol = new String (characters).intern(); 297 this.next = next; 298 } 299 300 } 302 } | Popular Tags |