1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 2005 The Apache Software Foundation. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 21 package com.sun.org.apache.xerces.internal.jaxp.validation; 22 23 import com.sun.org.apache.xerces.internal.impl.Constants; 24 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; 25 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 26 import com.sun.org.apache.xerces.internal.parsers.XML11Configuration; 27 import com.sun.org.apache.xerces.internal.utils.XMLSecurityManager; 28 import com.sun.org.apache.xerces.internal.xni.XNIException; 29 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 30 import com.sun.org.apache.xerces.internal.xni.parser.XMLParseException; 31 import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration; 32 import java.io.IOException; 33 import java.lang.ref.SoftReference; 34 import javax.xml.XMLConstants; 35 import javax.xml.transform.Result; 36 import javax.xml.transform.Source; 37 import javax.xml.transform.TransformerConfigurationException; 38 import javax.xml.transform.TransformerFactory; 39 import javax.xml.transform.TransformerFactoryConfigurationError; 40 import javax.xml.transform.sax.SAXTransformerFactory; 41 import javax.xml.transform.sax.TransformerHandler; 42 import javax.xml.transform.stream.StreamResult; 43 import javax.xml.transform.stream.StreamSource; 44 import org.xml.sax.SAXException; 45 46 /** 47 * <p>A validator helper for <code>StreamSource</code>s.</p> 48 * 49 * @author Michael Glavassevich, IBM 50 * @author <a href="mailto:Sunitha.Reddy@Sun.com">Sunitha Reddy</a> 51 */ 52 final class StreamValidatorHelper implements ValidatorHelper { 53 54 // feature identifiers 55 56 /** Feature identifier: parser settings. */ 57 private static final String PARSER_SETTINGS = 58 Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS; 59 60 // property identifiers 61 62 /** Property identifier: entity resolver. */ 63 private static final String ENTITY_RESOLVER = 64 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 65 66 /** Property identifier: error handler. */ 67 private static final String ERROR_HANDLER = 68 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 69 70 /** Property identifier: error reporter. */ 71 private static final String ERROR_REPORTER = 72 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 73 74 /** Property identifier: XML Schema validator. */ 75 private static final String SCHEMA_VALIDATOR = 76 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY; 77 78 /** Property identifier: symbol table. */ 79 private static final String SYMBOL_TABLE = 80 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 81 82 /** Property identifier: validation manager. */ 83 private static final String VALIDATION_MANAGER = 84 Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY; 85 86 private static final String DEFAULT_TRANSFORMER_IMPL = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"; 87 88 /** Property id: security manager. */ 89 private static final String SECURITY_MANAGER = 90 Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; 91 92 // 93 // Data 94 // 95 96 /** SoftReference to parser configuration. **/ 97 private SoftReference fConfiguration = new SoftReference(null); 98 99 /** Schema validator. **/ 100 private com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator fSchemaValidator; 101 102 /** Component manager. **/ 103 private XMLSchemaValidatorComponentManager fComponentManager; 104 105 private ValidatorHandlerImpl handler = null; 106 107 public StreamValidatorHelper(XMLSchemaValidatorComponentManager componentManager) { 108 fComponentManager = componentManager; 109 fSchemaValidator = (com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator) fComponentManager.getProperty(SCHEMA_VALIDATOR); 110 } 111 112 public void validate(Source source, Result result) 113 throws SAXException, IOException { 114 if (result == null || result instanceof StreamResult) { 115 final StreamSource streamSource = (StreamSource) source; 116 TransformerHandler identityTransformerHandler ; 117 118 if( result!=null ) { 119 try { 120 SAXTransformerFactory tf = fComponentManager.getFeature(Constants.ORACLE_FEATURE_SERVICE_MECHANISM) ? 121 (SAXTransformerFactory)SAXTransformerFactory.newInstance() 122 : (SAXTransformerFactory) TransformerFactory.newInstance(DEFAULT_TRANSFORMER_IMPL, StreamValidatorHelper.class.getClassLoader()); 123 identityTransformerHandler = tf.newTransformerHandler(); 124 } catch (TransformerConfigurationException e) { 125 throw new TransformerFactoryConfigurationError(e); 126 } 127 128 handler = new ValidatorHandlerImpl(fComponentManager); 129 handler.setContentHandler(identityTransformerHandler); 130 identityTransformerHandler.setResult(result); 131 } 132 133 XMLInputSource input = new XMLInputSource(streamSource.getPublicId(), streamSource.getSystemId(), null, false); 134 input.setByteStream(streamSource.getInputStream()); 135 input.setCharacterStream(streamSource.getReader()); 136 137 // Gets the parser configuration. We'll create and initialize a new one, if we 138 // haven't created one before or if the previous one was garbage collected. 139 XMLParserConfiguration config = (XMLParserConfiguration) fConfiguration.get(); 140 if (config == null) { 141 config = initialize(); 142 } 143 // If settings have changed on the component manager, refresh the error handler and entity resolver. 144 else if (fComponentManager.getFeature(PARSER_SETTINGS)) { 145 config.setProperty(ENTITY_RESOLVER, fComponentManager.getProperty(ENTITY_RESOLVER)); 146 config.setProperty(ERROR_HANDLER, fComponentManager.getProperty(ERROR_HANDLER)); 147 } 148 149 // prepare for parse 150 fComponentManager.reset(); 151 fSchemaValidator.setDocumentHandler(handler); 152 153 try { 154 config.parse(input); 155 } 156 catch (XMLParseException e) { 157 throw Util.toSAXParseException(e); 158 } 159 catch (XNIException e) { 160 throw Util.toSAXException(e); 161 } 162 return; 163 } 164 throw new IllegalArgumentException(JAXPValidationMessageFormatter.formatMessage(fComponentManager.getLocale(), 165 "SourceResultMismatch", 166 new Object [] {source.getClass().getName(), result.getClass().getName()})); 167 } 168 169 private XMLParserConfiguration initialize() { 170 XML11Configuration config = new XML11Configuration(); 171 if (fComponentManager.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)) { 172 config.setProperty(SECURITY_MANAGER, new XMLSecurityManager()); 173 } 174 config.setProperty(ENTITY_RESOLVER, fComponentManager.getProperty(ENTITY_RESOLVER)); 175 config.setProperty(ERROR_HANDLER, fComponentManager.getProperty(ERROR_HANDLER)); 176 XMLErrorReporter errorReporter = (XMLErrorReporter) fComponentManager.getProperty(ERROR_REPORTER); 177 config.setProperty(ERROR_REPORTER, errorReporter); 178 // add message formatters 179 if (errorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN) == null) { 180 XMLMessageFormatter xmft = new XMLMessageFormatter(); 181 errorReporter.putMessageFormatter(XMLMessageFormatter.XML_DOMAIN, xmft); 182 errorReporter.putMessageFormatter(XMLMessageFormatter.XMLNS_DOMAIN, xmft); 183 } 184 config.setProperty(SYMBOL_TABLE, fComponentManager.getProperty(SYMBOL_TABLE)); 185 config.setProperty(VALIDATION_MANAGER, fComponentManager.getProperty(VALIDATION_MANAGER)); 186 config.setDocumentHandler(fSchemaValidator); 187 config.setDTDHandler(null); 188 config.setDTDContentModelHandler(null); 189 config.setProperty(Constants.XML_SECURITY_PROPERTY_MANAGER, 190 fComponentManager.getProperty(Constants.XML_SECURITY_PROPERTY_MANAGER)); 191 config.setProperty(Constants.SECURITY_MANAGER, 192 fComponentManager.getProperty(Constants.SECURITY_MANAGER)); 193 fConfiguration = new SoftReference(config); 194 return config; 195 } 196 197 } // StreamValidatorHelper