00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 package org.w3c.domts;
00021
00022 import javax.xml.parsers.*;
00023 import org.w3c.dom.*;
00024 import org.w3c.domts.*;
00025 import org.xml.sax.*;
00026
00031 public class JAXPDOMTestDocumentBuilderFactory
00032 extends DOMTestDocumentBuilderFactory {
00033
00034 private DocumentBuilderFactory factory;
00035 private DocumentBuilder builder;
00036
00044 public JAXPDOMTestDocumentBuilderFactory(
00045 DocumentBuilderFactory baseFactory,
00046 DocumentBuilderSetting[] settings)
00047 throws DOMTestIncompatibleException {
00048 super(settings);
00049 if(baseFactory == null) {
00050 factory = DocumentBuilderFactory.newInstance();
00051 }
00052 else {
00053 factory = baseFactory;
00054 }
00055
00056
00057
00058 if(settings != null) {
00059 for(int i = 0; i < settings.length; i++) {
00060 settings[i].applySetting(factory);
00061 }
00062 }
00063 try {
00064 this.builder = factory.newDocumentBuilder();
00065 }
00066 catch(ParserConfigurationException ex) {
00067 throw new DOMTestIncompatibleException(ex,null);
00068 }
00069 }
00070
00071 public DOMTestDocumentBuilderFactory newInstance(DocumentBuilderSetting[] newSettings)
00072 throws DOMTestIncompatibleException {
00073 if(newSettings == null) {
00074 return this;
00075 }
00076 DocumentBuilderSetting[] mergedSettings = mergeSettings(newSettings);
00077 DocumentBuilderFactory newFactory = factory.newInstance();
00078 return new JAXPDOMTestDocumentBuilderFactory(newFactory,mergedSettings);
00079 }
00080
00081 private class LoadErrorHandler implements org.xml.sax.ErrorHandler {
00082 private SAXException parseException;
00083 private int errorCount;
00084 private int warningCount;
00085 public LoadErrorHandler() {
00086 parseException = null;
00087 errorCount = 0;
00088 warningCount = 0;
00089 }
00090 public void error(SAXParseException ex) {
00091 errorCount++;
00092 if(parseException == null) {
00093 parseException = ex;
00094 }
00095 }
00096
00097 public void warning(SAXParseException ex) {
00098 warningCount++;
00099 }
00100
00101 public void fatalError(SAXParseException ex) {
00102 if(parseException == null) {
00103 parseException = ex;
00104 }
00105 }
00106
00107 public SAXException getFirstException() {
00108 return parseException;
00109 }
00110 }
00111
00112
00113 public Document load(java.net.URL url) throws DOMTestLoadException {
00114 Document doc = null;
00115 Exception parseException = null;
00116 try {
00117 LoadErrorHandler errorHandler = new LoadErrorHandler();
00118 builder.setErrorHandler(errorHandler);
00119 doc = builder.parse(url.openStream(),url.toString());
00120 parseException = errorHandler.getFirstException();
00121 }
00122 catch(Exception ex) {
00123 parseException = ex;
00124 }
00125 builder.setErrorHandler(null);
00126 if(parseException != null) {
00127 throw new DOMTestLoadException(parseException);
00128 }
00129 return doc;
00130 }
00131
00132 public DOMImplementation getDOMImplementation() {
00133 return builder.getDOMImplementation();
00134 }
00135
00136 public boolean hasFeature(String feature, String version) {
00137 return builder.getDOMImplementation().hasFeature(feature,version);
00138 }
00139
00140
00141 public boolean isCoalescing() {
00142 return factory.isCoalescing();
00143 }
00144
00145 public boolean isExpandEntityReferences() {
00146 return factory.isExpandEntityReferences();
00147 }
00148
00149 public boolean isIgnoringElementContentWhitespace() {
00150 return factory.isIgnoringElementContentWhitespace();
00151 }
00152
00153 public boolean isNamespaceAware() {
00154 return factory.isNamespaceAware();
00155 }
00156
00157 public boolean isValidating() {
00158 return factory.isValidating();
00159 }
00160
00161 public static DocumentBuilderSetting[] getConfiguration1()
00162 {
00163 return new DocumentBuilderSetting[] {
00164 DocumentBuilderSetting.notCoalescing,
00165 DocumentBuilderSetting.notExpandEntityReferences,
00166 DocumentBuilderSetting.notIgnoringElementContentWhitespace,
00167 DocumentBuilderSetting.notNamespaceAware,
00168 DocumentBuilderSetting.notValidating };
00169 }
00170
00171 public static DocumentBuilderSetting[] getConfiguration2() {
00172 return new DocumentBuilderSetting[] {
00173 DocumentBuilderSetting.notCoalescing,
00174 DocumentBuilderSetting.expandEntityReferences,
00175 DocumentBuilderSetting.ignoringElementContentWhitespace,
00176 DocumentBuilderSetting.namespaceAware,
00177 DocumentBuilderSetting.validating };
00178
00179 }
00180
00181
00182 }
00183