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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
/*
* Copyright (c) 2001-2003 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 java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
/**
* This is an abstract base class for generated DOM tests
*
*/
public abstract class DOMTest /* wBM: implements EventListener */ {
private DOMTestDocumentBuilderFactory factory;
private int mutationCount = 0;
/**
* This is the appropriate constructor for tests that make no requirements
* on the parser configuration.
*
* @param factory
* must not be null
*/
public DOMTest(DOMTestDocumentBuilderFactory factory) {
if (factory == null) {
throw new NullPointerException("factory");
}
this.factory = factory;
}
/**
* This constructor is used by tests that must create a modified document
* factory to meet requirements on the parser configuration. setFactory
* should be called within the test's constructor.
*/
public DOMTest() {
factory = null;
}
/**
* Should only be called in the constructor of a derived type.
*/
protected void setFactory(DOMTestDocumentBuilderFactory factory) {
this.factory = factory;
}
public boolean hasFeature(String feature, String version) {
return factory.hasFeature(feature, version);
}
public boolean hasSetting(DocumentBuilderSetting setting) {
return setting.hasSetting(factory);
}
protected DOMTestDocumentBuilderFactory getFactory() {
return factory;
}
public DOMImplementation getImplementation() {
return factory.getDOMImplementation();
}
private URL resolveURI(String baseURI) throws DOMTestLoadException {
String docURI = factory.addExtension(baseURI);
URL resolvedURI = null;
try {
resolvedURI = new URL(docURI);
if (resolvedURI.getProtocol() != null) {
return resolvedURI;
}
}
catch (MalformedURLException ex) {
// throw new DOMTestLoadException(ex);
}
//
// build a URL for a test file in the JAR
//
resolvedURI = getClass().getResource("/" + docURI);
if (resolvedURI == null) {
//
// see if it is an absolute URI
//
int firstSlash = docURI.indexOf('/');
try {
if (firstSlash == 0
|| (firstSlash >= 1
&& docURI.charAt(firstSlash - 1) == ':')) {
resolvedURI = new URL(docURI);
}
else {
//
// try the files/level?/spec directory
//
String filename = getClass().getPackage().getName();
filename =
"tests/"
+ filename.substring(14).replace('.', '/')
+ "/files/"
+ docURI;
resolvedURI = new java.io.File(filename).toURL();
}
}
catch (MalformedURLException ex) {
throw new DOMTestLoadException(ex);
}
}
if (resolvedURI == null) {
throw new DOMTestLoadException(
new java.io.FileNotFoundException(docURI));
}
return resolvedURI;
}
public String getResourceURI(String href, String scheme, String contentType) throws
DOMTestLoadException {
if (scheme == null) {
throw new NullPointerException("scheme");
}
if ("file".equals(scheme)) {
return resolveURI(href).toString();
}
if ("http".equals(scheme)) {
StringBuffer httpURL = new StringBuffer(
System.getProperty("org.w3c.domts.httpbase",
"http://localhost:8080/webdav/"));
httpURL.append(href);
if ("application/pdf".equals(contentType)) {
httpURL.append(".pdf");
}
else {
httpURL.append(".xml");
}
return httpURL.toString();
}
throw new DOMTestLoadException(new Exception("Unrecognized URI scheme " +
scheme));
}
public String createTempURI(String scheme) throws DOMTestLoadException {
if (scheme == null) {
throw new NullPointerException("scheme");
}
if ("file".equals(scheme)) {
try {
File tempFile = File.createTempFile("domts", ".xml");
try {
//
// if available use JDK 1.4's File.toURI().toString()
//
Method method = File.class.getMethod("toURI", (Class<?>) null);
Object uri = method.invoke(tempFile, (Class<?>) null);
return uri.toString();
}
catch (NoSuchMethodException ex) {
//
// File.toURL is not as robust
//
URL url = tempFile.toURL();
return url.toString();
}
}
catch (Exception ex) {
throw new DOMTestLoadException(ex);
}
}
if ("http".equals(scheme)) {
String httpBase = System.getProperty("org.w3c.domts.httpbase",
"http://localhost:8080/webdav/");
java.lang.StringBuffer buf = new StringBuffer(httpBase);
if (!httpBase.endsWith("/")) {
buf.append("/");
}
buf.append("tmp");
buf.append( (new java.util.Random()).nextInt(Integer.MAX_VALUE));
buf.append(".xml");
return buf.toString();
}
throw new DOMTestLoadException(new Exception("Unrecognized URI scheme " +
scheme));
}
public Document load(String docURI, boolean willBeModified) throws
DOMTestLoadException {
Document doc = factory.load(resolveURI(docURI));
//
// if will be modified is false and doc is an EventTarget
//
/*
* wBM: if (!willBeModified && doc instanceof EventTarget) {
* ((EventTarget) doc).addEventListener("DOMSubtreeModified", this,
* false); }
*/
return doc;
}
public void preload(String contentType, String docURI, boolean willBeModified) throws
DOMTestIncompatibleException {
if ("text/html".equals(contentType) ||
"application/xhtml+xml".equals(contentType)) {
if (docURI.startsWith("staff") || docURI.equals("datatype_normalization")) {
throw DOMTestIncompatibleException.incompatibleLoad(docURI, contentType);
}
}
}
public Object createXPathEvaluator(Document doc) {
return factory.createXPathEvaluator(doc);
}
public InputStream createStream(String bytes) throws DOMTestLoadException,
IOException {
int byteCount = bytes.length() / 2;
byte[] array = new byte[byteCount];
for (int i = 0; i < byteCount; i++) {
array[i] = Byte.parseByte(bytes.substring(i * 2, i * 2 + 2), 16);
}
return new java.io.ByteArrayInputStream(array);
}
abstract public String getTargetURI();
public final boolean isCoalescing() {
return factory.isCoalescing();
}
public final boolean isExpandEntityReferences() {
return factory.isExpandEntityReferences();
}
public final boolean isIgnoringElementContentWhitespace() {
return factory.isIgnoringElementContentWhitespace();
}
public final boolean isNamespaceAware() {
return factory.isNamespaceAware();
}
public final boolean isValidating() {
return factory.isValidating();
}
public final boolean isSigned() {
return true;
}
public final boolean isHasNullString() {
return true;
}
public final String getContentType() {
return factory.getContentType();
}
/**
* Implementation of EventListener.handleEvent
*
* This method is called when a mutation is reported for a document that
* was declared to not be modified during testing
*
* @param evt
* mutation event
*/
/*
* wBM: public final void handleEvent(Event evt) { mutationCount++; }
*/
public final int getMutationCount() {
return mutationCount;
}
}
|