FileDocCategorySizeDatePackage
CachingParserPool.javaAPI DocApache Xerces 3.0.116013Fri Sep 14 20:33:54 BST 2007org.apache.xerces.parsers

CachingParserPool

public class CachingParserPool extends Object
A parser pool that enables caching of grammars. The caching parser pool is constructed with a specific symbol table and grammar pool that has already been populated with the grammars used by the application.

Once the caching parser pool is constructed, specific parser instances are created by calling the appropriate factory method on the parser pool.

Note: There is a performance penalty for using a caching parser pool due to thread safety. Access to the symbol table and grammar pool must be synchronized to ensure the safe operation of the symbol table and grammar pool.

Note: If performance is critical, then another mechanism needs to be used instead of the caching parser pool. One approach would be to create parser instances that do not share these structures. Instead, each instance would get its own copy to use while parsing. This avoids the synchronization overhead at the expense of more memory and the time required to copy the structures for each new parser instance. And even when a parser instance is re-used, there is a potential for a memory leak due to new symbols being added to the symbol table over time. In other words, always take caution to make sure that your application is thread-safe and avoids leaking memory.

author
Andy Clark, IBM
version
$Id: CachingParserPool.java 447239 2006-09-18 05:08:26Z mrglavas $

Fields Summary
public static final boolean
DEFAULT_SHADOW_SYMBOL_TABLE
Default shadow symbol table (false).
public static final boolean
DEFAULT_SHADOW_GRAMMAR_POOL
Default shadow grammar pool (false).
protected org.apache.xerces.util.SymbolTable
fSynchronizedSymbolTable
Symbol table. The symbol table that the caching parser pool is constructed with is automatically wrapped in a synchronized version for thread-safety.
protected org.apache.xerces.xni.grammars.XMLGrammarPool
fSynchronizedGrammarPool
Grammar pool. The grammar pool that the caching parser pool is constructed with is automatically wrapped in a synchronized version for thread-safety.
protected boolean
fShadowSymbolTable
Shadow the symbol table for new parser instances. If true, new parser instances use shadow copies of the main symbol table and are not allowed to add new symbols to the main symbol table. New symbols are added to the shadow symbol table and are local to the parser instance.
protected boolean
fShadowGrammarPool
Shadow the grammar pool for new parser instances. If true, new parser instances use shadow copies of the main grammar pool and are not allowed to add new grammars to the main grammar pool. New grammars are added to the shadow grammar pool and are local to the parser instance.
Constructors Summary
public CachingParserPool()
Default constructor.


    //
    // Constructors
    //

       
      
        this(new SymbolTable(), new XMLGrammarPoolImpl());
    
public CachingParserPool(org.apache.xerces.util.SymbolTable symbolTable, org.apache.xerces.xni.grammars.XMLGrammarPool grammarPool)
Constructs a caching parser pool with the specified symbol table and grammar pool.

param
symbolTable The symbol table.
param
grammarPool The grammar pool.

        fSynchronizedSymbolTable = new SynchronizedSymbolTable(symbolTable);
        fSynchronizedGrammarPool = new SynchronizedGrammarPool(grammarPool);
    
Methods Summary
public DOMParsercreateDOMParser()
Creates a new DOM parser.

        SymbolTable symbolTable = fShadowSymbolTable
                                ? new ShadowedSymbolTable(fSynchronizedSymbolTable)
                                : fSynchronizedSymbolTable;
        XMLGrammarPool grammarPool = fShadowGrammarPool
                                ? new ShadowedGrammarPool(fSynchronizedGrammarPool)
                                : fSynchronizedGrammarPool;
        return new DOMParser(symbolTable, grammarPool);
    
public SAXParsercreateSAXParser()
Creates a new SAX parser.

        SymbolTable symbolTable = fShadowSymbolTable
                                ? new ShadowedSymbolTable(fSynchronizedSymbolTable)
                                : fSynchronizedSymbolTable;
        XMLGrammarPool grammarPool = fShadowGrammarPool
                                ? new ShadowedGrammarPool(fSynchronizedGrammarPool)
                                : fSynchronizedGrammarPool;
        return new SAXParser(symbolTable, grammarPool);
    
public org.apache.xerces.util.SymbolTablegetSymbolTable()
Returns the symbol table.

        return fSynchronizedSymbolTable;
    
public org.apache.xerces.xni.grammars.XMLGrammarPoolgetXMLGrammarPool()
Returns the grammar pool.

        return fSynchronizedGrammarPool;
    
public voidsetShadowSymbolTable(boolean shadow)
Sets whether new parser instance receive shadow copies of the main symbol table.

param
shadow If true, new parser instances use shadow copies of the main symbol table and are not allowed to add new symbols to the main symbol table. New symbols are added to the shadow symbol table and are local to the parser instance. If false, new parser instances are allowed to add new symbols to the main symbol table.

        fShadowSymbolTable = shadow;