1 58 59 60 package org.enhydra.apache.xerces.jaxp; 61 62 import java.io.IOException ; 63 import java.util.Enumeration ; 64 import java.util.Hashtable ; 65 66 import javax.xml.parsers.DocumentBuilder ; 67 import javax.xml.parsers.DocumentBuilderFactory ; 68 69 import org.enhydra.apache.xerces.dom.DOMImplementationImpl; 70 import org.enhydra.apache.xerces.parsers.DOMParser; 71 import org.w3c.dom.DOMImplementation ; 72 import org.w3c.dom.Document ; 73 import org.xml.sax.EntityResolver ; 74 import org.xml.sax.ErrorHandler ; 75 import org.xml.sax.InputSource ; 76 import org.xml.sax.SAXException ; 77 import org.xml.sax.SAXNotRecognizedException ; 78 import org.xml.sax.SAXNotSupportedException ; 79 import org.xml.sax.helpers.DefaultHandler ; 80 81 86 public class DocumentBuilderImpl extends DocumentBuilder { 87 88 static final String XERCES_FEATURE_PREFIX = 89 "http://apache.org/xml/features/"; 90 static final String CREATE_ENTITY_REF_NODES_FEATURE = 91 "dom/create-entity-ref-nodes"; 92 static final String INCLUDE_IGNORABLE_WHITESPACE = 93 "dom/include-ignorable-whitespace"; 94 95 private EntityResolver er = null; 96 private ErrorHandler eh = null; 97 private DOMParser domParser = null; 98 99 private boolean namespaceAware = false; 100 private boolean validating = false; 101 102 DocumentBuilderImpl(DocumentBuilderFactory dbf, Hashtable dbfAttrs) 103 throws SAXNotRecognizedException , SAXNotSupportedException 104 { 105 domParser = new DOMParser(); 106 107 validating = dbf.isValidating(); 109 String validation = "http://xml.org/sax/features/validation"; 110 domParser.setFeature(validation, validating); 111 112 if (validating) { 116 setErrorHandler(new DefaultValidationErrorHandler()); 117 } 118 119 namespaceAware = dbf.isNamespaceAware(); 121 domParser.setFeature("http://xml.org/sax/features/namespaces", 122 namespaceAware); 123 124 domParser.setFeature(XERCES_FEATURE_PREFIX + 126 INCLUDE_IGNORABLE_WHITESPACE, 127 !dbf.isIgnoringElementContentWhitespace()); 128 domParser.setFeature(XERCES_FEATURE_PREFIX + 129 CREATE_ENTITY_REF_NODES_FEATURE, 130 !dbf.isExpandEntityReferences()); 131 132 135 setDocumentBuilderFactoryAttributes(dbfAttrs); 136 } 137 138 145 private void setDocumentBuilderFactoryAttributes(Hashtable dbfAttrs) 146 throws SAXNotSupportedException , SAXNotRecognizedException 147 { 148 if (dbfAttrs != null) { 149 for (Enumeration e = dbfAttrs.keys(); e.hasMoreElements();) { 150 String name = (String )e.nextElement(); 151 Object val = dbfAttrs.get(name); 152 if (val instanceof Boolean ) { 153 domParser.setFeature(name, ((Boolean )val).booleanValue()); 155 } else { 156 domParser.setProperty(name, val); 158 } 159 } 160 } 161 } 162 163 168 public Document newDocument() { 169 return new org.enhydra.apache.xerces.dom.DocumentImpl(); 170 } 171 172 public DOMImplementation getDOMImplementation() { 173 return DOMImplementationImpl.getDOMImplementation(); 174 } 175 176 public Document parse(InputSource is) throws SAXException , IOException { 177 if (is == null) { 178 throw new IllegalArgumentException ("InputSource cannot be null"); 179 } 180 181 if (er != null) { 182 domParser.setEntityResolver(er); 183 } 184 185 if (eh != null) { 186 domParser.setErrorHandler(eh); 187 } 188 189 domParser.parse(is); 190 return domParser.getDocument(); 191 } 192 193 public boolean isNamespaceAware() { 194 return namespaceAware; 195 } 196 197 public boolean isValidating() { 198 return validating; 199 } 200 201 public void setEntityResolver(org.xml.sax.EntityResolver er) { 202 this.er = er; 203 } 204 205 public void setErrorHandler(org.xml.sax.ErrorHandler eh) { 206 this.eh = (eh == null) ? new DefaultHandler () : eh; 209 } 210 211 DOMParser getDOMParser() { 213 return domParser; 214 } 215 } 216 | Popular Tags |