1 57 58 59 package com.sun.org.apache.xerces.internal.jaxp; 60 61 import java.io.IOException ; 62 import java.util.Enumeration ; 63 import java.util.Hashtable ; 64 65 import javax.xml.parsers.DocumentBuilder ; 66 import javax.xml.parsers.DocumentBuilderFactory ; 67 import javax.xml.validation.Schema ; 68 69 import com.sun.org.apache.xerces.internal.dom.DOMImplementationImpl; 70 import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter; 71 import com.sun.org.apache.xerces.internal.impl.Constants; 72 import com.sun.org.apache.xerces.internal.parsers.DOMParser; 73 import com.sun.org.apache.xerces.internal.parsers.JAXPConfiguration; 74 import com.sun.org.apache.xerces.internal.util.SecurityManager; 75 import com.sun.org.apache.xerces.internal.xni.XNIException; 76 import org.w3c.dom.DOMImplementation ; 77 import org.w3c.dom.Document ; 78 import org.xml.sax.EntityResolver ; 79 import org.xml.sax.ErrorHandler ; 80 import org.xml.sax.InputSource ; 81 import org.xml.sax.SAXException ; 82 import org.xml.sax.SAXNotRecognizedException ; 83 import org.xml.sax.SAXNotSupportedException ; 84 import org.xml.sax.helpers.DefaultHandler ; 85 90 public class DocumentBuilderImpl extends DocumentBuilder 91 implements JAXPConstants 92 { 93 private EntityResolver er = null; 94 private ErrorHandler eh = null; 95 private final DOMParser domParser; 96 private boolean enableSP = true; 97 private final Schema grammar; 98 99 103 private final SecurityManager secureProcessing ; 104 private final boolean xincludeAware; 105 106 107 protected DocumentBuilderImpl(DocumentBuilderFactory dbf, Hashtable dbfAttrs) 108 throws SAXNotRecognizedException , SAXNotSupportedException 109 { 110 grammar = dbf.getSchema(); 111 secureProcessing = new SecurityManager (); 112 this.domParser = new DOMParser(new JAXPConfiguration(grammar)); 113 this.xincludeAware = dbf.isXIncludeAware(); 114 115 domParser.setFeature( 116 Constants.XERCES_FEATURE_PREFIX + Constants.XINCLUDE_AWARE, 117 xincludeAware); 118 119 if (dbf.isValidating()) { 123 setErrorHandler(new DefaultValidationErrorHandler()); 124 } 125 126 domParser.setFeature(Constants.SAX_FEATURE_PREFIX + 127 Constants.VALIDATION_FEATURE, dbf.isValidating()); 128 129 domParser.setFeature(Constants.SAX_FEATURE_PREFIX + 131 Constants.NAMESPACES_FEATURE, 132 dbf.isNamespaceAware()); 133 134 domParser.setFeature(Constants.XERCES_FEATURE_PREFIX + 136 Constants.INCLUDE_IGNORABLE_WHITESPACE, 137 !dbf.isIgnoringElementContentWhitespace()); 138 domParser.setFeature(Constants.XERCES_FEATURE_PREFIX + 139 Constants.CREATE_ENTITY_REF_NODES_FEATURE, 140 !dbf.isExpandEntityReferences()); 141 domParser.setFeature(Constants.XERCES_FEATURE_PREFIX + 142 Constants.INCLUDE_COMMENTS_FEATURE, 143 !dbf.isIgnoringComments()); 144 domParser.setFeature(Constants.XERCES_FEATURE_PREFIX + 145 Constants.CREATE_CDATA_NODES_FEATURE, 146 !dbf.isCoalescing()); 147 148 setDocumentBuilderFactoryAttributes(dbfAttrs); 149 if( enableSP) 150 domParser.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY, secureProcessing); 151 } 152 153 167 168 public void reset(){ 169 if(domParser != null){ 170 try{ 171 domParser.reset(); 175 } 176 catch(XNIException ex){ 181 } 183 } 184 } 185 186 193 private void setDocumentBuilderFactoryAttributes(Hashtable dbfAttrs) 194 throws SAXNotSupportedException , SAXNotRecognizedException 195 { 196 if (dbfAttrs == null) { 197 return; 199 } 200 201 203 for (Enumeration e = dbfAttrs.keys(); e.hasMoreElements();) { 204 String name = (String )e.nextElement(); 205 Object val = dbfAttrs.get(name); 206 if (val instanceof Boolean ) { 207 if (Constants.FEATURE_SECURE_PROCESSING.equals(name)){ 209 enableSP = ((Boolean )val).booleanValue(); 210 }else 211 domParser.setFeature(name, ((Boolean )val).booleanValue()); 212 } else { 213 if (JAXP_SCHEMA_LANGUAGE.equals(name)) { 215 if ( W3C_XML_SCHEMA.equals(val) ) { 218 if( isValidating() ) { 219 domParser.setFeature( 220 Constants.XERCES_FEATURE_PREFIX + 221 Constants.SCHEMA_VALIDATION_FEATURE, true); 222 domParser.setFeature(Constants.XERCES_FEATURE_PREFIX + 224 Constants.SCHEMA_FULL_CHECKING, 225 true); 226 domParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); 229 } 230 } 231 } else if(JAXP_SCHEMA_SOURCE.equals(name)){ 232 if( isValidating() ) { 233 String value=(String )dbfAttrs.get(JAXP_SCHEMA_LANGUAGE); 234 if(value !=null && W3C_XML_SCHEMA.equals(value)){ 235 domParser.setProperty(name, val); 236 }else{ 237 throw new IllegalArgumentException ( 238 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, 239 "jaxp-order-not-supported", 240 new Object [] {JAXP_SCHEMA_LANGUAGE, JAXP_SCHEMA_SOURCE})); 241 } 242 } 243 } 244 else { 257 domParser.setProperty(name, val); 259 } 260 } 261 } 262 } 263 264 269 public Document newDocument() { 270 return new com.sun.org.apache.xerces.internal.dom.DocumentImpl(); 271 } 272 273 public DOMImplementation getDOMImplementation() { 274 return DOMImplementationImpl.getDOMImplementation(); 275 } 276 277 public Document parse(InputSource is) throws SAXException , IOException { 278 if (is == null) { 279 throw new IllegalArgumentException ( 280 DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, 281 "jaxp-null-input-source", null)); 282 } 283 284 if (er != null) { 285 domParser.setEntityResolver(er); 286 } 287 288 if (eh != null) { 289 domParser.setErrorHandler(eh); 290 } 291 292 domParser.parse(is); 293 return domParser.getDocument(); 294 } 295 296 public boolean isNamespaceAware() { 297 try { 298 return domParser.getFeature(Constants.SAX_FEATURE_PREFIX + 299 Constants.NAMESPACES_FEATURE); 300 } catch (SAXException x) { 301 throw new IllegalStateException (x.getMessage()); 302 } 303 } 304 305 public boolean isValidating() { 306 try { 307 return domParser.getFeature(Constants.SAX_FEATURE_PREFIX + 308 Constants.VALIDATION_FEATURE); 309 } catch (SAXException x) { 310 throw new IllegalStateException (x.getMessage()); 311 } 312 } 313 314 public void setEntityResolver(org.xml.sax.EntityResolver er) { 315 this.er = er; 316 } 317 318 public void setErrorHandler(org.xml.sax.ErrorHandler eh) { 319 this.eh = (eh == null) ? new DefaultHandler () : eh; 322 } 323 324 332 333 public Schema getSchema(){ 334 return grammar; 335 } 336 337 public boolean isXIncludeAware() { 338 return xincludeAware; 339 } 340 341 final DOMParser getDOMParser() { 342 return domParser; 343 } 344 345 } 346 | Popular Tags |