KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://jaxp.dev.java.net/CDDLv1.0.html.
9  * See the License for the specific language governing
10  * permissions and limitations under the License.
11  *
12  * When distributing Covered Code, include this CDDL
13  * HEADER in each file and include the License file at
14  * https://jaxp.dev.java.net/CDDLv1.0.html
15  * If applicable add the following below this CDDL HEADER
16  * with the fields enclosed by brackets "[]" replaced with
17  * your own identifying information: Portions Copyright
18  * [year] [name of copyright owner]
19  */

20
21 /*
22  * $Id: XMLEntityReader.java,v 1.3 2005/11/03 17:02:21 jeffsuttor Exp $
23  * @(#)StAXValidatorHelper.java 1.3 05/11/17
24  *
25  * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.org.apache.xerces.internal.jaxp.validation;
29
30 import java.io.IOException JavaDoc;
31 import java.util.Locale JavaDoc;
32
33 import javax.xml.transform.Result JavaDoc;
34 import javax.xml.transform.Source JavaDoc;
35 import javax.xml.transform.Transformer JavaDoc;
36 import javax.xml.transform.TransformerConfigurationException JavaDoc;
37 import javax.xml.transform.TransformerException JavaDoc;
38 import javax.xml.transform.TransformerFactoryConfigurationError JavaDoc;
39 import javax.xml.transform.sax.SAXResult JavaDoc;
40 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
41 import javax.xml.transform.sax.TransformerHandler JavaDoc;
42 import javax.xml.transform.stax.StAXResult;
43 import javax.xml.transform.stax.StAXSource;
44
45 import org.xml.sax.SAXException JavaDoc;
46
47 /**
48  * <p>A validator helper for <code>StAXSource</code>s.</p>
49  *
50  * @author <a HREF="mailto:Sunitha.Reddy@Sun.com">Sunitha Reddy</a>
51  */

52 public final class StAXValidatorHelper implements ValidatorHelper {
53     
54     /** Component manager. **/
55     private XMLSchemaValidatorComponentManager fComponentManager;
56     
57     private Transformer JavaDoc identityTransformer1 = null;
58     private TransformerHandler JavaDoc identityTransformer2 = null;
59     private ValidatorHandlerImpl handler = null;
60     
61     /** Creates a new instance of StaxValidatorHelper */
62     public StAXValidatorHelper(XMLSchemaValidatorComponentManager componentManager) {
63         fComponentManager = componentManager;
64     }
65     
66     public void validate(Source JavaDoc source, Result JavaDoc result)
67         throws SAXException JavaDoc, IOException JavaDoc {
68         
69         if (result == null || result instanceof StAXResult) {
70          
71             if( identityTransformer1==null ) {
72                 try {
73                     SAXTransformerFactory JavaDoc tf = (SAXTransformerFactory JavaDoc)SAXTransformerFactory.newInstance();
74                     identityTransformer1 = tf.newTransformer();
75                     identityTransformer2 = tf.newTransformerHandler();
76                 } catch (TransformerConfigurationException JavaDoc e) {
77                     // this is impossible, but again better safe than sorry
78
throw new TransformerFactoryConfigurationError JavaDoc(e);
79                 }
80             }
81
82             if( result!=null ) {
83                 handler = new ValidatorHandlerImpl(fComponentManager);
84                 handler.setContentHandler(identityTransformer2);
85                 identityTransformer2.setResult(result);
86             }
87
88             try {
89                 identityTransformer1.transform( source, new SAXResult JavaDoc(handler) );
90             } catch (TransformerException JavaDoc e) {
91                 if( e.getException() instanceof SAXException JavaDoc )
92                     throw (SAXException JavaDoc)e.getException();
93                 throw new SAXException JavaDoc(e);
94             } finally {
95                 handler.setContentHandler(null);
96             }
97             return;
98         }
99         throw new IllegalArgumentException JavaDoc(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),
100                 "SourceResultMismatch",
101                 new Object JavaDoc [] {source.getClass().getName(), result.getClass().getName()}));
102     }
103 }
104
Popular Tags