FileDocCategorySizeDatePackage
XMLGrammarPreparser.javaAPI DocJava SE 6 API13347Tue Jun 10 00:22:50 BST 2008com.sun.org.apache.xerces.internal.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,v 1.2.6.1 2005/09/08 04:03:52 sunithareddy Exp $

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 SymbolTable
fSymbolTable
protected XMLErrorReporter
fErrorReporter
protected XMLEntityResolver
fEntityResolver
protected XMLGrammarPool
fGrammarPool
protected Locale
fLocale
private Hashtable
fLoaders
Constructors Summary
public XMLGrammarPreparser()
Default constructor.


    //
    // Constructors
    //

       
      
        this(new SymbolTable());
    
public XMLGrammarPreparser(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
public com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolvergetEntityResolver()
Returns the registered entity resolver.

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

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

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

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

        return (XMLGrammarLoader)fLoaders.get(type);
    
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 = (XMLGrammarLoader)fLoaders.get(type);
        return gl.getProperty(propertyId);
    
public com.sun.org.apache.xerces.internal.xni.grammars.GrammarpreparseGrammar(java.lang.String type, com.sun.org.apache.xerces.internal.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)) {
            XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type);
            // 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...
                }
            }
            return gl.loadGrammar(is);
        }
        return null;
    
public booleanregisterPreparser(java.lang.String grammarType, com.sun.org.apache.xerces.internal.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, gl);
                } catch (Exception e) {
                    return false;
                }
                return true;
            }
            return false;
        }
        // were given one
        fLoaders.put(grammarType, loader);
        return true;
    
public voidsetEntityResolver(com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver entityResolver)
Sets the entity resolver.

param
entityResolver The new entity resolver.

        fEntityResolver = entityResolver;
    
public voidsetErrorHandler(com.sun.org.apache.xerces.internal.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 = (XMLGrammarLoader)loaders.nextElement();
            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(com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool grammarPool)
Sets the grammar pool.

param
grammarPool The new grammar pool.

        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 = (XMLGrammarLoader)loaders.nextElement();
            try {
                gl.setProperty(propId, value);
            } catch(Exception e) {
                // eat it up...
            }
        }