KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > parsers > XMLGrammarPreparser


1 /*
2  * Copyright 2001, 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.parsers;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Locale JavaDoc;
23
24 import org.apache.xerces.impl.Constants;
25 import org.apache.xerces.impl.XMLEntityManager;
26 import org.apache.xerces.impl.XMLErrorReporter;
27 import org.apache.xerces.util.SymbolTable;
28 import org.apache.xerces.xni.XNIException;
29 import org.apache.xerces.xni.grammars.Grammar;
30 import org.apache.xerces.xni.grammars.XMLGrammarDescription;
31 import org.apache.xerces.xni.grammars.XMLGrammarLoader;
32 import org.apache.xerces.xni.grammars.XMLGrammarPool;
33 import org.apache.xerces.xni.parser.XMLEntityResolver;
34 import org.apache.xerces.xni.parser.XMLErrorHandler;
35 import org.apache.xerces.xni.parser.XMLInputSource;
36
37 /**
38  * <p> This class provides an easy way for a user to preparse grammars
39  * of various types. By default, it knows how to preparse external
40  * DTD's and schemas; it provides an easy way for user applications to
41  * register classes that know how to parse additional grammar types.
42  * By default, it does no grammar caching; but it provides ways for
43  * user applications to do so.
44  *
45  * @author Neil Graham, IBM
46  *
47  * @version $Id: XMLGrammarPreparser.java,v 1.10 2004/03/25 04:03:23 mrglavas Exp $
48  */

49 public class XMLGrammarPreparser {
50
51     //
52
// Constants
53
//
54

55     // feature: continue-after-fatal-error
56
private final static String JavaDoc CONTINUE_AFTER_FATAL_ERROR =
57         Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE;
58
59     /** Property identifier: symbol table. */
60     protected static final String JavaDoc SYMBOL_TABLE =
61         Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
62
63     /** Property identifier: error reporter. */
64     protected static final String JavaDoc ERROR_REPORTER =
65         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
66
67     /** Property identifier: error handler. */
68     protected static final String JavaDoc ERROR_HANDLER =
69         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY;
70
71     /** Property identifier: entity resolver. */
72     protected static final String JavaDoc ENTITY_RESOLVER =
73         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY;
74
75     /** Property identifier: grammar pool . */
76     protected static final String JavaDoc GRAMMAR_POOL =
77         Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
78
79     // the "built-in" grammar loaders
80
private static final Hashtable JavaDoc KNOWN_LOADERS = new Hashtable JavaDoc();
81
82     static {
83         KNOWN_LOADERS.put(XMLGrammarDescription.XML_SCHEMA,
84             "org.apache.xerces.impl.xs.XMLSchemaLoader");
85         KNOWN_LOADERS.put(XMLGrammarDescription.XML_DTD,
86             "org.apache.xerces.impl.dtd.XMLDTDLoader");
87     }
88
89     /** Recognized properties. */
90     private static final String JavaDoc[] RECOGNIZED_PROPERTIES = {
91         SYMBOL_TABLE,
92         ERROR_REPORTER,
93         ERROR_HANDLER,
94         ENTITY_RESOLVER,
95         GRAMMAR_POOL,
96     };
97
98     // Data
99
protected SymbolTable fSymbolTable;
100     protected XMLErrorReporter fErrorReporter;
101     protected XMLEntityResolver fEntityResolver;
102     protected XMLGrammarPool fGrammarPool;
103
104     protected Locale JavaDoc fLocale;
105
106     // Hashtable holding our loaders
107
private Hashtable JavaDoc fLoaders;
108
109     //
110
// Constructors
111
//
112

113     /** Default constructor. */
114     public XMLGrammarPreparser() {
115         this(new SymbolTable());
116     } // <init>()
117

118     /**
119      * Constructs a preparser using the specified symbol table.
120      *
121      * @param symbolTable The symbol table to use.
122      */

123     public XMLGrammarPreparser (SymbolTable symbolTable) {
124         fSymbolTable = symbolTable;
125
126         fLoaders = new Hashtable JavaDoc();
127         setLocale(Locale.getDefault());
128         fErrorReporter = new XMLErrorReporter();
129         fErrorReporter.setLocale(fLocale);
130         fEntityResolver = new XMLEntityManager();
131         // those are all the basic properties...
132
} // <init>(SymbolTable)
133

134     //
135
// Public methods
136
//
137

138     /*
139     * Register a type of grammar to make it preparsable. If
140     * the second parameter is null, the parser will use its built-in
141     * facilities for that grammar type.
142     * This should be called by the application immediately
143     * after creating this object and before initializing any properties/features.
144     * @param type URI identifying the type of the grammar
145     * @param loader an object capable of preparsing that type; null if the ppreparser should use built-in knowledge.
146     * @return true if successful; false if no built-in knowledge of
147     * the type or if unable to instantiate the string we know about
148     */

149     public boolean registerPreparser(String JavaDoc grammarType, XMLGrammarLoader loader) {
150         if(loader == null) { // none specified!
151
if(KNOWN_LOADERS.containsKey(grammarType)) {
152                 // got one; just instantiate it...
153
String JavaDoc loaderName = (String JavaDoc)KNOWN_LOADERS.get(grammarType);
154                 try {
155                     ClassLoader JavaDoc cl = ObjectFactory.findClassLoader();
156                     XMLGrammarLoader gl = (XMLGrammarLoader)(ObjectFactory.newInstance(loaderName, cl, true));
157                     fLoaders.put(grammarType, gl);
158                 } catch (Exception JavaDoc e) {
159                     return false;
160                 }
161                 return true;
162             }
163             return false;
164         }
165         // were given one
166
fLoaders.put(grammarType, loader);
167         return true;
168     } // registerPreparser(String, XMLGrammarLoader): boolean
169

170     /**
171      * Parse a grammar from a location identified by an
172      * XMLInputSource.
173      * This method also adds this grammar to the XMLGrammarPool
174      *
175      * @param type The type of the grammar to be constructed
176      * @param is The XMLInputSource containing this grammar's
177      * information
178      * <strong>If a URI is included in the systemId field, the parser will not expand this URI or make it
179      * available to the EntityResolver</strong>
180      * @return The newly created <code>Grammar</code>.
181      * @exception XNIException thrown on an error in grammar
182      * construction
183      * @exception IOException thrown if an error is encountered
184      * in reading the file
185      */

186     public Grammar preparseGrammar(String JavaDoc type, XMLInputSource
187                 is) throws XNIException, IOException JavaDoc {
188         if(fLoaders.containsKey(type)) {
189             XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type);
190             // make sure gl's been set up with all the "basic" properties:
191
gl.setProperty(SYMBOL_TABLE, fSymbolTable);
192             gl.setProperty(ENTITY_RESOLVER, fEntityResolver);
193             gl.setProperty(ERROR_REPORTER, fErrorReporter);
194             // potentially, not all will support this one...
195
if(fGrammarPool != null) {
196                 try {
197                     gl.setProperty(GRAMMAR_POOL, fGrammarPool);
198                 } catch(Exception JavaDoc e) {
199                     // too bad...
200
}
201             }
202             return gl.loadGrammar(is);
203         }
204         return null;
205     } // preparseGrammar(String, XMLInputSource): Grammar
206

207     /**
208      * Set the locale to use for messages.
209      *
210      * @param locale The locale object to use for localization of messages.
211      *
212      * @exception XNIException Thrown if the parser does not support the
213      * specified locale.
214      */

215     public void setLocale(Locale JavaDoc locale) {
216         fLocale = locale;
217     } // setLocale(Locale)
218

219     /** Return the Locale the XMLGrammarLoader is using. */
220     public Locale JavaDoc getLocale() {
221         return fLocale;
222     } // getLocale(): Locale
223

224
225     /**
226      * Sets the error handler.
227      *
228      * @param errorHandler The error handler.
229      */

230     public void setErrorHandler(XMLErrorHandler errorHandler) {
231         fErrorReporter.setProperty(ERROR_HANDLER, errorHandler);
232     } // setErrorHandler(XMLErrorHandler)
233

234     /** Returns the registered error handler. */
235     public XMLErrorHandler getErrorHandler() {
236         return fErrorReporter.getErrorHandler();
237     } // getErrorHandler(): XMLErrorHandler
238

239     /**
240      * Sets the entity resolver.
241      *
242      * @param entityResolver The new entity resolver.
243      */

244     public void setEntityResolver(XMLEntityResolver entityResolver) {
245         fEntityResolver = entityResolver;
246     } // setEntityResolver(XMLEntityResolver)
247

248     /** Returns the registered entity resolver. */
249     public XMLEntityResolver getEntityResolver() {
250         return fEntityResolver;
251     } // getEntityResolver(): XMLEntityResolver
252

253     /**
254      * Sets the grammar pool.
255      *
256      * @param grammarPool The new grammar pool.
257      */

258     public void setGrammarPool(XMLGrammarPool grammarPool) {
259         fGrammarPool = grammarPool;
260     } // setGrammarPool(XMLGrammarPool)
261

262     /** Returns the registered grammar pool. */
263     public XMLGrammarPool getGrammarPool() {
264         return fGrammarPool;
265     } // getGrammarPool(): XMLGrammarPool
266

267     // it's possible the application may want access to a certain loader to do
268
// some custom work.
269
public XMLGrammarLoader getLoader(String JavaDoc type) {
270         return (XMLGrammarLoader)fLoaders.get(type);
271     } // getLoader(String): XMLGrammarLoader
272

273     // set a feature. This method tries to set it on all
274
// registered loaders; it eats any resulting exceptions. If
275
// an app needs to know if a particular feature is supported
276
// by a grammar loader of a particular type, it will have
277
// to retrieve that loader and use the loader's setFeature method.
278
public void setFeature(String JavaDoc featureId, boolean value) {
279         Enumeration JavaDoc loaders = fLoaders.elements();
280         while(loaders.hasMoreElements()){
281             XMLGrammarLoader gl = (XMLGrammarLoader)loaders.nextElement();
282             try {
283                 gl.setFeature(featureId, value);
284             } catch(Exception JavaDoc e) {
285                 // eat it up...
286
}
287         }
288         // since our error reporter is a property we set later,
289
// make sure features it understands are also set.
290
if(featureId.equals(CONTINUE_AFTER_FATAL_ERROR)) {
291             fErrorReporter.setFeature(CONTINUE_AFTER_FATAL_ERROR, value);
292         }
293     } //setFeature(String, boolean)
294

295     // set a property. This method tries to set it on all
296
// registered loaders; it eats any resulting exceptions. If
297
// an app needs to know if a particular property is supported
298
// by a grammar loader of a particular type, it will have
299
// to retrieve that loader and use the loader's setProperty method.
300
// <p> <strong>An application should use the explicit method
301
// in this class to set "standard" properties like error handler etc.</strong>
302
public void setProperty(String JavaDoc propId, Object JavaDoc value) {
303         Enumeration JavaDoc loaders = fLoaders.elements();
304         while(loaders.hasMoreElements()){
305             XMLGrammarLoader gl = (XMLGrammarLoader)loaders.nextElement();
306             try {
307                 gl.setProperty(propId, value);
308             } catch(Exception JavaDoc e) {
309                 // eat it up...
310
}
311         }
312     } //setProperty(String, Object)
313

314     // get status of feature in a particular loader. This
315
// catches no exceptions--including NPE's--so the application had
316
// better make sure the loader exists and knows about this feature.
317
// @param type type of grammar to look for the feature in.
318
// @param featureId the feature string to query.
319
// @return the value of the feature.
320
public boolean getFeature(String JavaDoc type, String JavaDoc featureId) {
321         XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type);
322         return gl.getFeature(featureId);
323     } // getFeature (String, String): boolean
324

325     // get status of property in a particular loader. This
326
// catches no exceptions--including NPE's--so the application had
327
// better make sure the loader exists and knows about this property.
328
// <strong>For standard properties--that will be supported
329
// by all loaders--the specific methods should be queried!</strong>
330
// @param type type of grammar to look for the property in.
331
// @param propertyId the property string to query.
332
// @return the value of the property.
333
public Object JavaDoc getProperty(String JavaDoc type, String JavaDoc propertyId) {
334         XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type);
335         return gl.getProperty(propertyId);
336     } // getProperty(String, String): Object
337
} // class XMLGrammarPreparser
338
Popular Tags