1 23 24 package org.enhydra.xml.io; 25 26 import java.io.IOException ; 27 28 import javax.xml.parsers.DocumentBuilder ; 29 30 import org.enhydra.apache.xerces.dom.DOMImplementationImpl; 31 import org.w3c.dom.DOMImplementation ; 32 import org.w3c.dom.Document ; 33 import org.xml.sax.EntityResolver ; 34 import org.xml.sax.ErrorHandler ; 35 import org.xml.sax.InputSource ; 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.SAXNotRecognizedException ; 38 39 43 56 public class DOMParser extends DocumentBuilder { 57 60 private String documentClassName = null; 61 62 65 private ErrorHandler errorHandler = null; 66 67 70 private EntityResolver entityResolver = null; 71 72 75 private boolean enableNamespaces = true; 76 77 80 private boolean validationEnabled = true; 81 82 85 88 class XercesParser extends org.enhydra.apache.xerces.parsers.DOMParser { 89 92 public XercesParser() throws SAXException { 93 if (documentClassName != null) { 94 super.setDocumentClassName(documentClassName); 95 } 96 if (errorHandler != null) { 97 super.setErrorHandler(errorHandler); 98 } 99 if (entityResolver != null) { 100 super.setEntityResolver(entityResolver); 101 } 102 super.setNamespaces(enableNamespaces); 103 super.setValidation(validationEnabled); 104 super.setDeferNodeExpansion(false); 105 106 } 107 108 113 private InputSource resolve(InputSource inputSource) 114 throws SAXException , IOException { 115 if ((inputSource.getByteStream() != null) 116 || (inputSource.getCharacterStream() != null)) { 117 return inputSource; } 119 120 EntityResolver resolver = this.getEntityResolver(); 121 if (resolver == null) { 122 return inputSource; } 124 125 String publicId = inputSource.getPublicId(); 126 String systemId = inputSource.getSystemId(); 127 if ((publicId == null) && (systemId == null)) { 128 return inputSource; 130 } 131 132 InputSource resolvedSource = resolver.resolveEntity(publicId, 133 systemId); 134 if (resolvedSource != null) { 135 return resolvedSource; 136 } else { 137 return inputSource; 138 } 139 } 140 141 145 public void parse(InputSource inputSource) 146 throws SAXException , IOException { 147 super.parse(resolve(inputSource)); 148 } 149 } 150 151 165 public Document parse(InputSource is) 166 throws SAXException , IOException { 167 XercesParser parser = new XercesParser(); 168 parser.parse(is); 169 return parser.getDocument(); 170 } 171 172 175 public void setNamespaceAware(boolean enable) { 176 enableNamespaces = enable; 177 } 178 179 184 public boolean isNamespaceAware() { 185 return enableNamespaces; 186 } 187 188 191 public void setValidation(boolean enable) { 192 validationEnabled = enable; 193 } 194 195 200 public boolean isValidating() { 201 return validationEnabled; 202 } 203 204 212 public void setEntityResolver(EntityResolver er) { 213 entityResolver = er; 214 } 215 216 219 public EntityResolver getEntityResolver() { 220 return entityResolver; 221 } 222 223 231 public void setErrorHandler(ErrorHandler eh) { 232 errorHandler = eh; 233 } 234 235 238 public ErrorHandler getErrorHandler() { 239 return errorHandler; 240 } 241 242 246 public void setDocumentClassName(String className) { 247 documentClassName = className; 248 } 249 250 253 public String getDocumentClassName() { 254 return documentClassName; 255 } 256 257 258 263 public Document newDocument() { 264 String className = (documentClassName != null) 265 ? documentClassName : org.enhydra.apache.xerces.parsers.DOMParser.DEFAULT_DOCUMENT_CLASS_NAME; 266 try { 267 Class documentClass = getClass().getClassLoader().loadClass(className); 268 return (Document )documentClass.newInstance(); 269 } catch (Exception except) { 270 throw new XMLIOError(except); 271 } 272 } 273 274 279 public DOMImplementation getDOMImplementation() { 280 return DOMImplementationImpl.getDOMImplementation(); 281 } 282 283 287 public String toString() { 288 try { 289 StringBuffer buf = new StringBuffer (4096); 290 XercesParser parser = new XercesParser(); 292 293 296 String [] features = parser.getFeaturesRecognized(); 297 buf.append("Parser features:\n"); 298 for (int idx = 0; idx < features.length; idx++) { 299 buf.append(" "); 300 buf.append(features[idx]); 301 buf.append("="); 302 try { 303 buf.append(parser.getFeature(features[idx])); 304 } catch (SAXNotRecognizedException except) { 305 buf.append("*** not recognized ***"); 306 } 307 buf.append('\n'); 308 } 309 310 String [] properties = parser.getPropertiesRecognized(); 311 buf.append("Parser properties:\n"); 312 for (int idx = 0; idx < properties.length; idx++) { 313 buf.append(" "); 314 buf.append(properties[idx]); 315 buf.append("="); 316 try { 317 buf.append(parser.getFeature(properties[idx])); 318 } catch (SAXNotRecognizedException except) { 319 buf.append("*** not recognized ***"); 320 } 321 buf.append('\n'); 322 } 323 return buf.toString(); 324 } catch (SAXException except) { 325 throw new XMLIOError(except); 326 } 327 } 328 } 329 | Popular Tags |