1 16 17 package org.apache.xerces.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.stream.StreamSource ; 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 ; 36 37 43 final class StreamValidatorHelper implements ValidatorHelper { 44 45 47 48 private static final String PARSER_SETTINGS = 49 Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS; 50 51 53 54 private static final String ENTITY_RESOLVER = 55 Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; 56 57 58 private static final String ERROR_HANDLER = 59 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_HANDLER_PROPERTY; 60 61 62 private static final String ERROR_REPORTER = 63 Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; 64 65 66 private static final String SCHEMA_VALIDATOR = 67 Constants.XERCES_PROPERTY_PREFIX + Constants.SCHEMA_VALIDATOR_PROPERTY; 68 69 70 private static final String SYMBOL_TABLE = 71 Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; 72 73 74 private static final String VALIDATION_MANAGER = 75 Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY; 76 77 81 82 private SoftReference fConfiguration = new SoftReference (null); 83 84 85 private org.apache.xerces.impl.xs.XMLSchemaValidator fSchemaValidator; 86 87 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 source, Result result) 96 throws SAXException , IOException { 97 if (result == null) { 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()); 102 103 XMLParserConfiguration config = (XMLParserConfiguration) fConfiguration.get(); 106 if (config == null) { 107 config = initialize(); 108 } 109 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 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 (JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(), 131 "SourceResultMismatch", 132 new Object [] {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 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 (config); 153 return config; 154 } 155 156 } | Popular Tags |