1 16 17 package org.apache.xerces.jaxp; 18 19 import java.io.IOException ; 20 import java.util.Enumeration ; 21 import java.util.Hashtable ; 22 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.validation.Schema ; 25 26 import org.apache.xerces.dom.DOMImplementationImpl; 27 import org.apache.xerces.dom.DOMMessageFormatter; 28 import org.apache.xerces.impl.Constants; 29 import org.apache.xerces.impl.validation.ValidationManager; 30 import org.apache.xerces.impl.xs.XMLSchemaValidator; 31 import org.apache.xerces.jaxp.validation.XSGrammarPoolContainer; 32 import org.apache.xerces.parsers.DOMParser; 33 import org.apache.xerces.util.SecurityManager; 34 import org.apache.xerces.xni.XMLDocumentHandler; 35 import org.apache.xerces.xni.parser.XMLComponent; 36 import org.apache.xerces.xni.parser.XMLComponentManager; 37 import org.apache.xerces.xni.parser.XMLConfigurationException; 38 import org.apache.xerces.xni.parser.XMLDTDFilter; 39 import org.apache.xerces.xni.parser.XMLDocumentSource; 40 import org.apache.xerces.xni.parser.XMLParserConfiguration; 41 import org.w3c.dom.DOMImplementation ; 42 import org.w3c.dom.Document ; 43 import org.xml.sax.EntityResolver ; 44 import org.xml.sax.ErrorHandler ; 45 import org.xml.sax.InputSource ; 46 import org.xml.sax.SAXException ; 47 import org.xml.sax.SAXNotRecognizedException ; 48 import org.xml.sax.SAXNotSupportedException ; 49 50 55 public class DocumentBuilderImpl extends DocumentBuilder 56 implements JAXPConstants 57 { 58 59 private static final String NAMESPACES_FEATURE = 60 Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE; 61 62 63 private static final String INCLUDE_IGNORABLE_WHITESPACE = 64 Constants.XERCES_FEATURE_PREFIX + Constants.INCLUDE_IGNORABLE_WHITESPACE; 65 66 67 private static final String CREATE_ENTITY_REF_NODES_FEATURE = 68 Constants.XERCES_FEATURE_PREFIX + Constants.CREATE_ENTITY_REF_NODES_FEATURE; 69 70 71 private static final String INCLUDE_COMMENTS_FEATURE = 72 Constants.XERCES_FEATURE_PREFIX + Constants.INCLUDE_COMMENTS_FEATURE; 73 74 75 private static final String CREATE_CDATA_NODES_FEATURE = 76 Constants.XERCES_FEATURE_PREFIX + Constants.CREATE_CDATA_NODES_FEATURE; 77 78 79 private static final String XINCLUDE_FEATURE = 80 Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_FEATURE; 81 82 83 private static final String XMLSCHEMA_VALIDATION_FEATURE = 84 Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE; 85 86 87 private static final String VALIDATION_FEATURE = 88 Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE; 89 90 91 private static final String SECURITY_MANAGER = 92 Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; 93 94 private DOMParser domParser = null; 95 private final Schema grammar; 96 97 private XMLComponent fSchemaValidator; 98 private XMLComponentManager fSchemaValidatorComponentManager; 99 private ValidationManager fSchemaValidationManager; 100 101 102 private final ErrorHandler fInitErrorHandler; 103 104 105 private final EntityResolver fInitEntityResolver; 106 107 DocumentBuilderImpl(DocumentBuilderFactoryImpl dbf, Hashtable dbfAttrs, Hashtable features) 108 throws SAXNotRecognizedException , SAXNotSupportedException { 109 this(dbf, dbfAttrs, features, false); 110 } 111 112 DocumentBuilderImpl(DocumentBuilderFactoryImpl dbf, Hashtable dbfAttrs, Hashtable features, boolean secureProcessing) 113 throws SAXNotRecognizedException , SAXNotSupportedException 114 { 115 domParser = new DOMParser(); 116 117 if (dbf.isValidating()) { 121 fInitErrorHandler = new DefaultValidationErrorHandler(); 122 setErrorHandler(fInitErrorHandler); 123 } 124 else { 125 fInitErrorHandler = domParser.getErrorHandler(); 126 } 127 128 domParser.setFeature(VALIDATION_FEATURE, dbf.isValidating()); 129 130 domParser.setFeature(NAMESPACES_FEATURE, dbf.isNamespaceAware()); 132 133 domParser.setFeature(INCLUDE_IGNORABLE_WHITESPACE, 135 !dbf.isIgnoringElementContentWhitespace()); 136 domParser.setFeature(CREATE_ENTITY_REF_NODES_FEATURE, 137 !dbf.isExpandEntityReferences()); 138 domParser.setFeature(INCLUDE_COMMENTS_FEATURE, 139 !dbf.isIgnoringComments()); 140 domParser.setFeature(CREATE_CDATA_NODES_FEATURE, 141 !dbf.isCoalescing()); 142 143 if (dbf.isXIncludeAware()) { 147 domParser.setFeature(XINCLUDE_FEATURE, true); 148 } 149 150 if (secureProcessing) { 152 domParser.setProperty(SECURITY_MANAGER, new SecurityManager ()); 153 } 154 155 this.grammar = dbf.getSchema(); 156 if (grammar != null) { 157 XMLParserConfiguration config = domParser.getXMLParserConfiguration(); 158 XMLComponent validatorComponent = null; 159 160 if (grammar instanceof XSGrammarPoolContainer) { 161 validatorComponent = new XMLSchemaValidator(); 162 fSchemaValidationManager = new ValidationManager(); 163 XMLDTDFilter entityHandler = new UnparsedEntityHandler(fSchemaValidationManager); 164 config.setDTDHandler(entityHandler); 165 entityHandler.setDTDHandler(domParser); 166 domParser.setDTDSource(entityHandler); 167 fSchemaValidatorComponentManager = new SchemaValidatorConfiguration(config, 168 (XSGrammarPoolContainer) grammar, fSchemaValidationManager); 169 } 170 171 else { 172 validatorComponent = new JAXPValidatorComponent(grammar.newValidatorHandler()); 173 fSchemaValidatorComponentManager = config; 174 } 175 config.addRecognizedFeatures(validatorComponent.getRecognizedFeatures()); 176 config.addRecognizedProperties(validatorComponent.getRecognizedProperties()); 177 config.setDocumentHandler((XMLDocumentHandler) validatorComponent); 178 ((XMLDocumentSource)validatorComponent).setDocumentHandler(domParser); 179 domParser.setDocumentSource((XMLDocumentSource) validatorComponent); 180 fSchemaValidator = validatorComponent; 181 } 182 183 setFeatures(features); 185 186 setDocumentBuilderFactoryAttributes(dbfAttrs); 188 189 fInitEntityResolver = domParser.getEntityResolver(); 191 } 192 193 private void setFeatures(Hashtable features) 194 throws SAXNotSupportedException , SAXNotRecognizedException { 195 if (features != null) { 196 for (Enumeration e = features.keys(); e.hasMoreElements();) { 197 String feature = (String )e.nextElement(); 198 boolean value = ((Boolean )features.get(feature)).booleanValue(); 199 domParser.setFeature(feature, value); 200 } 201 } 202 } 203 204 211 private void setDocumentBuilderFactoryAttributes(Hashtable dbfAttrs) 212 throws SAXNotSupportedException , SAXNotRecognizedException 213 { 214 if (dbfAttrs == null) { 215 return; 217 } 218 219 for (Enumeration e = dbfAttrs.keys(); e.hasMoreElements();) { 220 String name = (String )e.nextElement(); 221 Object val = dbfAttrs.get(name); 222 if (val instanceof Boolean ) { 223 domParser.setFeature(name, ((Boolean )val).booleanValue()); 225 } else { 226 if (JAXP_SCHEMA_LANGUAGE.equals(name)) { 228 if ( W3C_XML_SCHEMA.equals(val) ) { 231 if( isValidating() ) { 232 domParser.setFeature(XMLSCHEMA_VALIDATION_FEATURE, true); 233 domParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); 236 } 237 } 238 } else if(JAXP_SCHEMA_SOURCE.equals(name)){ 239 if( isValidating() ) { 240 String value=(String )dbfAttrs.get(JAXP_SCHEMA_LANGUAGE); 241 if(value !=null && W3C_XML_SCHEMA.equals(value)){ 242 domParser.setProperty(name, val); 243 }else{ 244 throw new IllegalArgumentException ( 245 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, 246 "jaxp-order-not-supported", 247 new Object [] {JAXP_SCHEMA_LANGUAGE, JAXP_SCHEMA_SOURCE})); 248 } 249 } 250 } else { 251 domParser.setProperty(name, val); 253 } 254 } 255 } 256 } 257 258 263 public Document newDocument() { 264 return new org.apache.xerces.dom.DocumentImpl(); 265 } 266 267 public DOMImplementation getDOMImplementation() { 268 return DOMImplementationImpl.getDOMImplementation(); 269 } 270 271 public Document parse(InputSource is) throws SAXException , IOException { 272 if (is == null) { 273 throw new IllegalArgumentException ( 274 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, 275 "jaxp-null-input-source", null)); 276 } 277 if (fSchemaValidator != null) { 278 if (fSchemaValidationManager != null) { 279 fSchemaValidationManager.reset(); 280 } 281 resetSchemaValidator(); 282 } 283 domParser.parse(is); 284 return domParser.getDocument(); 285 } 286 287 public boolean isNamespaceAware() { 288 try { 289 return domParser.getFeature(NAMESPACES_FEATURE); 290 } 291 catch (SAXException x) { 292 throw new IllegalStateException (x.getMessage()); 293 } 294 } 295 296 public boolean isValidating() { 297 try { 298 return domParser.getFeature(VALIDATION_FEATURE); 299 } 300 catch (SAXException x) { 301 throw new IllegalStateException (x.getMessage()); 302 } 303 } 304 305 309 public boolean isXIncludeAware() { 310 try { 311 return domParser.getFeature(XINCLUDE_FEATURE); 312 } 313 catch (SAXException exc) { 314 return false; 315 } 316 } 317 318 public void setEntityResolver(EntityResolver er) { 319 domParser.setEntityResolver(er); 320 } 321 322 public void setErrorHandler(ErrorHandler eh) { 323 domParser.setErrorHandler(eh); 324 } 325 326 public Schema getSchema() { 327 return grammar; 328 } 329 330 public void reset() { 331 332 if (domParser.getErrorHandler() != fInitErrorHandler) { 333 domParser.setErrorHandler(fInitErrorHandler); 334 } 335 336 if (domParser.getEntityResolver() != fInitEntityResolver) { 337 domParser.setEntityResolver(fInitEntityResolver); 338 } 339 } 340 341 DOMParser getDOMParser() { 343 return domParser; 344 } 345 346 private void resetSchemaValidator() throws SAXException { 347 try { 348 fSchemaValidator.reset(fSchemaValidatorComponentManager); 349 } 350 catch (XMLConfigurationException e) { 352 throw new SAXException (e); 353 } 354 } 355 } 356 | Popular Tags |