KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2005 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
21 import org.apache.xerces.impl.Constants;
22 import org.apache.xerces.impl.dtd.DTDGrammar;
23 import org.apache.xerces.impl.dtd.XMLDTDLoader;
24 import org.apache.xerces.impl.xs.SchemaGrammar;
25 import org.apache.xerces.impl.xs.XMLSchemaLoader;
26 import org.apache.xerces.impl.xs.XSMessageFormatter;
27 import org.apache.xerces.util.SymbolTable;
28 import org.apache.xerces.util.SynchronizedSymbolTable;
29 import org.apache.xerces.util.XMLGrammarPoolImpl;
30 import org.apache.xerces.xni.XNIException;
31 import org.apache.xerces.xni.grammars.Grammar;
32 import org.apache.xerces.xni.grammars.XMLGrammarDescription;
33 import org.apache.xerces.xni.grammars.XMLGrammarPool;
34 import org.apache.xerces.xni.parser.XMLComponentManager;
35 import org.apache.xerces.xni.parser.XMLConfigurationException;
36 import org.apache.xerces.xni.parser.XMLEntityResolver;
37 import org.apache.xerces.xni.parser.XMLInputSource;
38
39 /**
40  * <p> This configuration provides a generic way of using
41  * Xerces's grammar caching facilities. It extends the
42  * XIncludeAwareParserConfiguration and thus may validate documents
43  * according to XML schemas or DTD's. It also allows the user to
44  * preparse a grammar, and to lock the grammar pool
45  * implementation such that no more grammars will be added.</p>
46  * <p> Using the org.apache.xerces.xni.parser property, an
47  * application may instantiate a Xerces SAX or DOM parser with
48  * this configuration. When invoked in this manner, the default
49  * behaviour will be elicited; to use this configuration's
50  * specific facilities, the user will need to reference it
51  * directly.</p>
52  * <p>
53  * In addition to the features and properties recognized by the base
54  * parser configuration, this class recognizes these additional
55  * features and properties:
56  * <ul>
57  * </ul>
58  *
59  * @author Neil Graham, IBM
60  *
61  * @version $Id: XMLGrammarCachingConfiguration.java,v 1.19 2005/05/04 04:23:46 mrglavas Exp $
62  */

63 public class XMLGrammarCachingConfiguration
64     extends XIncludeAwareParserConfiguration {
65
66     //
67
// Constants
68
//
69

70     // a larg(ish) prime to use for a symbol table to be shared
71
// among
72
// potentially man parsers. Start one as close to 2K (20
73
// times larger than normal) and see what happens...
74
public static final int BIG_PRIME = 2039;
75
76     // the static symbol table to be shared amongst parsers
77
protected static final SynchronizedSymbolTable fStaticSymbolTable =
78             new SynchronizedSymbolTable(BIG_PRIME);
79
80     // the Grammar Pool to be shared similarly
81
protected static final XMLGrammarPoolImpl fStaticGrammarPool =
82             new XMLGrammarPoolImpl();
83
84     // schema full checking constant
85
protected static final String JavaDoc SCHEMA_FULL_CHECKING =
86             Constants.XERCES_FEATURE_PREFIX+Constants.SCHEMA_FULL_CHECKING;
87
88     // Data
89

90     // variables needed for caching schema grammars.
91
protected XMLSchemaLoader fSchemaLoader;
92
93     // the DTD grammar loader
94
protected XMLDTDLoader fDTDLoader;
95
96     //
97
// Constructors
98
//
99

100     /** Default constructor. */
101     public XMLGrammarCachingConfiguration() {
102         this(fStaticSymbolTable, fStaticGrammarPool, null);
103     } // <init>()
104

105     /**
106      * Constructs a parser configuration using the specified symbol table.
107      *
108      * @param symbolTable The symbol table to use.
109      */

110     public XMLGrammarCachingConfiguration(SymbolTable symbolTable) {
111         this(symbolTable, fStaticGrammarPool, null);
112     } // <init>(SymbolTable)
113

114     /**
115      * Constructs a parser configuration using the specified symbol table and
116      * grammar pool.
117      * <p>
118      * <strong>REVISIT:</strong>
119      * Grammar pool will be updated when the new validation engine is
120      * implemented.
121      *
122      * @param symbolTable The symbol table to use.
123      * @param grammarPool The grammar pool to use.
124      */

125     public XMLGrammarCachingConfiguration(SymbolTable symbolTable,
126                                        XMLGrammarPool grammarPool) {
127         this(symbolTable, grammarPool, null);
128     } // <init>(SymbolTable,XMLGrammarPool)
129

130     /**
131      * Constructs a parser configuration using the specified symbol table,
132      * grammar pool, and parent settings.
133      * <p>
134      * <strong>REVISIT:</strong>
135      * Grammar pool will be updated when the new validation engine is
136      * implemented.
137      *
138      * @param symbolTable The symbol table to use.
139      * @param grammarPool The grammar pool to use.
140      * @param parentSettings The parent settings.
141      */

142     public XMLGrammarCachingConfiguration(SymbolTable symbolTable,
143                                        XMLGrammarPool grammarPool,
144                                        XMLComponentManager parentSettings) {
145         super(symbolTable, grammarPool, parentSettings);
146
147         // REVISIT: may need to add some features/properties
148
// specific to this configuration at some point...
149

150         // add default recognized features
151
// set state for default features
152
// add default recognized properties
153
// create and register missing components
154
fSchemaLoader = new XMLSchemaLoader(fSymbolTable);
155         fSchemaLoader.setProperty(XMLGRAMMAR_POOL, fGrammarPool);
156
157         // and set up the DTD loader too:
158
fDTDLoader = new XMLDTDLoader(fSymbolTable, fGrammarPool);
159     } // <init>(SymbolTable,XMLGrammarPool, XMLComponentManager)
160

161     //
162
// Public methods
163
//
164

165     /*
166      * lock the XMLGrammarPoolImpl object so that it does not
167      * accept any more grammars from the validators.
168      */

169     public void lockGrammarPool() {
170         fGrammarPool.lockPool();
171     } // lockGrammarPool()
172

173     /*
174      * clear the XMLGrammarPoolImpl object so that it does not
175      * contain any more grammars.
176      */

177     public void clearGrammarPool() {
178         fGrammarPool.clear();
179     } // clearGrammarPool()
180

181     /*
182      * unlock the XMLGrammarPoolImpl object so that it
183      * accepts more grammars from the validators.
184      */

185     public void unlockGrammarPool() {
186         fGrammarPool.unlockPool();
187     } // unlockGrammarPool()
188

189     /**
190      * Parse a grammar from a location identified by an URI.
191      * This method also adds this grammar to the XMLGrammarPool
192      *
193      * @param type The type of the grammar to be constructed
194      * @param uri The location of the grammar to be constructed.
195      * <strong>The parser will not expand this URI or make it
196      * available to the EntityResolver</strong>
197      * @return The newly created <code>Grammar</code>.
198      * @exception XNIException thrown on an error in grammar
199      * construction
200      * @exception IOException thrown if an error is encountered
201      * in reading the file
202      */

203     public Grammar parseGrammar(String JavaDoc type, String JavaDoc uri)
204                               throws XNIException, IOException JavaDoc {
205         XMLInputSource source = new XMLInputSource(null, uri, null);
206         return parseGrammar(type, source);
207
208     }
209
210     /**
211      * Parse a grammar from a location identified by an
212      * XMLInputSource.
213      * This method also adds this grammar to the XMLGrammarPool
214      *
215      * @param type The type of the grammar to be constructed
216      * @param is The XMLInputSource containing this grammar's
217      * information
218      * <strong>If a URI is included in the systemId field, the parser will not expand this URI or make it
219      * available to the EntityResolver</strong>
220      * @return The newly created <code>Grammar</code>.
221      * @exception XNIException thrown on an error in grammar
222      * construction
223      * @exception IOException thrown if an error is encountered
224      * in reading the file
225      */

226     public Grammar parseGrammar(String JavaDoc type, XMLInputSource
227                 is) throws XNIException, IOException JavaDoc {
228         if(type.equals(XMLGrammarDescription.XML_SCHEMA)) {
229             // by default, make all XMLGrammarPoolImpl's schema grammars available to fSchemaHandler
230
return parseXMLSchema(is);
231         } else if(type.equals(XMLGrammarDescription.XML_DTD)) {
232             return parseDTD(is);
233         }
234         // don't know this grammar...
235
return null;
236     } // parseGrammar(String, XMLInputSource): Grammar
237

238     //
239
// Protected methods
240
//
241

242     // features and properties
243

244     /**
245      * Check a feature. If feature is known and supported, this method simply
246      * returns. Otherwise, the appropriate exception is thrown.
247      *
248      * @param featureId The unique identifier (URI) of the feature.
249      *
250      * @throws XMLConfigurationException Thrown for configuration error.
251      * In general, components should
252      * only throw this exception if
253      * it is <strong>really</strong>
254      * a critical error.
255      */

256     protected void checkFeature(String JavaDoc featureId)
257         throws XMLConfigurationException {
258
259         super.checkFeature(featureId);
260
261     } // checkFeature(String)
262

263     /**
264      * Check a property. If the property is known and supported, this method
265      * simply returns. Otherwise, the appropriate exception is thrown.
266      *
267      * @param propertyId The unique identifier (URI) of the property
268      * being set.
269      *
270      * @throws XMLConfigurationException Thrown for configuration error.
271      * In general, components should
272      * only throw this exception if
273      * it is <strong>really</strong>
274      * a critical error.
275      */

276     protected void checkProperty(String JavaDoc propertyId)
277         throws XMLConfigurationException {
278         super.checkProperty(propertyId);
279
280     } // checkProperty(String)
281

282     // package-protected methods
283

284     /* This method parses an XML Schema document.
285      * It requires a GrammarBucket parameter so that DOMASBuilder can
286      * extract the info it needs.
287      * Therefore, bucket must not be null!
288      */

289     SchemaGrammar parseXMLSchema(XMLInputSource is)
290                 throws IOException JavaDoc {
291         XMLEntityResolver resolver = getEntityResolver();
292         if(resolver != null) {
293             fSchemaLoader.setEntityResolver(resolver);
294         }
295         if (fErrorReporter.getMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN) == null) {
296             fErrorReporter.putMessageFormatter(XSMessageFormatter.SCHEMA_DOMAIN, new XSMessageFormatter());
297         }
298         fSchemaLoader.setProperty(ERROR_REPORTER, fErrorReporter);
299
300         String JavaDoc propPrefix = Constants.XERCES_PROPERTY_PREFIX;
301         String JavaDoc propName = propPrefix + Constants.SCHEMA_LOCATION;
302         fSchemaLoader.setProperty(propName, getProperty(propName));
303         propName = propPrefix + Constants.SCHEMA_NONS_LOCATION;
304         fSchemaLoader.setProperty(propName, getProperty(propName));
305         propName = Constants.JAXP_PROPERTY_PREFIX+Constants.SCHEMA_SOURCE;
306         fSchemaLoader.setProperty(propName, getProperty(propName));
307         fSchemaLoader.setFeature(SCHEMA_FULL_CHECKING, getFeature(SCHEMA_FULL_CHECKING));
308
309         // Should check whether the grammar with this namespace is already in
310
// the grammar resolver. But since we don't know the target namespace
311
// of the document here, we leave such check to XSDHandler
312
SchemaGrammar grammar = (SchemaGrammar)fSchemaLoader.loadGrammar(is);
313         // by default, hand it off to the grammar pool
314
if (grammar != null) {
315             fGrammarPool.cacheGrammars(XMLGrammarDescription.XML_SCHEMA,
316                                       new Grammar[]{grammar});
317         }
318         
319         return grammar;
320
321     } // parseXMLSchema(XMLInputSource) : SchemaGrammar
322

323     /* This method parses an external DTD entity.
324      */

325     DTDGrammar parseDTD(XMLInputSource is)
326                 throws IOException JavaDoc {
327         XMLEntityResolver resolver = getEntityResolver();
328         if(resolver != null) {
329             fDTDLoader.setEntityResolver(resolver);
330         }
331         fDTDLoader.setProperty(ERROR_REPORTER, fErrorReporter);
332
333         // Should check whether the grammar with this namespace is already in
334
// the grammar resolver. But since we don't know the target namespace
335
// of the document here, we leave such check to the application...
336
DTDGrammar grammar = (DTDGrammar)fDTDLoader.loadGrammar(is);
337         // by default, hand it off to the grammar pool
338
if (grammar != null) {
339             fGrammarPool.cacheGrammars(XMLGrammarDescription.XML_DTD,
340                                       new Grammar[]{grammar});
341         }
342         
343         return grammar;
344
345     } // parseXMLDTD(XMLInputSource) : DTDGrammar
346

347
348 } // class XMLGrammarCachingConfiguration
349
Popular Tags