1 16 17 package org.apache.xerces.parsers; 18 19 import org.apache.xerces.xni.grammars.Grammar; 20 import org.apache.xerces.xni.grammars.XMLGrammarPool; 21 import org.apache.xerces.xni.grammars.XMLGrammarDescription; 22 import org.apache.xerces.util.XMLGrammarPoolImpl; 23 24 import org.apache.xerces.util.ShadowedSymbolTable; 25 import org.apache.xerces.util.SymbolTable; 26 import org.apache.xerces.util.SynchronizedSymbolTable; 27 28 59 public class CachingParserPool { 60 61 65 66 public static final boolean DEFAULT_SHADOW_SYMBOL_TABLE = false; 67 68 69 public static final boolean DEFAULT_SHADOW_GRAMMAR_POOL = false; 70 71 75 80 protected SymbolTable fSynchronizedSymbolTable; 81 82 87 protected XMLGrammarPool fSynchronizedGrammarPool; 88 89 96 protected boolean fShadowSymbolTable = DEFAULT_SHADOW_SYMBOL_TABLE; 97 98 105 protected boolean fShadowGrammarPool = DEFAULT_SHADOW_GRAMMAR_POOL; 106 107 111 112 public CachingParserPool() { 113 this(new SymbolTable(), new XMLGrammarPoolImpl()); 114 } 116 123 public CachingParserPool(SymbolTable symbolTable, XMLGrammarPool grammarPool) { 124 fSynchronizedSymbolTable = new SynchronizedSymbolTable(symbolTable); 125 fSynchronizedGrammarPool = new SynchronizedGrammarPool(grammarPool); 126 } 128 132 133 public SymbolTable getSymbolTable() { 134 return fSynchronizedSymbolTable; 135 } 137 138 public XMLGrammarPool getXMLGrammarPool() { 139 return fSynchronizedGrammarPool; 140 } 142 144 156 public void setShadowSymbolTable(boolean shadow) { 157 fShadowSymbolTable = shadow; 158 } 160 162 163 public DOMParser createDOMParser() { 164 SymbolTable symbolTable = fShadowSymbolTable 165 ? new ShadowedSymbolTable(fSynchronizedSymbolTable) 166 : fSynchronizedSymbolTable; 167 XMLGrammarPool grammarPool = fShadowGrammarPool 168 ? new ShadowedGrammarPool(fSynchronizedGrammarPool) 169 : fSynchronizedGrammarPool; 170 return new DOMParser(symbolTable, grammarPool); 171 } 173 174 public SAXParser createSAXParser() { 175 SymbolTable symbolTable = fShadowSymbolTable 176 ? new ShadowedSymbolTable(fSynchronizedSymbolTable) 177 : fSynchronizedSymbolTable; 178 XMLGrammarPool grammarPool = fShadowGrammarPool 179 ? new ShadowedGrammarPool(fSynchronizedGrammarPool) 180 : fSynchronizedGrammarPool; 181 return new SAXParser(symbolTable, grammarPool); 182 } 184 188 193 public static final class SynchronizedGrammarPool 194 implements XMLGrammarPool { 195 196 200 201 private XMLGrammarPool fGrammarPool; 202 203 207 208 public SynchronizedGrammarPool(XMLGrammarPool grammarPool) { 209 fGrammarPool = grammarPool; 210 } 212 216 public Grammar [] retrieveInitialGrammarSet(String grammarType ) { 222 synchronized (fGrammarPool) { 223 return fGrammarPool.retrieveInitialGrammarSet(grammarType); 224 } 225 } 227 public Grammar retrieveGrammar(XMLGrammarDescription gDesc) { 232 synchronized (fGrammarPool) { 233 return fGrammarPool.retrieveGrammar(gDesc); 234 } 235 } 237 public void cacheGrammars(String grammarType, Grammar[] grammars) { 243 synchronized (fGrammarPool) { 244 fGrammarPool.cacheGrammars(grammarType, grammars); 245 } 246 } 248 249 public void lockPool() { 250 synchronized (fGrammarPool) { 251 fGrammarPool.lockPool(); 252 } 253 } 255 256 public void clear() { 257 synchronized (fGrammarPool) { 258 fGrammarPool.clear(); 259 } 260 } 262 263 public void unlockPool() { 264 synchronized (fGrammarPool) { 265 fGrammarPool.unlockPool(); 266 } 267 } 269 273 279 286 287 292 299 300 306 313 314 320 327 328 } 330 338 public static final class ShadowedGrammarPool 339 extends XMLGrammarPoolImpl { 340 341 345 346 private XMLGrammarPool fGrammarPool; 347 348 352 353 public ShadowedGrammarPool(XMLGrammarPool grammarPool) { 354 fGrammarPool = grammarPool; 355 } 357 361 368 public Grammar [] retrieveInitialGrammarSet(String grammarType ) { 369 Grammar [] grammars = super.retrieveInitialGrammarSet(grammarType); 370 if (grammars != null) return grammars; 371 return fGrammarPool.retrieveInitialGrammarSet(grammarType); 372 } 374 381 public Grammar retrieveGrammar(XMLGrammarDescription gDesc) { 382 Grammar g = super.retrieveGrammar(gDesc); 383 if(g != null) return g; 384 return fGrammarPool.retrieveGrammar(gDesc); 385 } 387 395 public void cacheGrammars(String grammarType, Grammar[] grammars) { 396 super.cacheGrammars(grammarType, grammars); 398 fGrammarPool.cacheGrammars(grammarType, grammars); 399 } 401 406 public Grammar getGrammar(XMLGrammarDescription desc) { 407 408 if (super.containsGrammar(desc)) { 409 return super.getGrammar(desc); 410 } 411 return null; 412 413 } 415 421 public boolean containsGrammar(XMLGrammarDescription desc) { 422 return super.containsGrammar(desc); 423 } 425 } 427 } | Popular Tags |