|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| package nu.xom.tests; |
|
23 |
| |
|
24 |
| import java.io.BufferedInputStream; |
|
25 |
| import java.io.ByteArrayInputStream; |
|
26 |
| import java.io.ByteArrayOutputStream; |
|
27 |
| import java.io.DataInputStream; |
|
28 |
| import java.io.File; |
|
29 |
| import java.io.FileInputStream; |
|
30 |
| import java.io.FilenameFilter; |
|
31 |
| import java.io.IOException; |
|
32 |
| import java.io.InputStream; |
|
33 |
| import java.io.OutputStream; |
|
34 |
| |
|
35 |
| import nu.xom.Attribute; |
|
36 |
| import nu.xom.Builder; |
|
37 |
| import nu.xom.Comment; |
|
38 |
| import nu.xom.DocType; |
|
39 |
| import nu.xom.Document; |
|
40 |
| import nu.xom.Element; |
|
41 |
| import nu.xom.Elements; |
|
42 |
| import nu.xom.Namespace; |
|
43 |
| import nu.xom.Nodes; |
|
44 |
| import nu.xom.ParsingException; |
|
45 |
| import nu.xom.ProcessingInstruction; |
|
46 |
| import nu.xom.Serializer; |
|
47 |
| import nu.xom.Text; |
|
48 |
| import nu.xom.XPathContext; |
|
49 |
| import nu.xom.canonical.CanonicalizationException; |
|
50 |
| import nu.xom.canonical.Canonicalizer; |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| public class CanonicalizerTest extends XOMTestCase { |
|
62 |
| |
|
63 |
| private final static double version = Double.parseDouble( |
|
64 |
| System.getProperty("java.version").substring(0,3) |
|
65 |
| ); |
|
66 |
| |
|
67 |
| private File canonical; |
|
68 |
| private File input; |
|
69 |
| private File output; |
|
70 |
| |
|
71 |
89
| public CanonicalizerTest(String name) {
|
|
72 |
89
| super(name);
|
|
73 |
| } |
|
74 |
| |
|
75 |
| |
|
76 |
| private Builder builder = new Builder(); |
|
77 |
| |
|
78 |
| |
|
79 |
89
| protected void setUp() {
|
|
80 |
89
| File data = new File("data");
|
|
81 |
89
| canonical = new File(data, "canonical");
|
|
82 |
89
| input = new File(canonical, "input");
|
|
83 |
89
| output = new File(canonical, "output");
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
1
| public void testCanonicalizeOnlyAttributes() throws IOException {
|
|
88 |
| |
|
89 |
1
| Element pdu = new Element("doc");
|
|
90 |
1
| pdu.addAttribute(new Attribute("a1", "v1"));
|
|
91 |
1
| pdu.addAttribute(new Attribute("a2", "v2"));
|
|
92 |
| |
|
93 |
1
| String expected = " a1=\"v1\" a2=\"v2\"";
|
|
94 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
95 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out);
|
|
96 |
| |
|
97 |
1
| Document doc = new Document(pdu);
|
|
98 |
1
| canonicalizer.write(doc.query("//@*"));
|
|
99 |
| |
|
100 |
1
| byte[] result = out.toByteArray();
|
|
101 |
1
| out.close();
|
|
102 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
103 |
1
| assertEquals(expected, s);
|
|
104 |
| |
|
105 |
| } |
|
106 |
| |
|
107 |
| |
|
108 |
1
| public void testRemoveDuplicateAttributes() throws IOException {
|
|
109 |
| |
|
110 |
1
| Element pdu = new Element("doc");
|
|
111 |
1
| Attribute a1 = new Attribute("a1", "v1");
|
|
112 |
1
| pdu.addAttribute(a1);
|
|
113 |
1
| Attribute a2 = new Attribute("a2", "v2");
|
|
114 |
1
| pdu.addAttribute(a2);
|
|
115 |
| |
|
116 |
1
| String expected = " a1=\"v1\" a2=\"v2\"";
|
|
117 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
118 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out);
|
|
119 |
| |
|
120 |
1
| Document doc = new Document(pdu);
|
|
121 |
1
| Nodes subset = doc.query("//@*");
|
|
122 |
1
| subset.append(a1);
|
|
123 |
1
| subset.append(a2);
|
|
124 |
1
| canonicalizer.write(subset);
|
|
125 |
| |
|
126 |
1
| byte[] result = out.toByteArray();
|
|
127 |
1
| out.close();
|
|
128 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
129 |
1
| assertEquals(expected, s);
|
|
130 |
| |
|
131 |
| } |
|
132 |
| |
|
133 |
| |
|
134 |
1
| public void testCanonicalizeOnlyNamespaces() throws IOException {
|
|
135 |
| |
|
136 |
1
| Element pdu = new Element("doc", "http://www.example.com");
|
|
137 |
| |
|
138 |
1
| String expected = " xmlns=\"http://www.example.com\"";
|
|
139 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
140 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out);
|
|
141 |
| |
|
142 |
1
| Document doc = new Document(pdu);
|
|
143 |
1
| canonicalizer.write(doc.query("//namespace::node()"));
|
|
144 |
| |
|
145 |
1
| byte[] result = out.toByteArray();
|
|
146 |
1
| out.close();
|
|
147 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
148 |
1
| assertEquals(expected, s);
|
|
149 |
| |
|
150 |
| } |
|
151 |
| |
|
152 |
| |
|
153 |
1
| public void testCanonicalizeOnlyPrefixedNamespaces()
|
|
154 |
| throws IOException { |
|
155 |
| |
|
156 |
1
| Element pdu = new Element("pre:doc", "http://www.example.com");
|
|
157 |
| |
|
158 |
1
| String expected = " xmlns:pre=\"http://www.example.com\"";
|
|
159 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
160 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out);
|
|
161 |
| |
|
162 |
1
| Document doc = new Document(pdu);
|
|
163 |
1
| canonicalizer.write(doc.query("//namespace::node()"));
|
|
164 |
| |
|
165 |
1
| byte[] result = out.toByteArray();
|
|
166 |
1
| out.close();
|
|
167 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
168 |
1
| assertEquals(expected, s);
|
|
169 |
| |
|
170 |
| } |
|
171 |
| |
|
172 |
| |
|
173 |
1
| public void testCanonicalizeWithNullAlgorithm()
|
|
174 |
| throws IOException { |
|
175 |
| |
|
176 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
177 |
1
| try {
|
|
178 |
1
| new Canonicalizer(out, null);
|
|
179 |
0
| fail("Allowed null algorithm");
|
|
180 |
| } |
|
181 |
| catch (NullPointerException success) { |
|
182 |
1
| assertNotNull(success.getMessage());
|
|
183 |
| } |
|
184 |
| |
|
185 |
| } |
|
186 |
| |
|
187 |
| |
|
188 |
1
| public void testCanonicalizeCommentsInPrologAndEpilog() throws IOException {
|
|
189 |
| |
|
190 |
1
| Element pdu = new Element("doc");
|
|
191 |
| |
|
192 |
1
| Document doc = new Document(pdu);
|
|
193 |
1
| doc.insertChild(new Comment("comment 1"), 0);
|
|
194 |
1
| doc.insertChild(new Comment("comment 2"), 1);
|
|
195 |
1
| doc.appendChild(new Comment("comment 3"));
|
|
196 |
1
| doc.appendChild(new Comment("comment 4"));
|
|
197 |
| |
|
198 |
1
| String expected = "<!--comment 1-->\n<!--comment 2-->\n\n<!--comment 3-->\n<!--comment 4-->";
|
|
199 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
200 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out);
|
|
201 |
| |
|
202 |
1
| canonicalizer.write(doc.query("//comment()"));
|
|
203 |
| |
|
204 |
1
| byte[] result = out.toByteArray();
|
|
205 |
1
| out.close();
|
|
206 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
207 |
1
| assertEquals(expected, s);
|
|
208 |
| |
|
209 |
| } |
|
210 |
| |
|
211 |
| |
|
212 |
1
| public void testTamin() throws ParsingException, IOException {
|
|
213 |
| |
|
214 |
1
| String input = "<ns1:root xmlns:ns1='http://www.example.org/'><elt1></elt1></ns1:root>";
|
|
215 |
1
| Document doc = builder.build(input, null);
|
|
216 |
| |
|
217 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
218 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out);
|
|
219 |
| |
|
220 |
1
| canonicalizer.write(doc);
|
|
221 |
| |
|
222 |
1
| byte[] result = out.toByteArray();
|
|
223 |
1
| out.close();
|
|
224 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
225 |
1
| assertEquals("<ns1:root xmlns:ns1=\"http://www.example.org/\"><elt1></elt1></ns1:root>", s);
|
|
226 |
| |
|
227 |
| } |
|
228 |
| |
|
229 |
| |
|
230 |
1
| public void testCanonicalizePrologAndEpilog() throws IOException {
|
|
231 |
| |
|
232 |
1
| Element pdu = new Element("doc");
|
|
233 |
| |
|
234 |
1
| Document doc = new Document(pdu);
|
|
235 |
1
| doc.insertChild(new ProcessingInstruction("target", "value"), 0);
|
|
236 |
1
| doc.insertChild(new Comment("comment 2"), 1);
|
|
237 |
1
| doc.appendChild(new Comment("comment 3"));
|
|
238 |
1
| doc.appendChild(new ProcessingInstruction("target", "value"));
|
|
239 |
| |
|
240 |
1
| String expected = "<?target value?>\n<!--comment 2-->\n<doc></doc>\n<!--comment 3-->\n<?target value?>";
|
|
241 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
242 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out);
|
|
243 |
| |
|
244 |
1
| canonicalizer.write(doc);
|
|
245 |
| |
|
246 |
1
| byte[] result = out.toByteArray();
|
|
247 |
1
| out.close();
|
|
248 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
249 |
1
| assertEquals(expected, s);
|
|
250 |
| |
|
251 |
| } |
|
252 |
| |
|
253 |
| |
|
254 |
1
| public void testCanonicalizeOnlyAttributesOnDifferentElements()
|
|
255 |
| throws IOException { |
|
256 |
| |
|
257 |
1
| Element pdu = new Element("doc");
|
|
258 |
1
| pdu.addAttribute(new Attribute("a2", "v1"));
|
|
259 |
1
| Element child = new Element("child");
|
|
260 |
1
| child.addAttribute(new Attribute("a1", "v2"));
|
|
261 |
1
| pdu.appendChild(child);
|
|
262 |
| |
|
263 |
1
| String expected = " a2=\"v1\" a1=\"v2\"";
|
|
264 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
265 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out);
|
|
266 |
| |
|
267 |
1
| Document doc = new Document(pdu);
|
|
268 |
1
| canonicalizer.write(doc.query("//@*"));
|
|
269 |
| |
|
270 |
1
| byte[] result = out.toByteArray();
|
|
271 |
1
| out.close();
|
|
272 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
273 |
1
| assertEquals(expected, s);
|
|
274 |
| |
|
275 |
| } |
|
276 |
| |
|
277 |
| |
|
278 |
1
| public void testCanonicalizeAttributesWithFunkyCharacters()
|
|
279 |
| throws IOException { |
|
280 |
| |
|
281 |
1
| Element pdu = new Element("doc");
|
|
282 |
1
| pdu.addAttribute(new Attribute("a2", "v1&<>\"\t\r\n"));
|
|
283 |
| |
|
284 |
1
| String expected = " a2=\"v1&<>"	
\"";
|
|
285 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
286 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out);
|
|
287 |
| |
|
288 |
1
| Document doc = new Document(pdu);
|
|
289 |
1
| canonicalizer.write(doc.query("//@*"));
|
|
290 |
| |
|
291 |
1
| byte[] result = out.toByteArray();
|
|
292 |
1
| out.close();
|
|
293 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
294 |
1
| assertEquals(expected, s);
|
|
295 |
| |
|
296 |
| } |
|
297 |
| |
|
298 |
| |
|
299 |
1
| public void testExclusiveEmptyRootElementInNoNamespace()
|
|
300 |
| throws IOException { |
|
301 |
| |
|
302 |
1
| Element pdu = new Element("doc");
|
|
303 |
| |
|
304 |
1
| String expected = "<doc></doc>";
|
|
305 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
306 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out,
|
|
307 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS); |
|
308 |
| |
|
309 |
1
| Document doc = new Document(pdu);
|
|
310 |
1
| canonicalizer.write(doc);
|
|
311 |
| |
|
312 |
1
| byte[] result = out.toByteArray();
|
|
313 |
1
| out.close();
|
|
314 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
315 |
1
| assertEquals(expected, s);
|
|
316 |
| |
|
317 |
| } |
|
318 |
| |
|
319 |
| |
|
320 |
1
| public void testExclusiveEmptyRootElementInNoNamespaceWithTwoAttributes()
|
|
321 |
| throws IOException { |
|
322 |
| |
|
323 |
1
| Element pdu = new Element("doc");
|
|
324 |
1
| pdu.addAttribute(new Attribute("a1", "v1"));
|
|
325 |
1
| pdu.addAttribute(new Attribute("a2", "v2"));
|
|
326 |
| |
|
327 |
1
| String expected = "<doc a1=\"v1\" a2=\"v2\"></doc>";
|
|
328 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
329 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out,
|
|
330 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS); |
|
331 |
| |
|
332 |
1
| Document doc = new Document(pdu);
|
|
333 |
1
| canonicalizer.write(doc);
|
|
334 |
| |
|
335 |
1
| byte[] result = out.toByteArray();
|
|
336 |
1
| out.close();
|
|
337 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
338 |
1
| assertEquals(expected, s);
|
|
339 |
| |
|
340 |
| } |
|
341 |
| |
|
342 |
| |
|
343 |
1
| public void testExclusiveDoesntRenderUnusedPrefix()
|
|
344 |
| throws IOException { |
|
345 |
| |
|
346 |
1
| Element pdu = new Element("n0:tuck", "http://a.example");
|
|
347 |
1
| pdu.addNamespaceDeclaration("pre", "http://www.example.org/");
|
|
348 |
| |
|
349 |
1
| String expected = "<n0:tuck xmlns:n0=\"http://a.example\"></n0:tuck>";
|
|
350 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
351 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out,
|
|
352 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS); |
|
353 |
| |
|
354 |
1
| Document doc = new Document(pdu);
|
|
355 |
1
| canonicalizer.write(doc);
|
|
356 |
| |
|
357 |
1
| byte[] result = out.toByteArray();
|
|
358 |
1
| out.close();
|
|
359 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
360 |
1
| assertEquals(expected, s);
|
|
361 |
| |
|
362 |
| } |
|
363 |
| |
|
364 |
| |
|
365 |
1
| public void testWriteDefaultNamespace() throws IOException {
|
|
366 |
| |
|
367 |
1
| Element pdu = new Element("tuck", "http://www.example.org/");
|
|
368 |
| |
|
369 |
1
| String expected = " xmlns=\"http://www.example.org/\"";
|
|
370 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
371 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out,
|
|
372 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS); |
|
373 |
| |
|
374 |
1
| Document doc = new Document(pdu);
|
|
375 |
1
| Nodes subset = doc.query("//namespace::node()");
|
|
376 |
1
| canonicalizer.write(subset);
|
|
377 |
| |
|
378 |
1
| byte[] result = out.toByteArray();
|
|
379 |
1
| out.close();
|
|
380 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
381 |
1
| assertEquals(expected, s);
|
|
382 |
| |
|
383 |
| } |
|
384 |
| |
|
385 |
| |
|
386 |
1
| public void testOutputAncestorAttributeAndChildHaveDifferentLanguages()
|
|
387 |
| throws IOException { |
|
388 |
| |
|
389 |
1
| Element pdu = new Element("tuck");
|
|
390 |
1
| pdu.addAttribute(new Attribute("xml:lang", Namespace.XML_NAMESPACE, "fr"));
|
|
391 |
| |
|
392 |
1
| Element middle = new Element("middle");
|
|
393 |
1
| pdu.appendChild(middle);
|
|
394 |
1
| Element child = new Element("child");
|
|
395 |
1
| child.addAttribute(new Attribute("xml:lang", Namespace.XML_NAMESPACE, "en"));
|
|
396 |
1
| middle.appendChild(child);
|
|
397 |
| |
|
398 |
1
| String expected = "<tuck xml:lang=\"fr\"><child xml:lang=\"en\"></child></tuck>";
|
|
399 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
400 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out,
|
|
401 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS); |
|
402 |
| |
|
403 |
1
| Document doc = new Document(pdu);
|
|
404 |
1
| Nodes subset = doc.query("/* | //child | //@*");
|
|
405 |
1
| canonicalizer.write(subset);
|
|
406 |
| |
|
407 |
1
| byte[] result = out.toByteArray();
|
|
408 |
1
| out.close();
|
|
409 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
410 |
1
| assertEquals(expected, s);
|
|
411 |
| |
|
412 |
| } |
|
413 |
| |
|
414 |
| |
|
415 |
1
| public void testOutputAncestorAttributeUsesPrefix()
|
|
416 |
| throws IOException { |
|
417 |
| |
|
418 |
1
| Element pdu = new Element("tuck");
|
|
419 |
1
| pdu.addAttribute(new Attribute("pre:foo", "http://www.example.org/", "value"));
|
|
420 |
1
| Element child = new Element("pre:test", "http://www.example.org/");
|
|
421 |
1
| pdu.appendChild(child);
|
|
422 |
| |
|
423 |
1
| String expected = "<tuck xmlns:pre=\"http://www.example.org/\" pre:foo=\"value\"><pre:test></pre:test></tuck>";
|
|
424 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
425 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out,
|
|
426 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS); |
|
427 |
| |
|
428 |
1
| Document doc = new Document(pdu);
|
|
429 |
1
| canonicalizer.write(doc);
|
|
430 |
| |
|
431 |
1
| byte[] result = out.toByteArray();
|
|
432 |
1
| out.close();
|
|
433 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
434 |
1
| assertEquals(expected, s);
|
|
435 |
| |
|
436 |
| } |
|
437 |
| |
|
438 |
| |
|
439 |
1
| public void testOutputAncestorAttributeRedefinesPrefix()
|
|
440 |
| throws IOException { |
|
441 |
| |
|
442 |
1
| Element pdu = new Element("tuck");
|
|
443 |
1
| pdu.addAttribute(new Attribute("pre:foo", "http://www.example.com/", "value"));
|
|
444 |
1
| Element child = new Element("pre:test", "http://www.example.org/");
|
|
445 |
1
| pdu.appendChild(child);
|
|
446 |
| |
|
447 |
1
| String expected = "<tuck xmlns:pre=\"http://www.example.com/\" "
|
|
448 |
| + "pre:foo=\"value\"><pre:test xmlns:pre=\"http://www.example.org/\"></pre:test></tuck>"; |
|
449 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
450 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out,
|
|
451 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS); |
|
452 |
| |
|
453 |
1
| Document doc = new Document(pdu);
|
|
454 |
1
| canonicalizer.write(doc);
|
|
455 |
| |
|
456 |
1
| byte[] result = out.toByteArray();
|
|
457 |
1
| out.close();
|
|
458 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
459 |
1
| assertEquals(expected, s);
|
|
460 |
| |
|
461 |
| } |
|
462 |
| |
|
463 |
| |
|
464 |
1
| public void testExclusiveDoesntRenderUnusedPrefixFromUnincludedAttribute()
|
|
465 |
| throws IOException { |
|
466 |
| |
|
467 |
1
| Element pdu = new Element("n0:tuck", "http://a.example");
|
|
468 |
1
| pdu.addAttribute(new Attribute("pre:foo", "http://www.example.org/", "test"));
|
|
469 |
| |
|
470 |
1
| String expected = "<n0:tuck xmlns:n0=\"http://a.example\"></n0:tuck>";
|
|
471 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
472 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out,
|
|
473 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS); |
|
474 |
| |
|
475 |
1
| Document doc = new Document(pdu);
|
|
476 |
1
| canonicalizer.write(doc.query("//* | //namespace::node()"));
|
|
477 |
| |
|
478 |
1
| byte[] result = out.toByteArray();
|
|
479 |
1
| out.close();
|
|
480 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
481 |
1
| assertEquals(expected, s);
|
|
482 |
| |
|
483 |
| } |
|
484 |
| |
|
485 |
| |
|
486 |
1
| public void testWithComments()
|
|
487 |
| throws ParsingException, IOException { |
|
488 |
| |
|
489 |
1
| File tests = input;
|
|
490 |
1
| String[] inputs = tests.list(new XMLFilter());
|
|
491 |
1
| for (int i = 0; i < inputs.length; i++) {
|
|
492 |
10
| File input = new File(tests, inputs[i]);
|
|
493 |
10
| Document doc = builder.build(input);
|
|
494 |
10
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
495 |
10
| try {
|
|
496 |
10
| Canonicalizer serializer = new Canonicalizer(out);
|
|
497 |
10
| serializer.write(doc);
|
|
498 |
| } |
|
499 |
| finally { |
|
500 |
10
| out.close();
|
|
501 |
| } |
|
502 |
10
| byte[] actual = out.toByteArray();
|
|
503 |
| |
|
504 |
| |
|
505 |
| |
|
506 |
| |
|
507 |
| |
|
508 |
| |
|
509 |
| |
|
510 |
| |
|
511 |
10
| File expected = new File(output, input.getName() + ".out");
|
|
512 |
10
| assertEquals(
|
|
513 |
| input.getName(), expected.length(), actual.length); |
|
514 |
10
| byte[] expectedBytes = new byte[actual.length];
|
|
515 |
10
| InputStream fin = new FileInputStream(expected);
|
|
516 |
10
| DataInputStream in = new DataInputStream(fin);
|
|
517 |
10
| try {
|
|
518 |
10
| in.readFully(expectedBytes);
|
|
519 |
| } |
|
520 |
| finally { |
|
521 |
10
| in.close();
|
|
522 |
| } |
|
523 |
10
| for (int j = 0; j < expectedBytes.length; j++) {
|
|
524 |
2199
| assertEquals(expectedBytes[i], actual[i]);
|
|
525 |
| } |
|
526 |
| |
|
527 |
| } |
|
528 |
| |
|
529 |
| } |
|
530 |
| |
|
531 |
| |
|
532 |
1
| public void testNamedAlgorithmWithComments()
|
|
533 |
| throws ParsingException, IOException { |
|
534 |
| |
|
535 |
1
| File tests = input;
|
|
536 |
1
| String[] inputs = tests.list(new XMLFilter());
|
|
537 |
1
| for (int i = 0; i < inputs.length; i++) {
|
|
538 |
10
| File input = new File(tests, inputs[i]);
|
|
539 |
10
| Document doc = builder.build(input);
|
|
540 |
10
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
541 |
10
| try {
|
|
542 |
10
| Canonicalizer serializer = new Canonicalizer(
|
|
543 |
| out, Canonicalizer.CANONICAL_XML_WITH_COMMENTS); |
|
544 |
10
| serializer.write(doc);
|
|
545 |
| } |
|
546 |
| finally { |
|
547 |
10
| out.close();
|
|
548 |
| } |
|
549 |
10
| byte[] actual = out.toByteArray();
|
|
550 |
| |
|
551 |
| |
|
552 |
| |
|
553 |
| |
|
554 |
| |
|
555 |
| |
|
556 |
| |
|
557 |
| |
|
558 |
10
| File expected = new File(output, input.getName() + ".out");
|
|
559 |
10
| assertEquals(
|
|
560 |
| input.getName(), expected.length(), actual.length); |
|
561 |
10
| byte[] expectedBytes = new byte[actual.length];
|
|
562 |
10
| InputStream fin = new FileInputStream(expected);
|
|
563 |
10
| DataInputStream in = new DataInputStream(fin);
|
|
564 |
10
| try {
|
|
565 |
10
| in.readFully(expectedBytes);
|
|
566 |
| } |
|
567 |
| finally { |
|
568 |
10
| in.close();
|
|
569 |
| } |
|
570 |
10
| for (int j = 0; j < expectedBytes.length; j++) {
|
|
571 |
2199
| assertEquals(expectedBytes[i], actual[i]);
|
|
572 |
| } |
|
573 |
| |
|
574 |
| } |
|
575 |
| |
|
576 |
| } |
|
577 |
| |
|
578 |
| |
|
579 |
1
| public void testWithoutComments()
|
|
580 |
| throws ParsingException, IOException { |
|
581 |
| |
|
582 |
1
| File tests = input;
|
|
583 |
1
| String[] inputs = tests.list(new XMLFilter());
|
|
584 |
1
| for (int i = 0; i < inputs.length; i++) {
|
|
585 |
10
| File input = new File(tests, inputs[i]);
|
|
586 |
10
| Document doc = builder.build(input);
|
|
587 |
| |
|
588 |
10
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
589 |
10
| try {
|
|
590 |
10
| Canonicalizer serializer
|
|
591 |
| = new Canonicalizer(out, false); |
|
592 |
10
| serializer.write(doc);
|
|
593 |
| } |
|
594 |
| finally { |
|
595 |
10
| out.close();
|
|
596 |
| } |
|
597 |
| |
|
598 |
10
| byte[] actual = out.toByteArray();
|
|
599 |
| |
|
600 |
10
| File expected = new File(canonical, "wocommentsoutput/");
|
|
601 |
10
| expected = new File(expected, input.getName() + ".out");
|
|
602 |
10
| byte[] expectedBytes = new byte[actual.length];
|
|
603 |
10
| InputStream fin = new FileInputStream(expected);
|
|
604 |
10
| DataInputStream in = new DataInputStream(fin);
|
|
605 |
10
| try {
|
|
606 |
10
| in.readFully(expectedBytes);
|
|
607 |
| } |
|
608 |
| finally { |
|
609 |
10
| in.close();
|
|
610 |
| } |
|
611 |
10
| for (int j = 0; j < expectedBytes.length; j++) {
|
|
612 |
2116
| assertEquals(expectedBytes[i], actual[i]);
|
|
613 |
| } |
|
614 |
10
| out.close();
|
|
615 |
| |
|
616 |
| } |
|
617 |
| |
|
618 |
| } |
|
619 |
| |
|
620 |
| |
|
621 |
1
| public void testNamedAlgorithmWithoutComments()
|
|
622 |
| throws ParsingException, IOException { |
|
623 |
| |
|
624 |
1
| File tests = input;
|
|
625 |
1
| String[] inputs = tests.list(new XMLFilter());
|
|
626 |
1
| for (int i = 0; i < inputs.length; i++) {
|
|
627 |
10
| File input = new File(tests, inputs[i]);
|
|
628 |
10
| Document doc = builder.build(input);
|
|
629 |
| |
|
630 |
10
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
631 |
10
| try {
|
|
632 |
10
| Canonicalizer serializer = new Canonicalizer(
|
|
633 |
| out, Canonicalizer.CANONICAL_XML); |
|
634 |
10
| serializer.write(doc);
|
|
635 |
| } |
|
636 |
| finally { |
|
637 |
10
| out.close();
|
|
638 |
| } |
|
639 |
| |
|
640 |
10
| byte[] actual = out.toByteArray();
|
|
641 |
| |
|
642 |
10
| File expected = new File(canonical, "wocommentsoutput/");
|
|
643 |
10
| expected = new File(expected, input.getName() + ".out");
|
|
644 |
10
| byte[] expectedBytes = new byte[actual.length];
|
|
645 |
10
| InputStream fin = new FileInputStream(expected);
|
|
646 |
10
| DataInputStream in = new DataInputStream(fin);
|
|
647 |
10
| try {
|
|
648 |
10
| in.readFully(expectedBytes);
|
|
649 |
| } |
|
650 |
| finally { |
|
651 |
10
| in.close();
|
|
652 |
| } |
|
653 |
10
| for (int j = 0; j < expectedBytes.length; j++) {
|
|
654 |
2116
| assertEquals(expectedBytes[i], actual[i]);
|
|
655 |
| } |
|
656 |
10
| out.close();
|
|
657 |
| |
|
658 |
| } |
|
659 |
| |
|
660 |
| } |
|
661 |
| |
|
662 |
| |
|
663 |
1
| public void testXMLNamespaceAttributeInheritance()
|
|
664 |
| throws IOException { |
|
665 |
| |
|
666 |
1
| Element root = new Element("root");
|
|
667 |
1
| Document doc = new Document(root);
|
|
668 |
1
| root.addAttribute(new Attribute("xml:id", Namespace.XML_NAMESPACE, "p1"));
|
|
669 |
1
| root.appendChild(new Element("child312"));
|
|
670 |
| |
|
671 |
1
| String expected = "<child312 xml:id=\"p1\"></child312>";
|
|
672 |
| |
|
673 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
674 |
1
| try {
|
|
675 |
1
| Canonicalizer serializer = new Canonicalizer(out, false);
|
|
676 |
1
| serializer.write(doc.query("/*/child312"));
|
|
677 |
| } |
|
678 |
| finally { |
|
679 |
1
| out.close();
|
|
680 |
| } |
|
681 |
| |
|
682 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
683 |
1
| assertEquals(expected, actual);
|
|
684 |
| |
|
685 |
| } |
|
686 |
| |
|
687 |
| |
|
688 |
1
| public void testXMLNamespaceAttributeInheritanceThroughMultipleLevels()
|
|
689 |
| throws IOException { |
|
690 |
| |
|
691 |
1
| Element superroot = new Element("superroot");
|
|
692 |
1
| Element root = new Element("root");
|
|
693 |
1
| superroot.appendChild(root);
|
|
694 |
1
| superroot.addAttribute(new Attribute("xml:id", Namespace.XML_NAMESPACE, "p0"));
|
|
695 |
1
| Document doc = new Document(superroot);
|
|
696 |
1
| root.addAttribute(new Attribute("xml:id", Namespace.XML_NAMESPACE, "p1"));
|
|
697 |
1
| root.appendChild(new Element("child312"));
|
|
698 |
| |
|
699 |
1
| String expected = "<child312 xml:id=\"p1\"></child312>";
|
|
700 |
| |
|
701 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
702 |
1
| try {
|
|
703 |
1
| Canonicalizer serializer = new Canonicalizer(out, false);
|
|
704 |
1
| Nodes result = doc.query("/*/*/child312");
|
|
705 |
1
| serializer.write(result);
|
|
706 |
| } |
|
707 |
| finally { |
|
708 |
1
| out.close();
|
|
709 |
| } |
|
710 |
| |
|
711 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
712 |
1
| assertEquals(expected, actual);
|
|
713 |
| |
|
714 |
| } |
|
715 |
| |
|
716 |
| |
|
717 |
1
| public void testXMLNamespaceAttributeInheritanceThroughMultipleLevelsWithSkippedMiddle()
|
|
718 |
| throws IOException { |
|
719 |
| |
|
720 |
1
| Element superroot = new Element("superroot");
|
|
721 |
1
| Element root = new Element("root");
|
|
722 |
1
| superroot.appendChild(root);
|
|
723 |
1
| superroot.addAttribute(new Attribute("xml:id", Namespace.XML_NAMESPACE, "p0"));
|
|
724 |
1
| Document doc = new Document(superroot);
|
|
725 |
1
| root.addAttribute(new Attribute("xml:id", Namespace.XML_NAMESPACE, "p1"));
|
|
726 |
1
| root.appendChild(new Element("child312"));
|
|
727 |
| |
|
728 |
1
| String expected = "<superroot xml:id=\"p0\"><child312 xml:id=\"p1\"></child312></superroot>";
|
|
729 |
| |
|
730 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
731 |
1
| try {
|
|
732 |
1
| Canonicalizer serializer = new Canonicalizer(out, false);
|
|
733 |
1
| serializer.write(doc.query("/* | //child312 | /*/@* | //child312/@*"));
|
|
734 |
| } |
|
735 |
| finally { |
|
736 |
1
| out.close();
|
|
737 |
| } |
|
738 |
| |
|
739 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
740 |
1
| assertEquals(expected, actual);
|
|
741 |
| |
|
742 |
| } |
|
743 |
| |
|
744 |
| |
|
745 |
1
| public void testXMLNamespaceAttributeInheritanceNearestIsInSubset()
|
|
746 |
| throws IOException { |
|
747 |
| |
|
748 |
1
| Element superroot = new Element("superroot");
|
|
749 |
1
| Element root = new Element("root");
|
|
750 |
1
| superroot.appendChild(root);
|
|
751 |
1
| superroot.addAttribute(new Attribute("xml:id", Namespace.XML_NAMESPACE, "p0"));
|
|
752 |
1
| Document doc = new Document(superroot);
|
|
753 |
1
| root.appendChild(new Element("child312"));
|
|
754 |
| |
|
755 |
1
| String expected = "<superroot xml:id=\"p0\"><child312></child312></superroot>";
|
|
756 |
| |
|
757 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
758 |
1
| try {
|
|
759 |
1
| Canonicalizer serializer = new Canonicalizer(out, false);
|
|
760 |
1
| serializer.write(doc.query("/* | //child312 | /*/@* | //child312/@*"));
|
|
761 |
| } |
|
762 |
| finally { |
|
763 |
1
| out.close();
|
|
764 |
| } |
|
765 |
| |
|
766 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
767 |
1
| assertEquals(expected, actual);
|
|
768 |
| |
|
769 |
| } |
|
770 |
| |
|
771 |
| |
|
772 |
1
| public void testXMLNamespaceAttributeNotOverridden()
|
|
773 |
| throws IOException { |
|
774 |
| |
|
775 |
1
| Element root = new Element("root");
|
|
776 |
1
| Document doc = new Document(root);
|
|
777 |
1
| Element child = new Element("child312");
|
|
778 |
| |
|
779 |
1
| root.addAttribute(new Attribute("xml:id", Namespace.XML_NAMESPACE, "p1"));
|
|
780 |
1
| child.addAttribute(new Attribute("xml:id", Namespace.XML_NAMESPACE, "p2"));
|
|
781 |
| |
|
782 |
1
| root.appendChild(child);
|
|
783 |
| |
|
784 |
1
| String expected = "<child312 xml:id=\"p2\"></child312>";
|
|
785 |
| |
|
786 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
787 |
1
| try {
|
|
788 |
1
| Canonicalizer serializer = new Canonicalizer(out, false);
|
|
789 |
1
| serializer.write(doc.query("/*/child312 | /*/*/@*"));
|
|
790 |
| } |
|
791 |
| finally { |
|
792 |
1
| out.close();
|
|
793 |
| } |
|
794 |
| |
|
795 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
796 |
1
| assertEquals(expected, actual);
|
|
797 |
| |
|
798 |
| } |
|
799 |
| |
|
800 |
| |
|
801 |
1
| public void testXMLNamespaceAttributeNotOverridden2()
|
|
802 |
| throws IOException { |
|
803 |
| |
|
804 |
1
| Element root = new Element("root");
|
|
805 |
1
| Document doc = new Document(root);
|
|
806 |
1
| Element child = new Element("child312");
|
|
807 |
| |
|
808 |
1
| root.addAttribute(new Attribute("xml:id", Namespace.XML_NAMESPACE, "p1"));
|
|
809 |
1
| child.addAttribute(new Attribute("xml:id", Namespace.XML_NAMESPACE, "p2"));
|
|
810 |
| |
|
811 |
1
| root.appendChild(child);
|
|
812 |
| |
|
813 |
1
| String expected = "<child312></child312>";
|
|
814 |
| |
|
815 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
816 |
1
| try {
|
|
817 |
1
| Canonicalizer serializer = new Canonicalizer(out, false);
|
|
818 |
1
| serializer.write(doc.query("/*/child312 "));
|
|
819 |
| } |
|
820 |
| finally { |
|
821 |
1
| out.close();
|
|
822 |
| } |
|
823 |
| |
|
824 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
825 |
1
| assertEquals(expected, actual);
|
|
826 |
| |
|
827 |
| } |
|
828 |
| |
|
829 |
| |
|
830 |
1
| public void testXMLNamespaceAttributeNotInheritedWithExclusiveCanonicalization()
|
|
831 |
| throws IOException { |
|
832 |
| |
|
833 |
1
| Element root = new Element("root");
|
|
834 |
1
| Document doc = new Document(root);
|
|
835 |
1
| root.addAttribute(new Attribute("xml:id", Namespace.XML_NAMESPACE, "p1"));
|
|
836 |
1
| root.appendChild(new Element("child312"));
|
|
837 |
| |
|
838 |
1
| String expected = "<child312></child312>";
|
|
839 |
| |
|
840 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
841 |
1
| try {
|
|
842 |
1
| Canonicalizer serializer = new Canonicalizer(out,
|
|
843 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION); |
|
844 |
1
| serializer.write(doc.query("/*/child312"));
|
|
845 |
| } |
|
846 |
| finally { |
|
847 |
1
| out.close();
|
|
848 |
| } |
|
849 |
| |
|
850 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
851 |
1
| assertEquals(expected, actual);
|
|
852 |
| |
|
853 |
| } |
|
854 |
| |
|
855 |
| |
|
856 |
1
| public void testXMLNSAttributeNotInheritedWithExclusiveCanonicalization()
|
|
857 |
| throws IOException { |
|
858 |
| |
|
859 |
1
| Element root = new Element("root", "http://www.example.org/");
|
|
860 |
1
| Document doc = new Document(root);
|
|
861 |
1
| root.appendChild(new Element("child312", "http://www.example.org/"));
|
|
862 |
| |
|
863 |
1
| String expected = "<child312 xmlns=\"http://www.example.org/\"></child312>";
|
|
864 |
| |
|
865 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
866 |
1
| try {
|
|
867 |
1
| Canonicalizer serializer = new Canonicalizer(out,
|
|
868 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION); |
|
869 |
1
| XPathContext context = new XPathContext("pre", "http://www.example.org/");
|
|
870 |
1
| serializer.write(doc.query("/*/pre:child312 | /*/pre:child312/namespace::node()",
|
|
871 |
| context)); |
|
872 |
| } |
|
873 |
| finally { |
|
874 |
1
| out.close();
|
|
875 |
| } |
|
876 |
| |
|
877 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
878 |
1
| assertEquals(expected, actual);
|
|
879 |
| |
|
880 |
| } |
|
881 |
| |
|
882 |
| |
|
883 |
1
| public void testXMLNSPrefixAttributeInheritedWithExclusiveCanonicalization()
|
|
884 |
| throws IOException { |
|
885 |
| |
|
886 |
1
| Element root = new Element("pre:root", "http://www.example.org/");
|
|
887 |
1
| Document doc = new Document(root);
|
|
888 |
1
| root.appendChild(new Element("pre:child312", "http://www.example.org/"));
|
|
889 |
| |
|
890 |
1
| String expected = "<pre:child312 xmlns:pre=\"http://www.example.org/\"></pre:child312>";
|
|
891 |
| |
|
892 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
893 |
1
| try {
|
|
894 |
1
| XPathContext context = new XPathContext("pre", "http://www.example.org/");
|
|
895 |
1
| Canonicalizer serializer = new Canonicalizer(out,
|
|
896 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION); |
|
897 |
1
| serializer.write(doc.query("/*/pre:child312 | /*/pre:child312/namespace::node()", context));
|
|
898 |
| } |
|
899 |
| finally { |
|
900 |
1
| out.close();
|
|
901 |
| } |
|
902 |
| |
|
903 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
904 |
1
| assertEquals(expected, actual);
|
|
905 |
| |
|
906 |
| } |
|
907 |
| |
|
908 |
| |
|
909 |
1
| public void testXMLNSEqualsEmptyString()
|
|
910 |
| throws IOException { |
|
911 |
| |
|
912 |
1
| Element root = new Element("root", "http://www.ietf.org");
|
|
913 |
1
| Document doc = new Document(root);
|
|
914 |
1
| root.appendChild(new Element("child"));
|
|
915 |
| |
|
916 |
1
| String expected = "<root xmlns=\"http://www.ietf.org\"><child xmlns=\"\"></child></root>";
|
|
917 |
| |
|
918 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
919 |
1
| try {
|
|
920 |
1
| Canonicalizer serializer = new Canonicalizer(out, false);
|
|
921 |
1
| serializer.write(doc);
|
|
922 |
| } |
|
923 |
| finally { |
|
924 |
1
| out.close();
|
|
925 |
| } |
|
926 |
| |
|
927 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
928 |
1
| assertEquals(expected, actual);
|
|
929 |
| |
|
930 |
| } |
|
931 |
| |
|
932 |
| |
|
933 |
| |
|
934 |
1
| public void testDocumentSubsetCanonicalization()
|
|
935 |
| throws ParsingException, IOException { |
|
936 |
| |
|
937 |
1
| String input = "<!DOCTYPE doc [\n"
|
|
938 |
| + "<!ATTLIST e2 xml:space (default|preserve) 'preserve'>\n" |
|
939 |
| + "<!ATTLIST e3 id ID #IMPLIED>\n" |
|
940 |
| + "]>\n" |
|
941 |
| + "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" |
|
942 |
| + " <e1>\n" |
|
943 |
| + " <e2 xmlns=\"\">\n" |
|
944 |
| + " <e3 id=\"E3\"/>\n" |
|
945 |
| + " </e2>\n" |
|
946 |
| + " </e1>\n" |
|
947 |
| + "</doc>"; |
|
948 |
| |
|
949 |
1
| Document doc = builder.build(input, null);
|
|
950 |
1
| XPathContext context = new XPathContext("ietf", "http://www.ietf.org");
|
|
951 |
1
| String xpath = "(//. | //@* | //namespace::*)\n"
|
|
952 |
| + "[\n" |
|
953 |
| + "self::ietf:e1 or (parent::ietf:e1 and not(self::text() or self::e2))" |
|
954 |
| + " or\n" |
|
955 |
| + " count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node())\n" |
|
956 |
| + "]"; |
|
957 |
| |
|
958 |
1
| String expected = "<e1 xmlns=\"http://www.ietf.org\" "
|
|
959 |
| + "xmlns:w3c=\"http://www.w3.org\"><e3 xmlns=\"\" id=\"E3\" xml:space=\"preserve\"></e3></e1>"; |
|
960 |
| |
|
961 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
962 |
1
| try {
|
|
963 |
1
| Canonicalizer serializer = new Canonicalizer(out, false);
|
|
964 |
1
| serializer.write(doc.query(xpath, context));
|
|
965 |
| } |
|
966 |
| finally { |
|
967 |
1
| out.close();
|
|
968 |
| } |
|
969 |
| |
|
970 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
971 |
1
| assertEquals(expected, actual);
|
|
972 |
| |
|
973 |
| } |
|
974 |
| |
|
975 |
| |
|
976 |
1
| public void testCanonicalizeEmptyDocumentSubset()
|
|
977 |
| throws ParsingException, IOException { |
|
978 |
| |
|
979 |
1
| String input = "<!DOCTYPE doc [\n"
|
|
980 |
| + "<!ATTLIST e2 xml:space (default|preserve) 'preserve'>\n" |
|
981 |
| + "<!ATTLIST e3 id ID #IMPLIED>\n" |
|
982 |
| + "]>\n" |
|
983 |
| + "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" |
|
984 |
| + " <e1>\n" |
|
985 |
| + " <e2 xmlns=\"\">\n" |
|
986 |
| + " <e3 id=\"E3\"/>\n" |
|
987 |
| + " </e2>\n" |
|
988 |
| + " </e1>\n" |
|
989 |
| + "</doc>"; |
|
990 |
| |
|
991 |
1
| Document doc = builder.build(input, null);
|
|
992 |
1
| XPathContext context = new XPathContext("ietf", "http://www.ietf.org");
|
|
993 |
1
| String xpath = "//aaa";
|
|
994 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
995 |
1
| try {
|
|
996 |
1
| Canonicalizer serializer = new Canonicalizer(out, false);
|
|
997 |
1
| serializer.write(doc.query(xpath, context));
|
|
998 |
| } |
|
999 |
| finally { |
|
1000 |
1
| out.close();
|
|
1001 |
| } |
|
1002 |
| |
|
1003 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
1004 |
1
| assertEquals("", actual);
|
|
1005 |
| |
|
1006 |
| } |
|
1007 |
| |
|
1008 |
| |
|
1009 |
1
| public void testDocumentSubsetCanonicalizationSkippingProcessingInstructions()
|
|
1010 |
| throws ParsingException, IOException { |
|
1011 |
| |
|
1012 |
1
| String input = "<!DOCTYPE doc [\n"
|
|
1013 |
| + "<!ATTLIST e3 id ID #IMPLIED>\n" |
|
1014 |
| + "]>\n<?test?>" |
|
1015 |
| + "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" |
|
1016 |
| + " <e1><?test?>\n" |
|
1017 |
| + " <e2 xmlns=\"\">\n" |
|
1018 |
| + " <e3 id=\"E3\"/>\n" |
|
1019 |
| + " </e2>\n" |
|
1020 |
| + " </e1>\n" |
|
1021 |
| + "</doc><?test?>"; |
|
1022 |
| |
|
1023 |
1
| String expected = "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n"
|
|
1024 |
| + " <e1>\n" |
|
1025 |
| + " <e2 xmlns=\"\">\n" |
|
1026 |
| + " <e3 id=\"E3\"></e3>\n" |
|
1027 |
| + " </e2>\n" |
|
1028 |
| + " </e1>\n" |
|
1029 |
| + "</doc>"; |
|
1030 |
| |
|
1031 |
1
| Document doc = builder.build(input, null);
|
|
1032 |
1
| XPathContext context = new XPathContext("ietf", "http://www.ietf.org");
|
|
1033 |
1
| String xpath = "//* | //text() | //@* | //namespace::*";
|
|
1034 |
| |
|
1035 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1036 |
1
| try {
|
|
1037 |
1
| Canonicalizer serializer = new Canonicalizer(out, false);
|
|
1038 |
1
| serializer.write(doc.query(xpath, context));
|
|
1039 |
| } |
|
1040 |
| finally { |
|
1041 |
1
| out.close();
|
|
1042 |
| } |
|
1043 |
| |
|
1044 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
1045 |
1
| assertEquals(expected, actual);
|
|
1046 |
| |
|
1047 |
| } |
|
1048 |
| |
|
1049 |
| |
|
1050 |
1
| public void testDocumentSubsetCanonicalizationWithoutSelectingPrologAndEpilog()
|
|
1051 |
| throws ParsingException, IOException { |
|
1052 |
| |
|
1053 |
1
| String input = "<!-- prolog -->\n"
|
|
1054 |
| + "<!DOCTYPE doc [\n" |
|
1055 |
| + "<!ATTLIST e2 xml:space (default|preserve) 'preserve'>\n" |
|
1056 |
| + "<!ATTLIST e3 id ID #IMPLIED>\n" |
|
1057 |
| + "]>\n" |
|
1058 |
| + "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" |
|
1059 |
| + " <e1>\n" |
|
1060 |
| + " <e2 xmlns=\"\">\n" |
|
1061 |
| + " <e3 id=\"E3\"/>\n" |
|
1062 |
| + " </e2>\n" |
|
1063 |
| + " </e1>\n" |
|
1064 |
| + "</doc><!-- epilog -->"; |
|
1065 |
| |
|
1066 |
1
| Document doc = builder.build(input, null);
|
|
1067 |
1
| XPathContext context = new XPathContext("ietf", "http://www.ietf.org");
|
|
1068 |
1
| String xpath = "(/*//. | //@* | //namespace::*)\n"
|
|
1069 |
| + "[\n" |
|
1070 |
| + "self::ietf:e1 or (parent::ietf:e1 and not(self::text() or self::e2))" |
|
1071 |
| + " or\n" |
|
1072 |
| + " count(id(\"E3\")|ancestor-or-self::node()) = count(ancestor-or-self::node())\n" |
|
1073 |
| + "]"; |
|
1074 |
| |
|
1075 |
1
| String expected = "<e1 xmlns=\"http://www.ietf.org\" "
|
|
1076 |
| + "xmlns:w3c=\"http://www.w3.org\"><e3 xmlns=\"\" id=\"E3\" xml:space=\"preserve\"></e3></e1>"; |
|
1077 |
| |
|
1078 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1079 |
1
| try {
|
|
1080 |
1
| Canonicalizer serializer = new Canonicalizer(out, Canonicalizer.CANONICAL_XML_WITH_COMMENTS);
|
|
1081 |
1
| serializer.write(doc.query(xpath, context));
|
|
1082 |
| } |
|
1083 |
| finally { |
|
1084 |
1
| out.close();
|
|
1085 |
| } |
|
1086 |
| |
|
1087 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
1088 |
1
| assertEquals(expected, actual);
|
|
1089 |
| |
|
1090 |
| } |
|
1091 |
| |
|
1092 |
| |
|
1093 |
1
| public void testEmptyDefaultNamespace()
|
|
1094 |
| throws ParsingException, IOException { |
|
1095 |
| |
|
1096 |
1
| String input = "<doc xmlns=\"http://www.ietf.org\">"
|
|
1097 |
| + "<e2 xmlns=\"\"></e2>" |
|
1098 |
| + "</doc>"; |
|
1099 |
| |
|
1100 |
1
| Document doc = builder.build(input, null);
|
|
1101 |
1
| String xpath = "(//* | //namespace::*)";
|
|
1102 |
| |
|
1103 |
1
| String expected = input;
|
|
1104 |
| |
|
1105 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1106 |
1
| try {
|
|
1107 |
1
| Canonicalizer serializer = new Canonicalizer(out, false);
|
|
1108 |
1
| serializer.write(doc.query(xpath));
|
|
1109 |
| } |
|
1110 |
| finally { |
|
1111 |
1
| out.close();
|
|
1112 |
| } |
|
1113 |
| |
|
1114 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
1115 |
1
| assertEquals(expected, actual);
|
|
1116 |
| |
|
1117 |
| } |
|
1118 |
| |
|
1119 |
| |
|
1120 |
| |
|
1121 |
1
| public void testDocumentSubsetCanonicalizationSimple()
|
|
1122 |
| throws ParsingException, IOException { |
|
1123 |
| |
|
1124 |
1
| String input = "<!DOCTYPE doc [\n"
|
|
1125 |
| + "<!ATTLIST e2 xml:space (default|preserve) 'preserve'>\n" |
|
1126 |
| + "<!ATTLIST e3 id ID #IMPLIED>\n" |
|
1127 |
| + "]>\n" |
|
1128 |
| + "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" |
|
1129 |
| + " <e1>\n" |
|
1130 |
| + " <e2 xmlns=\"\">\n" |
|
1131 |
| + " <e3 id=\"E3\"/>\n" |
|
1132 |
| + " </e2>\n" |
|
1133 |
| + " </e1>\n" |
|
1134 |
| + "</doc>"; |
|
1135 |
| |
|
1136 |
1
| Document doc = builder.build(input, null);
|
|
1137 |
1
| XPathContext context = new XPathContext("ietf", "http://www.ietf.org");
|
|
1138 |
1
| String xpath = "(/* | /*/namespace::*)\n";
|
|
1139 |
| |
|
1140 |
1
| String expected = "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\"></doc>";
|
|
1141 |
| |
|
1142 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1143 |
1
| try {
|
|
1144 |
1
| Canonicalizer serializer = new Canonicalizer(out, false);
|
|
1145 |
1
| serializer.write(doc.query(xpath, context));
|
|
1146 |
| } |
|
1147 |
| finally { |
|
1148 |
1
| out.close();
|
|
1149 |
| } |
|
1150 |
| |
|
1151 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
1152 |
1
| assertEquals(expected, actual);
|
|
1153 |
| |
|
1154 |
| } |
|
1155 |
| |
|
1156 |
| |
|
1157 |
1
| public void testCanonicalizeDocumentSubsetIncludingRoot()
|
|
1158 |
| throws ParsingException, IOException { |
|
1159 |
| |
|
1160 |
1
| String input = "<doc />";
|
|
1161 |
| |
|
1162 |
1
| Document doc = builder.build(input, null);
|
|
1163 |
| |
|
1164 |
1
| String expected = "<doc></doc>";
|
|
1165 |
| |
|
1166 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1167 |
1
| try {
|
|
1168 |
1
| Canonicalizer serializer = new Canonicalizer(out, false);
|
|
1169 |
1
| Nodes subset = doc.query("//.");
|
|
1170 |
1
| serializer.write(subset);
|
|
1171 |
| } |
|
1172 |
| finally { |
|
1173 |
1
| out.close();
|
|
1174 |
| } |
|
1175 |
| |
|
1176 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
1177 |
1
| assertEquals(expected, actual);
|
|
1178 |
| |
|
1179 |
| } |
|
1180 |
| |
|
1181 |
| |
|
1182 |
1
| public void testCanonicalizeDocumentSubsetThatOnlyContainsRoot()
|
|
1183 |
| throws IOException { |
|
1184 |
| |
|
1185 |
1
| Document doc = new Document(new Element("root"));
|
|
1186 |
| |
|
1187 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1188 |
1
| try {
|
|
1189 |
1
| Canonicalizer serializer = new Canonicalizer(out, false);
|
|
1190 |
1
| Nodes subset = doc.query("/");
|
|
1191 |
1
| serializer.write(subset);
|
|
1192 |
| } |
|
1193 |
| finally { |
|
1194 |
1
| out.close();
|
|
1195 |
| } |
|
1196 |
| |
|
1197 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
1198 |
1
| assertEquals("", actual);
|
|
1199 |
| |
|
1200 |
| } |
|
1201 |
| |
|
1202 |
| |
|
1203 |
1
| public void testDocumentSubsetCanonicalizationNamespaceInheritance()
|
|
1204 |
| throws ParsingException, IOException { |
|
1205 |
| |
|
1206 |
1
| String input = "<!DOCTYPE doc [\n"
|
|
1207 |
| + "<!ATTLIST e2 xml:space (default|preserve) 'preserve'>\n" |
|
1208 |
| + "<!ATTLIST e3 id ID #IMPLIED>\n" |
|
1209 |
| + "]>\n" |
|
1210 |
| + "<doc xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\">\n" |
|
1211 |
| + " <e1>\n" |
|
1212 |
| + " <e2 xmlns=\"\">\n" |
|
1213 |
| + " <e3 id=\"E3\"/>\n" |
|
1214 |
| + " </e2>\n" |
|
1215 |
| + " </e1>\n" |
|
1216 |
| + "</doc>"; |
|
1217 |
| |
|
1218 |
1
| Document doc = builder.build(input, null);
|
|
1219 |
1
| XPathContext context = new XPathContext("ietf", "http://www.ietf.org");
|
|
1220 |
1
| String xpath = "(/*/* | /*/*/namespace::*)\n";
|
|
1221 |
| |
|
1222 |
1
| String expected = "<e1 xmlns=\"http://www.ietf.org\" xmlns:w3c=\"http://www.w3.org\"></e1>";
|
|
1223 |
| |
|
1224 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1225 |
1
| try {
|
|
1226 |
1
| Canonicalizer serializer = new Canonicalizer(out, false);
|
|
1227 |
1
| serializer.write(doc.query(xpath, context));
|
|
1228 |
| } |
|
1229 |
| finally { |
|
1230 |
1
| out.close();
|
|
1231 |
| } |
|
1232 |
| |
|
1233 |
1
| String actual = new String(out.toByteArray(), "UTF-8");
|
|
1234 |
1
| assertEquals(expected, actual);
|
|
1235 |
| |
|
1236 |
| } |
|
1237 |
| |
|
1238 |
| |
|
1239 |
1
| public void testRelativeNamespaceURIsForbidden()
|
|
1240 |
| throws ParsingException, IOException { |
|
1241 |
| |
|
1242 |
1
| try {
|
|
1243 |
1
| String data = "<test xmlns=\"relative\">data</test>";
|
|
1244 |
1
| Document doc = builder.build(data, "http://www.ex.org/");
|
|
1245 |
0
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1246 |
0
| Canonicalizer serializer
|
|
1247 |
| = new Canonicalizer(out, false); |
|
1248 |
0
| serializer.write(doc);
|
|
1249 |
0
| fail("Canonicalized document with relative namespace URI");
|
|
1250 |
| } |
|
1251 |
| catch (ParsingException success) { |
|
1252 |
1
| assertNotNull(success.getMessage());
|
|
1253 |
| } |
|
1254 |
| |
|
1255 |
| } |
|
1256 |
| |
|
1257 |
| |
|
1258 |
| private static class XMLFilter implements FilenameFilter { |
|
1259 |
| |
|
1260 |
56
| public boolean accept(File directory, String name) {
|
|
1261 |
40
| if (name.endsWith(".xml")) return true;
|
|
1262 |
16
| return false;
|
|
1263 |
| } |
|
1264 |
| |
|
1265 |
| } |
|
1266 |
| |
|
1267 |
| |
|
1268 |
1
| public void testNFCFromISO88591()
|
|
1269 |
| throws ParsingException, IOException { |
|
1270 |
1
| isoNormalizationTest("ISO-8859-1");
|
|
1271 |
| } |
|
1272 |
| |
|
1273 |
| |
|
1274 |
1
| public void testNFCFromISO88592()
|
|
1275 |
| throws ParsingException, IOException { |
|
1276 |
1
| isoNormalizationTest("ISO-8859-2");
|
|
1277 |
| } |
|
1278 |
| |
|
1279 |
| |
|
1280 |
1
| public void testNFCFromISO88593()
|
|
1281 |
| throws ParsingException, IOException { |
|
1282 |
1
| isoNormalizationTest("ISO-8859-3");
|
|
1283 |
| } |
|
1284 |
| |
|
1285 |
| |
|
1286 |
1
| public void testNFCFromISO88594()
|
|
1287 |
| throws ParsingException, IOException { |
|
1288 |
1
| isoNormalizationTest("ISO-8859-4");
|
|
1289 |
| } |
|
1290 |
| |
|
1291 |
| |
|
1292 |
1
| public void testNFCFromISO88595()
|
|
1293 |
| throws ParsingException, IOException { |
|
1294 |
1
| isoNormalizationTest("ISO-8859-5");
|
|
1295 |
| } |
|
1296 |
| |
|
1297 |
| |
|
1298 |
1
| public void testNFCFromISO88596()
|
|
1299 |
| throws ParsingException, IOException { |
|
1300 |
| |
|
1301 |
| |
|
1302 |
| |
|
1303 |
| |
|
1304 |
1
| isoNormalizationTest("ISO-8859-6");
|
|
1305 |
| |
|
1306 |
| } |
|
1307 |
| |
|
1308 |
| |
|
1309 |
1
| public void testNFCFromISO88597()
|
|
1310 |
| throws ParsingException, IOException { |
|
1311 |
1
| isoNormalizationTest("ISO-8859-7");
|
|
1312 |
| } |
|
1313 |
| |
|
1314 |
| |
|
1315 |
1
| public void testNFCFromISO88598()
|
|
1316 |
| throws ParsingException, IOException { |
|
1317 |
1
| isoNormalizationTest("ISO-8859-8");
|
|
1318 |
| } |
|
1319 |
| |
|
1320 |
| |
|
1321 |
1
| public void testNFCFromISO88599()
|
|
1322 |
| throws ParsingException, IOException { |
|
1323 |
1
| isoNormalizationTest("ISO-8859-9");
|
|
1324 |
| } |
|
1325 |
| |
|
1326 |
| |
|
1327 |
1
| public void testNFCFromISO885913()
|
|
1328 |
| throws ParsingException, IOException { |
|
1329 |
| |
|
1330 |
1
| if (version >= 1.3) {
|
|
1331 |
| |
|
1332 |
1
| isoNormalizationTest("ISO-8859-13");
|
|
1333 |
| } |
|
1334 |
| |
|
1335 |
| } |
|
1336 |
| |
|
1337 |
| |
|
1338 |
1
| public void testNFCFromISO885915()
|
|
1339 |
| throws ParsingException, IOException { |
|
1340 |
1
| isoNormalizationTest("ISO-8859-15");
|
|
1341 |
| } |
|
1342 |
| |
|
1343 |
| |
|
1344 |
| |
|
1345 |
11
| private void isoNormalizationTest(String encoding)
|
|
1346 |
| throws ParsingException, IOException { |
|
1347 |
| |
|
1348 |
11
| String prolog = "<?xml version='1.0' encoding='"
|
|
1349 |
| + encoding + "'?>\r\n<root>"; |
|
1350 |
| |
|
1351 |
11
| byte[] prologData = prolog.getBytes(encoding);
|
|
1352 |
| |
|
1353 |
11
| String epilog = "</root>";
|
|
1354 |
11
| byte[] epilogData = epilog.getBytes(encoding);
|
|
1355 |
11
| byte[] data = new byte[prologData.length + epilogData.length + 255 - 160 + 1];
|
|
1356 |
11
| System.arraycopy(prologData, 0, data, 0, prologData.length);
|
|
1357 |
11
| System.arraycopy(epilogData, 0, data,
|
|
1358 |
| data.length - epilogData.length, epilogData.length); |
|
1359 |
11
| for (int i = 160; i <= 255; i++) {
|
|
1360 |
1056
| data[prologData.length + (i-160)] = (byte) i;
|
|
1361 |
| } |
|
1362 |
11
| InputStream in = new ByteArrayInputStream(data);
|
|
1363 |
11
| Document doc = builder.build(in);
|
|
1364 |
| |
|
1365 |
| |
|
1366 |
11
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1367 |
11
| Serializer serializer = new Serializer(out);
|
|
1368 |
11
| serializer.setUnicodeNormalizationFormC(true);
|
|
1369 |
11
| serializer.write(doc);
|
|
1370 |
11
| byte[] temp = out.toByteArray();
|
|
1371 |
11
| in = new ByteArrayInputStream(temp);
|
|
1372 |
11
| Document nfcDoc = builder.build(in);
|
|
1373 |
| |
|
1374 |
11
| assertEquals("Parser doesn't use NFC when converting from " + encoding,
|
|
1375 |
| doc, nfcDoc); |
|
1376 |
| |
|
1377 |
| } |
|
1378 |
| |
|
1379 |
| |
|
1380 |
1
| public void testEBCDIC()
|
|
1381 |
| throws ParsingException, IOException { |
|
1382 |
| |
|
1383 |
1
| String encoding = "IBM037";
|
|
1384 |
1
| String prolog = "<?xml version='1.0' encoding='"
|
|
1385 |
| + encoding + "'?>\r\n<root>"; |
|
1386 |
1
| byte[] prologData = prolog.getBytes(encoding);
|
|
1387 |
1
| String epilog = "</root>";
|
|
1388 |
1
| byte[] epilogData = epilog.getBytes(encoding);
|
|
1389 |
1
| byte[] data = new byte[prologData.length + epilogData.length + 255 - 160 + 1];
|
|
1390 |
1
| System.arraycopy(prologData, 0, data, 0, prologData.length);
|
|
1391 |
1
| System.arraycopy(epilogData, 0, data,
|
|
1392 |
| data.length - epilogData.length, epilogData.length); |
|
1393 |
1
| StringBuffer buffer = new StringBuffer(255 - 160 + 1);
|
|
1394 |
1
| for (int i = 160; i <= 255; i++) {
|
|
1395 |
96
| buffer.append((char) i);
|
|
1396 |
| } |
|
1397 |
1
| byte[] temp = buffer.toString().getBytes(encoding);
|
|
1398 |
1
| System.arraycopy(temp, 0, data, prologData.length, temp.length);
|
|
1399 |
1
| InputStream in = new ByteArrayInputStream(data);
|
|
1400 |
1
| Document doc = builder.build(in);
|
|
1401 |
| |
|
1402 |
| |
|
1403 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1404 |
1
| Serializer serializer = new Serializer(out);
|
|
1405 |
1
| serializer.setUnicodeNormalizationFormC(true);
|
|
1406 |
1
| serializer.write(doc);
|
|
1407 |
1
| temp = out.toByteArray();
|
|
1408 |
1
| in = new ByteArrayInputStream(temp);
|
|
1409 |
1
| Document nfcDoc = builder.build(in);
|
|
1410 |
| |
|
1411 |
| |
|
1412 |
1
| assertEquals("Parser doesn't use NFC when converting from " + encoding,
|
|
1413 |
| doc, nfcDoc); |
|
1414 |
| |
|
1415 |
| } |
|
1416 |
| |
|
1417 |
| |
|
1418 |
| |
|
1419 |
1
| public void testNullDocument()
|
|
1420 |
| throws IOException { |
|
1421 |
| |
|
1422 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1423 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out);
|
|
1424 |
1
| try {
|
|
1425 |
1
| canonicalizer.write((Document) null);
|
|
1426 |
0
| fail("Wrote null document");
|
|
1427 |
| } |
|
1428 |
| catch (NullPointerException success) { |
|
1429 |
| |
|
1430 |
| } |
|
1431 |
1
| byte[] result = out.toByteArray();
|
|
1432 |
1
| assertEquals(0, result.length);
|
|
1433 |
| |
|
1434 |
| } |
|
1435 |
| |
|
1436 |
| |
|
1437 |
1
| public void testWhiteSpaceTrimmingInNonCDATAAttribute()
|
|
1438 |
| throws IOException { |
|
1439 |
| |
|
1440 |
1
| Attribute attribute = new Attribute("name", " value1 value2 ");
|
|
1441 |
1
| attribute.setType(Attribute.Type.NMTOKENS);
|
|
1442 |
1
| Element root = new Element("root");
|
|
1443 |
1
| root.addAttribute(attribute);
|
|
1444 |
1
| Document doc = new Document(root);
|
|
1445 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1446 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out);
|
|
1447 |
1
| canonicalizer.write(doc);
|
|
1448 |
1
| out.close();
|
|
1449 |
1
| String result = new String(out.toByteArray(), "UTF8");
|
|
1450 |
1
| assertEquals("<root name=\"value1 value2\"></root>", result);
|
|
1451 |
| |
|
1452 |
| } |
|
1453 |
| |
|
1454 |
| |
|
1455 |
| |
|
1456 |
1
| public void testXMLConformanceTestSuiteDocuments()
|
|
1457 |
| throws ParsingException, IOException { |
|
1458 |
| |
|
1459 |
1
| File masterList = new File(canonical, "xmlconf");
|
|
1460 |
1
| masterList = new File(masterList, "xmlconf.xml");
|
|
1461 |
1
| if (masterList.exists()) {
|
|
1462 |
1
| Document xmlconf = builder.build(masterList);
|
|
1463 |
1
| Elements testcases = xmlconf.getRootElement().getChildElements("TESTCASES");
|
|
1464 |
1
| processTestCases(testcases);
|
|
1465 |
| } |
|
1466 |
| |
|
1467 |
| } |
|
1468 |
| |
|
1469 |
| |
|
1470 |
| |
|
1471 |
| |
|
1472 |
172
| private void processTestCases(Elements testcases)
|
|
1473 |
| throws ParsingException, IOException { |
|
1474 |
| |
|
1475 |
172
| for (int i = 0; i < testcases.size(); i++) {
|
|
1476 |
171
| Element testcase = testcases.get(i);
|
|
1477 |
171
| Elements tests = testcase.getChildElements("TEST");
|
|
1478 |
171
| processTests(tests);
|
|
1479 |
171
| Elements level2 = testcase.getChildElements("TESTCASES");
|
|
1480 |
| |
|
1481 |
171
| processTestCases(level2);
|
|
1482 |
| } |
|
1483 |
| |
|
1484 |
| } |
|
1485 |
| |
|
1486 |
| |
|
1487 |
171
| private void processTests(Elements tests)
|
|
1488 |
| throws ParsingException, IOException { |
|
1489 |
| |
|
1490 |
171
| Element parent = new Element("e");
|
|
1491 |
171
| Element child = new Element("a");
|
|
1492 |
171
| parent.appendChild(child);
|
|
1493 |
| |
|
1494 |
171
| int size = tests.size();
|
|
1495 |
171
| for (int i = 0; i < size; i++) {
|
|
1496 |
1811
| Element test = tests.get(i);
|
|
1497 |
1811
| String namespace = test.getAttributeValue("NAMESPACE");
|
|
1498 |
4
| if ("no".equals(namespace)) continue;
|
|
1499 |
1807
| String type = test.getAttributeValue("TYPE");
|
|
1500 |
1230
| if ("not-wf".equals(type)) continue;
|
|
1501 |
577
| String uri = test.getAttributeValue("URI");
|
|
1502 |
577
| String base = test.getBaseURI();
|
|
1503 |
| |
|
1504 |
| |
|
1505 |
577
| parent.setBaseURI(base);
|
|
1506 |
| |
|
1507 |
577
| child.addAttribute(new Attribute("xml:base",
|
|
1508 |
| "http://www.w3.org/XML/1998/namespace", uri)); |
|
1509 |
577
| String resolvedURI = child.getBaseURI();
|
|
1510 |
| |
|
1511 |
577
| Document doc = builder.build(resolvedURI);
|
|
1512 |
577
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1513 |
577
| try {
|
|
1514 |
577
| Canonicalizer serializer = new Canonicalizer(out);
|
|
1515 |
577
| serializer.write(doc);
|
|
1516 |
| } |
|
1517 |
| finally { |
|
1518 |
577
| out.close();
|
|
1519 |
| } |
|
1520 |
577
| byte[] actual = out.toByteArray();
|
|
1521 |
| |
|
1522 |
577
| File input = new File(resolvedURI.substring(5) + ".can");
|
|
1523 |
577
| assertEquals(resolvedURI, input.length(), actual.length);
|
|
1524 |
577
| byte[] expected = new byte[actual.length];
|
|
1525 |
577
| DataInputStream in = new DataInputStream(
|
|
1526 |
| new BufferedInputStream(new FileInputStream(input))); |
|
1527 |
577
| try {
|
|
1528 |
577
| in.readFully(expected);
|
|
1529 |
| } |
|
1530 |
| finally { |
|
1531 |
577
| in.close();
|
|
1532 |
| } |
|
1533 |
577
| assertEquals(expected, actual);
|
|
1534 |
| |
|
1535 |
| } |
|
1536 |
| |
|
1537 |
| } |
|
1538 |
| |
|
1539 |
| |
|
1540 |
1
| public void testExclusive() throws IOException {
|
|
1541 |
| |
|
1542 |
1
| Element pdu = new Element("n0:pdu", "http://a.example");
|
|
1543 |
1
| Element elem1 = new Element("n1:elem1", "http://b.example");
|
|
1544 |
1
| elem1.appendChild("content");
|
|
1545 |
1
| pdu.appendChild(elem1);
|
|
1546 |
| |
|
1547 |
1
| String expected = "<n1:elem1 xmlns:n1=\"http://b.example\">content</n1:elem1>";
|
|
1548 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1549 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out,
|
|
1550 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS); |
|
1551 |
| |
|
1552 |
1
| XPathContext context = new XPathContext("n1", "http://b.example");
|
|
1553 |
1
| Document doc = new Document(pdu);
|
|
1554 |
1
| canonicalizer.write(doc.query("(//. | //@* | //namespace::*)[ancestor-or-self::n1:elem1]", context));
|
|
1555 |
| |
|
1556 |
1
| byte[] result = out.toByteArray();
|
|
1557 |
1
| out.close();
|
|
1558 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
1559 |
1
| assertEquals(expected, s);
|
|
1560 |
| |
|
1561 |
| } |
|
1562 |
| |
|
1563 |
1
| public static void testAustB() throws IOException {
|
|
1564 |
| |
|
1565 |
1
| Element e1 = new Element("a:a", "urn:a");
|
|
1566 |
1
| Element e2 = new Element("b");
|
|
1567 |
1
| e1.appendChild(e2);
|
|
1568 |
| |
|
1569 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1570 |
1
| Canonicalizer c = new Canonicalizer(out,
|
|
1571 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION); |
|
1572 |
1
| c.write(e1);
|
|
1573 |
1
| String s = out.toString("UTF8");
|
|
1574 |
1
| assertEquals("<a:a xmlns:a=\"urn:a\"><b></b></a:a>", s);
|
|
1575 |
| |
|
1576 |
| } |
|
1577 |
| |
|
1578 |
| |
|
1579 |
1
| public static void testAustB2() throws IOException {
|
|
1580 |
| |
|
1581 |
1
| Element e1 = new Element("a:a", "urn:a");
|
|
1582 |
1
| Element e2 = new Element("b");
|
|
1583 |
1
| e1.appendChild(e2);
|
|
1584 |
| |
|
1585 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1586 |
1
| Canonicalizer c = new Canonicalizer(out);
|
|
1587 |
1
| c.write(e1);
|
|
1588 |
1
| String s = out.toString("UTF8");
|
|
1589 |
1
| assertEquals("<a:a xmlns:a=\"urn:a\"><b></b></a:a>", s);
|
|
1590 |
| |
|
1591 |
| } |
|
1592 |
| |
|
1593 |
| |
|
1594 |
1
| public static void testAust() throws Exception {
|
|
1595 |
| |
|
1596 |
1
| Element e1 = new Element("a:a", "urn:a");
|
|
1597 |
1
| Element e2 = new Element("a:b", "urn:a");
|
|
1598 |
1
| e1.appendChild(e2);
|
|
1599 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1600 |
1
| Canonicalizer c = new Canonicalizer(out,
|
|
1601 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION); |
|
1602 |
1
| c.write(e2);
|
|
1603 |
1
| String s = out.toString("UTF8");
|
|
1604 |
1
| assertEquals("<a:b xmlns:a=\"urn:a\"></a:b>", s);
|
|
1605 |
| |
|
1606 |
| } |
|
1607 |
| |
|
1608 |
| |
|
1609 |
1
| public static void testAust4() throws Exception {
|
|
1610 |
| |
|
1611 |
1
| Element e1 = new Element("a:a", "urn:a");
|
|
1612 |
1
| Element e2 = new Element("a:b", "urn:a");
|
|
1613 |
1
| e1.appendChild(e2);
|
|
1614 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1615 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out,
|
|
1616 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS); |
|
1617 |
1
| XPathContext context = new XPathContext("a", "urn:a");
|
|
1618 |
1
| Document doc = new Document(e1);
|
|
1619 |
1
| canonicalizer.write(doc.query("(//. | //@* | //namespace::*)[ancestor-or-self::a:b]", context));
|
|
1620 |
| |
|
1621 |
1
| String s = out.toString("UTF8");
|
|
1622 |
1
| assertEquals("<a:b xmlns:a=\"urn:a\"></a:b>", s);
|
|
1623 |
| |
|
1624 |
| } |
|
1625 |
| |
|
1626 |
| |
|
1627 |
1
| public static void testAust5() throws Exception {
|
|
1628 |
| |
|
1629 |
1
| Element e1 = new Element("a:a", "urn:a");
|
|
1630 |
1
| Element e2 = new Element("a:b", "urn:a");
|
|
1631 |
1
| e1.appendChild(e2);
|
|
1632 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1633 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out,
|
|
1634 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS); |
|
1635 |
1
| Document doc = new Document(e1);
|
|
1636 |
1
| Nodes set = new Nodes(e2);
|
|
1637 |
1
| canonicalizer.write(set);
|
|
1638 |
| |
|
1639 |
1
| String s = out.toString("UTF8");
|
|
1640 |
| |
|
1641 |
| |
|
1642 |
1
| assertEquals("<a:b></a:b>", s);
|
|
1643 |
| |
|
1644 |
| } |
|
1645 |
| |
|
1646 |
| |
|
1647 |
1
| public static void testAust3() throws Exception {
|
|
1648 |
| |
|
1649 |
1
| Element e2 = new Element("a:b", "urn:a");
|
|
1650 |
1
| ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
1651 |
1
| new Canonicalizer(os,
|
|
1652 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION).write(e2); |
|
1653 |
1
| String s = os.toString("UTF8");
|
|
1654 |
1
| assertEquals("<a:b xmlns:a=\"urn:a\"></a:b>", s);
|
|
1655 |
| |
|
1656 |
| } |
|
1657 |
| |
|
1658 |
| |
|
1659 |
1
| public static void testAust2() throws Exception {
|
|
1660 |
| |
|
1661 |
1
| Element e1 = new Element("a:a", "urn:a");
|
|
1662 |
1
| Element e2 = new Element("a:b", "urn:a");
|
|
1663 |
1
| e1.appendChild(e2);
|
|
1664 |
1
| ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
1665 |
1
| new Canonicalizer(os,
|
|
1666 |
| Canonicalizer.CANONICAL_XML).write(e2); |
|
1667 |
1
| String s = os.toString("UTF8");
|
|
1668 |
1
| assertEquals("<a:b xmlns:a=\"urn:a\"></a:b>", s);
|
|
1669 |
| |
|
1670 |
| } |
|
1671 |
| |
|
1672 |
| |
|
1673 |
1
| public void testExclusiveWithNamespacedAttributes() throws IOException {
|
|
1674 |
| |
|
1675 |
1
| Element pdu = new Element("n0:pdu", "http://a.example");
|
|
1676 |
1
| Element elem1 = new Element("n1:elem1", "http://b.example");
|
|
1677 |
1
| elem1.appendChild("content");
|
|
1678 |
1
| pdu.appendChild(elem1);
|
|
1679 |
1
| elem1.addAttribute(new Attribute("pre:foo", "http://www.example.org/", "value"));
|
|
1680 |
| |
|
1681 |
1
| String expected = "<n1:elem1 xmlns:n1=\"http://b.example\" "
|
|
1682 |
| + "xmlns:pre=\"http://www.example.org/\" pre:foo=\"value\">content</n1:elem1>"; |
|
1683 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1684 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out,
|
|
1685 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS); |
|
1686 |
| |
|
1687 |
1
| XPathContext context = new XPathContext("n1", "http://b.example");
|
|
1688 |
1
| Document doc = new Document(pdu);
|
|
1689 |
1
| canonicalizer.write(doc.query("(//. | //@* | //namespace::*)[ancestor-or-self::n1:elem1]", context));
|
|
1690 |
| |
|
1691 |
1
| byte[] result = out.toByteArray();
|
|
1692 |
1
| out.close();
|
|
1693 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
1694 |
1
| assertEquals(expected, s);
|
|
1695 |
| |
|
1696 |
| } |
|
1697 |
| |
|
1698 |
| |
|
1699 |
| |
|
1700 |
| |
|
1701 |
| |
|
1702 |
| |
|
1703 |
| |
|
1704 |
| |
|
1705 |
| |
|
1706 |
1
| public void testInheritanceOfXMLLang() throws IOException {
|
|
1707 |
| |
|
1708 |
1
| Element root = new Element("root");
|
|
1709 |
1
| root.addAttribute(new Attribute("xml:lang", Namespace.XML_NAMESPACE, "en"));
|
|
1710 |
1
| Element a = new Element("a");
|
|
1711 |
1
| Element b = new Element("b");
|
|
1712 |
1
| b.appendChild("test");
|
|
1713 |
1
| a.appendChild(b);
|
|
1714 |
1
| root.appendChild(a);
|
|
1715 |
| |
|
1716 |
1
| String expected = "<a xml:lang=\"en\"><b>test</b></a>";
|
|
1717 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1718 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out, Canonicalizer.CANONICAL_XML);
|
|
1719 |
| |
|
1720 |
1
| Document doc = new Document(root);
|
|
1721 |
1
| canonicalizer.write(doc.query("/root//node()"));
|
|
1722 |
| |
|
1723 |
1
| byte[] result = out.toByteArray();
|
|
1724 |
1
| out.close();
|
|
1725 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
1726 |
1
| assertEquals(expected, s);
|
|
1727 |
| |
|
1728 |
| } |
|
1729 |
| |
|
1730 |
| |
|
1731 |
1
| public void testExclusiveWithInclusiveNamespaces()
|
|
1732 |
| throws IOException { |
|
1733 |
| |
|
1734 |
1
| Element pdu = new Element("n0:pdu", "http://a.example");
|
|
1735 |
1
| Element elem1 = new Element("n1:elem1", "http://b.example");
|
|
1736 |
1
| elem1.appendChild("content");
|
|
1737 |
1
| pdu.appendChild(elem1);
|
|
1738 |
| |
|
1739 |
1
| String expected = "<n1:elem1 xmlns:n0=\"http://a.example\""
|
|
1740 |
| + " xmlns:n1=\"http://b.example\">content</n1:elem1>"; |
|
1741 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1742 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out,
|
|
1743 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS); |
|
1744 |
| |
|
1745 |
1
| XPathContext context = new XPathContext("n1", "http://b.example");
|
|
1746 |
1
| Document doc = new Document(pdu);
|
|
1747 |
1
| canonicalizer.setInclusiveNamespacePrefixList("n0");
|
|
1748 |
1
| Nodes subset = doc.query(
|
|
1749 |
| "(//. | //@* | //namespace::*)[ancestor-or-self::n1:elem1]", |
|
1750 |
| context); |
|
1751 |
1
| canonicalizer.write(subset);
|
|
1752 |
| |
|
1753 |
1
| byte[] result = out.toByteArray();
|
|
1754 |
1
| out.close();
|
|
1755 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
1756 |
1
| assertEquals(expected, s);
|
|
1757 |
| |
|
1758 |
| } |
|
1759 |
| |
|
1760 |
| |
|
1761 |
1
| public void testClearInclusiveNamespacePrefixes()
|
|
1762 |
| throws IOException { |
|
1763 |
| |
|
1764 |
1
| Element pdu = new Element("n0:pdu", "http://a.example");
|
|
1765 |
1
| Element elem1 = new Element("n1:elem1", "http://b.example");
|
|
1766 |
1
| elem1.appendChild("content");
|
|
1767 |
1
| pdu.appendChild(elem1);
|
|
1768 |
| |
|
1769 |
1
| String expected = "<n1:elem1"
|
|
1770 |
| + " xmlns:n1=\"http://b.example\">content</n1:elem1>"; |
|
1771 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1772 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out,
|
|
1773 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS); |
|
1774 |
| |
|
1775 |
1
| XPathContext context = new XPathContext("n1", "http://b.example");
|
|
1776 |
1
| Document doc = new Document(pdu);
|
|
1777 |
1
| canonicalizer.setInclusiveNamespacePrefixList("n0");
|
|
1778 |
1
| canonicalizer.setInclusiveNamespacePrefixList(null);
|
|
1779 |
1
| Nodes subset = doc.query(
|
|
1780 |
| "(//. | //@* | //namespace::*)[ancestor-or-self::n1:elem1]", |
|
1781 |
| context); |
|
1782 |
1
| canonicalizer.write(subset);
|
|
1783 |
| |
|
1784 |
1
| byte[] result = out.toByteArray();
|
|
1785 |
1
| out.close();
|
|
1786 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
1787 |
1
| assertEquals(expected, s);
|
|
1788 |
| |
|
1789 |
| } |
|
1790 |
| |
|
1791 |
| |
|
1792 |
1
| public void testExclusive22a() throws ParsingException, IOException {
|
|
1793 |
| |
|
1794 |
1
| Builder builder = new Builder();
|
|
1795 |
1
| String input = "<n0:local xmlns:n0='foo:bar' xmlns:n3='ftp://example.org'>" +
|
|
1796 |
| "<n1:elem2 xmlns:n1=\"http://example.net\" xml:lang=\"en\">" |
|
1797 |
| + "<n3:stuff xmlns:n3=\"ftp://example.org\"/></n1:elem2></n0:local>"; |
|
1798 |
1
| Document doc = builder.build(input, null);
|
|
1799 |
| |
|
1800 |
1
| String expected = "<n1:elem2 xmlns:n1=\"http://example.net\" xml:lang=\"en\">" +
|
|
1801 |
| "<n3:stuff xmlns:n3=\"ftp://example.org\"></n3:stuff></n1:elem2>"; |
|
1802 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
1803 |
1
| Canonicalizer canonicalizer = new Canonicalizer(out,
|
|
1804 |
| Canonicalizer.EXCLUSIVE_XML_CANONICALIZATION_WITH_COMMENTS); |
|
1805 |
| |
|
1806 |
1
| XPathContext context = new XPathContext("n1", "http://example.net");
|
|
1807 |
1
| canonicalizer.write(doc.query(
|
|
1808 |
| " (//. | //@* | //namespace::*)[ancestor-or-self::n1:elem2]", |
|
1809 |
| context)); |
|
1810 |
| |
|
1811 |
1
| byte[] result = out.toByteArray();
|
|
1812 |
1
| out.close();
|
|
1813 |
1
| String s = new String(out.toByteArray(), "UTF8");
|
|
1814 |
1
| assertEquals(expected, s);
|
|
1815 |
| |
|
1816 |
| } |
|
1817 |
| |
|
1818 |
| |
|
1819 |
1
| public void testExclusive22b() throws ParsingException, IOException {
|
|
1820 |
| |
|
1821 |
1
| Builder builder = new Builder();
|
|
1822 |
1
| String input = "<n2:pdu xmlns:n1='http://example.com' "
|
|
1823 |
| + "xmlns:n2='http://foo.example' xml:lang='fr' xml:space='retain'>" |
|
1824 |
| + "<n1:elem2 xmlns:n1='http://example.net' xml:lang='en'>" |
|
1825 |
| + "<n3:stuff xmlns:n3='ftp://example.org'/></n1:elem2></n2:pdu>"; |
|
1826 |
1
| Document doc = builder.build(input, null);
|
|
1827 |
| |
|