1 16 17 package org.apache.xerces.util; 18 19 import org.apache.xerces.xni.grammars.Grammar; 20 import org.apache.xerces.xni.grammars.XMLGrammarDescription; 21 import org.apache.xerces.xni.grammars.XMLGrammarPool; 22 23 39 public class XMLGrammarPoolImpl implements XMLGrammarPool { 40 41 45 46 protected static final int TABLE_SIZE = 11; 47 48 52 53 protected Entry[] fGrammars = null; 54 55 protected boolean fPoolIsLocked; 57 58 protected int fGrammarCount = 0; 60 61 private static final boolean DEBUG = false ; 62 63 67 68 public XMLGrammarPoolImpl() { 69 fGrammars = new Entry[TABLE_SIZE]; 70 fPoolIsLocked = false; 71 } 73 74 public XMLGrammarPoolImpl(int initialCapacity) { 75 fGrammars = new Entry[initialCapacity]; 76 fPoolIsLocked = false; 77 } 78 79 83 93 public Grammar [] retrieveInitialGrammarSet (String grammarType) { 94 synchronized (fGrammars) { 95 int grammarSize = fGrammars.length ; 96 Grammar [] tempGrammars = new Grammar[fGrammarCount]; 97 int pos = 0; 98 for (int i = 0; i < grammarSize; i++) { 99 for (Entry e = fGrammars[i]; e != null; e = e.next) { 100 if (e.desc.getGrammarType().equals(grammarType)) { 101 tempGrammars[pos++] = e.grammar; 102 } 103 } 104 } 105 Grammar[] toReturn = new Grammar[pos]; 106 System.arraycopy(tempGrammars, 0, toReturn, 0, pos); 107 return toReturn; 108 } 109 } 111 122 public void cacheGrammars(String grammarType, Grammar[] grammars) { 123 if(!fPoolIsLocked) { 124 for (int i = 0; i < grammars.length; i++) { 125 if(DEBUG) { 126 System.out.println("CACHED GRAMMAR " + (i+1) ) ; 127 Grammar temp = grammars[i] ; 128 } 130 putGrammar(grammars[i]); 131 } 132 } 133 } 135 149 public Grammar retrieveGrammar(XMLGrammarDescription desc) { 150 if(DEBUG){ 151 System.out.println("RETRIEVING GRAMMAR FROM THE APPLICATION WITH FOLLOWING DESCRIPTION :"); 152 } 154 return getGrammar(desc); 155 } 157 161 167 public void putGrammar(Grammar grammar) { 168 if(!fPoolIsLocked) { 169 synchronized (fGrammars) { 170 XMLGrammarDescription desc = grammar.getGrammarDescription(); 171 int hash = hashCode(desc); 172 int index = (hash & 0x7FFFFFFF) % fGrammars.length; 173 for (Entry entry = fGrammars[index]; entry != null; entry = entry.next) { 174 if (entry.hash == hash && equals(entry.desc, desc)) { 175 entry.grammar = grammar; 176 return; 177 } 178 } 179 Entry entry = new Entry(hash, desc, grammar, fGrammars[index]); 181 fGrammars[index] = entry; 182 fGrammarCount++; 183 } 184 } 185 } 187 194 public Grammar getGrammar(XMLGrammarDescription desc) { 195 synchronized (fGrammars) { 196 int hash = hashCode(desc); 197 int index = (hash & 0x7FFFFFFF) % fGrammars.length; 198 for (Entry entry = fGrammars[index] ; entry != null ; entry = entry.next) { 199 if ((entry.hash == hash) && equals(entry.desc, desc)) { 200 return entry.grammar; 201 } 202 } 203 return null; 204 } 205 } 207 216 public Grammar removeGrammar(XMLGrammarDescription desc) { 217 synchronized (fGrammars) { 218 int hash = hashCode(desc); 219 int index = (hash & 0x7FFFFFFF) % fGrammars.length; 220 for (Entry entry = fGrammars[index], prev = null ; entry != null ; prev = entry, entry = entry.next) { 221 if ((entry.hash == hash) && equals(entry.desc, desc)) { 222 if (prev != null) { 223 prev.next = entry.next; 224 } 225 else { 226 fGrammars[index] = entry.next; 227 } 228 Grammar tempGrammar = entry.grammar; 229 entry.grammar = null; 230 fGrammarCount--; 231 return tempGrammar; 232 } 233 } 234 return null; 235 } 236 } 238 246 public boolean containsGrammar(XMLGrammarDescription desc) { 247 synchronized (fGrammars) { 248 int hash = hashCode(desc); 249 int index = (hash & 0x7FFFFFFF) % fGrammars.length; 250 for (Entry entry = fGrammars[index] ; entry != null ; entry = entry.next) { 251 if ((entry.hash == hash) && equals(entry.desc, desc)) { 252 return true; 253 } 254 } 255 return false; 256 } 257 } 259 262 public void lockPool() { 263 fPoolIsLocked = true; 264 } 266 270 public void unlockPool() { 271 fPoolIsLocked = false; 272 } 274 278 public void clear() { 279 for (int i=0; i<fGrammars.length; i++) { 280 if(fGrammars[i] != null) { 281 fGrammars[i].clear(); 282 fGrammars[i] = null; 283 } 284 } 285 fGrammarCount = 0; 286 } 288 297 public boolean equals(XMLGrammarDescription desc1, XMLGrammarDescription desc2) { 298 return desc1.equals(desc2); 299 } 300 301 307 public int hashCode(XMLGrammarDescription desc) { 308 return desc.hashCode(); 309 } 310 311 315 protected static final class Entry { 316 public int hash; 317 public XMLGrammarDescription desc; 318 public Grammar grammar; 319 public Entry next; 320 321 protected Entry(int hash, XMLGrammarDescription desc, Grammar grammar, Entry next) { 322 this.hash = hash; 323 this.desc = desc; 324 this.grammar = grammar; 325 this.next = next; 326 } 327 328 protected void clear () { 331 desc = null; 332 grammar = null; 333 if(next != null) { 334 next.clear(); 335 next = null; 336 } 337 } } 340 341 362 363 } | Popular Tags |