001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.parsers;
019:
020: import java.util.Vector;
021:
022: import org.apache.xerces.dom.ASModelImpl;
023: import org.apache.xerces.dom3.as.ASModel;
024: import org.apache.xerces.dom3.as.DOMASBuilder;
025: import org.apache.xerces.dom3.as.DOMASException;
026: import org.apache.xerces.impl.Constants;
027: import org.apache.xerces.impl.xs.SchemaGrammar;
028: import org.apache.xerces.impl.xs.XSGrammarBucket;
029: import org.apache.xerces.util.SymbolTable;
030: import org.apache.xerces.util.XMLGrammarPoolImpl;
031: import org.apache.xerces.xni.XNIException;
032: import org.apache.xerces.xni.grammars.Grammar;
033: import org.apache.xerces.xni.grammars.XMLGrammarPool;
034: import org.apache.xerces.xni.parser.XMLInputSource;
035: import org.w3c.dom.ls.LSInput;
036:
037: /**
038: * This is Abstract Schema DOM Builder class. It extends the DOMParserImpl
039: * class. Provides support for preparsing schemas.
040: *
041: * @deprecated
042: * @author Pavani Mukthipudi, Sun Microsystems Inc.
043: * @author Neil Graham, IBM
044: * @version $Id: DOMASBuilderImpl.java 447239 2006-09-18 05:08:26Z mrglavas $
045: *
046: */
047:
048: public class DOMASBuilderImpl extends DOMParserImpl implements
049: DOMASBuilder {
050:
051: //
052: // Constants
053: //
054:
055: // Feature ids
056:
057: protected static final String SCHEMA_FULL_CHECKING = Constants.XERCES_FEATURE_PREFIX
058: + Constants.SCHEMA_FULL_CHECKING;
059:
060: // Property ids
061:
062: protected static final String ERROR_REPORTER = Constants.XERCES_PROPERTY_PREFIX
063: + Constants.ERROR_REPORTER_PROPERTY;
064:
065: protected static final String SYMBOL_TABLE = Constants.XERCES_PROPERTY_PREFIX
066: + Constants.SYMBOL_TABLE_PROPERTY;
067:
068: protected static final String ENTITY_MANAGER = Constants.XERCES_PROPERTY_PREFIX
069: + Constants.ENTITY_MANAGER_PROPERTY;
070:
071: //
072: // Data
073: //
074:
075: protected XSGrammarBucket fGrammarBucket;
076:
077: protected ASModelImpl fAbstractSchema;
078:
079: //
080: // Constructors
081: //
082:
083: /**
084: * Constructs a DOM Builder using the dtd/xml schema parser configuration.
085: */
086: public DOMASBuilderImpl() {
087: super (new XMLGrammarCachingConfiguration());
088: } // <init>
089:
090: /**
091: * Constructs a DOM Builder using the specified parser configuration.
092: * We must demand that the configuration extend XMLGrammarCachingConfiguration to make
093: * sure all relevant methods/features are available.
094: */
095: public DOMASBuilderImpl(XMLGrammarCachingConfiguration config) {
096: super (config);
097: } // <init>(XMLParserConfiguration)
098:
099: /**
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: * Constructs a DOM Builder using the specified symbol table and
108: * grammar pool.
109: * The grammarPool implementation should extent the default
110: * implementation; otherwise, correct functioning of this class may
111: * not occur.
112: */
113: public DOMASBuilderImpl(SymbolTable symbolTable,
114: XMLGrammarPool grammarPool) {
115: super (new XMLGrammarCachingConfiguration(symbolTable,
116: grammarPool));
117: }
118:
119: //
120: // DOMASBuilder methods
121: //
122:
123: /**
124: * Associate an <code>ASModel</code> with a document instance. This
125: * <code>ASModel</code> will be used by the "
126: * <code>validate-if-schema</code>" and "
127: * <code>datatype-normalization</code>" options during the load of a new
128: * <code>Document</code>.
129: */
130: public ASModel getAbstractSchema() {
131: return fAbstractSchema;
132: }
133:
134: /**
135: * Associate an <code>ASModel</code> with a document instance. This
136: * <code>ASModel</code> will be used by the "
137: * <code>validate-if-schema</code>" and "
138: * <code>datatype-normalization</code>" options during the load of a new
139: * <code>Document</code>.
140: */
141: public void setAbstractSchema(ASModel abstractSchema) {
142:
143: // since the ASModel associated with this object is an attribute
144: // according to the DOM IDL, we must obliterate anything
145: // that was set before, rather than adding to it.
146: // REVISIT: so shouldn't we attempt to clear the
147: // grammarPool before adding stuff to it? - NG
148: fAbstractSchema = (ASModelImpl) abstractSchema;
149:
150: // make sure the GrammarPool is properly initialized.
151: XMLGrammarPool grammarPool = (XMLGrammarPool) fConfiguration
152: .getProperty(StandardParserConfiguration.XMLGRAMMAR_POOL);
153: // if there is no grammar pool, create one
154: // REVISIT: ASBuilder should always create one.
155: if (grammarPool == null) {
156: // something's not right in this situation...
157: grammarPool = new XMLGrammarPoolImpl();
158: fConfiguration.setProperty(
159: StandardParserConfiguration.XMLGRAMMAR_POOL,
160: grammarPool);
161: }
162: if (fAbstractSchema != null) {
163: initGrammarPool(fAbstractSchema, grammarPool);
164: }
165: }
166:
167: /**
168: * Parse a Abstract Schema from a location identified by an URI.
169: *
170: * @param uri The location of the Abstract Schema to be read.
171: * @return The newly created <code>Abstract Schema</code>.
172: * @exception DOMASException
173: * Exceptions raised by <code>parseASURI()</code> originate with the
174: * installed ErrorHandler, and thus depend on the implementation of
175: * the <code>DOMErrorHandler</code> interfaces. The default error
176: * handlers will raise a <code>DOMASException</code> if any form of
177: * Abstract Schema inconsistencies or warning occurs during the parse,
178: * but application defined errorHandlers are not required to do so.
179: * <br> WRONG_MIME_TYPE_ERR: Raised when <code>mimeTypeCheck</code> is
180: * <code>true</code> and the inputsource has an incorrect MIME Type.
181: * See attribute <code>mimeTypeCheck</code>.
182: * @exception DOMSystemException
183: * Exceptions raised by <code>parseURI()</code> originate with the
184: * installed ErrorHandler, and thus depend on the implementation of
185: * the <code>DOMErrorHandler</code> interfaces. The default error
186: * handlers will raise a DOMSystemException if any form I/O or other
187: * system error occurs during the parse, but application defined error
188: * handlers are not required to do so.
189: */
190: public ASModel parseASURI(String uri) throws DOMASException,
191: Exception {
192: XMLInputSource source = new XMLInputSource(null, uri, null);
193: return parseASInputSource(source);
194: }
195:
196: /**
197: * Parse a Abstract Schema from a location identified by an
198: * <code>LSInput</code>.
199: *
200: * @param is The <code>LSInput</code> from which the source
201: * Abstract Schema is to be read.
202: * @return The newly created <code>ASModel</code>.
203: * @exception DOMASException
204: * Exceptions raised by <code>parseASURI()</code> originate with the
205: * installed ErrorHandler, and thus depend on the implementation of
206: * the <code>DOMErrorHandler</code> interfaces. The default error
207: * handlers will raise a <code>DOMASException</code> if any form of
208: * Abstract Schema inconsistencies or warning occurs during the parse,
209: * but application defined errorHandlers are not required to do so.
210: * <br> WRONG_MIME_TYPE_ERR: Raised when <code>mimeTypeCheck</code> is
211: * true and the inputsource has an incorrect MIME Type. See attribute
212: * <code>mimeTypeCheck</code>.
213: * @exception DOMSystemException
214: * Exceptions raised by <code>parseURI()</code> originate with the
215: * installed ErrorHandler, and thus depend on the implementation of
216: * the <code>DOMErrorHandler</code> interfaces. The default error
217: * handlers will raise a DOMSystemException if any form I/O or other
218: * system error occurs during the parse, but application defined error
219: * handlers are not required to do so.
220: */
221: public ASModel parseASInputSource(LSInput is)
222: throws DOMASException, Exception {
223:
224: // need to wrap the LSInput with an XMLInputSource
225: XMLInputSource xis = this .dom2xmlInputSource(is);
226: try {
227: return parseASInputSource(xis);
228: } catch (XNIException e) {
229: Exception ex = e.getException();
230: throw ex;
231: }
232: }
233:
234: ASModel parseASInputSource(XMLInputSource is) throws Exception {
235:
236: if (fGrammarBucket == null) {
237: fGrammarBucket = new XSGrammarBucket();
238: }
239:
240: initGrammarBucket();
241:
242: // actually do the parse:
243: // save some casting
244: XMLGrammarCachingConfiguration gramConfig = (XMLGrammarCachingConfiguration) fConfiguration;
245: // ensure grammarPool doesn't absorb grammars while it's parsing
246: gramConfig.lockGrammarPool();
247: SchemaGrammar grammar = gramConfig.parseXMLSchema(is);
248: gramConfig.unlockGrammarPool();
249:
250: ASModelImpl newAsModel = null;
251: if (grammar != null) {
252: newAsModel = new ASModelImpl();
253: fGrammarBucket.putGrammar(grammar, true);
254: addGrammars(newAsModel, fGrammarBucket);
255: }
256: return newAsModel;
257: }
258:
259: // put all the grammars we have access to in the GrammarBucket
260: private void initGrammarBucket() {
261: fGrammarBucket.reset();
262: if (fAbstractSchema != null)
263: initGrammarBucketRecurse(fAbstractSchema);
264: }
265:
266: private void initGrammarBucketRecurse(ASModelImpl currModel) {
267: if (currModel.getGrammar() != null) {
268: fGrammarBucket.putGrammar(currModel.getGrammar());
269: }
270: for (int i = 0; i < currModel.getInternalASModels().size(); i++) {
271: ASModelImpl nextModel = (ASModelImpl) (currModel
272: .getInternalASModels().elementAt(i));
273: initGrammarBucketRecurse(nextModel);
274: }
275: }
276:
277: private void addGrammars(ASModelImpl model,
278: XSGrammarBucket grammarBucket) {
279: SchemaGrammar[] grammarList = grammarBucket.getGrammars();
280: for (int i = 0; i < grammarList.length; i++) {
281: ASModelImpl newModel = new ASModelImpl();
282: newModel.setGrammar(grammarList[i]);
283: model.addASModel(newModel);
284: }
285: } // addGrammars
286:
287: private void initGrammarPool(ASModelImpl currModel,
288: XMLGrammarPool grammarPool) {
289: // put all the grammars in fAbstractSchema into the grammar pool.
290: // grammarPool must never be null!
291: Grammar[] grammars = new Grammar[1];
292: if ((grammars[0] = (Grammar) currModel.getGrammar()) != null) {
293: grammarPool
294: .cacheGrammars(grammars[0].getGrammarDescription()
295: .getGrammarType(), grammars);
296: }
297: Vector modelStore = currModel.getInternalASModels();
298: for (int i = 0; i < modelStore.size(); i++) {
299: initGrammarPool((ASModelImpl) modelStore.elementAt(i),
300: grammarPool);
301: }
302: }
303: } // class DOMASBuilderImpl
|