JAXPDOMTestDocumentBuilderFactory.java revision adc854b798c1cfe3bfd4c27d68d5cee38ca617da
1/*
2 * Copyright (c) 2001-2004 World Wide Web Consortium,
3 * (Massachusetts Institute of Technology, Institut National de
4 * Recherche en Informatique et en Automatique, Keio University). All
5 * Rights Reserved. This program is distributed under the W3C's Software
6 * Intellectual Property License. This program is distributed in the
7 * hope that it will be useful, but WITHOUT ANY WARRANTY; without even
8 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9 * PURPOSE.
10 * See W3C License http://www.w3.org/Consortium/Legal/ for more details.
11 */
12
13package org.w3c.domts;
14
15import javax.xml.parsers.DocumentBuilder;
16import javax.xml.parsers.DocumentBuilderFactory;
17import javax.xml.parsers.ParserConfigurationException;
18
19import org.w3c.dom.DOMImplementation;
20import org.w3c.dom.Document;
21import org.xml.sax.SAXException;
22import org.xml.sax.SAXParseException;
23
24/**
25 *   This class implements the generic parser and configuation
26 *   abstract class for JAXP supporting parsers.
27 */
28public class JAXPDOMTestDocumentBuilderFactory
29    extends DOMTestDocumentBuilderFactory {
30
31  private DocumentBuilderFactory factory;
32  private DocumentBuilder builder;
33
34  /**
35   * Creates a JAXP implementation of DOMTestDocumentBuilderFactory.
36   * @param factory null for default JAXP provider.  If not null,
37   * factory will be mutated in constructor and should be released
38   * by calling code upon return.
39   * @param settings array of settings, may be null.
40   */
41  public JAXPDOMTestDocumentBuilderFactory(
42      DocumentBuilderFactory baseFactory,
43      DocumentBuilderSetting[] settings) throws DOMTestIncompatibleException {
44    super(settings);
45    if (baseFactory == null) {
46      factory = DocumentBuilderFactory.newInstance();
47    }
48    else {
49      factory = baseFactory;
50    }
51    //
52    //    apply settings to selected document builder
53    //         may throw exception if incompatible
54    if (settings != null) {
55      for (int i = 0; i < settings.length; i++) {
56        settings[i].applySetting(factory);
57      }
58    }
59    try {
60      this.builder = factory.newDocumentBuilder();
61    }
62    catch (ParserConfigurationException ex) {
63      throw new DOMTestIncompatibleException(ex, null);
64    }
65  }
66
67  protected DOMTestDocumentBuilderFactory createInstance(DocumentBuilderFactory
68      newFactory,
69      DocumentBuilderSetting[] mergedSettings) throws
70      DOMTestIncompatibleException {
71    return new JAXPDOMTestDocumentBuilderFactory(newFactory, mergedSettings);
72  }
73
74  public DOMTestDocumentBuilderFactory newInstance(DocumentBuilderSetting[]
75      newSettings) throws DOMTestIncompatibleException {
76    if (newSettings == null) {
77      return this;
78    }
79    DocumentBuilderSetting[] mergedSettings = mergeSettings(newSettings);
80    DocumentBuilderFactory newFactory = factory.newInstance();
81    return createInstance(newFactory, mergedSettings);
82  }
83
84  private class LoadErrorHandler
85      implements org.xml.sax.ErrorHandler {
86    private SAXException parseException;
87    private int errorCount;
88    private int warningCount;
89    public LoadErrorHandler() {
90      parseException = null;
91      errorCount = 0;
92      warningCount = 0;
93    }
94
95    public void error(SAXParseException ex) {
96      errorCount++;
97      if (parseException == null) {
98        parseException = ex;
99      }
100    }
101
102    public void warning(SAXParseException ex) {
103      warningCount++;
104    }
105
106    public void fatalError(SAXParseException ex) {
107      if (parseException == null) {
108        parseException = ex;
109      }
110    }
111
112    public SAXException getFirstException() {
113      return parseException;
114    }
115  }
116
117  public Document load(java.net.URL url) throws DOMTestLoadException {
118    Document doc = null;
119    Exception parseException = null;
120    try {
121      LoadErrorHandler errorHandler = new LoadErrorHandler();
122      builder.setErrorHandler(errorHandler);
123      doc = builder.parse(url.openStream(), url.toString());
124      parseException = errorHandler.getFirstException();
125    }
126    catch (Exception ex) {
127      parseException = ex;
128    }
129    builder.setErrorHandler(null);
130    if (parseException != null) {
131      throw new DOMTestLoadException(parseException);
132    }
133    return doc;
134  }
135
136  public DOMImplementation getDOMImplementation() {
137    return builder.getDOMImplementation();
138  }
139
140  public boolean hasFeature(String feature, String version) {
141    return builder.getDOMImplementation().hasFeature(feature, version);
142  }
143
144  public boolean isCoalescing() {
145    return factory.isCoalescing();
146  }
147
148  public boolean isExpandEntityReferences() {
149    return factory.isExpandEntityReferences();
150  }
151
152  public boolean isIgnoringElementContentWhitespace() {
153    return factory.isIgnoringElementContentWhitespace();
154  }
155
156  public boolean isNamespaceAware() {
157    return factory.isNamespaceAware();
158  }
159
160  public boolean isValidating() {
161    return factory.isValidating();
162  }
163
164  public static DocumentBuilderSetting[] getConfiguration1() {
165    return new DocumentBuilderSetting[] {
166        DocumentBuilderSetting.notCoalescing,
167        DocumentBuilderSetting.notExpandEntityReferences,
168        DocumentBuilderSetting.notIgnoringElementContentWhitespace,
169        DocumentBuilderSetting.notNamespaceAware,
170        DocumentBuilderSetting.notValidating};
171  }
172
173  public static DocumentBuilderSetting[] getConfiguration2() {
174    return new DocumentBuilderSetting[] {
175        DocumentBuilderSetting.notCoalescing,
176        DocumentBuilderSetting.expandEntityReferences,
177        DocumentBuilderSetting.ignoringElementContentWhitespace,
178        DocumentBuilderSetting.namespaceAware,
179        DocumentBuilderSetting.validating};
180
181  }
182
183}
184