KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.ref.SoftReference JavaDoc;
20 import java.util.Locale JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 import javax.xml.transform.Result JavaDoc;
24 import javax.xml.transform.Source JavaDoc;
25 import javax.xml.transform.sax.SAXTransformerFactory JavaDoc;
26 import javax.xml.transform.sax.TransformerHandler JavaDoc;
27 import javax.xml.transform.stream.StreamSource JavaDoc;
28 import javax.xml.transform.stream.StreamResult JavaDoc;
29 import javax.xml.transform.TransformerConfigurationException JavaDoc;
30 import javax.xml.transform.TransformerException JavaDoc;
31 import javax.xml.transform.TransformerFactoryConfigurationError JavaDoc;
32
33 import com.sun.org.apache.xerces.internal.impl.Constants;
34 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
35 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
36 import com.sun.org.apache.xerces.internal.parsers.XML11Configuration;
37 import com.sun.org.apache.xerces.internal.xni.XNIException;
38 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
39 import com.sun.org.apache.xerces.internal.xni.parser.XMLParseException;
40 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration;
41 import org.xml.sax.SAXException JavaDoc;
42
43 /**
44  * <p>A validator helper for <code>StreamSource</code>s.</p>
45  *
46  * @author Michael Glavassevich, IBM
47  * @author <a HREF="mailto:Sunitha.Reddy@Sun.com">Sunitha Reddy</a>
48  * @version $Id: StreamValidatorHelper.java,v 1.2 2005/09/26 13:02:51 sunithareddy Exp $
49  */

50 final class StreamValidatorHelper implements ValidatorHelper {
51     
52     // feature identifiers
53

54     /** Feature identifier: parser settings. */
55     private static final String JavaDoc PARSER_SETTINGS =
56         Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS;
57     
58     // property identifiers
59

60     /** Property identifier: entity resolver. */
61     private static final String JavaDoc ENTITY_RESOLVER =
62         Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY;
63     
64     /** Property identifier: error handler. */
65     private static final String JavaDoc ERROR_HANDLER =
66         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY;
67     
68     /** Property identifier: error reporter. */
69     private static final String JavaDoc ERROR_REPORTER =
70         Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
71     
72     /** Property identifier: XML Schema validator. */
73     private static final String JavaDoc SCHEMA_VALIDATOR =
74         Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY;
75     
76     /** Property identifier: symbol table. */
77     private static final String JavaDoc SYMBOL_TABLE =
78         Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
79     
80     /** Property identifier: validation manager. */
81     private static final String JavaDoc VALIDATION_MANAGER =
82         Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY;
83     
84     //
85
// Data
86
//
87

88     /** SoftReference to parser configuration. **/
89     private SoftReference JavaDoc fConfiguration = new SoftReference JavaDoc(null);
90     
91     /** Schema validator. **/
92     private com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator fSchemaValidator;
93     
94     /** Component manager. **/
95     private XMLSchemaValidatorComponentManager fComponentManager;
96     
97     private ValidatorHandlerImpl handler = null;
98     
99     public StreamValidatorHelper(XMLSchemaValidatorComponentManager componentManager) {
100         fComponentManager = componentManager;
101         fSchemaValidator = (com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator) fComponentManager.getProperty(SCHEMA_VALIDATOR);
102     }
103
104     public void validate(Source JavaDoc source, Result JavaDoc result)
105         throws SAXException JavaDoc, IOException JavaDoc {
106         if (result == null || result instanceof StreamResult JavaDoc) {
107             final StreamSource JavaDoc streamSource = (StreamSource JavaDoc) source;
108             TransformerHandler JavaDoc identityTransformerHandler ;
109             
110             if( result!=null ) {
111                 try {
112                     SAXTransformerFactory JavaDoc tf = (SAXTransformerFactory JavaDoc)SAXTransformerFactory.newInstance();
113                     identityTransformerHandler = tf.newTransformerHandler();
114                 } catch (TransformerConfigurationException JavaDoc e) {
115                     throw new TransformerFactoryConfigurationError JavaDoc(e);
116                 }
117                 
118                 handler = new ValidatorHandlerImpl(fComponentManager);
119                 handler.setContentHandler(identityTransformerHandler);
120                 identityTransformerHandler.setResult(result);
121             }
122             
123             XMLInputSource input = new XMLInputSource(streamSource.getPublicId(), streamSource.getSystemId(), null);
124             input.setByteStream(streamSource.getInputStream());
125             input.setCharacterStream(streamSource.getReader());
126             
127             // Gets the parser configuration. We'll create and initialize a new one, if we
128
// haven't created one before or if the previous one was garbage collected.
129
XMLParserConfiguration config = (XMLParserConfiguration) fConfiguration.get();
130             if (config == null) {
131                 config = initialize();
132             }
133             // If settings have changed on the component manager, refresh the error handler and entity resolver.
134
else if (fComponentManager.getFeature(PARSER_SETTINGS)) {
135                 config.setProperty(ENTITY_RESOLVER, fComponentManager.getProperty(ENTITY_RESOLVER));
136                 config.setProperty(ERROR_HANDLER, fComponentManager.getProperty(ERROR_HANDLER));
137             }
138             
139             // prepare for parse
140
fComponentManager.reset();
141             fSchemaValidator.setDocumentHandler(handler);
142                                     
143             try {
144                 config.parse(input);
145             }
146             catch (XMLParseException e) {
147                 throw Util.toSAXParseException(e);
148             }
149             catch (XNIException e) {
150                 throw Util.toSAXException(e);
151             }
152             return;
153         }
154         throw new IllegalArgumentException JavaDoc(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),
155                 "SourceResultMismatch",
156                 new Object JavaDoc [] {source.getClass().getName(), result.getClass().getName()}));
157     }
158     
159     private XMLParserConfiguration initialize() {
160         XML11Configuration config = new XML11Configuration();
161         config.setProperty(ENTITY_RESOLVER, fComponentManager.getProperty(ENTITY_RESOLVER));
162         config.setProperty(ERROR_HANDLER, fComponentManager.getProperty(ERROR_HANDLER));
163         XMLErrorReporter errorReporter = (XMLErrorReporter) fComponentManager.getProperty(ERROR_REPORTER);
164         config.setProperty(ERROR_REPORTER, errorReporter);
165         // add message formatters
166
if (errorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) {
167             XMLMessageFormatter xmft = new XMLMessageFormatter();
168             errorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft);
169             errorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft);
170         }
171         config.setProperty(SYMBOL_TABLE, fComponentManager.getProperty(SYMBOL_TABLE));
172         config.setProperty(VALIDATION_MANAGER, fComponentManager.getProperty(VALIDATION_MANAGER));
173         config.setDocumentHandler(fSchemaValidator);
174         config.setDTDHandler(null);
175         config.setDTDContentModelHandler(null);
176         fConfiguration = new SoftReference JavaDoc(config);
177         return config;
178     }
179
180 } // StreamValidatorHelper
181
Popular Tags