KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > 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 com.sun.org.apache.xerces.internal.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 com.sun.org.apache.xerces.internal.impl.Constants;
33 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader;
34 import com.sun.org.apache.xerces.internal.util.DOMEntityResolverWrapper;
35 import com.sun.org.apache.xerces.internal.util.DOMInputSource;
36 import com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper;
37 import com.sun.org.apache.xerces.internal.util.SAXInputSource;
38 import com.sun.org.apache.xerces.internal.util.SAXMessageFormatter;
39 import com.sun.org.apache.xerces.internal.util.SecurityManager;
40 import com.sun.org.apache.xerces.internal.util.XMLGrammarPoolImpl;
41 import com.sun.org.apache.xerces.internal.xni.XNIException;
42 import com.sun.org.apache.xerces.internal.xni.grammars.Grammar;
43 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
44 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
45 import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
46 import com.sun.org.apache.xerces.internal.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.2.2.2 2007/10/20 17:56:45 joehw 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         // Enable secure processing feature by default
113
fSecurityManager = new SecurityManager JavaDoc();
114         fXMLSchemaLoader.setProperty(SECURITY_MANAGER, fSecurityManager);
115     }
116     
117     /**
118      * <p>Is specified schema supported by this <code>SchemaFactory</code>?</p>
119      *
120      * @param schemaLanguage Specifies the schema language which the returned <code>SchemaFactory</code> will understand.
121      * <code>schemaLanguage</code> must specify a <a HREF="#schemaLanguage">valid</a> schema language.
122      *
123      * @return <code>true</code> if <code>SchemaFactory</code> supports <code>schemaLanguage</code>, else <code>false</code>.
124      *
125      * @throws NullPointerException If <code>schemaLanguage</code> is <code>null</code>.
126      * @throws IllegalArgumentException If <code>schemaLanguage.length() == 0</code>
127      * or <code>schemaLanguage</code> does not specify a <a HREF="#schemaLanguage">valid</a> schema language.
128      */

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

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

390     /**
391      * A grammar pool which wraps another.
392      */

393     static class XMLGrammarPoolWrapper implements XMLGrammarPool {
394
395         private XMLGrammarPool fGrammarPool;
396         
397         /*
398          * XMLGrammarPool methods
399          */

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

428         
429         void setGrammarPool(XMLGrammarPool grammarPool) {
430             fGrammarPool = grammarPool;
431         }
432         
433         XMLGrammarPool getGrammarPool() {
434             return fGrammarPool;
435         }
436         
437     } // XMLSchemaFactory.XMLGrammarPoolWrapper
438

439 } // XMLSchemaFactory
440
Popular Tags