KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
6  * 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.util.Vector JavaDoc;
61
62 import com.sun.org.apache.xerces.internal.dom.ASModelImpl;
63 import com.sun.org.apache.xerces.internal.dom3.as.ASModel;
64 import com.sun.org.apache.xerces.internal.dom3.as.DOMASBuilder;
65 import com.sun.org.apache.xerces.internal.dom3.as.DOMASException;
66 import com.sun.org.apache.xerces.internal.impl.Constants;
67 import com.sun.org.apache.xerces.internal.impl.xs.SchemaGrammar;
68 import com.sun.org.apache.xerces.internal.impl.xs.XSGrammarBucket;
69 import com.sun.org.apache.xerces.internal.util.SymbolTable;
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.XMLGrammarPool;
74 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
75 import org.w3c.dom.ls.LSInput JavaDoc;
76
77 /**
78  * This is Abstract Schema DOM Builder class. It extends the DOMParserImpl
79  * class. Provides support for preparsing schemas.
80  *
81  * @deprecated
82  * @author Pavani Mukthipudi, Sun Microsystems Inc.
83  * @author Neil Graham, IBM
84  * @version $Id: DOMASBuilderImpl.java,v 1.24 2003/11/17 13:48:41 venu Exp $
85  *
86  */

87
88 public class DOMASBuilderImpl
89     extends DOMParserImpl implements DOMASBuilder {
90
91     //
92
// Constants
93
//
94

95     // Feature ids
96

97     protected static final String JavaDoc SCHEMA_FULL_CHECKING =
98         Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING;
99
100     // Property ids
101

102     protected static final String JavaDoc ERROR_REPORTER =
103         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
104
105     protected static final String JavaDoc SYMBOL_TABLE =
106         Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
107
108     protected static final String JavaDoc ENTITY_MANAGER =
109         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
110
111
112     //
113
// Data
114
//
115

116     protected XSGrammarBucket fGrammarBucket;
117
118     protected ASModelImpl fAbstractSchema;
119
120     //
121
// Constructors
122
//
123

124     /**
125      * Constructs a DOM Builder using the dtd/xml schema parser configuration.
126      */

127     public DOMASBuilderImpl() {
128         super(new XMLGrammarCachingConfiguration());
129     } // <init>
130

131     /**
132      * Constructs a DOM Builder using the specified parser configuration.
133      * We must demand that the configuration extend XMLGrammarCachingConfiguration to make
134      * sure all relevant methods/features are available.
135      */

136     public DOMASBuilderImpl(XMLGrammarCachingConfiguration config) {
137         super(config);
138     } // <init>(XMLParserConfiguration)
139

140     /**
141      * Constructs a DOM Builder using the specified symbol table.
142      */

143     public DOMASBuilderImpl(SymbolTable symbolTable) {
144         super(new XMLGrammarCachingConfiguration(symbolTable));
145     } // <init>(SymbolTable)
146

147
148     /**
149      * Constructs a DOM Builder using the specified symbol table and
150      * grammar pool.
151      * The grammarPool implementation should extent the default
152      * implementation; otherwise, correct functioning of this class may
153      * not occur.
154      */

155     public DOMASBuilderImpl(SymbolTable symbolTable, XMLGrammarPool grammarPool) {
156         super(new XMLGrammarCachingConfiguration(symbolTable, grammarPool));
157     }
158
159     //
160
// DOMASBuilder methods
161
//
162

163     /**
164      * Associate an <code>ASModel</code> with a document instance. This
165      * <code>ASModel</code> will be used by the "
166      * <code>validate-if-schema</code>" and "
167      * <code>datatype-normalization</code>" options during the load of a new
168      * <code>Document</code>.
169      */

170     public ASModel getAbstractSchema() {
171         return fAbstractSchema;
172     }
173
174     /**
175      * Associate an <code>ASModel</code> with a document instance. This
176      * <code>ASModel</code> will be used by the "
177      * <code>validate-if-schema</code>" and "
178      * <code>datatype-normalization</code>" options during the load of a new
179      * <code>Document</code>.
180      */

181     public void setAbstractSchema(ASModel abstractSchema) {
182
183         // since the ASModel associated with this object is an attribute
184
// according to the DOM IDL, we must obliterate anything
185
// that was set before, rather than adding to it.
186
// REVISIT: so shouldn't we attempt to clear the
187
// grammarPool before adding stuff to it? - NG
188
fAbstractSchema = (ASModelImpl)abstractSchema;
189
190         // make sure the GrammarPool is properly initialized.
191
XMLGrammarPool grammarPool = (XMLGrammarPool)fConfiguration.getProperty(StandardParserConfiguration.XMLGRAMMAR_POOL);
192         // if there is no grammar pool, create one
193
// REVISIT: ASBuilder should always create one.
194
if (grammarPool == null) {
195             // something's not right in this situation...
196
grammarPool = new XMLGrammarPoolImpl();
197             fConfiguration.setProperty(StandardParserConfiguration.XMLGRAMMAR_POOL,
198                                        grammarPool);
199         }
200         if (fAbstractSchema != null) {
201             initGrammarPool(fAbstractSchema, grammarPool);
202         }
203     }
204
205     /**
206      * Parse a Abstract Schema from a location identified by an URI.
207      *
208      * @param uri The location of the Abstract Schema to be read.
209      * @return The newly created <code>Abstract Schema</code>.
210      * @exception DOMASException
211      * Exceptions raised by <code>parseASURI()</code> originate with the
212      * installed ErrorHandler, and thus depend on the implementation of
213      * the <code>DOMErrorHandler</code> interfaces. The default error
214      * handlers will raise a <code>DOMASException</code> if any form of
215      * Abstract Schema inconsistencies or warning occurs during the parse,
216      * but application defined errorHandlers are not required to do so.
217      * <br> WRONG_MIME_TYPE_ERR: Raised when <code>mimeTypeCheck</code> is
218      * <code>true</code> and the inputsource has an incorrect MIME Type.
219      * See attribute <code>mimeTypeCheck</code>.
220      * @exception DOMSystemException
221      * Exceptions raised by <code>parseURI()</code> originate with the
222      * installed ErrorHandler, and thus depend on the implementation of
223      * the <code>DOMErrorHandler</code> interfaces. The default error
224      * handlers will raise a DOMSystemException if any form I/O or other
225      * system error occurs during the parse, but application defined error
226      * handlers are not required to do so.
227      */

228     public ASModel parseASURI(String JavaDoc uri)
229                               throws DOMASException, Exception JavaDoc {
230         XMLInputSource source = new XMLInputSource(null, uri, null);
231         return parseASInputSource(source);
232     }
233
234     /**
235      * Parse a Abstract Schema from a location identified by an
236      * <code>LSInput</code>.
237      *
238      * @param is The <code>LSInput</code> from which the source
239      * Abstract Schema is to be read.
240      * @return The newly created <code>ASModel</code>.
241      * @exception DOMASException
242      * Exceptions raised by <code>parseASURI()</code> originate with the
243      * installed ErrorHandler, and thus depend on the implementation of
244      * the <code>DOMErrorHandler</code> interfaces. The default error
245      * handlers will raise a <code>DOMASException</code> if any form of
246      * Abstract Schema inconsistencies or warning occurs during the parse,
247      * but application defined errorHandlers are not required to do so.
248      * <br> WRONG_MIME_TYPE_ERR: Raised when <code>mimeTypeCheck</code> is
249      * true and the inputsource has an incorrect MIME Type. See attribute
250      * <code>mimeTypeCheck</code>.
251      * @exception DOMSystemException
252      * Exceptions raised by <code>parseURI()</code> originate with the
253      * installed ErrorHandler, and thus depend on the implementation of
254      * the <code>DOMErrorHandler</code> interfaces. The default error
255      * handlers will raise a DOMSystemException if any form I/O or other
256      * system error occurs during the parse, but application defined error
257      * handlers are not required to do so.
258      */

259     public ASModel parseASInputSource(LSInput JavaDoc is)
260                                       throws DOMASException, Exception JavaDoc {
261                                       
262         // need to wrap the LSInput with an XMLInputSource
263
XMLInputSource xis = this.dom2xmlInputSource(is);
264         try {
265             return parseASInputSource(xis);
266         }
267         catch (XNIException e) {
268             Exception JavaDoc ex = e.getException();
269             throw ex;
270         }
271     }
272
273     ASModel parseASInputSource(XMLInputSource is) throws Exception JavaDoc {
274                                       
275         if (fGrammarBucket == null) {
276             fGrammarBucket = new XSGrammarBucket();
277         }
278
279         initGrammarBucket();
280
281         // actually do the parse:
282
// save some casting
283
XMLGrammarCachingConfiguration gramConfig = (XMLGrammarCachingConfiguration)fConfiguration;
284         // ensure grammarPool doesn't absorb grammars while it's parsing
285
gramConfig.lockGrammarPool();
286         SchemaGrammar grammar = gramConfig.parseXMLSchema(is);
287         gramConfig.unlockGrammarPool();
288
289         ASModelImpl newAsModel = null;
290         if (grammar != null) {
291             newAsModel = new ASModelImpl();
292             fGrammarBucket.putGrammar (grammar, true);
293             addGrammars(newAsModel, fGrammarBucket);
294         }
295         return newAsModel;
296     }
297
298     // put all the grammars we have access to in the GrammarBucket
299
private void initGrammarBucket() {
300         fGrammarBucket.reset();
301         if (fAbstractSchema != null)
302             initGrammarBucketRecurse(fAbstractSchema);
303     }
304     private void initGrammarBucketRecurse(ASModelImpl currModel) {
305         if(currModel.getGrammar() != null) {
306             fGrammarBucket.putGrammar(currModel.getGrammar());
307         }
308         for(int i = 0; i < currModel.getInternalASModels().size(); i++) {
309             ASModelImpl nextModel = (ASModelImpl)(currModel.getInternalASModels().elementAt(i));
310             initGrammarBucketRecurse(nextModel);
311         }
312     }
313
314     private void addGrammars(ASModelImpl model, XSGrammarBucket grammarBucket) {
315         SchemaGrammar [] grammarList = grammarBucket.getGrammars();
316         for(int i=0; i<grammarList.length; i++) {
317             ASModelImpl newModel = new ASModelImpl();
318             newModel.setGrammar(grammarList[i]);
319             model.addASModel(newModel);
320         }
321     } // addGrammars
322

323     private void initGrammarPool(ASModelImpl currModel, XMLGrammarPool grammarPool) {
324         // put all the grammars in fAbstractSchema into the grammar pool.
325
// grammarPool must never be null!
326
Grammar[] grammars = new Grammar[1];
327         if ((grammars[0] = (Grammar)currModel.getGrammar()) != null) {
328             grammarPool.cacheGrammars(grammars[0].getGrammarDescription().getGrammarType(), grammars);
329         }
330         Vector JavaDoc modelStore = currModel.getInternalASModels();
331         for (int i = 0; i < modelStore.size(); i++) {
332             initGrammarPool((ASModelImpl)modelStore.elementAt(i), grammarPool);
333         }
334     }
335 } // class DOMASBuilderImpl
336
Popular Tags