1 57 58 package com.sun.org.apache.xerces.internal.parsers; 59 60 import com.sun.org.apache.xerces.internal.xni.grammars.Grammar; 61 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool; 62 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription; 63 import com.sun.org.apache.xerces.internal.util.XMLGrammarPoolImpl; 64 65 import com.sun.org.apache.xerces.internal.util.ShadowedSymbolTable; 66 import com.sun.org.apache.xerces.internal.util.SymbolTable; 67 import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable; 68 69 100 public class CachingParserPool { 101 102 106 107 public static final boolean DEFAULT_SHADOW_SYMBOL_TABLE = false; 108 109 110 public static final boolean DEFAULT_SHADOW_GRAMMAR_POOL = false; 111 112 116 121 protected SymbolTable fSynchronizedSymbolTable; 122 123 128 protected XMLGrammarPool fSynchronizedGrammarPool; 129 130 137 protected boolean fShadowSymbolTable = DEFAULT_SHADOW_SYMBOL_TABLE; 138 139 146 protected boolean fShadowGrammarPool = DEFAULT_SHADOW_GRAMMAR_POOL; 147 148 152 153 public CachingParserPool() { 154 this(new SymbolTable(), new XMLGrammarPoolImpl()); 155 } 157 164 public CachingParserPool(SymbolTable symbolTable, XMLGrammarPool grammarPool) { 165 fSynchronizedSymbolTable = new SynchronizedSymbolTable(symbolTable); 166 fSynchronizedGrammarPool = new SynchronizedGrammarPool(grammarPool); 167 } 169 173 174 public SymbolTable getSymbolTable() { 175 return fSynchronizedSymbolTable; 176 } 178 179 public XMLGrammarPool getXMLGrammarPool() { 180 return fSynchronizedGrammarPool; 181 } 183 185 197 public void setShadowSymbolTable(boolean shadow) { 198 fShadowSymbolTable = shadow; 199 } 201 203 204 public DOMParser createDOMParser() { 205 SymbolTable symbolTable = fShadowSymbolTable 206 ? new ShadowedSymbolTable(fSynchronizedSymbolTable) 207 : fSynchronizedSymbolTable; 208 XMLGrammarPool grammarPool = fShadowGrammarPool 209 ? new ShadowedGrammarPool(fSynchronizedGrammarPool) 210 : fSynchronizedGrammarPool; 211 return new DOMParser(symbolTable, grammarPool); 212 } 214 215 public SAXParser createSAXParser() { 216 SymbolTable symbolTable = fShadowSymbolTable 217 ? new ShadowedSymbolTable(fSynchronizedSymbolTable) 218 : fSynchronizedSymbolTable; 219 XMLGrammarPool grammarPool = fShadowGrammarPool 220 ? new ShadowedGrammarPool(fSynchronizedGrammarPool) 221 : fSynchronizedGrammarPool; 222 return new SAXParser(symbolTable, grammarPool); 223 } 225 229 234 public static final class SynchronizedGrammarPool 235 implements XMLGrammarPool { 236 237 241 242 private XMLGrammarPool fGrammarPool; 243 244 248 249 public SynchronizedGrammarPool(XMLGrammarPool grammarPool) { 250 fGrammarPool = grammarPool; 251 } 253 257 public Grammar [] retrieveInitialGrammarSet(String grammarType ) { 263 synchronized (fGrammarPool) { 264 return fGrammarPool.retrieveInitialGrammarSet(grammarType); 265 } 266 } 268 public Grammar retrieveGrammar(XMLGrammarDescription gDesc) { 273 synchronized (fGrammarPool) { 274 return fGrammarPool.retrieveGrammar(gDesc); 275 } 276 } 278 public void cacheGrammars(String grammarType, Grammar[] grammars) { 284 synchronized (fGrammarPool) { 285 fGrammarPool.cacheGrammars(grammarType, grammars); 286 } 287 } 289 290 public void lockPool() { 291 synchronized (fGrammarPool) { 292 fGrammarPool.lockPool(); 293 } 294 } 296 297 public void clear() { 298 synchronized (fGrammarPool) { 299 fGrammarPool.clear(); 300 } 301 } 303 304 public void unlockPool() { 305 synchronized (fGrammarPool) { 306 fGrammarPool.unlockPool(); 307 } 308 } 310 314 320 327 328 333 340 341 347 354 355 361 368 369 } 371 379 public static final class ShadowedGrammarPool 380 extends XMLGrammarPoolImpl { 381 382 386 387 private XMLGrammarPool fGrammarPool; 388 389 393 394 public ShadowedGrammarPool(XMLGrammarPool grammarPool) { 395 fGrammarPool = grammarPool; 396 } 398 402 409 public Grammar [] retrieveInitialGrammarSet(String grammarType ) { 410 Grammar [] grammars = super.retrieveInitialGrammarSet(grammarType); 411 if (grammars != null) return grammars; 412 return fGrammarPool.retrieveInitialGrammarSet(grammarType); 413 } 415 422 public Grammar retrieveGrammar(XMLGrammarDescription gDesc) { 423 Grammar g = super.retrieveGrammar(gDesc); 424 if(g != null) return g; 425 return fGrammarPool.retrieveGrammar(gDesc); 426 } 428 436 public void cacheGrammars(String grammarType, Grammar[] grammars) { 437 super.cacheGrammars(grammarType, grammars); 439 fGrammarPool.cacheGrammars(grammarType, grammars); 440 } 442 447 public Grammar getGrammar(XMLGrammarDescription desc) { 448 449 if (super.containsGrammar(desc)) { 450 return super.getGrammar(desc); 451 } 452 return null; 453 454 } 456 462 public boolean containsGrammar(XMLGrammarDescription desc) { 463 return super.containsGrammar(desc); 464 } 466 } 468 } | Popular Tags |