1 57 58 package com.sun.org.apache.xerces.internal.util; 59 60 import com.sun.org.apache.xerces.internal.xni.grammars.Grammar; 61 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription; 62 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool; 63 64 80 public class XMLGrammarPoolImpl implements XMLGrammarPool { 81 82 86 87 protected static final int TABLE_SIZE = 11; 88 89 93 94 protected Entry[] fGrammars = null; 95 96 protected boolean fPoolIsLocked; 98 99 protected int fGrammarCount = 0; 101 102 private static final boolean DEBUG = false ; 103 104 108 109 public XMLGrammarPoolImpl() { 110 fGrammars = new Entry[TABLE_SIZE]; 111 fPoolIsLocked = false; 112 } 114 115 public XMLGrammarPoolImpl(int initialCapacity) { 116 fGrammars = new Entry[initialCapacity]; 117 fPoolIsLocked = false; 118 } 119 120 124 134 public Grammar [] retrieveInitialGrammarSet (String grammarType) { 135 synchronized (fGrammars) { 136 int grammarSize = fGrammars.length ; 137 Grammar [] tempGrammars = new Grammar[fGrammarCount]; 138 int pos = 0; 139 for (int i = 0; i < grammarSize; i++) { 140 for (Entry e = fGrammars[i]; e != null; e = e.next) { 141 if (e.desc.getGrammarType().equals(grammarType)) { 142 tempGrammars[pos++] = e.grammar; 143 } 144 } 145 } 146 Grammar[] toReturn = new Grammar[pos]; 147 System.arraycopy(tempGrammars, 0, toReturn, 0, pos); 148 return toReturn; 149 } 150 } 152 163 public void cacheGrammars(String grammarType, Grammar[] grammars) { 164 if(!fPoolIsLocked) { 165 for (int i = 0; i < grammars.length; i++) { 166 if(DEBUG) { 167 System.out.println("CACHED GRAMMAR " + (i+1) ) ; 168 Grammar temp = grammars[i] ; 169 } 171 putGrammar(grammars[i]); 172 } 173 } 174 } 176 190 public Grammar retrieveGrammar(XMLGrammarDescription desc) { 191 if(DEBUG){ 192 System.out.println("RETRIEVING GRAMMAR FROM THE APPLICATION WITH FOLLOWING DESCRIPTION :"); 193 } 195 return getGrammar(desc); 196 } 198 202 208 public void putGrammar(Grammar grammar) { 209 if(!fPoolIsLocked) { 210 synchronized (fGrammars) { 211 XMLGrammarDescription desc = grammar.getGrammarDescription(); 212 int hash = hashCode(desc); 213 int index = (hash & 0x7FFFFFFF) % fGrammars.length; 214 for (Entry entry = fGrammars[index]; entry != null; entry = entry.next) { 215 if (entry.hash == hash && equals(entry.desc, desc)) { 216 entry.grammar = grammar; 217 return; 218 } 219 } 220 Entry entry = new Entry(hash, desc, grammar, fGrammars[index]); 222 fGrammars[index] = entry; 223 fGrammarCount++; 224 } 225 } 226 } 228 235 public Grammar getGrammar(XMLGrammarDescription desc) { 236 synchronized (fGrammars) { 237 int hash = hashCode(desc); 238 int index = (hash & 0x7FFFFFFF) % fGrammars.length; 239 for (Entry entry = fGrammars[index] ; entry != null ; entry = entry.next) { 240 if ((entry.hash == hash) && equals(entry.desc, desc)) { 241 return entry.grammar; 242 } 243 } 244 return null; 245 } 246 } 248 257 public Grammar removeGrammar(XMLGrammarDescription desc) { 258 synchronized (fGrammars) { 259 int hash = hashCode(desc); 260 int index = (hash & 0x7FFFFFFF) % fGrammars.length; 261 for (Entry entry = fGrammars[index], prev = null ; entry != null ; prev = entry, entry = entry.next) { 262 if ((entry.hash == hash) && equals(entry.desc, desc)) { 263 if (prev != null) { 264 prev.next = entry.next; 265 } 266 else { 267 fGrammars[index] = entry.next; 268 } 269 Grammar tempGrammar = entry.grammar; 270 entry.grammar = null; 271 fGrammarCount--; 272 return tempGrammar; 273 } 274 } 275 return null; 276 } 277 } 279 287 public boolean containsGrammar(XMLGrammarDescription desc) { 288 synchronized (fGrammars) { 289 int hash = hashCode(desc); 290 int index = (hash & 0x7FFFFFFF) % fGrammars.length; 291 for (Entry entry = fGrammars[index] ; entry != null ; entry = entry.next) { 292 if ((entry.hash == hash) && equals(entry.desc, desc)) { 293 return true; 294 } 295 } 296 return false; 297 } 298 } 300 303 public void lockPool() { 304 fPoolIsLocked = true; 305 } 307 311 public void unlockPool() { 312 fPoolIsLocked = false; 313 } 315 319 public void clear() { 320 for (int i=0; i<fGrammars.length; i++) { 321 if(fGrammars[i] != null) { 322 fGrammars[i].clear(); 323 fGrammars[i] = null; 324 } 325 } 326 fGrammarCount = 0; 327 } 329 338 public boolean equals(XMLGrammarDescription desc1, XMLGrammarDescription desc2) { 339 return desc1.equals(desc2); 340 } 341 342 348 public int hashCode(XMLGrammarDescription desc) { 349 return desc.hashCode(); 350 } 351 352 356 protected static final class Entry { 357 public int hash; 358 public XMLGrammarDescription desc; 359 public Grammar grammar; 360 public Entry next; 361 362 protected Entry(int hash, XMLGrammarDescription desc, Grammar grammar, Entry next) { 363 this.hash = hash; 364 this.desc = desc; 365 this.grammar = grammar; 366 this.next = next; 367 } 368 369 protected void clear () { 372 desc = null; 373 grammar = null; 374 if(next != null) { 375 next.clear(); 376 next = null; 377 } 378 } } 381 382 403 404 } | Popular Tags |