286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2001, 2002,2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xerces.internal.parsers;
286N/A
286N/Aimport java.io.IOException;
286N/Aimport java.util.Enumeration;
286N/Aimport java.util.Hashtable;
286N/Aimport java.util.Locale;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.impl.Constants;
286N/Aimport com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
286N/Aimport com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
286N/Aimport com.sun.org.apache.xerces.internal.util.SymbolTable;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XNIException;
286N/Aimport com.sun.org.apache.xerces.internal.xni.grammars.Grammar;
286N/Aimport com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
286N/Aimport com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarLoader;
286N/Aimport com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
286N/Aimport com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
286N/Aimport com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;
286N/Aimport com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
286N/Aimport com.sun.org.apache.xerces.internal.utils.ObjectFactory;
286N/A
286N/A/**
286N/A * <p> This class provides an easy way for a user to preparse grammars
286N/A * of various types. By default, it knows how to preparse external
286N/A * DTD's and schemas; it provides an easy way for user applications to
286N/A * register classes that know how to parse additional grammar types.
286N/A * By default, it does no grammar caching; but it provides ways for
286N/A * user applications to do so.
286N/A *
286N/A * @author Neil Graham, IBM
286N/A *
286N/A * @version $Id: XMLGrammarPreparser.java,v 1.7 2010-11-01 04:40:10 joehw Exp $
286N/A */
286N/Apublic class XMLGrammarPreparser {
286N/A
286N/A //
286N/A // Constants
286N/A //
286N/A
286N/A // feature: continue-after-fatal-error
286N/A private final static String CONTINUE_AFTER_FATAL_ERROR =
286N/A Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE;
286N/A
286N/A /** Property identifier: symbol table. */
286N/A protected static final String SYMBOL_TABLE =
286N/A Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
286N/A
286N/A /** Property identifier: error reporter. */
286N/A protected static final String ERROR_REPORTER =
286N/A Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
286N/A
286N/A /** Property identifier: error handler. */
286N/A protected static final String ERROR_HANDLER =
286N/A Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY;
286N/A
286N/A /** Property identifier: entity resolver. */
286N/A protected static final String ENTITY_RESOLVER =
286N/A Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY;
286N/A
286N/A /** Property identifier: grammar pool . */
286N/A protected static final String GRAMMAR_POOL =
286N/A Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
286N/A
286N/A // the "built-in" grammar loaders
286N/A private static final Hashtable KNOWN_LOADERS = new Hashtable();
286N/A
286N/A static {
286N/A KNOWN_LOADERS.put(XMLGrammarDescription.XML_SCHEMA,
286N/A "com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader");
286N/A KNOWN_LOADERS.put(XMLGrammarDescription.XML_DTD,
286N/A "com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDLoader");
286N/A }
286N/A
286N/A /** Recognized properties. */
286N/A private static final String[] RECOGNIZED_PROPERTIES = {
286N/A SYMBOL_TABLE,
286N/A ERROR_REPORTER,
286N/A ERROR_HANDLER,
286N/A ENTITY_RESOLVER,
286N/A GRAMMAR_POOL,
286N/A };
286N/A
286N/A // Data
286N/A protected SymbolTable fSymbolTable;
286N/A protected XMLErrorReporter fErrorReporter;
286N/A protected XMLEntityResolver fEntityResolver;
286N/A protected XMLGrammarPool fGrammarPool;
286N/A
286N/A protected Locale fLocale;
286N/A
286N/A // Hashtable holding our loaders
286N/A private Hashtable fLoaders;
286N/A
286N/A //
286N/A // Constructors
286N/A //
286N/A
286N/A /** Default constructor. */
286N/A public XMLGrammarPreparser() {
286N/A this(new SymbolTable());
286N/A } // <init>()
286N/A
286N/A /**
286N/A * Constructs a preparser using the specified symbol table.
286N/A *
286N/A * @param symbolTable The symbol table to use.
286N/A */
286N/A public XMLGrammarPreparser (SymbolTable symbolTable) {
286N/A fSymbolTable = symbolTable;
286N/A
286N/A fLoaders = new Hashtable();
286N/A fErrorReporter = new XMLErrorReporter();
286N/A setLocale(Locale.getDefault());
286N/A fEntityResolver = new XMLEntityManager();
286N/A // those are all the basic properties...
286N/A } // <init>(SymbolTable)
286N/A
286N/A //
286N/A // Public methods
286N/A //
286N/A
286N/A /*
286N/A * Register a type of grammar to make it preparsable. If
286N/A * the second parameter is null, the parser will use its built-in
286N/A * facilities for that grammar type.
286N/A * This should be called by the application immediately
286N/A * after creating this object and before initializing any properties/features.
286N/A * @param type URI identifying the type of the grammar
286N/A * @param loader an object capable of preparsing that type; null if the ppreparser should use built-in knowledge.
286N/A * @return true if successful; false if no built-in knowledge of
286N/A * the type or if unable to instantiate the string we know about
286N/A */
286N/A public boolean registerPreparser(String grammarType, XMLGrammarLoader loader) {
286N/A if(loader == null) { // none specified!
286N/A if(KNOWN_LOADERS.containsKey(grammarType)) {
286N/A // got one; just instantiate it...
286N/A String loaderName = (String)KNOWN_LOADERS.get(grammarType);
286N/A try {
286N/A XMLGrammarLoader gl = (XMLGrammarLoader)(ObjectFactory.newInstance(loaderName, true));
286N/A fLoaders.put(grammarType, gl);
286N/A } catch (Exception e) {
286N/A return false;
286N/A }
286N/A return true;
286N/A }
286N/A return false;
286N/A }
286N/A // were given one
286N/A fLoaders.put(grammarType, loader);
286N/A return true;
286N/A } // registerPreparser(String, XMLGrammarLoader): boolean
286N/A
286N/A /**
286N/A * Parse a grammar from a location identified by an
286N/A * XMLInputSource.
286N/A * This method also adds this grammar to the XMLGrammarPool
286N/A *
286N/A * @param type The type of the grammar to be constructed
286N/A * @param is The XMLInputSource containing this grammar's
286N/A * information
286N/A * <strong>If a URI is included in the systemId field, the parser will not expand this URI or make it
286N/A * available to the EntityResolver</strong>
286N/A * @return The newly created <code>Grammar</code>.
286N/A * @exception XNIException thrown on an error in grammar
286N/A * construction
286N/A * @exception IOException thrown if an error is encountered
286N/A * in reading the file
286N/A */
286N/A public Grammar preparseGrammar(String type, XMLInputSource
286N/A is) throws XNIException, IOException {
286N/A if(fLoaders.containsKey(type)) {
286N/A XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type);
286N/A // make sure gl's been set up with all the "basic" properties:
286N/A gl.setProperty(SYMBOL_TABLE, fSymbolTable);
286N/A gl.setProperty(ENTITY_RESOLVER, fEntityResolver);
286N/A gl.setProperty(ERROR_REPORTER, fErrorReporter);
286N/A // potentially, not all will support this one...
286N/A if(fGrammarPool != null) {
286N/A try {
286N/A gl.setProperty(GRAMMAR_POOL, fGrammarPool);
286N/A } catch(Exception e) {
286N/A // too bad...
286N/A }
286N/A }
286N/A return gl.loadGrammar(is);
286N/A }
286N/A return null;
286N/A } // preparseGrammar(String, XMLInputSource): Grammar
286N/A
286N/A /**
286N/A * Set the locale to use for messages.
286N/A *
286N/A * @param locale The locale object to use for localization of messages.
286N/A *
286N/A * @exception XNIException Thrown if the parser does not support the
286N/A * specified locale.
286N/A */
286N/A public void setLocale(Locale locale) {
286N/A fLocale = locale;
286N/A fErrorReporter.setLocale(locale);
286N/A } // setLocale(Locale)
286N/A
286N/A /** Return the Locale the XMLGrammarLoader is using. */
286N/A public Locale getLocale() {
286N/A return fLocale;
286N/A } // getLocale(): Locale
286N/A
286N/A
286N/A /**
286N/A * Sets the error handler.
286N/A *
286N/A * @param errorHandler The error handler.
286N/A */
286N/A public void setErrorHandler(XMLErrorHandler errorHandler) {
286N/A fErrorReporter.setProperty(ERROR_HANDLER, errorHandler);
286N/A } // setErrorHandler(XMLErrorHandler)
286N/A
286N/A /** Returns the registered error handler. */
286N/A public XMLErrorHandler getErrorHandler() {
286N/A return fErrorReporter.getErrorHandler();
286N/A } // getErrorHandler(): XMLErrorHandler
286N/A
286N/A /**
286N/A * Sets the entity resolver.
286N/A *
286N/A * @param entityResolver The new entity resolver.
286N/A */
286N/A public void setEntityResolver(XMLEntityResolver entityResolver) {
286N/A fEntityResolver = entityResolver;
286N/A } // setEntityResolver(XMLEntityResolver)
286N/A
286N/A /** Returns the registered entity resolver. */
286N/A public XMLEntityResolver getEntityResolver() {
286N/A return fEntityResolver;
286N/A } // getEntityResolver(): XMLEntityResolver
286N/A
286N/A /**
286N/A * Sets the grammar pool.
286N/A *
286N/A * @param grammarPool The new grammar pool.
286N/A */
286N/A public void setGrammarPool(XMLGrammarPool grammarPool) {
286N/A fGrammarPool = grammarPool;
286N/A } // setGrammarPool(XMLGrammarPool)
286N/A
286N/A /** Returns the registered grammar pool. */
286N/A public XMLGrammarPool getGrammarPool() {
286N/A return fGrammarPool;
286N/A } // getGrammarPool(): XMLGrammarPool
286N/A
286N/A // it's possible the application may want access to a certain loader to do
286N/A // some custom work.
286N/A public XMLGrammarLoader getLoader(String type) {
286N/A return (XMLGrammarLoader)fLoaders.get(type);
286N/A } // getLoader(String): XMLGrammarLoader
286N/A
286N/A // set a feature. This method tries to set it on all
286N/A // registered loaders; it eats any resulting exceptions. If
286N/A // an app needs to know if a particular feature is supported
286N/A // by a grammar loader of a particular type, it will have
286N/A // to retrieve that loader and use the loader's setFeature method.
286N/A public void setFeature(String featureId, boolean value) {
286N/A Enumeration loaders = fLoaders.elements();
286N/A while(loaders.hasMoreElements()){
286N/A XMLGrammarLoader gl = (XMLGrammarLoader)loaders.nextElement();
286N/A try {
286N/A gl.setFeature(featureId, value);
286N/A } catch(Exception e) {
286N/A // eat it up...
286N/A }
286N/A }
286N/A // since our error reporter is a property we set later,
286N/A // make sure features it understands are also set.
286N/A if(featureId.equals(CONTINUE_AFTER_FATAL_ERROR)) {
286N/A fErrorReporter.setFeature(CONTINUE_AFTER_FATAL_ERROR, value);
286N/A }
286N/A } //setFeature(String, boolean)
286N/A
286N/A // set a property. This method tries to set it on all
286N/A // registered loaders; it eats any resulting exceptions. If
286N/A // an app needs to know if a particular property is supported
286N/A // by a grammar loader of a particular type, it will have
286N/A // to retrieve that loader and use the loader's setProperty method.
286N/A // <p> <strong>An application should use the explicit method
286N/A // in this class to set "standard" properties like error handler etc.</strong>
286N/A public void setProperty(String propId, Object value) {
286N/A Enumeration loaders = fLoaders.elements();
286N/A while(loaders.hasMoreElements()){
286N/A XMLGrammarLoader gl = (XMLGrammarLoader)loaders.nextElement();
286N/A try {
286N/A gl.setProperty(propId, value);
286N/A } catch(Exception e) {
286N/A // eat it up...
286N/A }
286N/A }
286N/A } //setProperty(String, Object)
286N/A
286N/A // get status of feature in a particular loader. This
286N/A // catches no exceptions--including NPE's--so the application had
286N/A // better make sure the loader exists and knows about this feature.
286N/A // @param type type of grammar to look for the feature in.
286N/A // @param featureId the feature string to query.
286N/A // @return the value of the feature.
286N/A public boolean getFeature(String type, String featureId) {
286N/A XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type);
286N/A return gl.getFeature(featureId);
286N/A } // getFeature (String, String): boolean
286N/A
286N/A // get status of property in a particular loader. This
286N/A // catches no exceptions--including NPE's--so the application had
286N/A // better make sure the loader exists and knows about this property.
286N/A // <strong>For standard properties--that will be supported
286N/A // by all loaders--the specific methods should be queried!</strong>
286N/A // @param type type of grammar to look for the property in.
286N/A // @param propertyId the property string to query.
286N/A // @return the value of the property.
286N/A public Object getProperty(String type, String propertyId) {
286N/A XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type);
286N/A return gl.getProperty(propertyId);
286N/A } // getProperty(String, String): Object
286N/A} // class XMLGrammarPreparser