KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > xs > XSLoaderImpl


1 /*
2  * Copyright 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 com.sun.org.apache.xerces.internal.impl.xs;
18
19 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader;
20 import com.sun.org.apache.xerces.internal.impl.xs.util.XSGrammarPool;
21 import com.sun.org.apache.xerces.internal.xni.grammars.Grammar;
22 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
23 import com.sun.org.apache.xerces.internal.xni.grammars.XSGrammar;
24 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
25 import com.sun.org.apache.xerces.internal.xs.LSInputList;
26 import com.sun.org.apache.xerces.internal.xs.StringList;
27 import com.sun.org.apache.xerces.internal.xs.XSConstants;
28 import com.sun.org.apache.xerces.internal.xs.XSLoader;
29 import com.sun.org.apache.xerces.internal.xs.XSModel;
30 import com.sun.org.apache.xerces.internal.xs.XSNamedMap;
31 import com.sun.org.apache.xerces.internal.xs.XSObjectList;
32 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
33 import org.w3c.dom.DOMConfiguration JavaDoc;
34 import org.w3c.dom.DOMException JavaDoc;
35 import org.w3c.dom.DOMStringList JavaDoc;
36 import org.w3c.dom.ls.LSInput JavaDoc;
37
38 /**
39  * <p>An implementation of XSLoader which wraps XMLSchemaLoader.</p>
40  *
41  * @xerces.internal
42  *
43  * @author Michael Glavassevich, IBM
44  *
45  * @version $Id: XSLoaderImpl.java,v 1.2 2005/09/26 13:02:36 sunithareddy Exp $
46  */

47 public final class XSLoaderImpl implements XSLoader, DOMConfiguration JavaDoc {
48     
49     /**
50      * Grammar pool. Need this to prevent us from
51      * getting two grammars from the same namespace.
52      */

53     private final XSGrammarPool fGrammarPool = new XSGrammarMerger();
54     
55     /** Schema loader. **/
56     private final XMLSchemaLoader fSchemaLoader = new XMLSchemaLoader();
57     
58     /**
59      * No-args constructor.
60      */

61     public XSLoaderImpl() {
62         fSchemaLoader.setProperty(XMLSchemaLoader.XMLGRAMMAR_POOL, fGrammarPool);
63     }
64
65     /**
66      * The configuration of a document. It maintains a table of recognized
67      * parameters. Using the configuration, it is possible to change the
68      * behavior of the load methods. The configuration may support the
69      * setting of and the retrieval of the following non-boolean parameters
70      * defined on the <code>DOMConfiguration</code> interface:
71      * <code>error-handler</code> (<code>DOMErrorHandler</code>) and
72      * <code>resource-resolver</code> (<code>LSResourceResolver</code>).
73      * <br> The following list of boolean parameters is defined:
74      * <dl>
75      * <dt>
76      * <code>"validate"</code></dt>
77      * <dd>
78      * <dl>
79      * <dt><code>true</code></dt>
80      * <dd>[required] (default) Validate an XML
81      * Schema during loading. If validation errors are found, the error
82      * handler is notified. </dd>
83      * <dt><code>false</code></dt>
84      * <dd>[optional] Do not
85      * report errors during the loading of an XML Schema document. </dd>
86      * </dl></dd>
87      * </dl>
88      */

89     public DOMConfiguration JavaDoc getConfig() {
90         return this;
91     }
92
93     /**
94      * Parses the content of XML Schema documents specified as the list of URI
95      * references. If the URI contains a fragment identifier, the behavior
96      * is not defined by this specification.
97      * @param uri The list of URI locations.
98      * @return An XSModel representing the schema documents.
99      */

100     public XSModel loadURIList(StringList uriList) {
101         int length = uriList.getLength();
102         if (length == 0) {
103             return null;
104         }
105         try {
106             fGrammarPool.clear();
107             for (int i = 0; i < length; ++i) {
108                 fSchemaLoader.loadGrammar(new XMLInputSource(null, uriList.item(i), null));
109             }
110             return fGrammarPool.toXSModel();
111         }
112         catch (Exception JavaDoc e) {
113             fSchemaLoader.reportDOMFatalError(e);
114             return null;
115         }
116     }
117
118     /**
119      * Parses the content of XML Schema documents specified as a list of
120      * <code>LSInput</code>s.
121      * @param is The list of <code>LSInput</code>s from which the XML
122      * Schema documents are to be read.
123      * @return An XSModel representing the schema documents.
124      */

125     public XSModel loadInputList(LSInputList is) {
126         final int length = is.getLength();
127         if (length == 0) {
128             return null;
129         }
130         try {
131             fGrammarPool.clear();
132             for (int i = 0; i < length; ++i) {
133                 fSchemaLoader.loadGrammar(fSchemaLoader.dom2xmlInputSource(is.item(i)));
134             }
135             return fGrammarPool.toXSModel();
136         }
137         catch (Exception JavaDoc e) {
138             fSchemaLoader.reportDOMFatalError(e);
139             return null;
140         }
141     }
142
143     /**
144      * Parse an XML Schema document from a location identified by a URI
145      * reference. If the URI contains a fragment identifier, the behavior is
146      * not defined by this specification.
147      * @param uri The location of the XML Schema document to be read.
148      * @return An XSModel representing this schema.
149      */

150     public XSModel loadURI(String JavaDoc uri) {
151         try {
152             fGrammarPool.clear();
153             return ((XSGrammar) fSchemaLoader.loadGrammar(new XMLInputSource(null, uri, null))).toXSModel();
154         }
155         catch (Exception JavaDoc e){
156             fSchemaLoader.reportDOMFatalError(e);
157             return null;
158         }
159     }
160
161     /**
162      * Parse an XML Schema document from a resource identified by a
163      * <code>LSInput</code> .
164      * @param is The <code>DOMInputSource</code> from which the source
165      * document is to be read.
166      * @return An XSModel representing this schema.
167      */

168     public XSModel load(LSInput JavaDoc is) {
169         try {
170             fGrammarPool.clear();
171             return ((XSGrammar) fSchemaLoader.loadGrammar(fSchemaLoader.dom2xmlInputSource(is))).toXSModel();
172         }
173         catch (Exception JavaDoc e) {
174             fSchemaLoader.reportDOMFatalError(e);
175             return null;
176         }
177     }
178
179     /* (non-Javadoc)
180      * @see DOMConfiguration#setParameter(String, Object)
181      */

182     public void setParameter(String JavaDoc name, Object JavaDoc value) throws DOMException JavaDoc {
183         fSchemaLoader.setParameter(name, value);
184     }
185
186     /* (non-Javadoc)
187      * @see DOMConfiguration#getParameter(String)
188      */

189     public Object JavaDoc getParameter(String JavaDoc name) throws DOMException JavaDoc {
190         return fSchemaLoader.getParameter(name);
191     }
192
193     /* (non-Javadoc)
194      * @see DOMConfiguration#canSetParameter(String, Object)
195      */

196     public boolean canSetParameter(String JavaDoc name, Object JavaDoc value) {
197         return fSchemaLoader.canSetParameter(name, value);
198     }
199
200     /* (non-Javadoc)
201      * @see DOMConfiguration#getParameterNames()
202      */

203     public DOMStringList JavaDoc getParameterNames() {
204         return fSchemaLoader.getParameterNames();
205     }
206     
207     /**
208      * Grammar pool which merges grammars from the same namespace into one. This eliminates
209      * duplicate named components. It doesn't ensure that the grammar is consistent, however
210      * this no worse than than the behaviour of XMLSchemaLoader alone when used as an XSLoader.
211      */

212     private static final class XSGrammarMerger extends XSGrammarPool {
213         
214         public XSGrammarMerger () {}
215         
216         public void putGrammar(Grammar grammar) {
217             SchemaGrammar cachedGrammar =
218                 toSchemaGrammar(super.getGrammar(grammar.getGrammarDescription()));
219             if (cachedGrammar != null) {
220                 SchemaGrammar newGrammar = toSchemaGrammar(grammar);
221                 if (newGrammar != null) {
222                     mergeSchemaGrammars(cachedGrammar, newGrammar);
223                 }
224             }
225             else {
226                 super.putGrammar(grammar);
227             }
228         }
229         
230         private SchemaGrammar toSchemaGrammar (Grammar grammar) {
231             return (grammar instanceof SchemaGrammar) ? (SchemaGrammar) grammar : null;
232         }
233         
234         private void mergeSchemaGrammars(SchemaGrammar cachedGrammar, SchemaGrammar newGrammar) {
235
236             /** Add new top-level element declarations. **/
237             XSNamedMap map = newGrammar.getComponents(XSConstants.ELEMENT_DECLARATION);
238             int length = map.getLength();
239             for (int i = 0; i < length; ++i) {
240                 XSElementDecl decl = (XSElementDecl) map.item(i);
241                 if (cachedGrammar.getGlobalElementDecl(decl.getName()) == null) {
242                     cachedGrammar.addGlobalElementDecl(decl);
243                 }
244             }
245             
246             /** Add new top-level attribute declarations. **/
247             map = newGrammar.getComponents(XSConstants.ATTRIBUTE_DECLARATION);
248             length = map.getLength();
249             for (int i = 0; i < length; ++i) {
250                 XSAttributeDecl decl = (XSAttributeDecl) map.item(i);
251                 if (cachedGrammar.getGlobalAttributeDecl(decl.getName()) == null) {
252                     cachedGrammar.addGlobalAttributeDecl(decl);
253                 }
254             }
255             
256             /** Add new top-level type definitions. **/
257             map = newGrammar.getComponents(XSConstants.TYPE_DEFINITION);
258             length = map.getLength();
259             for (int i = 0; i < length; ++i) {
260                 XSTypeDefinition decl = (XSTypeDefinition) map.item(i);
261                 if (cachedGrammar.getGlobalTypeDecl(decl.getName()) == null) {
262                     cachedGrammar.addGlobalTypeDecl(decl);
263                 }
264             }
265             
266             /** Add new top-level attribute group definitions. **/
267             map = newGrammar.getComponents(XSConstants.ATTRIBUTE_GROUP);
268             length = map.getLength();
269             for (int i = 0; i < length; ++i) {
270                 XSAttributeGroupDecl decl = (XSAttributeGroupDecl) map.item(i);
271                 if (cachedGrammar.getGlobalAttributeGroupDecl(decl.getName()) == null) {
272                     cachedGrammar.addGlobalAttributeGroupDecl(decl);
273                 }
274             }
275             
276             /** Add new top-level model group definitions. **/
277             map = newGrammar.getComponents(XSConstants.MODEL_GROUP);
278             length = map.getLength();
279             for (int i = 0; i < length; ++i) {
280                 XSGroupDecl decl = (XSGroupDecl) map.item(i);
281                 if (cachedGrammar.getGlobalGroupDecl(decl.getName()) == null) {
282                     cachedGrammar.addGlobalGroupDecl(decl);
283                 }
284             }
285             
286             /** Add new top-level notation declarations. **/
287             map = newGrammar.getComponents(XSConstants.NOTATION_DECLARATION);
288             length = map.getLength();
289             for (int i = 0; i < length; ++i) {
290                 XSNotationDecl decl = (XSNotationDecl) map.item(i);
291                 if (cachedGrammar.getGlobalNotationDecl(decl.getName()) == null) {
292                     cachedGrammar.addGlobalNotationDecl(decl);
293                 }
294             }
295             
296             /**
297              * Add all annotations. Since these components are not named it's
298              * possible we'll add duplicate components. There isn't much we can
299              * do. It's no worse than XMLSchemaLoader when used as an XSLoader.
300              */

301             XSObjectList annotations = newGrammar.getAnnotations();
302             length = annotations.getLength();
303             for (int i = 0; i < length; ++i) {
304                 cachedGrammar.addAnnotation((XSAnnotationImpl) annotations.item(i));
305             }
306             
307         }
308         
309         public boolean containsGrammar(XMLGrammarDescription desc) {
310             return false;
311         }
312         
313         public Grammar getGrammar(XMLGrammarDescription desc) {
314             return null;
315         }
316         
317         public Grammar retrieveGrammar(XMLGrammarDescription desc) {
318             return null;
319         }
320         
321         public Grammar [] retrieveInitialGrammarSet (String JavaDoc grammarType) {
322             return new Grammar[0];
323         }
324     }
325 }
326
Popular Tags