KickJava   Java API By Example, From Geeks To Geeks.

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


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;
58
59 import java.io.IOException JavaDoc;
60 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
61 import javax.xml.parsers.SAXParserFactory JavaDoc;
62 import javax.xml.transform.Result JavaDoc;
63 import javax.xml.transform.Source JavaDoc;
64 import javax.xml.transform.Transformer JavaDoc;
65 import javax.xml.transform.TransformerConfigurationException JavaDoc;
66 import javax.xml.transform.TransformerException JavaDoc;
67 import javax.xml.transform.TransformerFactoryConfigurationError JavaDoc;
68 import javax.xml.transform.dom.DOMResult JavaDoc;
69 import javax.xml.transform.dom.DOMSource JavaDoc;
70 import javax.xml.transform.sax.SAXResult JavaDoc;
71 import javax.xml.transform.sax.SAXSource JavaDoc;
72 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
73 import javax.xml.transform.sax.TransformerHandler JavaDoc;
74 import javax.xml.transform.stream.StreamSource JavaDoc;
75 import javax.xml.validation.Schema JavaDoc;
76 import javax.xml.validation.Validator JavaDoc;
77 import javax.xml.validation.ValidatorHandler JavaDoc;
78 import org.w3c.dom.ls.LSInput JavaDoc;
79 import org.w3c.dom.ls.LSResourceResolver JavaDoc;
80 import org.xml.sax.EntityResolver JavaDoc;
81 import org.xml.sax.ErrorHandler JavaDoc;
82 import org.xml.sax.InputSource JavaDoc;
83 import org.xml.sax.SAXException JavaDoc;
84 import org.xml.sax.SAXParseException JavaDoc;
85 import org.xml.sax.XMLReader JavaDoc;
86
87 /**
88  * <b>(For Implementors)</b> Default implementation of {@link Validator}.
89  *
90  * <p>
91  * This class is intended to be used in conjunction with
92  * {@link AbstractSchemaImpl} to promote consistent
93  * behaviors among {@link Schema} implementations.
94  *
95  * <p>
96  * This class wraps a {@link javax.xml.validation.ValidatorHandler}
97  * object and implements the {@link Validator} semantics.
98  *
99  * @author <a HREF="mailto:Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi</a>
100  * @version $Revision: 1.5 $, $Date: 2004/07/12 20:38:39 $
101  * @since 1.5
102  */

103 class ValidatorImpl extends Validator JavaDoc {
104     
105     /**
106      * The actual validation will be done by this object.
107      */

108     private final ValidatorHandlerImpl handler;
109     
110     /**
111      * Lazily created identity transformer.
112      */

113     private Transformer JavaDoc identityTransformer1 = null;
114     private TransformerHandler JavaDoc identityTransformer2 = null;
115     
116     ValidatorImpl( ValidatorHandlerImpl _handler ) {
117         this.handler = _handler;
118     }
119     
120     public LSResourceResolver JavaDoc getResourceResolver() {
121         return handler.getResourceResolver();
122     }
123     
124     
125     public ErrorHandler JavaDoc getErrorHandler() {
126         return handler.getErrorHandler();
127     }
128     
129     public void setResourceResolver(LSResourceResolver JavaDoc resolver) {
130         handler.setResourceResolver(resolver);
131     }
132     
133     public void setErrorHandler(ErrorHandler JavaDoc errorHandler) {
134         handler.setErrorHandler(errorHandler);
135     }
136     
137     public void validate(Source JavaDoc source, Result JavaDoc result) throws SAXException JavaDoc, IOException JavaDoc {
138         if( source instanceof DOMSource JavaDoc ) {
139             if( result!=null && !(result instanceof DOMResult JavaDoc) )
140                 throw new IllegalArgumentException JavaDoc(result.getClass().getName());
141             process( (DOMSource JavaDoc)source, (DOMResult JavaDoc)result );
142             return;
143         }
144         if( source instanceof SAXSource JavaDoc ) {
145             if( result!=null && !(result instanceof SAXResult JavaDoc) )
146                 throw new IllegalArgumentException JavaDoc(result.getClass().getName());
147             process( (SAXSource JavaDoc)source, (SAXResult JavaDoc)result );
148             return;
149         }
150         if( source instanceof StreamSource JavaDoc ) {
151             if( result!=null )
152                 throw new IllegalArgumentException JavaDoc(result.getClass().getName());
153             StreamSource JavaDoc ss = (StreamSource JavaDoc)source;
154             InputSource JavaDoc is = new InputSource JavaDoc();
155             is.setByteStream(ss.getInputStream());
156             is.setCharacterStream(ss.getReader());
157             is.setPublicId(ss.getPublicId());
158             is.setSystemId(ss.getSystemId());
159             process( new SAXSource JavaDoc(is), null );
160             return;
161         }
162         throw new IllegalArgumentException JavaDoc(source.getClass().getName());
163     }
164     
165     /**
166      * Parses a {@link SAXSource} potentially to a {@link SAXResult}.
167      */

168     private void process(SAXSource JavaDoc source, SAXResult JavaDoc result) throws IOException JavaDoc, SAXException JavaDoc {
169         if( result!=null ) {
170             handler.setContentHandler(result.getHandler());
171         }
172         
173         try {
174             XMLReader JavaDoc reader = source.getXMLReader();
175             if( reader==null ) {
176                 // create one now
177
SAXParserFactory JavaDoc spf = SAXParserFactory.newInstance();
178                 spf.setNamespaceAware(true);
179                 try {
180                     reader = spf.newSAXParser().getXMLReader();
181                 } catch( Exception JavaDoc e ) {
182                     // this is impossible, but better safe than sorry
183
throw new FactoryConfigurationError JavaDoc(e);
184                 }
185             }
186             
187             reader.setErrorHandler(errorForwarder);
188             reader.setEntityResolver(resolutionForwarder);
189             reader.setContentHandler(handler);
190             
191             InputSource JavaDoc is = source.getInputSource();
192             reader.parse(is);
193         } finally {
194             // release the reference to user's handler ASAP
195
handler.setContentHandler(null);
196         }
197     }
198     
199     /**
200      * Parses a {@link DOMSource} potentially to a {@link DOMResult}.
201      */

202     private void process( DOMSource JavaDoc source, DOMResult JavaDoc result ) throws SAXException JavaDoc {
203         if( identityTransformer1==null ) {
204             try {
205                 SAXTransformerFactory JavaDoc tf = (SAXTransformerFactory JavaDoc)SAXTransformerFactory.newInstance();
206                 identityTransformer1 = tf.newTransformer();
207                 identityTransformer2 = tf.newTransformerHandler();
208             } catch (TransformerConfigurationException JavaDoc e) {
209                 // this is impossible, but again better safe than sorry
210
throw new TransformerFactoryConfigurationError JavaDoc(e);
211             }
212         }
213
214         if( result!=null ) {
215             handler.setContentHandler(identityTransformer2);
216             identityTransformer2.setResult(result);
217         }
218         
219         try {
220             identityTransformer1.transform( source, new SAXResult JavaDoc(handler) );
221         } catch (TransformerException JavaDoc e) {
222             if( e.getException() instanceof SAXException JavaDoc )
223                 throw (SAXException JavaDoc)e.getException();
224             throw new SAXException JavaDoc(e);
225         } finally {
226             handler.setContentHandler(null);
227         }
228     }
229     
230     /**
231      * Forwards the error to the {@link ValidatorHandler}.
232      * If the {@link ValidatorHandler} doesn't have its own
233      * {@link ErrorHandler}, behave draconian.
234      */

235     private final ErrorHandler JavaDoc errorForwarder = new ErrorHandler JavaDoc() {
236         public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
237             ErrorHandler JavaDoc realHandler = handler.getErrorHandler();
238             if( realHandler!=null )
239                 realHandler.warning(exception);
240         }
241         
242         public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
243             ErrorHandler JavaDoc realHandler = handler.getErrorHandler();
244             if( realHandler!=null )
245                 realHandler.error(exception);
246             else
247                 throw exception;
248         }
249         
250         public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
251             ErrorHandler JavaDoc realHandler = handler.getErrorHandler();
252             if( realHandler!=null )
253                 realHandler.fatalError(exception);
254             else
255                 throw exception;
256         }
257     };
258     
259     /**
260      * Forwards the entity resolution to the {@link ValidatorHandler}.
261      * If the {@link ValidatorHandler} doesn't have its own
262      * {@link DOMResourceResolver}, let the parser do the resolution.
263      */

264     private final EntityResolver JavaDoc resolutionForwarder = new EntityResolver JavaDoc() {
265         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
266             LSResourceResolver JavaDoc resolver = handler.getResourceResolver();
267             if( resolver==null ) return null;
268             
269             LSInput JavaDoc di = resolver.resolveResource(null,null,publicId,systemId,null);
270             if(di==null) return null;
271             
272             InputSource JavaDoc r = new InputSource JavaDoc();
273             r.setByteStream(di.getByteStream());
274             r.setCharacterStream(di.getCharacterStream());
275             r.setEncoding(di.getEncoding());
276             r.setPublicId(di.getPublicId());
277             r.setSystemId(di.getSystemId());
278             return r;
279         }
280     };
281     
282     public void reset() {
283         handler.reset();
284         
285         // I don't think this is necessary, but I don't think it hurts either.
286
// so reset just for the kick.
287
if(identityTransformer1!=null) {
288             identityTransformer1.reset();
289         }
290     }
291 }
292
Popular Tags