KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > 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 org.apache.xerces.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.stream.StreamSource JavaDoc;
26
27 import org.apache.xerces.impl.Constants;
28 import org.apache.xerces.impl.XMLErrorReporter;
29 import org.apache.xerces.impl.msg.XMLMessageFormatter;
30 import org.apache.xerces.parsers.XML11Configuration;
31 import org.apache.xerces.xni.XNIException;
32 import org.apache.xerces.xni.parser.XMLInputSource;
33 import org.apache.xerces.xni.parser.XMLParseException;
34 import org.apache.xerces.xni.parser.XMLParserConfiguration;
35 import org.xml.sax.SAXException JavaDoc;
36
37 /**
38  * <p>A validator helper for <code>StreamSource</code>s.</p>
39  *
40  * @author Michael Glavassevich, IBM
41  * @version $Id: StreamValidatorHelper.java,v 1.2 2005/07/24 23:36:14 mrglavas Exp $
42  */

43 final class StreamValidatorHelper implements ValidatorHelper {
44     
45     // feature identifiers
46

47     /** Feature identifier: parser settings. */
48     private static final String JavaDoc PARSER_SETTINGS =
49         Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS;
50     
51     // property identifiers
52

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

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