1 16 17 package com.sun.org.apache.xerces.internal.jaxp.validation; 18 19 import java.lang.ref.SoftReference ; 20 import java.util.Locale ; 21 import java.io.IOException ; 22 23 import javax.xml.transform.Result ; 24 import javax.xml.transform.Source ; 25 import javax.xml.transform.sax.SAXTransformerFactory ; 26 import javax.xml.transform.sax.TransformerHandler ; 27 import javax.xml.transform.stream.StreamSource ; 28 import javax.xml.transform.stream.StreamResult ; 29 import javax.xml.transform.TransformerConfigurationException ; 30 import javax.xml.transform.TransformerException ; 31 import javax.xml.transform.TransformerFactoryConfigurationError ; 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 ; 42 43 50 final class StreamValidatorHelper implements ValidatorHelper { 51 52 54 55 private static final String PARSER_SETTINGS = 56 Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS; 57 58 60 61 private static final String ENTITY_RESOLVER = 62 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 63 64 65 private static final String ERROR_HANDLER = 66 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 67 68 69 private static final String ERROR_REPORTER = 70 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 71 72 73 private static final String SCHEMA_VALIDATOR = 74 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY; 75 76 77 private static final String SYMBOL_TABLE = 78 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 79 80 81 private static final String VALIDATION_MANAGER = 82 Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY; 83 84 88 89 private SoftReference fConfiguration = new SoftReference (null); 90 91 92 private com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator fSchemaValidator; 93 94 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 source, Result result) 105 throws SAXException , IOException { 106 if (result == null || result instanceof StreamResult ) { 107 final StreamSource streamSource = (StreamSource ) source; 108 TransformerHandler identityTransformerHandler ; 109 110 if( result!=null ) { 111 try { 112 SAXTransformerFactory tf = (SAXTransformerFactory )SAXTransformerFactory.newInstance(); 113 identityTransformerHandler = tf.newTransformerHandler(); 114 } catch (TransformerConfigurationException e) { 115 throw new TransformerFactoryConfigurationError (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 XMLParserConfiguration config = (XMLParserConfiguration) fConfiguration.get(); 130 if (config == null) { 131 config = initialize(); 132 } 133 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 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 (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 155 "SourceResultMismatch", 156 new Object [] {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 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 (config); 177 return config; 178 } 179 180 } | Popular Tags |