KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2000-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.util.Vector JavaDoc;
20
21 import org.apache.xerces.dom.ASModelImpl;
22 import org.apache.xerces.dom3.as.ASModel;
23 import org.apache.xerces.dom3.as.DOMASBuilder;
24 import org.apache.xerces.dom3.as.DOMASException;
25 import org.apache.xerces.impl.Constants;
26 import org.apache.xerces.impl.xs.SchemaGrammar;
27 import org.apache.xerces.impl.xs.XSGrammarBucket;
28 import org.apache.xerces.util.SymbolTable;
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.XMLGrammarPool;
33 import org.apache.xerces.xni.parser.XMLInputSource;
34 import org.w3c.dom.ls.LSInput JavaDoc;
35
36 /**
37  * This is Abstract Schema DOM Builder class. It extends the DOMParserImpl
38  * class. Provides support for preparsing schemas.
39  *
40  * @deprecated
41  * @author Pavani Mukthipudi, Sun Microsystems Inc.
42  * @author Neil Graham, IBM
43  * @version $Id: DOMASBuilderImpl.java,v 1.25 2004/02/24 23:15:57 mrglavas Exp $
44  *
45  */

46
47 public class DOMASBuilderImpl
48     extends DOMParserImpl implements DOMASBuilder {
49
50     //
51
// Constants
52
//
53

54     // Feature ids
55

56     protected static final String JavaDoc SCHEMA_FULL_CHECKING =
57         Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING;
58
59     // Property ids
60

61     protected static final String JavaDoc ERROR_REPORTER =
62         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
63
64     protected static final String JavaDoc SYMBOL_TABLE =
65         Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
66
67     protected static final String JavaDoc ENTITY_MANAGER =
68         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
69
70
71     //
72
// Data
73
//
74

75     protected XSGrammarBucket fGrammarBucket;
76
77     protected ASModelImpl fAbstractSchema;
78
79     //
80
// Constructors
81
//
82

83     /**
84      * Constructs a DOM Builder using the dtd/xml schema parser configuration.
85      */

86     public DOMASBuilderImpl() {
87         super(new XMLGrammarCachingConfiguration());
88     } // <init>
89

90     /**
91      * Constructs a DOM Builder using the specified parser configuration.
92      * We must demand that the configuration extend XMLGrammarCachingConfiguration to make
93      * sure all relevant methods/features are available.
94      */

95     public DOMASBuilderImpl(XMLGrammarCachingConfiguration config) {
96         super(config);
97     } // <init>(XMLParserConfiguration)
98

99     /**
100      * Constructs a DOM Builder using the specified symbol table.
101      */

102     public DOMASBuilderImpl(SymbolTable symbolTable) {
103         super(new XMLGrammarCachingConfiguration(symbolTable));
104     } // <init>(SymbolTable)
105

106
107     /**
108      * Constructs a DOM Builder using the specified symbol table and
109      * grammar pool.
110      * The grammarPool implementation should extent the default
111      * implementation; otherwise, correct functioning of this class may
112      * not occur.
113      */

114     public DOMASBuilderImpl(SymbolTable symbolTable, XMLGrammarPool grammarPool) {
115         super(new XMLGrammarCachingConfiguration(symbolTable, grammarPool));
116     }
117
118     //
119
// DOMASBuilder methods
120
//
121

122     /**
123      * Associate an <code>ASModel</code> with a document instance. This
124      * <code>ASModel</code> will be used by the "
125      * <code>validate-if-schema</code>" and "
126      * <code>datatype-normalization</code>" options during the load of a new
127      * <code>Document</code>.
128      */

129     public ASModel getAbstractSchema() {
130         return fAbstractSchema;
131     }
132
133     /**
134      * Associate an <code>ASModel</code> with a document instance. This
135      * <code>ASModel</code> will be used by the "
136      * <code>validate-if-schema</code>" and "
137      * <code>datatype-normalization</code>" options during the load of a new
138      * <code>Document</code>.
139      */

140     public void setAbstractSchema(ASModel abstractSchema) {
141
142         // since the ASModel associated with this object is an attribute
143
// according to the DOM IDL, we must obliterate anything
144
// that was set before, rather than adding to it.
145
// REVISIT: so shouldn't we attempt to clear the
146
// grammarPool before adding stuff to it? - NG
147
fAbstractSchema = (ASModelImpl)abstractSchema;
148
149         // make sure the GrammarPool is properly initialized.
150
XMLGrammarPool grammarPool = (XMLGrammarPool)fConfiguration.getProperty(StandardParserConfiguration.XMLGRAMMAR_POOL);
151         // if there is no grammar pool, create one
152
// REVISIT: ASBuilder should always create one.
153
if (grammarPool == null) {
154             // something's not right in this situation...
155
grammarPool = new XMLGrammarPoolImpl();
156             fConfiguration.setProperty(StandardParserConfiguration.XMLGRAMMAR_POOL,
157                                        grammarPool);
158         }
159         if (fAbstractSchema != null) {
160             initGrammarPool(fAbstractSchema, grammarPool);
161         }
162     }
163
164     /**
165      * Parse a Abstract Schema from a location identified by an URI.
166      *
167      * @param uri The location of the Abstract Schema to be read.
168      * @return The newly created <code>Abstract Schema</code>.
169      * @exception DOMASException
170      * Exceptions raised by <code>parseASURI()</code> originate with the
171      * installed ErrorHandler, and thus depend on the implementation of
172      * the <code>DOMErrorHandler</code> interfaces. The default error
173      * handlers will raise a <code>DOMASException</code> if any form of
174      * Abstract Schema inconsistencies or warning occurs during the parse,
175      * but application defined errorHandlers are not required to do so.
176      * <br> WRONG_MIME_TYPE_ERR: Raised when <code>mimeTypeCheck</code> is
177      * <code>true</code> and the inputsource has an incorrect MIME Type.
178      * See attribute <code>mimeTypeCheck</code>.
179      * @exception DOMSystemException
180      * Exceptions raised by <code>parseURI()</code> originate with the
181      * installed ErrorHandler, and thus depend on the implementation of
182      * the <code>DOMErrorHandler</code> interfaces. The default error
183      * handlers will raise a DOMSystemException if any form I/O or other
184      * system error occurs during the parse, but application defined error
185      * handlers are not required to do so.
186      */

187     public ASModel parseASURI(String JavaDoc uri)
188                               throws DOMASException, Exception JavaDoc {
189         XMLInputSource source = new XMLInputSource(null, uri, null);
190         return parseASInputSource(source);
191     }
192
193     /**
194      * Parse a Abstract Schema from a location identified by an
195      * <code>LSInput</code>.
196      *
197      * @param is The <code>LSInput</code> from which the source
198      * Abstract Schema is to be read.
199      * @return The newly created <code>ASModel</code>.
200      * @exception DOMASException
201      * Exceptions raised by <code>parseASURI()</code> originate with the
202      * installed ErrorHandler, and thus depend on the implementation of
203      * the <code>DOMErrorHandler</code> interfaces. The default error
204      * handlers will raise a <code>DOMASException</code> if any form of
205      * Abstract Schema inconsistencies or warning occurs during the parse,
206      * but application defined errorHandlers are not required to do so.
207      * <br> WRONG_MIME_TYPE_ERR: Raised when <code>mimeTypeCheck</code> is
208      * true and the inputsource has an incorrect MIME Type. See attribute
209      * <code>mimeTypeCheck</code>.
210      * @exception DOMSystemException
211      * Exceptions raised by <code>parseURI()</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 DOMSystemException if any form I/O or other
215      * system error occurs during the parse, but application defined error
216      * handlers are not required to do so.
217      */

218     public ASModel parseASInputSource(LSInput JavaDoc is)
219                                       throws DOMASException, Exception JavaDoc {
220                                       
221         // need to wrap the LSInput with an XMLInputSource
222
XMLInputSource xis = this.dom2xmlInputSource(is);
223         try {
224             return parseASInputSource(xis);
225         }
226         catch (XNIException e) {
227             Exception JavaDoc ex = e.getException();
228             throw ex;
229         }
230     }
231
232     ASModel parseASInputSource(XMLInputSource is) throws Exception JavaDoc {
233                                       
234         if (fGrammarBucket == null) {
235             fGrammarBucket = new XSGrammarBucket();
236         }
237
238         initGrammarBucket();
239
240         // actually do the parse:
241
// save some casting
242
XMLGrammarCachingConfiguration gramConfig = (XMLGrammarCachingConfiguration)fConfiguration;
243         // ensure grammarPool doesn't absorb grammars while it's parsing
244
gramConfig.lockGrammarPool();
245         SchemaGrammar grammar = gramConfig.parseXMLSchema(is);
246         gramConfig.unlockGrammarPool();
247
248         ASModelImpl newAsModel = null;
249         if (grammar != null) {
250             newAsModel = new ASModelImpl();
251             fGrammarBucket.putGrammar (grammar, true);
252             addGrammars(newAsModel, fGrammarBucket);
253         }
254         return newAsModel;
255     }
256
257     // put all the grammars we have access to in the GrammarBucket
258
private void initGrammarBucket() {
259         fGrammarBucket.reset();
260         if (fAbstractSchema != null)
261             initGrammarBucketRecurse(fAbstractSchema);
262     }
263     private void initGrammarBucketRecurse(ASModelImpl currModel) {
264         if(currModel.getGrammar() != null) {
265             fGrammarBucket.putGrammar(currModel.getGrammar());
266         }
267         for(int i = 0; i < currModel.getInternalASModels().size(); i++) {
268             ASModelImpl nextModel = (ASModelImpl)(currModel.getInternalASModels().elementAt(i));
269             initGrammarBucketRecurse(nextModel);
270         }
271     }
272
273     private void addGrammars(ASModelImpl model, XSGrammarBucket grammarBucket) {
274         SchemaGrammar [] grammarList = grammarBucket.getGrammars();
275         for(int i=0; i<grammarList.length; i++) {
276             ASModelImpl newModel = new ASModelImpl();
277             newModel.setGrammar(grammarList[i]);
278             model.addASModel(newModel);
279         }
280     } // addGrammars
281

282     private void initGrammarPool(ASModelImpl currModel, XMLGrammarPool grammarPool) {
283         // put all the grammars in fAbstractSchema into the grammar pool.
284
// grammarPool must never be null!
285
Grammar[] grammars = new Grammar[1];
286         if ((grammars[0] = (Grammar)currModel.getGrammar()) != null) {
287             grammarPool.cacheGrammars(grammars[0].getGrammarDescription().getGrammarType(), grammars);
288         }
289         Vector JavaDoc modelStore = currModel.getInternalASModels();
290         for (int i = 0; i < modelStore.size(); i++) {
291             initGrammarPool((ASModelImpl)modelStore.elementAt(i), grammarPool);
292         }
293     }
294 } // class DOMASBuilderImpl
295
Popular Tags