KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > parsers > XMLGrammarPreparser


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001, 2002 The Apache Software Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.parsers;
59
60 import java.io.IOException JavaDoc;
61 import java.util.Enumeration JavaDoc;
62 import java.util.Hashtable JavaDoc;
63 import java.util.Locale JavaDoc;
64
65 import com.sun.org.apache.xerces.internal.impl.Constants;
66 import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
67 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
68 import com.sun.org.apache.xerces.internal.util.SymbolTable;
69 import com.sun.org.apache.xerces.internal.xni.XNIException;
70 import com.sun.org.apache.xerces.internal.xni.grammars.Grammar;
71 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
72 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarLoader;
73 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
74 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
75 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler;
76 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
77
78 /**
79  * <p> This class provides an easy way for a user to preparse grammars
80  * of various types. By default, it knows how to preparse external
81  * DTD's and schemas; it provides an easy way for user applications to
82  * register classes that know how to parse additional grammar types.
83  * By default, it does no grammar caching; but it provides ways for
84  * user applications to do so.
85  *
86  * @author Neil Graham, IBM
87  *
88  * @version $Id: XMLGrammarPreparser.java,v 1.8 2004/02/17 07:14:49 neeraj Exp $
89  */

90 public class XMLGrammarPreparser {
91
92     //
93
// Constants
94
//
95

96     // feature: continue-after-fatal-error
97
private final static String JavaDoc CONTINUE_AFTER_FATAL_ERROR =
98         Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE;
99
100     /** Property identifier: symbol table. */
101     protected static final String JavaDoc SYMBOL_TABLE =
102         Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
103
104     /** Property identifier: error reporter. */
105     protected static final String JavaDoc ERROR_REPORTER =
106         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
107
108     /** Property identifier: error handler. */
109     protected static final String JavaDoc ERROR_HANDLER =
110         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY;
111
112     /** Property identifier: entity resolver. */
113     protected static final String JavaDoc ENTITY_RESOLVER =
114         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY;
115
116     /** Property identifier: grammar pool . */
117     protected static final String JavaDoc GRAMMAR_POOL =
118         Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
119
120     // the "built-in" grammar loaders
121
private static final Hashtable JavaDoc KNOWN_LOADERS = new Hashtable JavaDoc();
122
123     static {
124         KNOWN_LOADERS.put(XMLGrammarDescription.XML_SCHEMA,
125             "com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader");
126         KNOWN_LOADERS.put(XMLGrammarDescription.XML_DTD,
127             "com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDLoader");
128     }
129
130     /** Recognized properties. */
131     private static final String JavaDoc[] RECOGNIZED_PROPERTIES = {
132         SYMBOL_TABLE,
133         ERROR_REPORTER,
134         ERROR_HANDLER,
135         ENTITY_RESOLVER,
136         GRAMMAR_POOL,
137     };
138
139     // Data
140
protected SymbolTable fSymbolTable;
141     protected XMLErrorReporter fErrorReporter;
142     protected XMLEntityResolver fEntityResolver;
143     protected XMLGrammarPool fGrammarPool;
144
145     protected Locale JavaDoc fLocale;
146
147     // Hashtable holding our loaders
148
private Hashtable JavaDoc fLoaders;
149
150     //
151
// Constructors
152
//
153

154     /** Default constructor. */
155     public XMLGrammarPreparser() {
156         this(new SymbolTable());
157     } // <init>()
158

159     /**
160      * Constructs a preparser using the specified symbol table.
161      *
162      * @param symbolTable The symbol table to use.
163      */

164     public XMLGrammarPreparser (SymbolTable symbolTable) {
165         fSymbolTable = symbolTable;
166
167         fLoaders = new Hashtable JavaDoc();
168         setLocale(Locale.getDefault());
169         fErrorReporter = new XMLErrorReporter();
170         fErrorReporter.setLocale(fLocale);
171         fEntityResolver = new XMLEntityManager();
172         // those are all the basic properties...
173
} // <init>(SymbolTable)
174

175     //
176
// Public methods
177
//
178

179     /*
180     * Register a type of grammar to make it preparsable. If
181     * the second parameter is null, the parser will use its built-in
182     * facilities for that grammar type.
183     * This should be called by the application immediately
184     * after creating this object and before initializing any properties/features.
185     * @param type URI identifying the type of the grammar
186     * @param loader an object capable of preparsing that type; null if the ppreparser should use built-in knowledge.
187     * @return true if successful; false if no built-in knowledge of
188     * the type or if unable to instantiate the string we know about
189     */

190     public boolean registerPreparser(String JavaDoc grammarType, XMLGrammarLoader loader) {
191         if(loader == null) { // none specified!
192
if(KNOWN_LOADERS.containsKey(grammarType)) {
193                 // got one; just instantiate it...
194
String JavaDoc loaderName = (String JavaDoc)KNOWN_LOADERS.get(grammarType);
195                 try {
196                     ClassLoader JavaDoc cl = ObjectFactory.findClassLoader();
197                     XMLGrammarLoader gl = (XMLGrammarLoader)(ObjectFactory.newInstance(loaderName, cl, true));
198                     fLoaders.put(grammarType, gl);
199                 } catch (Exception JavaDoc e) {
200                     return false;
201                 }
202                 return true;
203             }
204             return false;
205         }
206         // were given one
207
fLoaders.put(grammarType, loader);
208         return true;
209     } // registerPreparser(String, XMLGrammarLoader): boolean
210

211     /**
212      * Parse a grammar from a location identified by an
213      * XMLInputSource.
214      * This method also adds this grammar to the XMLGrammarPool
215      *
216      * @param type The type of the grammar to be constructed
217      * @param source The XMLInputSource containing this grammar's
218      * information
219      * <strong>If a URI is included in the systemId field, the parser will not expand this URI or make it
220      * available to the EntityResolver</strong>
221      * @return The newly created <code>Grammar</code>.
222      * @exception XNIException thrown on an error in grammar
223      * construction
224      * @exception IOException thrown if an error is encountered
225      * in reading the file
226      */

227     public Grammar preparseGrammar(String JavaDoc type, XMLInputSource
228                 is) throws XNIException, IOException JavaDoc {
229         if(fLoaders.containsKey(type)) {
230             XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type);
231             // make sure gl's been set up with all the "basic" properties:
232
gl.setProperty(SYMBOL_TABLE, fSymbolTable);
233             gl.setProperty(ENTITY_RESOLVER, fEntityResolver);
234             gl.setProperty(ERROR_REPORTER, fErrorReporter);
235             // potentially, not all will support this one...
236
if(fGrammarPool != null) {
237                 try {
238                     gl.setProperty(GRAMMAR_POOL, fGrammarPool);
239                 } catch(Exception JavaDoc e) {
240                     // too bad...
241
}
242             }
243             return gl.loadGrammar(is);
244         }
245         return null;
246     } // preparseGrammar(String, XMLInputSource): Grammar
247

248     /**
249      * Set the locale to use for messages.
250      *
251      * @param locale The locale object to use for localization of messages.
252      *
253      * @exception XNIException Thrown if the parser does not support the
254      * specified locale.
255      */

256     public void setLocale(Locale JavaDoc locale) {
257         fLocale = locale;
258     } // setLocale(Locale)
259

260     /** Return the Locale the XMLGrammarLoader is using. */
261     public Locale JavaDoc getLocale() {
262         return fLocale;
263     } // getLocale(): Locale
264

265
266     /**
267      * Sets the error handler.
268      *
269      * @param errorHandler The error handler.
270      */

271     public void setErrorHandler(XMLErrorHandler errorHandler) {
272         fErrorReporter.setProperty(ERROR_HANDLER, errorHandler);
273     } // setErrorHandler(XMLErrorHandler)
274

275     /** Returns the registered error handler. */
276     public XMLErrorHandler getErrorHandler() {
277         return fErrorReporter.getErrorHandler();
278     } // getErrorHandler(): XMLErrorHandler
279

280     /**
281      * Sets the entity resolver.
282      *
283      * @param entityResolver The new entity resolver.
284      */

285     public void setEntityResolver(XMLEntityResolver entityResolver) {
286         fEntityResolver = entityResolver;
287     } // setEntityResolver(XMLEntityResolver)
288

289     /** Returns the registered entity resolver. */
290     public XMLEntityResolver getEntityResolver() {
291         return fEntityResolver;
292     } // getEntityResolver(): XMLEntityResolver
293

294     /**
295      * Sets the grammar pool.
296      *
297      * @param grammarPool The new grammar pool.
298      */

299     public void setGrammarPool(XMLGrammarPool grammarPool) {
300         fGrammarPool = grammarPool;
301     } // setGrammarPool(XMLGrammarPool)
302

303     /** Returns the registered grammar pool. */
304     public XMLGrammarPool getGrammarPool() {
305         return fGrammarPool;
306     } // getGrammarPool(): XMLGrammarPool
307

308     // it's possible the application may want access to a certain loader to do
309
// some custom work.
310
public XMLGrammarLoader getLoader(String JavaDoc type) {
311         return (XMLGrammarLoader)fLoaders.get(type);
312     } // getLoader(String): XMLGrammarLoader
313

314     // set a feature. This method tries to set it on all
315
// registered loaders; it eats any resulting exceptions. If
316
// an app needs to know if a particular feature is supported
317
// by a grammar loader of a particular type, it will have
318
// to retrieve that loader and use the loader's setFeature method.
319
public void setFeature(String JavaDoc featureId, boolean value) {
320         Enumeration JavaDoc loaders = fLoaders.elements();
321         while(loaders.hasMoreElements()){
322             XMLGrammarLoader gl = (XMLGrammarLoader)loaders.nextElement();
323             try {
324                 gl.setFeature(featureId, value);
325             } catch(Exception JavaDoc e) {
326                 // eat it up...
327
}
328         }
329         // since our error reporter is a property we set later,
330
// make sure features it understands are also set.
331
if(featureId.equals(CONTINUE_AFTER_FATAL_ERROR)) {
332             fErrorReporter.setFeature(CONTINUE_AFTER_FATAL_ERROR, value);
333         }
334     } //setFeature(String, boolean)
335

336     // set a property. This method tries to set it on all
337
// registered loaders; it eats any resulting exceptions. If
338
// an app needs to know if a particular property is supported
339
// by a grammar loader of a particular type, it will have
340
// to retrieve that loader and use the loader's setProperty method.
341
// <p> <strong>An application should use the explicit method
342
// in this class to set "standard" properties like error handler etc.</strong>
343
public void setProperty(String JavaDoc propId, Object JavaDoc value) {
344         Enumeration JavaDoc loaders = fLoaders.elements();
345         while(loaders.hasMoreElements()){
346             XMLGrammarLoader gl = (XMLGrammarLoader)loaders.nextElement();
347             try {
348                 gl.setProperty(propId, value);
349             } catch(Exception JavaDoc e) {
350                 // eat it up...
351
}
352         }
353     } //setProperty(String, Object)
354

355     // get status of feature in a particular loader. This
356
// catches no exceptions--including NPE's--so the application had
357
// better make sure the loader exists and knows about this feature.
358
// @param type type of grammar to look for the feature in.
359
// @param featureId the feature string to query.
360
// @return the value of the feature.
361
public boolean getFeature(String JavaDoc type, String JavaDoc featureId) {
362         XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type);
363         return gl.getFeature(featureId);
364     } // getFeature (String, String): boolean
365

366     // get status of property in a particular loader. This
367
// catches no exceptions--including NPE's--so the application had
368
// better make sure the loader exists and knows about this property.
369
// <strong>For standard properties--that will be supported
370
// by all loaders--the specific methods should be queried!</strong>
371
// @param type type of grammar to look for the property in.
372
// @param propertyId the property string to query.
373
// @return the value of the property.
374
public Object JavaDoc getProperty(String JavaDoc type, String JavaDoc propertyId) {
375         XMLGrammarLoader gl = (XMLGrammarLoader)fLoaders.get(type);
376         return gl.getProperty(propertyId);
377     } // getProperty(String, String): Object
378
} // class XMLGrammarPreparser
379
Popular Tags