KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > jaxp > validation > XMLSchemaFactory


1 /*
2  * Copyright 2005 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.jaxp.validation;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.Reader JavaDoc;
22 import java.util.Locale JavaDoc;
23
24 import javax.xml.XMLConstants JavaDoc;
25 import javax.xml.transform.Source JavaDoc;
26 import javax.xml.transform.dom.DOMSource JavaDoc;
27 import javax.xml.transform.sax.SAXSource JavaDoc;
28 import javax.xml.transform.stream.StreamSource JavaDoc;
29 import javax.xml.validation.Schema JavaDoc;
30 import javax.xml.validation.SchemaFactory JavaDoc;
31
32 import org.apache.xerces.impl.Constants;
33 import org.apache.xerces.impl.xs.XMLSchemaLoader;
34 import org.apache.xerces.util.DOMEntityResolverWrapper;
35 import org.apache.xerces.util.DOMInputSource;
36 import org.apache.xerces.util.ErrorHandlerWrapper;
37 import org.apache.xerces.util.SAXInputSource;
38 import org.apache.xerces.util.SAXMessageFormatter;
39 import org.apache.xerces.util.SecurityManager;
40 import org.apache.xerces.util.XMLGrammarPoolImpl;
41 import org.apache.xerces.xni.XNIException;
42 import org.apache.xerces.xni.grammars.Grammar;
43 import org.apache.xerces.xni.grammars.XMLGrammarDescription;
44 import org.apache.xerces.xni.grammars.XMLGrammarPool;
45 import org.apache.xerces.xni.parser.XMLConfigurationException;
46 import org.apache.xerces.xni.parser.XMLInputSource;
47 import org.w3c.dom.Node JavaDoc;
48 import org.w3c.dom.ls.LSResourceResolver JavaDoc;
49 import org.xml.sax.ErrorHandler JavaDoc;
50 import org.xml.sax.InputSource JavaDoc;
51 import org.xml.sax.SAXException JavaDoc;
52 import org.xml.sax.SAXNotRecognizedException JavaDoc;
53 import org.xml.sax.SAXNotSupportedException JavaDoc;
54 import org.xml.sax.SAXParseException JavaDoc;
55
56 /**
57  * {@link SchemaFactory} for XML Schema.
58  *
59  * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
60  * @version $Id: XMLSchemaFactory.java,v 1.8 2005/06/14 15:43:57 mrglavas Exp $
61  */

62 public final class XMLSchemaFactory extends SchemaFactory JavaDoc {
63     
64     // property identifiers
65

66     /** Feature identifier: schema full checking. */
67     private static final String JavaDoc SCHEMA_FULL_CHECKING =
68         Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING;
69     
70     /** Property identifier: grammar pool. */
71     private static final String JavaDoc XMLGRAMMAR_POOL =
72         Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY;
73     
74     /** Property identifier: SecurityManager. */
75     private static final String JavaDoc SECURITY_MANAGER =
76         Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY;
77     
78     //
79
// Data
80
//
81

82     /** The XMLSchemaLoader */
83     private final XMLSchemaLoader fXMLSchemaLoader = new XMLSchemaLoader();
84     
85     /** User-specified ErrorHandler; can be null. */
86     private ErrorHandler JavaDoc fErrorHandler;
87     
88     /** The LSResrouceResolver */
89     private LSResourceResolver JavaDoc fLSResourceResolver;
90     
91     /** The DOMEntityResolverWrapper */
92     private final DOMEntityResolverWrapper fDOMEntityResolverWrapper;
93     
94     /** The ErrorHandlerWrapper */
95     private ErrorHandlerWrapper fErrorHandlerWrapper;
96     
97     /** The SecurityManager. */
98     private SecurityManager JavaDoc fSecurityManager;
99     
100     /** The container for the real grammar pool. */
101     private XMLGrammarPoolWrapper fXMLGrammarPoolWrapper;
102     
103     public XMLSchemaFactory() {
104         fErrorHandlerWrapper = new ErrorHandlerWrapper(DraconianErrorHandler.getInstance());
105         fDOMEntityResolverWrapper = new DOMEntityResolverWrapper();
106         fXMLGrammarPoolWrapper = new XMLGrammarPoolWrapper();
107         fXMLSchemaLoader.setFeature(SCHEMA_FULL_CHECKING, true);
108         fXMLSchemaLoader.setProperty(XMLGRAMMAR_POOL, fXMLGrammarPoolWrapper);
109         fXMLSchemaLoader.setEntityResolver(fDOMEntityResolverWrapper);
110         fXMLSchemaLoader.setErrorHandler(fErrorHandlerWrapper);
111     }
112     
113     /**
114      * <p>Is specified schema supported by this <code>SchemaFactory</code>?</p>
115      *
116      * @param schemaLanguage Specifies the schema language which the returned <code>SchemaFactory</code> will understand.
117      * <code>schemaLanguage</code> must specify a <a HREF="#schemaLanguage">valid</a> schema language.
118      *
119      * @return <code>true</code> if <code>SchemaFactory</code> supports <code>schemaLanguage</code>, else <code>false</code>.
120      *
121      * @throws NullPointerException If <code>schemaLanguage</code> is <code>null</code>.
122      * @throws IllegalArgumentException If <code>schemaLanguage.length() == 0</code>
123      * or <code>schemaLanguage</code> does not specify a <a HREF="#schemaLanguage">valid</a> schema language.
124      */

125     public boolean isSchemaLanguageSupported(String JavaDoc schemaLanguage) {
126         if (schemaLanguage == null) {
127             throw new NullPointerException JavaDoc(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),
128                     "SchemaLanguageNull", null));
129         }
130         if (schemaLanguage.length() == 0) {
131             throw new IllegalArgumentException JavaDoc(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),
132                     "SchemaLanguageLengthZero", null));
133         }
134         // only W3C XML Schema 1.0 is supported
135
return schemaLanguage.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI);
136     }
137     
138     public LSResourceResolver JavaDoc getResourceResolver() {
139         return fLSResourceResolver;
140     }
141     
142     public void setResourceResolver(LSResourceResolver JavaDoc resourceResolver) {
143         fLSResourceResolver = resourceResolver;
144         fDOMEntityResolverWrapper.setEntityResolver(resourceResolver);
145         fXMLSchemaLoader.setEntityResolver(fDOMEntityResolverWrapper);
146     }
147     
148     public ErrorHandler JavaDoc getErrorHandler() {
149         return fErrorHandler;
150     }
151     
152     public void setErrorHandler(ErrorHandler JavaDoc errorHandler) {
153         fErrorHandler = errorHandler;
154         fErrorHandlerWrapper.setErrorHandler(errorHandler != null ? errorHandler : DraconianErrorHandler.getInstance());
155         fXMLSchemaLoader.setErrorHandler(fErrorHandlerWrapper);
156     }
157     
158     public Schema JavaDoc newSchema( Source JavaDoc[] schemas ) throws SAXException JavaDoc {
159         
160         // this will let the loader store parsed Grammars into the pool.
161
XMLGrammarPoolImplExtension pool = new XMLGrammarPoolImplExtension();
162         fXMLGrammarPoolWrapper.setGrammarPool(pool);
163         
164         XMLInputSource[] xmlInputSources = new XMLInputSource[schemas.length];
165         InputStream JavaDoc inputStream;
166         Reader JavaDoc reader;
167         for( int i=0; i<schemas.length; i++ ) {
168             Source JavaDoc source = schemas[i];
169             if (source instanceof StreamSource JavaDoc) {
170                 StreamSource JavaDoc streamSource = (StreamSource JavaDoc) source;
171                 String JavaDoc publicId = streamSource.getPublicId();
172                 String JavaDoc systemId = streamSource.getSystemId();
173                 inputStream = streamSource.getInputStream();
174                 reader = streamSource.getReader();
175                 xmlInputSources[i] = new XMLInputSource(publicId, systemId, null);
176                 xmlInputSources[i].setByteStream(inputStream);
177                 xmlInputSources[i].setCharacterStream(reader);
178             }
179             else if (source instanceof SAXSource JavaDoc) {
180                 SAXSource JavaDoc saxSource = (SAXSource JavaDoc) source;
181                 InputSource inputSource = saxSource.getInputSource();
182                 if (inputSource == null) {
183                     throw new SAXException JavaDoc(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),
184                             "SAXSourceNullInputSource", null));
185                 }
186                 xmlInputSources[i] = new SAXInputSource(saxSource.getXMLReader(), inputSource);
187             }
188             else if (source instanceof DOMSource JavaDoc) {
189                 DOMSource JavaDoc domSource = (DOMSource JavaDoc) source;
190                 Node JavaDoc node = domSource.getNode();
191                 String JavaDoc systemID = domSource.getSystemId();
192                 xmlInputSources[i] = new DOMInputSource(node, systemID);
193             }
194             else if (source == null) {
195                 throw new NullPointerException JavaDoc(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),
196                         "SchemaSourceArrayMemberNull", null));
197             }
198             else {
199                 throw new IllegalArgumentException JavaDoc(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),
200                         "SchemaFactorySourceUnrecognized",
201                         new Object JavaDoc [] {source.getClass().getName()}));
202             }
203         }
204         
205         try {
206             fXMLSchemaLoader.loadGrammar(xmlInputSources);
207         }
208         catch (XNIException e) {
209             // this should have been reported to users already.
210
throw Util.toSAXException(e);
211         }
212         catch (IOException JavaDoc e) {
213             // this hasn't been reported, so do so now.
214
SAXParseException JavaDoc se = new SAXParseException JavaDoc(e.getMessage(),null,e);
215             fErrorHandler.error(se);
216             throw se; // and we must throw it.
217
}
218         
219         // Clear reference to grammar pool.
220
fXMLGrammarPoolWrapper.setGrammarPool(null);
221         
222         // Select Schema implementation based on grammar count.
223
final int grammarCount = pool.getGrammarCount();
224         if (grammarCount > 1) {
225             return new XMLSchema(new ReadOnlyGrammarPool(pool));
226         }
227         else if (grammarCount == 1) {
228             Grammar[] grammars = pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
229             return new SimpleXMLSchema(grammars[0]);
230         }
231         else {
232             return EmptyXMLSchema.getInstance();
233         }
234     }
235     
236     public Schema JavaDoc newSchema() throws SAXException JavaDoc {
237         // Use a Schema that uses the system id as the equality source.
238
return new WeakReferenceXMLSchema();
239     }
240     
241     public boolean getFeature(String JavaDoc name)
242         throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
243         if (name == null) {
244             throw new NullPointerException JavaDoc(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),
245                     "FeatureNameNull", null));
246         }
247         if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
248             return (fSecurityManager != null);
249         }
250         try {
251             return fXMLSchemaLoader.getFeature(name);
252         }
253         catch (XMLConfigurationException e) {
254             String JavaDoc identifier = e.getIdentifier();
255             if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
256                 throw new SAXNotRecognizedException JavaDoc(
257                         SAXMessageFormatter.formatMessage(Locale.getDefault(),
258                         "feature-not-recognized", new Object JavaDoc [] {identifier}));
259             }
260             else {
261                 throw new SAXNotSupportedException JavaDoc(
262                         SAXMessageFormatter.formatMessage(Locale.getDefault(),
263                         "feature-not-supported", new Object JavaDoc [] {identifier}));
264             }
265         }
266     }
267     
268     public Object JavaDoc getProperty(String JavaDoc name)
269         throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
270         if (name == null) {
271             throw new NullPointerException JavaDoc(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),
272                     "ProperyNameNull", null));
273         }
274         if (name.equals(SECURITY_MANAGER)) {
275             return fSecurityManager;
276         }
277         else if (name.equals(XMLGRAMMAR_POOL)) {
278             throw new SAXNotSupportedException JavaDoc(
279                     SAXMessageFormatter.formatMessage(Locale.getDefault(),
280                     "property-not-supported", new Object JavaDoc [] {name}));
281         }
282         try {
283             return fXMLSchemaLoader.getProperty(name);
284         }
285         catch (XMLConfigurationException e) {
286             String JavaDoc identifier = e.getIdentifier();
287             if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
288                 throw new SAXNotRecognizedException JavaDoc(
289                         SAXMessageFormatter.formatMessage(Locale.getDefault(),
290                         "property-not-recognized", new Object JavaDoc [] {identifier}));
291             }
292             else {
293                 throw new SAXNotSupportedException JavaDoc(
294                         SAXMessageFormatter.formatMessage(Locale.getDefault(),
295                         "property-not-supported", new Object JavaDoc [] {identifier}));
296             }
297         }
298     }
299     
300     public void setFeature(String JavaDoc name, boolean value)
301         throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
302         if (name == null) {
303             throw new NullPointerException JavaDoc(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),
304                     "FeatureNameNull", null));
305         }
306         if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
307             fSecurityManager = value ? new SecurityManager JavaDoc() : null;
308             fXMLSchemaLoader.setProperty(SECURITY_MANAGER, fSecurityManager);
309             return;
310         }
311         try {
312             fXMLSchemaLoader.setFeature(name, value);
313         }
314         catch (XMLConfigurationException e) {
315             String JavaDoc identifier = e.getIdentifier();
316             if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
317                 throw new SAXNotRecognizedException JavaDoc(
318                         SAXMessageFormatter.formatMessage(Locale.getDefault(),
319                         "feature-not-recognized", new Object JavaDoc [] {identifier}));
320             }
321             else {
322                 throw new SAXNotSupportedException JavaDoc(
323                         SAXMessageFormatter.formatMessage(Locale.getDefault(),
324                         "feature-not-supported", new Object JavaDoc [] {identifier}));
325             }
326         }
327     }
328     
329     public void setProperty(String JavaDoc name, Object JavaDoc object)
330         throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
331         if (name == null) {
332             throw new NullPointerException JavaDoc(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),
333                     "ProperyNameNull", null));
334         }
335         if (name.equals(SECURITY_MANAGER)) {
336             fSecurityManager = (SecurityManager JavaDoc) object;
337             fXMLSchemaLoader.setProperty(SECURITY_MANAGER, fSecurityManager);
338             return;
339         }
340         else if (name.equals(XMLGRAMMAR_POOL)) {
341             throw new SAXNotSupportedException JavaDoc(
342                     SAXMessageFormatter.formatMessage(Locale.getDefault(),
343                     "property-not-supported", new Object JavaDoc [] {name}));
344         }
345         try {
346             fXMLSchemaLoader.setProperty(name, object);
347         }
348         catch (XMLConfigurationException e) {
349             String JavaDoc identifier = e.getIdentifier();
350             if (e.getType() == XMLConfigurationException.NOT_RECOGNIZED) {
351                 throw new SAXNotRecognizedException JavaDoc(
352                         SAXMessageFormatter.formatMessage(Locale.getDefault(),
353                         "property-not-recognized", new Object JavaDoc [] {identifier}));
354             }
355             else {
356                 throw new SAXNotSupportedException JavaDoc(
357                         SAXMessageFormatter.formatMessage(Locale.getDefault(),
358                         "property-not-supported", new Object JavaDoc [] {identifier}));
359             }
360         }
361     }
362     
363     /**
364      * Extension of XMLGrammarPoolImpl which exposes the number of
365      * grammars stored in the grammar pool.
366      */

367     static class XMLGrammarPoolImplExtension extends XMLGrammarPoolImpl {
368         
369         /** Constructs a grammar pool with a default number of buckets. */
370         public XMLGrammarPoolImplExtension() {
371             super();
372         }
373
374         /** Constructs a grammar pool with a specified number of buckets. */
375         public XMLGrammarPoolImplExtension(int initialCapacity) {
376             super(initialCapacity);
377         }
378         
379         /** Returns the number of grammars contained in this pool. */
380         int getGrammarCount() {
381             return fGrammarCount;
382         }
383         
384     } // XMLSchemaFactory.XMLGrammarPoolImplExtension
385

386     /**
387      * A grammar pool which wraps another.
388      */

389     static class XMLGrammarPoolWrapper implements XMLGrammarPool {
390
391         private XMLGrammarPool fGrammarPool;
392         
393         /*
394          * XMLGrammarPool methods
395          */

396         
397         public Grammar[] retrieveInitialGrammarSet(String JavaDoc grammarType) {
398             return fGrammarPool.retrieveInitialGrammarSet(grammarType);
399         }
400
401         public void cacheGrammars(String JavaDoc grammarType, Grammar[] grammars) {
402             fGrammarPool.cacheGrammars(grammarType, grammars);
403         }
404
405         public Grammar retrieveGrammar(XMLGrammarDescription desc) {
406             return fGrammarPool.retrieveGrammar(desc);
407         }
408
409         public void lockPool() {
410             fGrammarPool.lockPool();
411         }
412
413         public void unlockPool() {
414             fGrammarPool.unlockPool();
415         }
416
417         public void clear() {
418             fGrammarPool.clear();
419         }
420         
421         /*
422          * Other methods
423          */

424         
425         void setGrammarPool(XMLGrammarPool grammarPool) {
426             fGrammarPool = grammarPool;
427         }
428         
429         XMLGrammarPool getGrammarPool() {
430             return fGrammarPool;
431         }
432         
433     } // XMLSchemaFactory.XMLGrammarPoolWrapper
434

435 } // XMLSchemaFactory
436
Popular Tags