KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2004 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
62 import com.sun.org.apache.xerces.internal.impl.Constants;
63 import com.sun.org.apache.xerces.internal.impl.dtd.DTDGrammar;
64 import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDLoader;
65 import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar;
66 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader;
67 import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter;
68 import com.sun.org.apache.xerces.internal.util.SymbolTable;
69 import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
70 import com.sun.org.apache.xerces.internal.util.XMLGrammarPoolImpl;
71 import com.sun.org.apache.xerces.internal.xni.XNIException;
72 import com.sun.org.apache.xerces.internal.xni.grammars.Grammar;
73 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
74 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
75 import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
76 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
77 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver;
78 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
79
80 /**
81  * <p> This configuration provides a generic way of using
82  * Xerces's grammar caching facilities. It extends the
83  * XML11Configuration and thus may validate documents
84  * according to XML schemas or DTD's. It also allows the user to
85  * preparse a grammar, and to lock the grammar pool
86  * implementation such that no more grammars will be added.</p>
87  * <p> Using the com.sun.org.apache.xerces.internal.xni.parser property, an
88  * application may instantiate a Xerces SAX or DOM parser with
89  * this configuration. When invoked in this manner, the default
90  * behaviour will be elicited; to use this configuration's
91  * specific facilities, the user will need to reference it
92  * directly.</p>
93  * <p>
94  * In addition to the features and properties recognized by the base
95  * parser configuration, this class recognizes these additional
96  * features and properties:
97  * <ul>
98  * </ul>
99  *
100  * @author Neil Graham, IBM
101  *
102  * @version $Id: XMLGrammarCachingConfiguration.java,v 1.15 2004/02/18 22:18:31 mrglavas Exp $
103  */

104 public class XMLGrammarCachingConfiguration
105     extends XML11Configuration {
106
107     //
108
// Constants
109
//
110

111     // a larg(ish) prime to use for a symbol table to be shared
112
// among
113
// potentially man parsers. Start one as close to 2K (20
114
// times larger than normal) and see what happens...
115
public static final int BIG_PRIME = 2039;
116
117     // the static symbol table to be shared amongst parsers
118
protected static final SynchronizedSymbolTable fStaticSymbolTable =
119             new SynchronizedSymbolTable(BIG_PRIME);
120
121     // the Grammar Pool to be shared similarly
122
protected static final XMLGrammarPoolImpl fStaticGrammarPool =
123             new XMLGrammarPoolImpl();
124
125     // schema full checking constant
126
protected static final String JavaDoc SCHEMA_FULL_CHECKING =
127             Constants.XERCES_FEATURE_PREFIX+Constants.SCHEMA_FULL_CHECKING;
128
129     // Data
130

131     // variables needed for caching schema grammars.
132
protected XMLSchemaLoader fSchemaLoader;
133
134     // the DTD grammar loader
135
protected XMLDTDLoader fDTDLoader;
136
137     //
138
// Constructors
139
//
140

141     /** Default constructor. */
142     public XMLGrammarCachingConfiguration() {
143         this(fStaticSymbolTable, fStaticGrammarPool, null);
144     } // <init>()
145

146     /**
147      * Constructs a parser configuration using the specified symbol table.
148      *
149      * @param symbolTable The symbol table to use.
150      */

151     public XMLGrammarCachingConfiguration(SymbolTable symbolTable) {
152         this(symbolTable, fStaticGrammarPool, null);
153     } // <init>(SymbolTable)
154

155     /**
156      * Constructs a parser configuration using the specified symbol table and
157      * grammar pool.
158      * <p>
159      * <strong>REVISIT:</strong>
160      * Grammar pool will be updated when the new validation engine is
161      * implemented.
162      *
163      * @param symbolTable The symbol table to use.
164      * @param grammarPool The grammar pool to use.
165      */

166     public XMLGrammarCachingConfiguration(SymbolTable symbolTable,
167                                        XMLGrammarPool grammarPool) {
168         this(symbolTable, grammarPool, null);
169     } // <init>(SymbolTable,XMLGrammarPool)
170

171     /**
172      * Constructs a parser configuration using the specified symbol table,
173      * grammar pool, and parent settings.
174      * <p>
175      * <strong>REVISIT:</strong>
176      * Grammar pool will be updated when the new validation engine is
177      * implemented.
178      *
179      * @param symbolTable The symbol table to use.
180      * @param grammarPool The grammar pool to use.
181      * @param parentSettings The parent settings.
182      */

183     public XMLGrammarCachingConfiguration(SymbolTable symbolTable,
184                                        XMLGrammarPool grammarPool,
185                                        XMLComponentManager parentSettings) {
186         super(symbolTable, grammarPool, parentSettings);
187
188         // REVISIT: may need to add some features/properties
189
// specific to this configuration at some point...
190

191         // add default recognized features
192
// set state for default features
193
// add default recognized properties
194
// create and register missing components
195
fSchemaLoader = new XMLSchemaLoader(fSymbolTable);
196         fSchemaLoader.setProperty(XMLGRAMMAR_POOL, fGrammarPool);
197
198         // and set up the DTD loader too:
199
fDTDLoader = new XMLDTDLoader(fSymbolTable, fGrammarPool);
200     } // <init>(SymbolTable,XMLGrammarPool, XMLComponentManager)
201

202     //
203
// Public methods
204
//
205

206     /*
207      * lock the XMLGrammarPoolImpl object so that it does not
208      * accept any more grammars from the validators.
209      */

210     public void lockGrammarPool() {
211         fGrammarPool.lockPool();
212     } // lockGrammarPool()
213

214     /*
215      * clear the XMLGrammarPoolImpl object so that it does not
216      * contain any more grammars.
217      */

218     public void clearGrammarPool() {
219         fGrammarPool.clear();
220     } // clearGrammarPool()
221

222     /*
223      * unlock the XMLGrammarPoolImpl object so that it
224      * accepts more grammars from the validators.
225      */

226     public void unlockGrammarPool() {
227         fGrammarPool.unlockPool();
228     } // unlockGrammarPool()
229

230     /**
231      * Parse a grammar from a location identified by an URI.
232      * This method also adds this grammar to the XMLGrammarPool
233      *
234      * @param type The type of the grammar to be constructed
235      * @param uri The location of the grammar to be constructed.
236      * <strong>The parser will not expand this URI or make it
237      * available to the EntityResolver</strong>
238      * @return The newly created <code>Grammar</code>.
239      * @exception XNIException thrown on an error in grammar
240      * construction
241      * @exception IOException thrown if an error is encountered
242      * in reading the file
243      */

244     public Grammar parseGrammar(String JavaDoc type, String JavaDoc uri)
245                               throws XNIException, IOException JavaDoc {
246         XMLInputSource source = new XMLInputSource(null, uri, null);
247         return parseGrammar(type, source);
248
249     }
250
251     /**
252      * Parse a grammar from a location identified by an
253      * XMLInputSource.
254      * This method also adds this grammar to the XMLGrammarPool
255      *
256      * @param type The type of the grammar to be constructed
257      * @param source The XMLInputSource containing this grammar's
258      * information
259      * <strong>If a URI is included in the systemId field, the parser will not expand this URI or make it
260      * available to the EntityResolver</strong>
261      * @return The newly created <code>Grammar</code>.
262      * @exception XNIException thrown on an error in grammar
263      * construction
264      * @exception IOException thrown if an error is encountered
265      * in reading the file
266      */

267     public Grammar parseGrammar(String JavaDoc type, XMLInputSource
268                 is) throws XNIException, IOException JavaDoc {
269         if(type.equals(XMLGrammarDescription.XML_SCHEMA)) {
270             // by default, make all XMLGrammarPoolImpl's schema grammars available to fSchemaHandler
271
return parseXMLSchema(is);
272         } else if(type.equals(XMLGrammarDescription.XML_DTD)) {
273             return parseDTD(is);
274         }
275         // don't know this grammar...
276
return null;
277     } // parseGrammar(String, XMLInputSource): Grammar
278

279     //
280
// Protected methods
281
//
282

283     // features and properties
284

285     /**
286      * Check a feature. If feature is known and supported, this method simply
287      * returns. Otherwise, the appropriate exception is thrown.
288      *
289      * @param featureId The unique identifier (URI) of the feature.
290      *
291      * @throws XMLConfigurationException Thrown for configuration error.
292      * In general, components should
293      * only throw this exception if
294      * it is <strong>really</strong>
295      * a critical error.
296      */

297     protected void checkFeature(String JavaDoc featureId)
298         throws XMLConfigurationException {
299
300         super.checkFeature(featureId);
301
302     } // checkFeature(String)
303

304     /**
305      * Check a property. If the property is known and supported, this method
306      * simply returns. Otherwise, the appropriate exception is thrown.
307      *
308      * @param propertyId The unique identifier (URI) of the property
309      * being set.
310      *
311      * @throws XMLConfigurationException Thrown for configuration error.
312      * In general, components should
313      * only throw this exception if
314      * it is <strong>really</strong>
315      * a critical error.
316      */

317     protected void checkProperty(String JavaDoc propertyId)
318         throws XMLConfigurationException {
319         super.checkProperty(propertyId);
320
321     } // checkProperty(String)
322

323     // package-protected methods
324

325     /* This method parses an XML Schema document.
326      * It requires a GrammarBucket parameter so that DOMASBuilder can
327      * extract the info it needs.
328      * Therefore, bucket must not be null!
329      */

330     SchemaGrammar parseXMLSchema(XMLInputSource is)
331                 throws IOException JavaDoc {
332         XMLEntityResolver resolver = getEntityResolver();
333         if(resolver != null) {
334             fSchemaLoader.setEntityResolver(resolver);
335         }
336         if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
337             fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter());
338         }
339         fSchemaLoader.setProperty(ERROR_REPORTER, fErrorReporter);
340
341         String JavaDoc propPrefix = Constants.XERCES_PROPERTY_PREFIX;
342         String JavaDoc propName = propPrefix + Constants.SCHEMA_LOCATION;
343         fSchemaLoader.setProperty(propName, getProperty(propName));
344         propName = propPrefix + Constants.SCHEMA_NONS_LOCATION;
345         fSchemaLoader.setProperty(propName, getProperty(propName));
346         propName = Constants.JAXP_PROPERTY_PREFIX+Constants.SCHEMA_SOURCE;
347         fSchemaLoader.setProperty(propName, getProperty(propName));
348         fSchemaLoader.setFeature(SCHEMA_FULL_CHECKING, getFeature(SCHEMA_FULL_CHECKING));
349
350         // Should check whether the grammar with this namespace is already in
351
// the grammar resolver. But since we don't know the target namespace
352
// of the document here, we leave such check to XSDHandler
353
SchemaGrammar grammar = (SchemaGrammar)fSchemaLoader.loadGrammar(is);
354         // by default, hand it off to the grammar pool
355
if (grammar != null) {
356             fGrammarPool.cacheGrammars(XMLGrammarDescription.XML_SCHEMA,
357                                       new Grammar[]{grammar});
358         }
359         
360         return grammar;
361
362     } // parseXMLSchema(XMLInputSource) : SchemaGrammar
363

364     /* This method parses an external DTD entity.
365      */

366     DTDGrammar parseDTD(XMLInputSource is)
367                 throws IOException JavaDoc {
368         XMLEntityResolver resolver = getEntityResolver();
369         if(resolver != null) {
370             fDTDLoader.setEntityResolver(resolver);
371         }
372         fDTDLoader.setProperty(ERROR_REPORTER, fErrorReporter);
373
374         // Should check whether the grammar with this namespace is already in
375
// the grammar resolver. But since we don't know the target namespace
376
// of the document here, we leave such check to the application...
377
DTDGrammar grammar = (DTDGrammar)fDTDLoader.loadGrammar(is);
378         // by default, hand it off to the grammar pool
379
if (grammar != null) {
380             fGrammarPool.cacheGrammars(XMLGrammarDescription.XML_DTD,
381                                       new Grammar[]{grammar});
382         }
383         
384         return grammar;
385
386     } // parseXMLDTD(XMLInputSource) : DTDGrammar
387

388
389 } // class XMLGrammarCachingConfiguration
390
Popular Tags