summaryrefslogtreecommitdiffstats
path: root/dom/src/test/java/org/w3c/domts/JAXPDOMTestDocumentBuilderFactory.java
blob: c2d105e82c316d2e0abac5857a32138b0a872cc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (c) 2001-2004 World Wide Web Consortium,
 * (Massachusetts Institute of Technology, Institut National de
 * Recherche en Informatique et en Automatique, Keio University). All
 * Rights Reserved. This program is distributed under the W3C's Software
 * Intellectual Property License. This program is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.
 * See W3C License http://www.w3.org/Consortium/Legal/ for more details.
 */

package org.w3c.domts;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 *   This class implements the generic parser and configuation
 *   abstract class for JAXP supporting parsers.
 */
public class JAXPDOMTestDocumentBuilderFactory
    extends DOMTestDocumentBuilderFactory {

  private DocumentBuilderFactory factory;
  private DocumentBuilder builder;

  /**
   * Creates a JAXP implementation of DOMTestDocumentBuilderFactory.
   * @param factory null for default JAXP provider.  If not null,
   * factory will be mutated in constructor and should be released
   * by calling code upon return.
   * @param settings array of settings, may be null.
   */
  public JAXPDOMTestDocumentBuilderFactory(
      DocumentBuilderFactory baseFactory,
      DocumentBuilderSetting[] settings) throws DOMTestIncompatibleException {
    super(settings);
    if (baseFactory == null) {
      factory = DocumentBuilderFactory.newInstance();
    }
    else {
      factory = baseFactory;
    }
    //
    //    apply settings to selected document builder
    //         may throw exception if incompatible
    if (settings != null) {
      for (int i = 0; i < settings.length; i++) {
        settings[i].applySetting(factory);
      }
    }
    try {
      this.builder = factory.newDocumentBuilder();
    }
    catch (ParserConfigurationException ex) {
      throw new DOMTestIncompatibleException(ex, null);
    }
  }

  protected DOMTestDocumentBuilderFactory createInstance(DocumentBuilderFactory
      newFactory,
      DocumentBuilderSetting[] mergedSettings) throws
      DOMTestIncompatibleException {
    return new JAXPDOMTestDocumentBuilderFactory(newFactory, mergedSettings);
  }

  public DOMTestDocumentBuilderFactory newInstance(DocumentBuilderSetting[]
      newSettings) throws DOMTestIncompatibleException {
    if (newSettings == null) {
      return this;
    }
    DocumentBuilderSetting[] mergedSettings = mergeSettings(newSettings);
    DocumentBuilderFactory newFactory = factory.newInstance();
    return createInstance(newFactory, mergedSettings);
  }

  private class LoadErrorHandler
      implements org.xml.sax.ErrorHandler {
    private SAXException parseException;
    private int errorCount;
    private int warningCount;
    public LoadErrorHandler() {
      parseException = null;
      errorCount = 0;
      warningCount = 0;
    }

    public void error(SAXParseException ex) {
      errorCount++;
      if (parseException == null) {
        parseException = ex;
      }
    }

    public void warning(SAXParseException ex) {
      warningCount++;
    }

    public void fatalError(SAXParseException ex) {
      if (parseException == null) {
        parseException = ex;
      }
    }

    public SAXException getFirstException() {
      return parseException;
    }
  }

  public Document load(java.net.URL url) throws DOMTestLoadException {
    Document doc = null;
    Exception parseException = null;
    try {
      LoadErrorHandler errorHandler = new LoadErrorHandler();
      builder.setErrorHandler(errorHandler);
      doc = builder.parse(url.openStream(), url.toString());
      parseException = errorHandler.getFirstException();
    }
    catch (Exception ex) {
      parseException = ex;
    }
    builder.setErrorHandler(null);
    if (parseException != null) {
      throw new DOMTestLoadException(parseException);
    }
    return doc;
  }

  public DOMImplementation getDOMImplementation() {
    return builder.getDOMImplementation();
  }

  public boolean hasFeature(String feature, String version) {
    return builder.getDOMImplementation().hasFeature(feature, version);
  }

  public boolean isCoalescing() {
    return factory.isCoalescing();
  }

  public boolean isExpandEntityReferences() {
    return factory.isExpandEntityReferences();
  }

  public boolean isIgnoringElementContentWhitespace() {
    return factory.isIgnoringElementContentWhitespace();
  }

  public boolean isNamespaceAware() {
    return factory.isNamespaceAware();
  }

  public boolean isValidating() {
    return factory.isValidating();
  }

  public static DocumentBuilderSetting[] getConfiguration1() {
    return new DocumentBuilderSetting[] {
        DocumentBuilderSetting.notCoalescing,
        DocumentBuilderSetting.notExpandEntityReferences,
        DocumentBuilderSetting.notIgnoringElementContentWhitespace,
        DocumentBuilderSetting.notNamespaceAware,
        DocumentBuilderSetting.notValidating};
  }

  public static DocumentBuilderSetting[] getConfiguration2() {
    return new DocumentBuilderSetting[] {
        DocumentBuilderSetting.notCoalescing,
        DocumentBuilderSetting.expandEntityReferences,
        DocumentBuilderSetting.ignoringElementContentWhitespace,
        DocumentBuilderSetting.namespaceAware,
        DocumentBuilderSetting.validating};

  }

}