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

XMLGrammarPreparser

public class XMLGrammarPreparser extends Object

This class provides an easy way for a user to preparse grammars of various types. By default, it knows how to preparse external DTD's and schemas; it provides an easy way for user applications to register classes that know how to parse additional grammar types. By default, it does no grammar caching; but it provides ways for user applications to do so.

author
Neil Graham, IBM
version
$Id: XMLGrammarPreparser.java 521449 2007-03-22 20:35:42Z mrglavas $

Fields Summary
private static final String
CONTINUE_AFTER_FATAL_ERROR
protected static final String
SYMBOL_TABLE
Property identifier: symbol table.
protected static final String
ERROR_REPORTER
Property identifier: error reporter.
protected static final String
ERROR_HANDLER
Property identifier: error handler.
protected static final String
ENTITY_RESOLVER
Property identifier: entity resolver.
protected static final String
GRAMMAR_POOL
Property identifier: grammar pool .
private static final Hashtable
KNOWN_LOADERS
private static final String[]
RECOGNIZED_PROPERTIES
Recognized properties.
protected org.apache.xerces.util.SymbolTable
fSymbolTable
protected org.apache.xerces.impl.XMLErrorReporter
fErrorReporter
protected org.apache.xerces.xni.parser.XMLEntityResolver
fEntityResolver
protected org.apache.xerces.xni.grammars.XMLGrammarPool
fGrammarPool
protected Locale
fLocale
private Hashtable
fLoaders
private int
fModCount
Constructors Summary
public XMLGrammarPreparser()
Default constructor.


    //
    // Constructors
    //

       
      
        this(new SymbolTable());
    
public XMLGrammarPreparser(org.apache.xerces.util.SymbolTable symbolTable)
Constructs a preparser using the specified symbol table.

param
symbolTable The symbol table to use.

        fSymbolTable = symbolTable;

        fLoaders = new Hashtable();
        setLocale(Locale.getDefault());
        fErrorReporter = new XMLErrorReporter();
        fErrorReporter.setLocale(fLocale);
        fEntityResolver = new XMLEntityManager();
        // those are all the basic properties...
    
Methods Summary
private voidclearModCounts()

        Enumeration loaders = fLoaders.elements();
        while (loaders.hasMoreElements()) {
            XMLGrammarLoaderContainer xglc = (XMLGrammarLoaderContainer) loaders.nextElement();
            xglc.modCount = 0;
        }
        fModCount = 1;
    
public org.apache.xerces.xni.parser.XMLEntityResolvergetEntityResolver()
Returns the registered entity resolver.

        return fEntityResolver;
    
public org.apache.xerces.xni.parser.XMLErrorHandlergetErrorHandler()
Returns the registered error handler.

        return fErrorReporter.getErrorHandler();
    
public booleangetFeature(java.lang.String type, java.lang.String featureId)

        XMLGrammarLoader gl = ((XMLGrammarLoaderContainer)fLoaders.get(type)).loader;
        return gl.getFeature(featureId);
    
public org.apache.xerces.xni.grammars.XMLGrammarPoolgetGrammarPool()
Returns the registered grammar pool.

        return fGrammarPool;
    
public org.apache.xerces.xni.grammars.XMLGrammarLoadergetLoader(java.lang.String type)

        XMLGrammarLoaderContainer xglc = (XMLGrammarLoaderContainer) fLoaders.get(type);
        return (xglc != null) ? xglc.loader : null;
    
public java.util.LocalegetLocale()
Return the Locale the XMLGrammarLoader is using.

        return fLocale;
    
public java.lang.ObjectgetProperty(java.lang.String type, java.lang.String propertyId)

        XMLGrammarLoader gl = ((XMLGrammarLoaderContainer)fLoaders.get(type)).loader;
        return gl.getProperty(propertyId);
    
public org.apache.xerces.xni.grammars.GrammarpreparseGrammar(java.lang.String type, org.apache.xerces.xni.parser.XMLInputSource is)
Parse a grammar from a location identified by an XMLInputSource. This method also adds this grammar to the XMLGrammarPool

param
type The type of the grammar to be constructed
param
is The XMLInputSource containing this grammar's information If a URI is included in the systemId field, the parser will not expand this URI or make it available to the EntityResolver
return
The newly created Grammar.
exception
XNIException thrown on an error in grammar construction
exception
IOException thrown if an error is encountered in reading the file

        if (fLoaders.containsKey(type)) {
            XMLGrammarLoaderContainer xglc = (XMLGrammarLoaderContainer) fLoaders.get(type);
            XMLGrammarLoader gl = xglc.loader;
            if (xglc.modCount != fModCount) {
                // make sure gl's been set up with all the "basic" properties:
                gl.setProperty(SYMBOL_TABLE, fSymbolTable);
                gl.setProperty(ENTITY_RESOLVER, fEntityResolver);
                gl.setProperty(ERROR_REPORTER, fErrorReporter);
                // potentially, not all will support this one...
                if (fGrammarPool != null) {
                    try {
                        gl.setProperty(GRAMMAR_POOL, fGrammarPool);
                    } catch(Exception e) {
                        // too bad...
                    }
                }
                xglc.modCount = fModCount;
            }
            return gl.loadGrammar(is);
        }
        return null;
    
public booleanregisterPreparser(java.lang.String grammarType, org.apache.xerces.xni.grammars.XMLGrammarLoader loader)

        if(loader == null) { // none specified!
            if(KNOWN_LOADERS.containsKey(grammarType)) {
                // got one; just instantiate it...
                String loaderName = (String)KNOWN_LOADERS.get(grammarType);
                try {
                    ClassLoader cl = ObjectFactory.findClassLoader();
                    XMLGrammarLoader gl = (XMLGrammarLoader)(ObjectFactory.newInstance(loaderName, cl, true));
                    fLoaders.put(grammarType, new XMLGrammarLoaderContainer(gl));
                } catch (Exception e) {
                    return false;
                }
                return true;
            }
            return false;
        }
        // were given one
        fLoaders.put(grammarType, new XMLGrammarLoaderContainer(loader));
        return true;
    
public voidsetEntityResolver(org.apache.xerces.xni.parser.XMLEntityResolver entityResolver)
Sets the entity resolver.

param
entityResolver The new entity resolver.

        if (fEntityResolver != entityResolver) {
            // Overflow. We actually need to reset the 
            // modCount on every XMLGrammarLoaderContainer.
            if (++fModCount < 0) {
                clearModCounts();
            }
            fEntityResolver = entityResolver;
        }
    
public voidsetErrorHandler(org.apache.xerces.xni.parser.XMLErrorHandler errorHandler)
Sets the error handler.

param
errorHandler The error handler.

        fErrorReporter.setProperty(ERROR_HANDLER, errorHandler);
    
public voidsetFeature(java.lang.String featureId, boolean value)

        Enumeration loaders = fLoaders.elements();
        while (loaders.hasMoreElements()) {
            XMLGrammarLoader gl = ((XMLGrammarLoaderContainer)loaders.nextElement()).loader;
            try {
                gl.setFeature(featureId, value);
            } catch(Exception e) {
                // eat it up...
            }
        }
        // since our error reporter is a property we set later,
        // make sure features it understands are also set.
        if(featureId.equals(CONTINUE_AFTER_FATAL_ERROR)) {
            fErrorReporter.setFeature(CONTINUE_AFTER_FATAL_ERROR, value);
        }
    
public voidsetGrammarPool(org.apache.xerces.xni.grammars.XMLGrammarPool grammarPool)
Sets the grammar pool.

param
grammarPool The new grammar pool.

        if (fGrammarPool != grammarPool) {
            // Overflow. We actually need to reset the 
            // modCount on every XMLGrammarLoaderContainer.
            if (++fModCount < 0) {
                clearModCounts();
            }
            fGrammarPool = grammarPool;
        }
    
public voidsetLocale(java.util.Locale locale)
Set the locale to use for messages.

param
locale The locale object to use for localization of messages.
exception
XNIException Thrown if the parser does not support the specified locale.

        fLocale = locale;
    
public voidsetProperty(java.lang.String propId, java.lang.Object value)

        Enumeration loaders = fLoaders.elements();
        while (loaders.hasMoreElements()) {
            XMLGrammarLoader gl = ((XMLGrammarLoaderContainer)loaders.nextElement()).loader;
            try {
                gl.setProperty(propId, value);
            } catch(Exception e) {
                // eat it up...
            }
        }