2
* Copyright 2005 The Apache Software Foundation.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.xerces.jaxp.validation;
19
import java.lang.ref.SoftReference;
20
import java.util.Locale;
21
import java.io.IOException;
23
import javax.xml.transform.Result;
24
import javax.xml.transform.Source;
25
import javax.xml.transform.stream.StreamSource;
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;
38
* <p>A validator helper for <code>StreamSource</code>s.</p>
40
* @author Michael Glavassevich, IBM
41
* @version $Id: StreamValidatorHelper.java 320552 2005-07-24 23:36:14Z mrglavas $
43
final class StreamValidatorHelper implements ValidatorHelper {
45
// feature identifiers
47
/** Feature identifier: parser settings. */
48
private static final String PARSER_SETTINGS =
49
Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS;
51
// property identifiers
53
/** Property identifier: entity resolver. */
54
private static final String ENTITY_RESOLVER =
55
Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY;
57
/** Property identifier: error handler. */
58
private static final String ERROR_HANDLER =
59
Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY;
61
/** Property identifier: error reporter. */
62
private static final String ERROR_REPORTER =
63
Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
65
/** Property identifier: XML Schema validator. */
66
private static final String SCHEMA_VALIDATOR =
67
Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY;
69
/** Property identifier: symbol table. */
70
private static final String SYMBOL_TABLE =
71
Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
73
/** Property identifier: validation manager. */
74
private static final String VALIDATION_MANAGER =
75
Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY;
81
/** SoftReference to parser configuration. **/
82
private SoftReference fConfiguration = new SoftReference(null);
84
/** Schema validator. **/
85
private org.apache.xerces.impl.xs.XMLSchemaValidator fSchemaValidator;
87
/** Component manager. **/
88
private XMLSchemaValidatorComponentManager fComponentManager;
90
public StreamValidatorHelper(XMLSchemaValidatorComponentManager componentManager) {
91
fComponentManager = componentManager;
92
fSchemaValidator = (org.apache.xerces.impl.xs.XMLSchemaValidator) fComponentManager.getProperty(SCHEMA_VALIDATOR);
95
public void validate(Source source, Result result)
96
throws SAXException, IOException {
98
final StreamSource streamSource = (StreamSource) source;
99
XMLInputSource input = new XMLInputSource(streamSource.getPublicId(), streamSource.getSystemId(), null);
100
input.setByteStream(streamSource.getInputStream());
101
input.setCharacterStream(streamSource.getReader());
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();
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));
116
fComponentManager.reset();
117
fSchemaValidator.setDocumentHandler(null);
122
catch (XMLParseException e) {
123
throw Util.toSAXParseException(e);
125
catch (XNIException e) {
126
throw Util.toSAXException(e);
130
throw new IllegalArgumentException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),
131
"SourceResultMismatch",
132
new Object [] {source.getClass().getName(), result.getClass().getName()}));
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);
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(config);
156
} // StreamValidatorHelper