KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > jaxp > validation > xs > SchemaFactoryImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57 package com.sun.org.apache.xerces.internal.jaxp.validation.xs;
58
59 import java.io.IOException JavaDoc;
60 import java.util.Locale JavaDoc;
61
62 import javax.xml.XMLConstants JavaDoc;
63 import javax.xml.transform.Source JavaDoc;
64 import javax.xml.validation.Schema JavaDoc;
65 import javax.xml.validation.SchemaFactory JavaDoc;
66
67 import com.sun.org.apache.xerces.internal.impl.Constants;
68 import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader;
69 import com.sun.org.apache.xerces.internal.impl.xs.XSMessageFormatter;
70 import com.sun.org.apache.xerces.internal.jaxp.validation.ReadonlyGrammarPool;
71 import com.sun.org.apache.xerces.internal.jaxp.validation.Util;
72 import com.sun.org.apache.xerces.internal.jaxp.validation.XercesConstants;
73 import com.sun.org.apache.xerces.internal.util.DOMEntityResolverWrapper;
74 import com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper;
75 import com.sun.org.apache.xerces.internal.util.SAXMessageFormatter;
76 import com.sun.org.apache.xerces.internal.util.XMLGrammarPoolImpl;
77 import com.sun.org.apache.xerces.internal.xni.XNIException;
78 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;
79 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarPool;
80 import org.w3c.dom.ls.LSResourceResolver JavaDoc;
81 import org.xml.sax.ErrorHandler JavaDoc;
82 import org.xml.sax.SAXException JavaDoc;
83 import org.xml.sax.SAXParseException JavaDoc;
84 import com.sun.org.apache.xerces.internal.util.SecurityManager;
85 import org.xml.sax.SAXNotRecognizedException JavaDoc;
86 import org.xml.sax.SAXNotSupportedException JavaDoc;
87
88 /**
89  * {@link SchemaFactory} for XML Schema.
90  *
91  * @author
92  * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
93  */

94 public class SchemaFactoryImpl extends SchemaFactory JavaDoc {
95     
96     private final XMLSchemaLoader loader = new XMLSchemaLoader();
97     private static XSMessageFormatter messageFormatter = new XSMessageFormatter();
98     /**
99      * User-specified ErrorHandler. can be null.
100      */

101     private ErrorHandler JavaDoc errorHandler;
102     
103     private LSResourceResolver JavaDoc resourceResolver;
104     
105     private SAXParseException JavaDoc lastException;
106     
107     private final SecurityManager JavaDoc secureProcessing ;
108     
109     private boolean enableSP;
110     
111     public SchemaFactoryImpl() {
112         secureProcessing = new SecurityManager JavaDoc();
113         // intercept error report and remember the last thrown exception.
114
loader.setErrorHandler(new ErrorHandlerWrapper(new ErrorHandler JavaDoc() {
115             public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
116                 if( errorHandler!=null ) errorHandler.warning(exception);
117             }
118             
119             public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
120                 lastException = exception;
121                 if( errorHandler!=null ) errorHandler.error(exception);
122                 else throw exception;
123             }
124             
125             public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
126                 lastException = exception;
127                 if( errorHandler!=null ) errorHandler.fatalError(exception);
128                 else throw exception;
129             }
130         }));
131     }
132     
133     
134     /**
135      * <p>Is specified schema supported by this <code>SchemaFactory</code>?</p>
136      *
137      * @param schemaLanguage Specifies the schema language which the returned <code>SchemaFactory</code> will understand.
138      * <code>schemaLanguage</code> must specify a <a HREF="#schemaLanguage">valid</a> schema language.
139      *
140      * @return <code>true</code> if <code>SchemaFactory</code> supports <code>schemaLanguage</code>, else <code>false</code>.
141      *
142      * @throws NullPointerException If <code>schemaLanguage</code> is <code>null</code>.
143      * @throws IllegalArgumentException If <code>schemaLanguage.length() == 0</code>
144      * or <code>schemaLanguage</code> does not specify a <a HREF="#schemaLanguage">valid</a> schema language.
145      */

146     public boolean isSchemaLanguageSupported(String JavaDoc schemaLanguage) {
147         
148         if (schemaLanguage == null) {
149             throw new NullPointerException JavaDoc(
150             messageFormatter.formatMessage(Locale.getDefault(),
151             "SchemaLanguageSupportedErrorWhenNull",
152             new Object JavaDoc [] {this.getClass().getName()}));
153         }
154         
155         if (schemaLanguage.length() == 0) {
156             throw new IllegalArgumentException JavaDoc(
157             messageFormatter.formatMessage(Locale.getDefault(),
158             "SchemaLanguageSupportedErrorWhenLength",
159             new Object JavaDoc [] {this.getClass().getName()}));
160         }
161         
162         // understand W3C Schema and RELAX NG
163
if (schemaLanguage.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)
164         || schemaLanguage.equals(XMLConstants.RELAXNG_NS_URI)) {
165             return true;
166         }
167         
168         // don't know how to validate anything else
169
return false;
170     }
171     
172     public LSResourceResolver JavaDoc getResourceResolver() {
173         return resourceResolver;
174     }
175     
176     public void setResourceResolver(LSResourceResolver JavaDoc resourceResolver) {
177         this.resourceResolver = resourceResolver;
178         loader.setEntityResolver(new DOMEntityResolverWrapper(resourceResolver));
179     }
180     
181     public ErrorHandler JavaDoc getErrorHandler() {
182         return errorHandler;
183     }
184     
185     public void setErrorHandler(ErrorHandler JavaDoc errorHandler) {
186         this.errorHandler = errorHandler;
187     }
188     
189     
190     
191     public Schema JavaDoc newSchema( Source JavaDoc[] schemas ) throws SAXException JavaDoc {
192         
193         lastException = null;
194         
195         // this will let the loader store parsed Grammars into the pool.
196
XMLGrammarPool pool = new XMLGrammarPoolImpl();
197         loader.setProperty(XercesConstants.XMLGRAMMAR_POOL,pool);
198         loader.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING,true);
199         if(enableSP)
200             loader.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY,secureProcessing);
201         else
202             loader.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY,null);
203         
204         for( int i=0; i<schemas.length; i++ ) {
205             try {
206                 loader.loadGrammar(schemas[i]);
207             } catch (XNIException e) {
208                 // this should have been reported to users already.
209
throw Util.toSAXException(e);
210             } catch (IOException JavaDoc e) {
211                 // this hasn't been reported, so do so now.
212
SAXParseException JavaDoc se = new SAXParseException JavaDoc(e.getMessage(),null,e);
213                 errorHandler.error(se);
214                 throw se; // and we must throw it.
215
}
216         }
217         
218         // if any error had been reported, throw it.
219
if( lastException!=null )
220             throw lastException;
221         
222         // make sure no further grammars are added by making it read-only.
223
return new SchemaImpl(new ReadonlyGrammarPool(pool),true);
224     }
225     
226     public Schema JavaDoc newSchema() throws SAXException JavaDoc {
227         // use a pool that uses the system id as the equality source.
228
return new SchemaImpl(new XMLGrammarPoolImpl() {
229             public boolean equals(XMLGrammarDescription desc1, XMLGrammarDescription desc2) {
230                 String JavaDoc sid1 = desc1.getExpandedSystemId();
231                 String JavaDoc sid2 = desc2.getExpandedSystemId();
232                 if( sid1!=null && sid2!=null )
233                     return sid1.equals(sid2);
234                 if( sid1==null && sid2==null )
235                     return true;
236                 return false;
237             }
238             public int hashCode(XMLGrammarDescription desc) {
239                 String JavaDoc s = desc.getExpandedSystemId();
240                 if(s!=null) return s.hashCode();
241                 return 0;
242             }
243         }, false);
244     }
245     
246     public void setFeature(String JavaDoc name, boolean value) throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
247         if(name==null) throw new NullPointerException JavaDoc(SAXMessageFormatter.formatMessage(Locale.getDefault(),
248         "nullparameter",new Object JavaDoc[] {"setFeature(String,boolean)"}));
249         if(name.equals(Constants.FEATURE_SECURE_PROCESSING)){
250             enableSP = value;
251         }else throw new SAXNotRecognizedException JavaDoc(SAXMessageFormatter.formatMessage(Locale.getDefault(),
252         "feature-not-supported", new Object JavaDoc [] {name}));
253         
254     }
255     
256     public boolean getFeature(String JavaDoc name) throws SAXNotRecognizedException JavaDoc, SAXNotSupportedException JavaDoc {
257         if(name==null) throw new NullPointerException JavaDoc(SAXMessageFormatter.formatMessage(Locale.getDefault(),
258         "nullparameter",new Object JavaDoc[] {"getFeature(String)"}));
259         if(name.equals(Constants.FEATURE_SECURE_PROCESSING))
260             return enableSP;
261         else throw new SAXNotRecognizedException JavaDoc(SAXMessageFormatter.formatMessage(Locale.getDefault(),
262         "feature-not-supported", new Object JavaDoc [] {name}));
263     }
264 }
265
Popular Tags