001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.jaxp.validation;
019:
020: import java.lang.ref.SoftReference;
021: import java.util.Locale;
022: import java.io.IOException;
023:
024: import javax.xml.transform.Result;
025: import javax.xml.transform.Source;
026: import javax.xml.transform.stream.StreamSource;
027:
028: import org.apache.xerces.impl.Constants;
029: import org.apache.xerces.impl.XMLErrorReporter;
030: import org.apache.xerces.impl.msg.XMLMessageFormatter;
031: import org.apache.xerces.impl.xs.XMLSchemaValidator;
032: import org.apache.xerces.parsers.XML11Configuration;
033: import org.apache.xerces.xni.XNIException;
034: import org.apache.xerces.xni.parser.XMLInputSource;
035: import org.apache.xerces.xni.parser.XMLParseException;
036: import org.apache.xerces.xni.parser.XMLParserConfiguration;
037: import org.xml.sax.SAXException;
038:
039: /**
040: * <p>A validator helper for <code>StreamSource</code>s.</p>
041: *
042: * @author Michael Glavassevich, IBM
043: * @version $Id: StreamValidatorHelper.java 542521 2007-05-29 13:58:42Z mrglavas $
044: */
045: final class StreamValidatorHelper implements ValidatorHelper {
046:
047: // feature identifiers
048:
049: /** Feature identifier: parser settings. */
050: private static final String PARSER_SETTINGS = Constants.XERCES_FEATURE_PREFIX
051: + Constants.PARSER_SETTINGS;
052:
053: // property identifiers
054:
055: /** Property identifier: entity resolver. */
056: private static final String ENTITY_RESOLVER = Constants.XERCES_PROPERTY_PREFIX
057: + Constants.ENTITY_RESOLVER_PROPERTY;
058:
059: /** Property identifier: error handler. */
060: private static final String ERROR_HANDLER = Constants.XERCES_PROPERTY_PREFIX
061: + Constants.ERROR_HANDLER_PROPERTY;
062:
063: /** Property identifier: error reporter. */
064: private static final String ERROR_REPORTER = Constants.XERCES_PROPERTY_PREFIX
065: + Constants.ERROR_REPORTER_PROPERTY;
066:
067: /** Property identifier: XML Schema validator. */
068: private static final String SCHEMA_VALIDATOR = Constants.XERCES_PROPERTY_PREFIX
069: + Constants.SCHEMA_VALIDATOR_PROPERTY;
070:
071: /** Property identifier: symbol table. */
072: private static final String SYMBOL_TABLE = Constants.XERCES_PROPERTY_PREFIX
073: + Constants.SYMBOL_TABLE_PROPERTY;
074:
075: /** Property identifier: validation manager. */
076: private static final String VALIDATION_MANAGER = Constants.XERCES_PROPERTY_PREFIX
077: + Constants.VALIDATION_MANAGER_PROPERTY;
078:
079: //
080: // Data
081: //
082:
083: /** SoftReference to parser configuration. **/
084: private SoftReference fConfiguration = new SoftReference(null);
085:
086: /** Schema validator. **/
087: private final XMLSchemaValidator fSchemaValidator;
088:
089: /** Component manager. **/
090: private final XMLSchemaValidatorComponentManager fComponentManager;
091:
092: public StreamValidatorHelper(
093: XMLSchemaValidatorComponentManager componentManager) {
094: fComponentManager = componentManager;
095: fSchemaValidator = (XMLSchemaValidator) fComponentManager
096: .getProperty(SCHEMA_VALIDATOR);
097: }
098:
099: public void validate(Source source, Result result)
100: throws SAXException, IOException {
101: if (result == null) {
102: final StreamSource streamSource = (StreamSource) source;
103: XMLInputSource input = new XMLInputSource(streamSource
104: .getPublicId(), streamSource.getSystemId(), null);
105: input.setByteStream(streamSource.getInputStream());
106: input.setCharacterStream(streamSource.getReader());
107:
108: // Gets the parser configuration. We'll create and initialize a new one, if we
109: // haven't created one before or if the previous one was garbage collected.
110: XMLParserConfiguration config = (XMLParserConfiguration) fConfiguration
111: .get();
112: if (config == null) {
113: config = initialize();
114: }
115: // If settings have changed on the component manager, refresh the error handler and entity resolver.
116: else if (fComponentManager.getFeature(PARSER_SETTINGS)) {
117: config.setProperty(ENTITY_RESOLVER, fComponentManager
118: .getProperty(ENTITY_RESOLVER));
119: config.setProperty(ERROR_HANDLER, fComponentManager
120: .getProperty(ERROR_HANDLER));
121: }
122:
123: // prepare for parse
124: fComponentManager.reset();
125: fSchemaValidator.setDocumentHandler(null);
126:
127: try {
128: config.parse(input);
129: } catch (XMLParseException e) {
130: throw Util.toSAXParseException(e);
131: } catch (XNIException e) {
132: throw Util.toSAXException(e);
133: }
134: return;
135: }
136: throw new IllegalArgumentException(
137: JAXPValidationMessageFormatter.formatMessage(Locale
138: .getDefault(), "SourceResultMismatch",
139: new Object[] { source.getClass().getName(),
140: result.getClass().getName() }));
141: }
142:
143: private XMLParserConfiguration initialize() {
144: XML11Configuration config = new XML11Configuration();
145: config.setProperty(ENTITY_RESOLVER, fComponentManager
146: .getProperty(ENTITY_RESOLVER));
147: config.setProperty(ERROR_HANDLER, fComponentManager
148: .getProperty(ERROR_HANDLER));
149: XMLErrorReporter errorReporter = (XMLErrorReporter) fComponentManager
150: .getProperty(ERROR_REPORTER);
151: config.setProperty(ERROR_REPORTER, errorReporter);
152: // add message formatters
153: if (errorReporter
154: .getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) {
155: XMLMessageFormatter xmft = new XMLMessageFormatter();
156: errorReporter.putMessageFormatter(
157: XMLMessageFormatter.XML_DOMAIN, xmft);
158: errorReporter.putMessageFormatter(
159: XMLMessageFormatter.XMLNS_DOMAIN, xmft);
160: }
161: config.setProperty(SYMBOL_TABLE, fComponentManager
162: .getProperty(SYMBOL_TABLE));
163: config.setProperty(VALIDATION_MANAGER, fComponentManager
164: .getProperty(VALIDATION_MANAGER));
165: config.setDocumentHandler(fSchemaValidator);
166: config.setDTDHandler(null);
167: config.setDTDContentModelHandler(null);
168: fConfiguration = new SoftReference(config);
169: return config;
170: }
171:
172: } // StreamValidatorHelper
|