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.impl.xs;
019:
020: import org.apache.xerces.impl.xs.XMLSchemaLoader;
021: import org.apache.xerces.impl.xs.util.XSGrammarPool;
022: import org.apache.xerces.xni.grammars.Grammar;
023: import org.apache.xerces.xni.grammars.XMLGrammarDescription;
024: import org.apache.xerces.xni.grammars.XSGrammar;
025: import org.apache.xerces.xni.parser.XMLInputSource;
026: import org.apache.xerces.xs.LSInputList;
027: import org.apache.xerces.xs.StringList;
028: import org.apache.xerces.xs.XSConstants;
029: import org.apache.xerces.xs.XSLoader;
030: import org.apache.xerces.xs.XSModel;
031: import org.apache.xerces.xs.XSNamedMap;
032: import org.apache.xerces.xs.XSObjectList;
033: import org.apache.xerces.xs.XSTypeDefinition;
034: import org.w3c.dom.DOMConfiguration;
035: import org.w3c.dom.DOMException;
036: import org.w3c.dom.DOMStringList;
037: import org.w3c.dom.ls.LSInput;
038:
039: /**
040: * <p>An implementation of XSLoader which wraps XMLSchemaLoader.</p>
041: *
042: * @xerces.internal
043: *
044: * @author Michael Glavassevich, IBM
045: *
046: * @version $Id: XSLoaderImpl.java 449487 2006-09-24 21:11:28Z mrglavas $
047: */
048: public final class XSLoaderImpl implements XSLoader, DOMConfiguration {
049:
050: /**
051: * Grammar pool. Need this to prevent us from
052: * getting two grammars from the same namespace.
053: */
054: private final XSGrammarPool fGrammarPool = new XSGrammarMerger();
055:
056: /** Schema loader. **/
057: private final XMLSchemaLoader fSchemaLoader = new XMLSchemaLoader();
058:
059: /**
060: * No-args constructor.
061: */
062: public XSLoaderImpl() {
063: fSchemaLoader.setProperty(XMLSchemaLoader.XMLGRAMMAR_POOL,
064: fGrammarPool);
065: }
066:
067: /**
068: * The configuration of a document. It maintains a table of recognized
069: * parameters. Using the configuration, it is possible to change the
070: * behavior of the load methods. The configuration may support the
071: * setting of and the retrieval of the following non-boolean parameters
072: * defined on the <code>DOMConfiguration</code> interface:
073: * <code>error-handler</code> (<code>DOMErrorHandler</code>) and
074: * <code>resource-resolver</code> (<code>LSResourceResolver</code>).
075: * <br> The following list of boolean parameters is defined:
076: * <dl>
077: * <dt>
078: * <code>"validate"</code></dt>
079: * <dd>
080: * <dl>
081: * <dt><code>true</code></dt>
082: * <dd>[required] (default) Validate an XML
083: * Schema during loading. If validation errors are found, the error
084: * handler is notified. </dd>
085: * <dt><code>false</code></dt>
086: * <dd>[optional] Do not
087: * report errors during the loading of an XML Schema document. </dd>
088: * </dl></dd>
089: * </dl>
090: */
091: public DOMConfiguration getConfig() {
092: return this ;
093: }
094:
095: /**
096: * Parses the content of XML Schema documents specified as the list of URI
097: * references. If the URI contains a fragment identifier, the behavior
098: * is not defined by this specification.
099: * @param uriList The list of URI locations.
100: * @return An XSModel representing the schema documents.
101: */
102: public XSModel loadURIList(StringList uriList) {
103: int length = uriList.getLength();
104: if (length == 0) {
105: return null;
106: }
107: try {
108: fGrammarPool.clear();
109: for (int i = 0; i < length; ++i) {
110: fSchemaLoader.loadGrammar(new XMLInputSource(null,
111: uriList.item(i), null));
112: }
113: return fGrammarPool.toXSModel();
114: } catch (Exception e) {
115: fSchemaLoader.reportDOMFatalError(e);
116: return null;
117: }
118: }
119:
120: /**
121: * Parses the content of XML Schema documents specified as a list of
122: * <code>LSInput</code>s.
123: * @param is The list of <code>LSInput</code>s from which the XML
124: * Schema documents are to be read.
125: * @return An XSModel representing the schema documents.
126: */
127: public XSModel loadInputList(LSInputList is) {
128: final int length = is.getLength();
129: if (length == 0) {
130: return null;
131: }
132: try {
133: fGrammarPool.clear();
134: for (int i = 0; i < length; ++i) {
135: fSchemaLoader.loadGrammar(fSchemaLoader
136: .dom2xmlInputSource(is.item(i)));
137: }
138: return fGrammarPool.toXSModel();
139: } catch (Exception e) {
140: fSchemaLoader.reportDOMFatalError(e);
141: return null;
142: }
143: }
144:
145: /**
146: * Parse an XML Schema document from a location identified by a URI
147: * reference. If the URI contains a fragment identifier, the behavior is
148: * not defined by this specification.
149: * @param uri The location of the XML Schema document to be read.
150: * @return An XSModel representing this schema.
151: */
152: public XSModel loadURI(String uri) {
153: try {
154: fGrammarPool.clear();
155: return ((XSGrammar) fSchemaLoader
156: .loadGrammar(new XMLInputSource(null, uri, null)))
157: .toXSModel();
158: } catch (Exception e) {
159: fSchemaLoader.reportDOMFatalError(e);
160: return null;
161: }
162: }
163:
164: /**
165: * Parse an XML Schema document from a resource identified by a
166: * <code>LSInput</code> .
167: * @param is The <code>LSInput</code> from which the source
168: * document is to be read.
169: * @return An XSModel representing this schema.
170: */
171: public XSModel load(LSInput is) {
172: try {
173: fGrammarPool.clear();
174: return ((XSGrammar) fSchemaLoader.loadGrammar(fSchemaLoader
175: .dom2xmlInputSource(is))).toXSModel();
176: } catch (Exception e) {
177: fSchemaLoader.reportDOMFatalError(e);
178: return null;
179: }
180: }
181:
182: /* (non-Javadoc)
183: * @see org.apache.xerces.dom3.DOMConfiguration#setParameter(java.lang.String, java.lang.Object)
184: */
185: public void setParameter(String name, Object value)
186: throws DOMException {
187: fSchemaLoader.setParameter(name, value);
188: }
189:
190: /* (non-Javadoc)
191: * @see org.apache.xerces.dom3.DOMConfiguration#getParameter(java.lang.String)
192: */
193: public Object getParameter(String name) throws DOMException {
194: return fSchemaLoader.getParameter(name);
195: }
196:
197: /* (non-Javadoc)
198: * @see org.apache.xerces.dom3.DOMConfiguration#canSetParameter(java.lang.String, java.lang.Object)
199: */
200: public boolean canSetParameter(String name, Object value) {
201: return fSchemaLoader.canSetParameter(name, value);
202: }
203:
204: /* (non-Javadoc)
205: * @see org.apache.xerces.dom3.DOMConfiguration#getParameterNames()
206: */
207: public DOMStringList getParameterNames() {
208: return fSchemaLoader.getParameterNames();
209: }
210:
211: /**
212: * Grammar pool which merges grammars from the same namespace into one. This eliminates
213: * duplicate named components. It doesn't ensure that the grammar is consistent, however
214: * this no worse than than the behaviour of XMLSchemaLoader alone when used as an XSLoader.
215: */
216: private static final class XSGrammarMerger extends XSGrammarPool {
217:
218: public XSGrammarMerger() {
219: }
220:
221: public void putGrammar(Grammar grammar) {
222: SchemaGrammar cachedGrammar = toSchemaGrammar(super
223: .getGrammar(grammar.getGrammarDescription()));
224: if (cachedGrammar != null) {
225: SchemaGrammar newGrammar = toSchemaGrammar(grammar);
226: if (newGrammar != null) {
227: mergeSchemaGrammars(cachedGrammar, newGrammar);
228: }
229: } else {
230: super .putGrammar(grammar);
231: }
232: }
233:
234: private SchemaGrammar toSchemaGrammar(Grammar grammar) {
235: return (grammar instanceof SchemaGrammar) ? (SchemaGrammar) grammar
236: : null;
237: }
238:
239: private void mergeSchemaGrammars(SchemaGrammar cachedGrammar,
240: SchemaGrammar newGrammar) {
241:
242: /** Add new top-level element declarations. **/
243: XSNamedMap map = newGrammar
244: .getComponents(XSConstants.ELEMENT_DECLARATION);
245: int length = map.getLength();
246: for (int i = 0; i < length; ++i) {
247: XSElementDecl decl = (XSElementDecl) map.item(i);
248: if (cachedGrammar.getGlobalElementDecl(decl.getName()) == null) {
249: cachedGrammar.addGlobalElementDecl(decl);
250: }
251: }
252:
253: /** Add new top-level attribute declarations. **/
254: map = newGrammar
255: .getComponents(XSConstants.ATTRIBUTE_DECLARATION);
256: length = map.getLength();
257: for (int i = 0; i < length; ++i) {
258: XSAttributeDecl decl = (XSAttributeDecl) map.item(i);
259: if (cachedGrammar
260: .getGlobalAttributeDecl(decl.getName()) == null) {
261: cachedGrammar.addGlobalAttributeDecl(decl);
262: }
263: }
264:
265: /** Add new top-level type definitions. **/
266: map = newGrammar.getComponents(XSConstants.TYPE_DEFINITION);
267: length = map.getLength();
268: for (int i = 0; i < length; ++i) {
269: XSTypeDefinition decl = (XSTypeDefinition) map.item(i);
270: if (cachedGrammar.getGlobalTypeDecl(decl.getName()) == null) {
271: cachedGrammar.addGlobalTypeDecl(decl);
272: }
273: }
274:
275: /** Add new top-level attribute group definitions. **/
276: map = newGrammar.getComponents(XSConstants.ATTRIBUTE_GROUP);
277: length = map.getLength();
278: for (int i = 0; i < length; ++i) {
279: XSAttributeGroupDecl decl = (XSAttributeGroupDecl) map
280: .item(i);
281: if (cachedGrammar.getGlobalAttributeGroupDecl(decl
282: .getName()) == null) {
283: cachedGrammar.addGlobalAttributeGroupDecl(decl);
284: }
285: }
286:
287: /** Add new top-level model group definitions. **/
288: map = newGrammar.getComponents(XSConstants.MODEL_GROUP);
289: length = map.getLength();
290: for (int i = 0; i < length; ++i) {
291: XSGroupDecl decl = (XSGroupDecl) map.item(i);
292: if (cachedGrammar.getGlobalGroupDecl(decl.getName()) == null) {
293: cachedGrammar.addGlobalGroupDecl(decl);
294: }
295: }
296:
297: /** Add new top-level notation declarations. **/
298: map = newGrammar
299: .getComponents(XSConstants.NOTATION_DECLARATION);
300: length = map.getLength();
301: for (int i = 0; i < length; ++i) {
302: XSNotationDecl decl = (XSNotationDecl) map.item(i);
303: if (cachedGrammar.getGlobalNotationDecl(decl.getName()) == null) {
304: cachedGrammar.addGlobalNotationDecl(decl);
305: }
306: }
307:
308: /**
309: * Add all annotations. Since these components are not named it's
310: * possible we'll add duplicate components. There isn't much we can
311: * do. It's no worse than XMLSchemaLoader when used as an XSLoader.
312: */
313: XSObjectList annotations = newGrammar.getAnnotations();
314: length = annotations.getLength();
315: for (int i = 0; i < length; ++i) {
316: cachedGrammar
317: .addAnnotation((XSAnnotationImpl) annotations
318: .item(i));
319: }
320:
321: }
322:
323: public boolean containsGrammar(XMLGrammarDescription desc) {
324: return false;
325: }
326:
327: public Grammar getGrammar(XMLGrammarDescription desc) {
328: return null;
329: }
330:
331: public Grammar retrieveGrammar(XMLGrammarDescription desc) {
332: return null;
333: }
334:
335: public Grammar[] retrieveInitialGrammarSet(String grammarType) {
336: return new Grammar[0];
337: }
338: }
339: }
|