1
/* Copyright 2002-2005 Elliotte Rusty Harold
3
This library is free software; you can redistribute it and/or modify
4
it under the terms of version 2.1 of the GNU Lesser General Public
5
License as published by the Free Software Foundation.
7
This library is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU Lesser General Public License for more details.
12
You should have received a copy of the GNU Lesser General Public
13
License along with this library; if not, write to the
14
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15
Boston, MA 02111-1307 USA
17
You can contact Elliotte Rusty Harold by sending e-mail to
18
elharo@metalab.unc.edu. Please include the word "XOM" in the
19
subject line. The XOM home page is located at http://www.xom.nu/
23
import java.io.ByteArrayOutputStream;
25
import java.io.FileNotFoundException;
26
import java.io.FileOutputStream;
27
import java.io.IOException;
28
import java.io.OutputStream;
29
import java.io.PrintStream;
30
import java.net.UnknownHostException;
31
import java.util.MissingResourceException;
33
import nu.xom.Attribute;
34
import nu.xom.Builder;
35
import nu.xom.Comment;
36
import nu.xom.DocType;
37
import nu.xom.Document;
38
import nu.xom.Element;
39
import nu.xom.Elements;
40
import nu.xom.IllegalAddException;
41
import nu.xom.MalformedURIException;
42
import nu.xom.Namespace;
44
import nu.xom.NodeFactory;
46
import nu.xom.ParentNode;
47
import nu.xom.ParsingException;
48
import nu.xom.ProcessingInstruction;
49
import nu.xom.Serializer;
51
import nu.xom.XMLException;
52
import nu.xom.xslt.XSLException;
53
import nu.xom.xslt.XSLTransform;
57
* Unit tests for the XSLT engine.
61
* Many of the tests in this suite use an identity transformation.
62
* This is often done to make sure I get a particular content into
63
* the output tree in order to test the XSLTHandler.
66
* @author Elliotte Rusty Harold
70
public class XSLTransformTest extends XOMTestCase {
73
public XSLTransformTest(String name) {
78
// Not a literal result element as stylesheet
79
// because it's missing the xsl:version attribute
80
private String notAStyleSheet =
81
"<?xml-stylesheet href=\"file.css\" type=\"text/css\"?>"
83
+ "<test xmlns:xlink='http://www.w3.org/TR/1999/xlink'>Hello dear"
84
+ "\r\n<em id=\"p1\" xmlns:none=\"http://www.example.com\">"
85
+ "very important</em>"
86
+ "<span xlink:type='simple'>here's the link</span>\r\n"
87
+ "<svg:svg xmlns:svg='http://www.w3.org/TR/2000/svg'>"
88
+ "<svg:text>text in a namespace</svg:text></svg:svg>\r\n"
89
+ "<svg xmlns='http://www.w3.org/TR/2000/svg'>"
90
+ "<text>text in a namespace</text></svg>"
95
// This class tests a lot of error conditions, which
96
// Xalan annoyingly logs to System.err. This hides System.err
97
// before each test and restores it after each test.
98
private PrintStream systemErr = System.err;
100
private File inputDir;
102
protected void setUp() {
104
System.setErr(new PrintStream(new ByteArrayOutputStream()));
106
inputDir = new File("data");
107
inputDir = new File(inputDir, "xslt");
108
inputDir = new File(inputDir, "input");
113
protected void tearDown() {
114
System.setErr(systemErr);
117
// Primarily this makes sure the XSLTHandler can handle various
119
public void testIdentityTransform()
120
throws ParsingException, IOException, XSLException {
122
File stylesheet = new File(inputDir, "identity.xsl");
123
Builder builder = new Builder();
124
Document stylesheetDoc = builder.build(stylesheet);
125
XSLTransform xform = new XSLTransform(stylesheetDoc);
126
Element root = new Element("root", "http://www.example.org");
127
root.appendChild(new Text("some data"));
128
root.appendChild(new Element("something"));
129
root.addAttribute(new Attribute("test", "test"));
130
root.addAttribute(new Attribute("pre:red", "http://www.red.com/", "value"));
131
Document input = new Document(root);
132
Nodes output = xform.transform(input);
133
assertEquals(root, output.get(0));
138
public void testPrefixMappingIssues()
139
throws XSLException, ParsingException, IOException {
141
String doc = "<test>"
142
+ "<span xmlns:a='http://www.example.com'/>"
143
+ "<span xmlns:b='http://www.example.net'/>"
145
File stylesheet = new File(inputDir, "identity.xsl");
146
Builder builder = new Builder();
147
Document stylesheetDoc = builder.build(stylesheet);
148
XSLTransform xform = new XSLTransform(stylesheetDoc);
149
Document input = builder.build(doc, "http://example.org/");
150
Nodes result = xform.transform(input);
151
assertEquals(input.getRootElement(), result.get(0));
156
public void testDocumentConstructor()
157
throws ParsingException, IOException {
160
Builder builder = new Builder();
161
Document doc = builder.build(notAStyleSheet,
162
"http://www.example.com");
163
new XSLTransform(doc);
164
/*Document input = builder.build("<root />", "http://example.org/");
165
Nodes result = xform.transform(input); */
166
fail("Compiled non-stylesheet");
168
catch (XSLException success) {
169
assertNotNull(success.getMessage());
175
public void testLiteralResultElementUsedAsStylesheet()
176
throws ParsingException, IOException, XSLException {
178
String literalResultElementAsStylesheet =
179
"<html xsl:version='1.0'\n"
180
+ " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'\n"
181
+ " xmlns='http://www.w3.org/TR/xhtml1/strict'>\n"
183
+ " <title>Expense Report Summary</title>\n"
186
+ " <p>Total Amount: <xsl:value-of select='expense-report/total'/></p>\n"
190
Builder builder = new Builder();
191
Document stylesheet = builder.build(literalResultElementAsStylesheet,
192
"http://www.example.com");
193
XSLTransform transform = new XSLTransform(stylesheet);
194
Document doc = builder.build(notAStyleSheet,
195
"http://www.example.com");
196
Nodes result = transform.transform(doc);
197
Element root = (Element) (result.get(0));
198
assertEquals("html", root.getQualifiedName());
199
assertEquals(2, root.getChildCount());
204
/* <xsl:template match="/">
205
<element1>some data and <content/> for a test</element1>
206
<element2>Remember, the XSLT processor is going to strip out the literal white space</element2>
207
<element3>some data and <content/> for a <!--test--></element3>
209
<xsl:comment>test</xsl:comment>
210
<xsl:processing-instruction name="test">PIs are not treated as literals in XSLT?</xsl:processing-instruction>
213
public void testCreateDocumentFragment()
214
throws ParsingException, IOException, XSLException {
216
Element element1 = new Element("element1");
217
element1.appendChild("some data and ");
218
element1.appendChild(new Element("content"));
219
element1.appendChild(" for a test");
221
Element element2 = new Element("element2");
222
element2.appendChild(
223
"Remember, the XSLT processor is going to strip out the literal white space"
225
File doc = new File(inputDir, "8-14.xml");
226
File stylesheet = new File(inputDir, "fragment.xsl");
227
Builder builder = new Builder();
228
Document stylesheetDoc = builder.build(stylesheet);
229
XSLTransform xform = new XSLTransform(stylesheetDoc);
230
Document input = builder.build(doc);
231
Nodes output = xform.transform(input);
232
assertEquals(6, output.size());
233
assertEquals(element1, output.get(0));
234
assertEquals(element2, output.get(1));
235
assertEquals(new Element("element4"), output.get(3));
236
assertEquals(new Comment("test"), output.get(4));
237
assertEquals(new ProcessingInstruction("test",
238
"PIs are not treated as literals in XSLT?"), output.get(5));
243
public void testTransform()
244
throws ParsingException, IOException, XSLException {
246
File doc = new File(inputDir, "8-1.xml");
247
File stylesheet = new File(inputDir, "8-8.xsl");
248
Builder builder = new Builder();
249
Document stylesheetDoc = builder.build(stylesheet);
250
XSLTransform xform = new XSLTransform(stylesheetDoc);
251
Nodes output = xform.transform(builder.build(doc));
252
assertEquals(1, output.size());
253
Document result = new Document((Element) (output.get(0)));
255
Document expected = builder.build("data/xslt/output/8-8.xml");
256
assertEquals(expected, result);
261
public void testSetParameter()
262
throws ParsingException, IOException, XSLException {
264
File doc = new File(inputDir, "8-1.xml");
265
File stylesheet = new File(inputDir, "paramtest.xsl");
266
Builder builder = new Builder();
267
Document stylesheetDoc = builder.build(stylesheet);
268
XSLTransform xform = new XSLTransform(stylesheetDoc);
269
xform.setParameter("test", new Double(2));
270
Nodes output = xform.transform(builder.build(doc));
271
assertEquals(1, output.size());
272
Element result = (Element) output.get(0);
273
assertEquals("<root>2</root>", result.toXML());
278
public void testSetParameterWithNamespace()
279
throws ParsingException, IOException, XSLException {
281
File doc = new File(inputDir, "8-1.xml");
282
File stylesheet = new File(inputDir, "namespaceparamtest.xsl");
283
Builder builder = new Builder();
284
Document stylesheetDoc = builder.build(stylesheet);
285
XSLTransform xform = new XSLTransform(stylesheetDoc);
286
xform.setParameter("test", "http://www.xom.nu/", new Double(2));
287
Nodes output = xform.transform(builder.build(doc));
288
assertEquals(1, output.size());
289
Element result = (Element) output.get(0);
290
assertEquals("<root xmlns:pre=\"http://www.xom.nu/\">2</root>",
296
public void testRemoveParameterWithNamespace()
297
throws ParsingException, IOException, XSLException {
299
File doc = new File(inputDir, "8-1.xml");
300
File stylesheet = new File(inputDir, "namespaceparamtest.xsl");
301
Builder builder = new Builder();
302
Document stylesheetDoc = builder.build(stylesheet);
303
XSLTransform xform = new XSLTransform(stylesheetDoc);
304
xform.setParameter("test", "http://www.xom.nu/", new Double(2));
305
xform.setParameter("test", "http://www.xom.nu/", null);
306
Nodes output = xform.transform(builder.build(doc));
307
assertEquals(1, output.size());
308
Element result = (Element) output.get(0);
309
assertEquals("<root xmlns:pre=\"http://www.xom.nu/\">1</root>",
315
public void testRemoveParameter()
316
throws ParsingException, IOException, XSLException {
318
File doc = new File(inputDir, "8-1.xml");
319
File stylesheet = new File(inputDir, "paramtest.xsl");
320
Builder builder = new Builder();
321
Document stylesheetDoc = builder.build(stylesheet);
322
XSLTransform xform = new XSLTransform(stylesheetDoc);
323
xform.setParameter("test", new Double(2));
324
xform.setParameter("test", null);
325
Nodes output = xform.transform(builder.build(doc));
326
assertEquals(1, output.size());
327
Element result = (Element) output.get(0);
328
assertEquals("<root>1</root>", result.toXML());
333
public void testTransformWithCFilter()
334
throws ParsingException, IOException, XSLException {
336
File doc = new File(inputDir, "8-1.xml");
337
File stylesheet = new File(inputDir, "8-8.xsl");
338
Builder builder = new Builder();
339
Document stylesheetDoc = builder.build(stylesheet);
340
XSLTransform xform = new XSLTransform(
341
stylesheetDoc, new NodeFactoryTest.CFactory());
343
Nodes output = xform.transform(builder.build(doc));
344
assertEquals(1, output.size());
345
Document result = new Document((Element) (output.get(0)));
347
Document expected = builder.build("data/xslt/output/8-8c.xml");
348
assertEquals(expected, result);
353
public void testCreateDocumentFragmentWithCommentFilter()
354
throws ParsingException, IOException, XSLException {
356
Element element1 = new Element("element1");
357
element1.appendChild("some data and ");
358
element1.appendChild(new Element("content"));
359
element1.appendChild(" for a test");
361
Element element2 = new Element("element2");
362
element2.appendChild(
363
"Remember, the XSLT processor is going to strip out the literal white space"
365
File doc = new File(inputDir, "8-14.xml");
366
File stylesheet = new File(inputDir, "fragment.xsl");
367
Builder builder = new Builder();
368
Document stylesheetDoc = builder.build(stylesheet);
369
XSLTransform xform = new XSLTransform(
370
stylesheetDoc, new NodeFactoryTest.CommentFilter());
372
Document input = builder.build(doc);
373
Nodes output = xform.transform(input);
374
assertEquals(5, output.size());
375
assertEquals(element1, output.get(0));
376
assertEquals(element2, output.get(1));
377
assertEquals(new Element("element4"), output.get(3));
378
assertEquals(new ProcessingInstruction("test",
379
"PIs are not treated as literals in XSLT?"), output.get(4));
384
public void testCreateDocumentFragmentWithProcessingInstructionFilter()
385
throws ParsingException, IOException, XSLException {
387
Element element1 = new Element("element1");
388
element1.appendChild("some data and ");
389
element1.appendChild(new Element("content"));
390
element1.appendChild(" for a test");
392
Element element2 = new Element("element2");
393
element2.appendChild(
394
"Remember, the XSLT processor is going to strip out the literal white space"
396
File doc = new File(inputDir, "8-14.xml");
397
File stylesheet = new File(inputDir, "fragment.xsl");
398
Builder builder = new Builder();
399
Document stylesheetDoc = builder.build(stylesheet);
400
XSLTransform xform = new XSLTransform(stylesheetDoc,
401
new NodeFactoryTest.ProcessingInstructionFilter());
403
Document input = builder.build(doc);
404
Nodes output = xform.transform(input);
405
assertEquals(5, output.size());
406
assertEquals(element1, output.get(0));
407
assertEquals(element2, output.get(1));
408
assertEquals(new Element("element4"), output.get(3));
409
assertEquals(new Comment("test"), output.get(4));
414
public void testCreateDocumentFragmentWithUncommentFilter()
415
throws ParsingException, IOException, XSLException {
417
Element element1 = new Element("element1");
418
element1.appendChild("some data and ");
419
element1.appendChild(new Element("content"));
420
element1.appendChild(" for a test");
422
Element element2 = new Element("element2");
423
element2.appendChild(
424
"Remember, the XSLT processor is going to strip out the literal white space"
426
File doc = new File(inputDir, "8-14.xml");
427
File stylesheet = new File(inputDir, "fragment.xsl");
428
Builder builder = new Builder();
429
Document stylesheetDoc = builder.build(stylesheet);
430
XSLTransform xform = new XSLTransform(stylesheetDoc,
431
new NodeFactoryTest.UncommentFilter());
433
Document input = builder.build(doc);
434
Nodes output = xform.transform(input);
435
assertEquals(6, output.size());
436
assertEquals(element1, output.get(0));
437
assertEquals(element2, output.get(1));
438
assertEquals(new Element("element4"), output.get(3));
439
assertEquals(new Text("test"), output.get(4));
440
assertEquals(new ProcessingInstruction("test",
441
"PIs are not treated as literals in XSLT?"), output.get(5));
446
public void testTransform2()
447
throws ParsingException, IOException, XSLException {
449
File doc = new File(inputDir, "8-1.xml");
450
File stylesheet = new File(inputDir, "8-12.xsl");
451
Builder builder = new Builder();
452
Document stylesheetDoc = builder.build(stylesheet);
453
XSLTransform xform = new XSLTransform(stylesheetDoc);
454
Nodes output = xform.transform(builder.build(doc));
455
assertEquals(1, output.size());
456
Document result = new Document((Element) (output.get(0)));
458
Document expected = builder.build("data/xslt/output/8-12.xml");
459
assertEquals(expected, result);
466
private static void dumpResult(Document result, String filename)
469
File debug = new File("data");
470
debug = new File(debug, "xslt");
471
debug = new File(debug, "debug/" + filename);
472
OutputStream out = new FileOutputStream(debug);
473
Serializer serializer = new Serializer(out);
474
serializer.write(result);
481
public void testTransformFromDocument()
482
throws ParsingException, IOException, XSLException {
484
File doc = new File(inputDir, "8-1.xml");
485
Builder builder = new Builder();
486
Document stylesheet = builder.build("data/xslt/input/8-12.xsl");
487
XSLTransform xform = new XSLTransform(stylesheet);
488
Nodes output = xform.transform(builder.build(doc));
489
assertEquals(1, output.size());
490
Document result = new Document((Element) (output.get(0)));
492
Document expected = builder.build("data/xslt/output/8-12.xml");
493
assertEquals(expected, result);
498
public void testTransformFromSystemID()
499
throws ParsingException, IOException, XSLException {
501
File doc = new File(inputDir, "8-1.xml");
502
Builder builder = new Builder();
503
String stylesheet = "data/xslt/input/8-12.xsl";
504
Document stylesheetDoc = builder.build(stylesheet);
505
XSLTransform xform = new XSLTransform(stylesheetDoc);
506
Nodes output = xform.transform(builder.build(doc));
507
assertEquals(1, output.size());
508
Document result = new Document((Element) (output.get(0)));
510
Document expected = builder.build("data/xslt/output/8-12.xml");
511
assertEquals(expected, result);
516
public void testTransformWithNamespaces()
517
throws ParsingException, IOException, XSLException {
519
File doc = new File(inputDir, "8-14.xml");
520
File stylesheet = new File(inputDir, "8-15.xsl");
521
Builder builder = new Builder();
522
Document stylesheetDoc = builder.build(stylesheet);
523
XSLTransform xform = new XSLTransform(stylesheetDoc);
524
Document input = builder.build(doc);
525
Nodes output = xform.transform(input);
526
assertEquals(1, output.size());
527
Document result = new Document((Element) (output.get(0)));
529
Document expected = builder.build("data/xslt/output/8-15.xml");
530
assertEquals(expected, result);
535
public void testSingleTextNode()
536
throws ParsingException, IOException, XSLException {
538
File doc = new File(inputDir, "8-14.xml");
539
File stylesheet = new File(inputDir, "singlestring.xsl");
540
Builder builder = new Builder();
541
Document stylesheetDoc = builder.build(stylesheet);
542
XSLTransform xform = new XSLTransform(stylesheetDoc);
543
Document input = builder.build(doc);
544
Nodes output = xform.transform(input);
545
assertEquals(1, output.size());
546
Text data = (Text) (output.get(0));
547
assertEquals("Data", data.getValue());
552
public void testToString()
553
throws XSLException, ParsingException, IOException {
555
File stylesheet = new File(inputDir, "singlestring.xsl");
556
Builder builder = new Builder();
557
Document stylesheetDoc = builder.build(stylesheet);
558
XSLTransform xform = new XSLTransform(stylesheetDoc);
559
assertTrue(xform.toString().startsWith("[nu.xom.xslt.XSLTransform: "));
564
// Make sure that method="text" doesn't affect what we get
565
// since this is not a serialized transform
566
// See http://issues.apache.org/bugzilla/show_bug.cgi?id=30197
567
public void testTextMethod()
568
throws ParsingException, IOException, XSLException {
570
File doc = new File(inputDir, "8-14.xml");
571
File stylesheet = new File(inputDir, "textmethod.xsl");
572
Builder builder = new Builder();
573
Document stylesheetDoc = builder.build(stylesheet);
574
XSLTransform xform = new XSLTransform(stylesheetDoc);
575
Document input = builder.build(doc);
576
Nodes output = xform.transform(input);
577
assertEquals(6, output.size());
578
assertEquals("12345", output.get(0).getValue());
579
assertEquals("67890", output.get(1).getValue());
580
assertEquals("", output.get(2).getValue());
581
assertEquals("0987654321", output.get(3).getValue());
582
assertTrue(output.get(4) instanceof Comment);
583
assertTrue(output.get(5) instanceof ProcessingInstruction);
588
public void testCommentWithParent()
589
throws XSLException, ParsingException, IOException {
591
Builder builder = new Builder();
592
File stylesheet = new File(inputDir, "commentwithparent.xsl");
593
Document stylesheetDoc = builder.build(stylesheet);
594
XSLTransform xform = new XSLTransform(stylesheetDoc);
595
Document input = new Document(new Element("root"));
596
Nodes output = xform.transform(input);
597
assertEquals(1, output.size());
598
assertEquals("", output.get(0).getValue());
599
Element root = (Element) output.get(0);
600
assertEquals(1, root.getChildCount());
601
Comment child = (Comment) root.getChild(0);
602
assertEquals("test", child.getValue());
607
public void testProcessingInstructionWithParent()
608
throws XSLException, ParsingException, IOException {
610
Builder builder = new Builder();
611
File stylesheet = new File(inputDir, "piwithparent.xsl");
612
Document stylesheetDoc = builder.build(stylesheet);
613
XSLTransform xform = new XSLTransform(stylesheetDoc);
614
Document input = new Document(new Element("root"));
615
Nodes output = xform.transform(input);
616
assertEquals(1, output.size());
617
assertEquals("", output.get(0).getValue());
618
Element root = (Element) output.get(0);
619
assertEquals(1, root.getChildCount());
620
ProcessingInstruction child = (ProcessingInstruction) root.getChild(0);
621
assertEquals("target", child.getTarget());
622
assertEquals("test", child.getValue());
627
public void testTransformNodes()
628
throws XSLException, ParsingException, IOException {
630
File stylesheet = new File(inputDir, "piwithparent.xsl");
631
Builder builder = new Builder();
632
Nodes input = new Nodes(new Element("root"));
633
Document stylesheetDoc = builder.build(stylesheet);
634
XSLTransform xform = new XSLTransform(stylesheetDoc);
635
Nodes output = xform.transform(input);
636
assertEquals(1, output.size());
637
assertEquals("", output.get(0).getValue());
638
Element root = (Element) output.get(0);
639
assertEquals(1, root.getChildCount());
640
ProcessingInstruction child = (ProcessingInstruction) root.getChild(0);
641
assertEquals("target", child.getTarget());
642
assertEquals("test", child.getValue());
647
public void testCopyXMLBaseAttribute()
648
throws XSLException, ParsingException, IOException {
650
File stylesheet = new File(inputDir, "id_transform.xsl");
651
Builder builder = new Builder();
652
Element root = new Element("root");
653
root.addAttribute(new Attribute("xml:base",
654
Namespace.XML_NAMESPACE, "http://www.example.org/"));
655
Document input = new Document(root);
656
Document stylesheetDoc = builder.build(stylesheet);
657
XSLTransform xform = new XSLTransform(stylesheetDoc);
658
Nodes output = xform.transform(input);
659
assertEquals(1, output.size());
660
assertEquals("", output.get(0).getValue());
661
Element rootOut = (Element) output.get(0);
662
assertEquals(0, rootOut.getChildCount());
663
assertEquals(1, rootOut.getAttributeCount());
664
assertEquals("http://www.example.org/", rootOut.getBaseURI());
669
public void testCopyXMLLangAttribute()
670
throws XSLException, ParsingException, IOException {
672
File stylesheet = new File(inputDir, "id_transform.xsl");
673
Builder builder = new Builder();
674
Element root = new Element("root");
675
root.addAttribute(new Attribute("xml:lang",
676
Namespace.XML_NAMESPACE, "http://www.example.org/"));
677
Document input = new Document(root);
678
Document stylesheetDoc = builder.build(stylesheet);
679
XSLTransform xform = new XSLTransform(stylesheetDoc);
680
Nodes output = xform.transform(input);
681
assertEquals(1, output.size());
682
assertEquals("", output.get(0).getValue());
683
Element rootOut = (Element) output.get(0);
684
assertEquals(0, rootOut.getChildCount());
685
assertEquals(1, rootOut.getAttributeCount());
690
public void testTriple()
691
throws IOException, ParsingException, XSLException {
693
File stylesheet = new File(inputDir, "identity.xsl");
694
Builder builder = new Builder();
695
Document stylesheetDoc = builder.build(stylesheet);
696
XSLTransform xform = new XSLTransform(stylesheetDoc,
697
new NodeFactoryTest.TripleElementFilter());
699
String data = "<a><b><c/></b></a>";
700
Document doc = builder.build(data, "http://www.example.org/");
702
Nodes result = xform.transform(doc);
704
assertEquals(3, result.size());
705
assertEquals(result.get(0), result.get(1));
706
assertEquals(result.get(1), result.get(2));
707
Element a = (Element) result.get(2);
708
assertEquals("a", a.getLocalName());
709
assertEquals(3, a.getChildCount());
710
assertEquals(0, a.getAttributeCount());
711
Element b = (Element) a.getChild(1);
712
assertEquals(3, b.getChildCount());
713
assertEquals("b", b.getLocalName());
718
public void testPassingNullSetsDefaultFactory()
719
throws IOException, ParsingException, XSLException {
721
File stylesheet = new File(inputDir, "identity.xsl");
722
Builder builder = new Builder();
723
Document stylesheetDoc = builder.build(stylesheet);
724
XSLTransform xform = new XSLTransform(stylesheetDoc, null);
726
String data = "<a><b><c/></b></a>";
727
Document doc = builder.build(data, "http://www.example.org/");
729
Nodes result = xform.transform(doc);
731
assertEquals(1, result.size());
732
Element a = (Element) result.get(0);
733
assertEquals("a", a.getLocalName());
734
assertEquals(1, a.getChildCount());
735
assertEquals(0, a.getAttributeCount());
736
Element b = (Element) a.getChild(0);
737
assertEquals(1, b.getChildCount());
738
assertEquals("b", b.getLocalName());
743
public void testTransformEmptyNodesList()
744
throws IOException, ParsingException, XSLException {
746
File stylesheet = new File(inputDir, "identity.xsl");
747
Builder builder = new Builder();
748
Document stylesheetDoc = builder.build(stylesheet);
749
XSLTransform xform = new XSLTransform(stylesheetDoc);
751
Nodes result = xform.transform(new Nodes());
753
assertEquals(0, result.size());
758
public void testMinimizingFactory()
759
throws XSLException, ParsingException, IOException {
761
File stylesheet = new File(inputDir, "identity.xsl");
762
Builder builder = new Builder();
763
Document stylesheetDoc = builder.build(stylesheet);
764
XSLTransform xform = new XSLTransform(stylesheetDoc,
765
new NodeFactoryTest.MinimizingFactory());
767
Document input = builder.build("<!-- test--><test>" +
768
"<em>data</em>\r\n<span>test</span></test>" +
769
"<?target data?>", "http://example.org/");
770
Nodes output = xform.transform(input);
771
assertEquals(0, output.size());
776
public void testIllegalTransform()
777
throws XSLException, ParsingException, IOException {
779
File stylesheet = new File(inputDir, "illegaltransform.xsl");
780
Builder builder = new Builder();
781
Document stylesheetDoc = builder.build(stylesheet);
782
XSLTransform xform = new XSLTransform(stylesheetDoc);
783
Element root = new Element("root", "http://www.example.org");
784
Document input = new Document(root);
786
xform.transform(input);
787
fail("Allowed illegal transform");
789
catch (XSLException ex) {
790
assertNotNull(ex.getMessage());
796
public void testRemapPrefixToSameURI()
797
throws IOException, ParsingException, XSLException {
799
File stylesheet = new File(inputDir, "identity.xsl");
800
Builder builder = new Builder();
801
Document stylesheetDoc = builder.build(stylesheet);
802
XSLTransform xform = new XSLTransform(stylesheetDoc);
804
String data = "<a xmlns:pre='http://www.example.org/'>" +
805
"<b xmlns:pre='http://www.example.org/'>in B</b></a>";
806
Document doc = builder.build(data, "http://www.example.org/");
808
Nodes result = xform.transform(doc);
810
assertEquals(doc.getRootElement(), result.get(0));
815
public void testElementsToAttributes()
816
throws IOException, ParsingException, XSLException {
818
File stylesheet = new File(inputDir, "identity.xsl");
819
Builder builder = new Builder();
820
Document stylesheetDoc = builder.build(stylesheet);
821
XSLTransform xform = new XSLTransform(stylesheetDoc,
822
new AttributeFactory());
824
String data = "<a><b>in B<c>in C</c></b></a>";
825
Document doc = builder.build(data, "http://www.example.org/");
827
Nodes result = xform.transform(doc);
829
assertEquals(1, result.size());
830
Element a = (Element) result.get(0);
831
assertEquals("a", a.getLocalName());
832
assertEquals(0, a.getChildCount());
833
assertEquals(1, a.getAttributeCount());
834
assertEquals("in B", a.getAttribute("b").getValue());
839
private static class AttributeFactory extends NodeFactory {
841
public Nodes finishMakingElement(Element element) {
842
ParentNode parent = element.getParent();
843
if (parent == null || parent instanceof Document) {
844
return new Nodes(element);
846
return new Nodes(new Attribute(element.getQualifiedName(),
847
element.getNamespaceURI(), element.getValue()));
853
public void testAttributesToElements()
854
throws IOException, ParsingException, XSLException {
856
File stylesheet = new File(inputDir, "identity.xsl");
857
Builder builder = new Builder();
858
Document stylesheetDoc = builder.build(stylesheet);
859
XSLTransform xform = new XSLTransform(stylesheetDoc,
860
new AttributesToElements());
862
String data = "<a name='value'><b x='y' a='b'/></a>";
863
Document doc = builder.build(data, "http://www.example.org/");
865
Nodes result = xform.transform(doc);
867
assertEquals(1, result.size());
868
Element a = (Element) result.get(0);
869
assertEquals("a", a.getLocalName());
870
assertEquals(2, a.getChildCount());
871
assertEquals(0, a.getAttributeCount());
872
Element name = (Element) a.getChild(0);
873
assertEquals("name", name.getLocalName());
874
assertEquals("value", name.getValue());
875
Element b = (Element) a.getChild(1);
876
assertEquals("b", b.getLocalName());
877
assertEquals(2, b.getChildCount());
878
assertEquals("y", b.getFirstChildElement("x").getValue());
879
assertEquals("b", b.getFirstChildElement("a").getValue());
884
private static class AttributesToElements extends NodeFactory {
886
public Nodes makeAttribute(String name, String URI,
887
String value, Attribute.Type type) {
888
Element element = new Element(name, URI);
889
element.appendChild(value);
890
return new Nodes(element);
896
public void testCommentsAreTransformed()
897
throws IOException, ParsingException, XSLException {
899
File stylesheet = new File(inputDir, "identity.xsl");
900
Builder builder = new Builder();
901
Document stylesheetDoc = builder.build(stylesheet);
902
XSLTransform xform = new XSLTransform(stylesheetDoc);
904
String data = "<a><!--test--></a>";
905
Document doc = builder.build(data, "http://www.example.org/");
907
Nodes result = xform.transform(doc);
909
assertEquals(1, result.size());
910
Element a = (Element) result.get(0);
911
assertEquals("a", a.getLocalName());
912
assertEquals(1, a.getChildCount());
913
assertEquals(0, a.getAttributeCount());
914
Node child = a.getChild(0);
915
assertTrue(child instanceof Comment);
916
assertTrue(child.getValue().equals("test"));
921
public void testCommentToAttribute()
922
throws IOException, ParsingException, XSLException {
924
File stylesheet = new File(inputDir, "identity.xsl");
925
Builder builder = new Builder();
926
Document stylesheetDoc = builder.build(stylesheet);
927
XSLTransform xform = new XSLTransform(stylesheetDoc,
929
public Nodes makeComment(String text) {
930
return new Nodes(new Attribute("comment", text));
934
String data = "<a><!--test--></a>";
935
Document doc = builder.build(data, "http://www.example.org/");
937
Nodes result = xform.transform(doc);
939
assertEquals(1, result.size());
940
Element a = (Element) result.get(0);
941
assertEquals("a", a.getLocalName());
942
assertEquals(0, a.getChildCount());
943
assertEquals(1, a.getAttributeCount());
944
Attribute comment = a.getAttribute(0);
945
assertEquals("comment", comment.getLocalName());
946
assertEquals("test", comment.getValue());
951
public void testAdditionalDefaultNamespace()
952
throws IOException, ParsingException, XSLException {
954
File stylesheet = new File(inputDir, "identity.xsl");
955
Builder builder = new Builder();
956
Document stylesheetDoc = builder.build(stylesheet);
957
XSLTransform xform = new XSLTransform(stylesheetDoc);
959
String data = "<pre:a xmlns:pre='http://www.example.org' " +
960
"xmlns='http://www.example.net'>data</pre:a>";
961
Document doc = builder.build(data, "http://www.example.org/");
963
Nodes result = xform.transform(doc);
965
assertEquals(1, result.size());
966
Element a = (Element) result.get(0);
967
assertEquals("a", a.getLocalName());
968
assertEquals("pre:a", a.getQualifiedName());
969
assertEquals("data", a.getValue());
970
assertEquals("http://www.example.org", a.getNamespaceURI("pre"));
971
assertEquals("http://www.example.net", a.getNamespaceURI(""));
972
assertEquals(2, a.getNamespaceDeclarationCount());
978
private static boolean indentYes(Document styleDoc) {
980
Element output = styleDoc
982
.getFirstChildElement("output",
983
"http://www.w3.org/1999/XSL/Transform");
984
if (output == null) return false;
986
String indent = output.getAttributeValue("indent");
987
if ("yes".equals(indent)) {
995
private static class StrippingFactory extends NodeFactory {
997
public Nodes makeText(String s) {
999
String stripped = stripSpace(s);
1000
if (stripped.length() == 0) return new Nodes();
1001
Text result = new Text(stripped);
1002
return new Nodes(result);
1005
public Nodes makeAttribute(String name, String URI,
1006
String value, Attribute.Type type) {
1007
return new Nodes(new Attribute(name, URI, stripSpace(value), type));
1010
private String stripSpace(String s) {
1012
StringBuffer sb = new StringBuffer();
1013
for (int i = 0; i < s.length(); i++) {
1014
if (!Character.isWhitespace(s.charAt(i))) {
1015
sb.append(s.charAt(i));
1019
return sb.toString();
1025
// XXX need to upgrade to final release of XSLT test suites
1026
// and then split into individual tests
1027
public void testOASISXalanConformanceSuite()
1028
throws IOException, ParsingException, XSLException {
1030
Builder builder = new Builder();
1031
NodeFactory stripper = new StrippingFactory();
1032
Builder strippingBuilder = new Builder(stripper);
1034
File base = new File("data");
1035
base = new File(base, "oasis-xslt-testsuite");
1036
base = new File(base, "TESTS");
1037
base = new File(base, "Xalan_Conformance_Tests");
1038
File catalog = new File(base, "catalog.xml");
1040
// The test suite need to be installed separately. If we can't
1041
// find the catalog, we just don't run these tests.
1042
if (catalog.exists()) {
1043
Document doc = builder.build(catalog);
1044
Element testsuite = doc.getRootElement();
1045
Elements submitters = testsuite.getChildElements("test-catalog");
1046
for (int i = 0; i < submitters.size(); i++) {
1047
Element submitter = submitters.get(i);
1048
Elements testcases = submitter.getChildElements("test-case");
1049
for (int j = 0; j < testcases.size(); j++) {
1051
Element testcase = testcases.get(j);
1052
String id = testcase.getAttributeValue("id");
1053
if (id.startsWith("output_")) {
1054
// These test cases are mostly about producing
1055
// HTML and plain text output that isn't
1059
File root = new File(base, testcase.getFirstChildElement("file-path").getValue());
1063
Element scenario = testcase.getFirstChildElement("scenario");
1064
Elements inputs = scenario.getChildElements("input-file");
1065
for (int k = 0; k < inputs.size(); k++) {
1066
Element file = inputs.get(k);
1067
String role = file.getAttributeValue("role");
1068
if ("principal-data".equals(role)) {
1069
input = new File(root, file.getValue());
1071
else if ("principal-stylesheet".equals(role)) {
1072
style = new File(root, file.getValue());
1075
Elements outputs = scenario.getChildElements("output-file");
1076
for (int k = 0; k < outputs.size(); k++) {
1077
Element file = outputs.get(k);
1078
String role = file.getAttributeValue("role");
1079
if ("principal".equals(role)) {
1080
// Fix up OASIS catalog bugs
1081
File parent = new File(root.getParent());
1082
parent = new File(parent, "REF_OUT");
1083
parent = new File(parent, root.getName());
1084
String outputFileName = file.getValue();
1085
output = new File(parent, outputFileName);
1090
Document inputDoc = builder.build(input);
1091
Document styleDoc = builder.build(style);
1092
// If the transform specifies indent="yes".
1093
// we remove all white space before comparing
1095
if (indentYes(styleDoc)) {
1096
xform = new XSLTransform(styleDoc, stripper);
1098
else xform = new XSLTransform(styleDoc);
1099
Nodes result = xform.transform(inputDoc);
1100
if (output == null) {
1101
// transform should have failed
1102
fail("Transformed " + id);
1106
Document expectedResult;
1107
if (indentYes(styleDoc)) {
1108
expectedResult = strippingBuilder.build(output);
1111
expectedResult = builder.build(output);
1113
Document actualResult = XSLTransform.toDocument(result);
1115
if (id.equals("attribset_attribset40")) {
1116
// This test does not necessarily
1117
// produce an identical infoset due
1118
// to necessary remapping of
1119
// namespace prefixes.
1122
else if (id.equals("axes_axes129")) {
1123
// Xalan bug. Fixed in more recent
1124
// version than bundled with the JDK 1.4.2_05
1126
else if (id.equals("copy_copy56")
1127
|| id.equals("copy_copy58")
1128
|| id.equals("copy_copy60")
1129
|| id.equals("copy_copy59")) {
1131
// See http://issues.apache.org/jira/browse/XALANJ-1081
1132
// Also see erratum E27 to the XSLT spec.
1134
else if (id.equals("expression_expression02")) {
1135
// requires unparsed entities XOM doesn't support
1137
else if (id.equals("idkey_idkey31")) {
1139
// See http://issues.apache.org/jira/browse/XALANJ-1325
1141
else if (id.equals("idkey_idkey61")
1142
|| id.equals("idkey_idkey62")) {
1143
// Xalan bug. Fixed in more recent
1144
// version than bundled with the JDK 1.4.2_05
1145
// See http://issues.apache.org/jira/browse/XALANJ-1318
1147
else if (id.equals("impincl_impincl11")) {
1148
// Test case bug; reported 2004-09-18
1149
// See http://lists.oasis-open.org/archives/xslt-conformance-comment/200409/msg00001.html
1151
else if (id.equals("math_math110")
1152
|| id.equals("math_math111")) {
1153
// Xalan bug. Fixed in more recent
1154
// version than bundled with the JDK 1.4.2_05
1155
// See http://issues.apache.org/jira/browse/XALANJ-1278
1157
else if (id.equals("numbering_numbering17")
1158
|| id.equals("numbering_numbering79")) {
1159
// test suite bug per XSLT 1.0 erratum 24. See
1160
// See http://issues.apache.org/jira/browse/XALANJ-1979
1162
else if (id.equals("position_position104")) {
1163
// Xalan bug. Fixed in more recent
1164
// version than bundled with the JDK 1.4.2_05
1166
else if (id.equals("position_position106")) {
1167
// Xalan bug. Fixed in more recent
1168
// version than bundled with the JDK 1.4.2_05
1170
else if (id.equals("position_position107")
1171
|| id.equals("position_position109")) {
1172
// Xalan bug. Fixed in more recent
1173
// version than bundled with the JDK 1.4.2_05
1174
// See http://issues.apache.org/jira/browse/XALANJ-1289
1177
assertEquals("Problem with " + id,
1178
expectedResult, actualResult);
1181
catch (ParsingException ex) {
1182
// a few of the test cases generate
1183
// text or HTML output rather than
1184
// well-formed XML. For the moment, I
1188
catch (IllegalAddException ex) {
1189
// A few of the test cases generate
1190
// incomplete documents so we can't
1191
// compare output. Perhaps I could
1192
// wrap in an element, then get children
1193
// to build a Nodes object rather than a
1194
// Document???? i.e. a fragment parser?
1195
// Could use a SequenceInputStream to hack this
1200
catch (ParsingException ex) {
1201
// Some of the test cases contain relative
1202
// namespace URIs XOM does not support
1203
if (ex.getCause() instanceof MalformedURIException) continue;
1206
catch (XSLException ex) {
1207
// If the output was null the transformation
1208
// was expected to fail
1209
if (output != null) {
1210
// a few of the test cases use relative namespace URIs
1211
// XOM doesn't support
1212
Throwable cause = ex.getCause();
1213
if (cause instanceof MalformedURIException) {
1217
if ("impincl_impincl27".equals(id)) {
1218
// Test case uses file: URI XOM doesn't support
1221
else if ("numberformat_numberformat45".equals(id)
1222
|| "numberformat_numberformat46".equals(id)) {
1223
// This has been fixed in Xalan 2.5.2.
1224
// However, it's a bug in earlier versions of Xalan
1225
// including the one bundled with the JDK 1.4.2_05
1226
// See http://issues.apache.org/jira/browse/XALANJ-805
1227
// XXX I think this might need an updated version of the test cases
1228
// and expected output at this point.
1232
System.out.println(id);
1233
System.out.println(ex.getMessage());
1245
public void testOASISMicrosoftConformanceSuite()
1246
throws IOException, ParsingException, XSLException {
1248
Builder builder = new Builder();
1249
NodeFactory stripper = new StrippingFactory();
1250
Builder strippingBuilder = new Builder(stripper);
1251
File base = new File("data");
1252
base = new File(base, "oasis-xslt-testsuite");
1253
base = new File(base, "TESTS");
1254
File catalog = new File(base, "catalog.xml");
1256
// The test suite need to be installed separately. If we can't
1257
// find the catalog, we just don't run these tests.
1258
if (catalog.exists()) {
1259
Document doc = builder.build(catalog);
1260
Element testsuite = doc.getRootElement();
1261
Elements submitters = testsuite.getChildElements("test-catalog");
1262
Element submitter = submitters.get(1);
1263
Elements testcases = submitter.getChildElements("test-case");
1264
for (int j = 0; j < testcases.size(); j++) {
1265
Element testcase = testcases.get(j);
1266
String id = testcase.getAttributeValue("id");
1267
File root = new File(base, "MSFT_Conformance_Tests");
1268
root = new File(root, testcase.getFirstChildElement("file-path").getValue());
1272
Element scenario = testcase.getFirstChildElement("scenario");
1273
Elements inputs = scenario.getChildElements("input-file");
1274
for (int k = 0; k < inputs.size(); k++) {
1275
Element file = inputs.get(k);
1276
String role = file.getAttributeValue("role");
1277
if ("principal-data".equals(role)) {
1278
input = new File(root, file.getValue());
1280
else if ("principal-stylesheet".equals(role)) {
1281
style = new File(root, file.getValue());
1284
Elements outputs = scenario.getChildElements("output-file");
1285
for (int k = 0; k < outputs.size(); k++) {
1286
Element file = outputs.get(k);
1287
String role = file.getAttributeValue("role");
1288
if ("principal".equals(role)) {
1289
// Fix up OASIS catalog bugs
1290
File parent = new File(root.getParent());
1291
parent = new File(parent, "REF_OUT");
1292
parent = new File(parent, root.getName());
1293
String outputFileName = file.getValue();
1294
output = new File(parent, outputFileName);
1299
Document styleDoc = builder.build(style);
1300
boolean strip = indentYes(styleDoc);
1301
if ("BVTs_bvt002".equals(id) || "BVTs_bvt077".equals(id)) {
1302
// This has been fixed at least as of Xalan 2.6.0.
1303
// However, it's a bug in earlier versions of Xalan
1304
// including the one bundled with the JDK 1.4.2_05
1307
else if ("XSLTFunctions_Bug76984".equals(id)) {
1308
// This has been fixed at least as of Xalan 2.6.0.
1309
// However, it's a bug in earlier versions of Xalan
1310
// including the one bundled with the JDK 1.4.2_05
1313
else if ("BVTs_bvt020".equals(id) || "BVTs_bvt022".equals(id)
1314
|| "BVTs_bvt024".equals(id) || "BVTs_bvt058".equals(id)) {
1315
// Either a test suite bug, or a recoverable
1316
// error Xalan doesn't recover from.
1319
else if ("BVTs_bvt038".equals(id)
1320
|| "Namespace-alias__91785".equals(id)
1321
|| "Namespace-alias__91786".equals(id)) {
1322
// a recoverable error Xalan doesn't recover from properly
1323
// http://issues.apache.org/jira/browse/XALANJ-1957
1326
else if ("Namespace_XPath_CopyNamespaceNodeToOutput".equals(id)) {
1328
// http://issues.apache.org/jira/browse/XALANJ-1959
1331
else if ("Namespace-alias_Namespace-Alias_WithinRTF".equals(id)) {
1333
// http://issues.apache.org/jira/browse/XALANJ-1960
1336
else if ("Completeness__84361".equals(id)
1337
|| "Namespace-alias__91781".equals(id)
1338
|| "Namespace-alias__91782".equals(id)
1339
|| "Namespace-alias_Namespace-Alias_Test1".equals(id)
1340
|| "Namespace-alias_Namespace-Alias_Test2".equals(id)
1342
// a recoverable error Xalan doesn't recover from
1345
else if ("Output__84008".equals(id)) {
1346
// a recoverable error Xalan doesn't recover from
1349
else if ("XSLTFunctions_ElementAvailFunctionFalseTest".equals(id)) {
1351
// http://issues.apache.org/jira/browse/XALANJ-1961
1354
else if ("XSLTFunctions_GenereateIdAppliedToNamespaceNodesOnDifferentElements".equals(id)) {
1356
// http://issues.apache.org/jira/browse/XALANJ-1962
1359
else if ("XSLTFunctions__specialCharInPattern".equals(id)) {
1360
// a recoverable error Xalan doesn't recover from
1363
else if ("XSLTFunctions_DocumentFunctionWithAbsoluteArgument".equals(id)) {
1364
// test case bug; bad URL passed to document function
1367
else if ("BVTs_bvt052".equals(id) || "Keys_PerfRepro2".equals(id)) {
1368
// Requires a non-standard extension function
1371
else if ("BVTs_bvt044".equals(id)) {
1372
// a recoverable error Xalan doesn't recover from
1373
// http://issues.apache.org/jira/browse/XALANJ-1957
1376
else if ("BVTs_bvt039".equals(id)) {
1380
else if ("BVTs_bvt033".equals(id) || "BVTs_bvt034".equals(id)) {
1381
// Test suite bug; 2.0 is not unrecognized
1384
else if ("Text__78274".equals(id) || "Text__78276".equals(id)) {
1385
// Test suite bug; no xsl:preserve-space attribute
1388
else if ("XSLTFunctions__minimumValue".equals(id)
1389
|| "XSLTFunctions__minimalValue".equals(id)) {
1393
else if ("Errors_err073".equals(id)) {
1394
// Xalan bug: StackOverflowError
1397
else if ("Sorting_SortExprWithCurrentInsideForEach1".equals(id)) {
1399
// http://issues.apache.org/jira/browse/XALANJ-1970
1402
else if ("BVTs_bvt041".equals(id) || "BVTs_bvt063".equals(id)
1403
|| "BVTs_bvt070".equals(id)) {
1404
// Xalan bundled with JDK 1.4.2_05 does not recover
1405
// from this error involving multiple conflicting
1406
// xsl:output at same import precedence, though
1410
Document inputDoc = builder.build(input);
1412
if (strip) xform = new XSLTransform(styleDoc, stripper);
1413
else xform = new XSLTransform(styleDoc);
1414
Nodes result = xform.transform(inputDoc);
1415
if (output == null) {
1416
if ("Attributes__89463".equals(id)
1417
|| "Attributes__89465".equals(id)) {
1418
// Processors are allowed to recover from
1420
assertEquals(0, result.size());
1422
else if ("Attributes__89464".equals(id)) {
1423
// Processors are allowed to recover from
1425
assertEquals(0, ((Element) result.get(0)).getAttributeCount());
1427
else if ("Namespace-alias__91772".equals(id)
1428
|| "Namespace-alias__91774".equals(id)
1429
|| "Namespace-alias__91780".equals(id)
1430
|| "Namespace-alias__91790".equals(id)
1431
|| "Namespace-alias__91791".equals(id)
1432
|| "Sorting__84006".equals(id)
1433
|| "Sorting__91754".equals(id)
1435
// Processors are allowed to recover from
1439
else if (id.startsWith("Errors_")) {
1440
// Processors are allowed to recover from
1441
// most of these problems.
1443
else if (id.startsWith("FormatNumber")) {
1444
// Processors are allowed to recover from
1445
// most of these problems.
1447
else if ("BVTs_bvt074".equals(id)) {
1448
// Processors are allowed to recover from
1450
assertEquals(0, result.get(0).getChildCount());
1452
else if ("XSLTFunctions__currency".equals(id)
1453
|| "XSLTFunctions__mixingInvalids".equals(id)) {
1454
// Processors are allowed to recover from
1458
else if ("Attributes_Attribute_UseXmlnsNsAsNamespaceForAttribute".equals(id)
1459
|| "Attributes_Attribute_UseXmlnsAsNamespaceForAttributeImplicitly".equals(id)
1460
|| "Elements_Element_UseXslElementWithNameSpaceAttrEqualToXmlnsUri".equalsIgnoreCase(id)
1461
|| "Elements_Element_UseXslElementWithNameSpaceEqualToXmlnsUri".equalsIgnoreCase(id)
1463
// test follows namespace errata we don't accept
1465
else if ("AttributeSets_RefToUndefinedAttributeSet".equals(id)) {
1466
// I think the test case is wrong; I see
1467
// nothing in the spec that says this is
1470
else if ("Namespace__77665".equals(id)
1471
|| "Namespace__77675".equals(id)) {
1472
// I think the test case is wrong; I see
1473
// nothing in the spec that says this is
1475
// http://lists.oasis-open.org/archives/xslt-conformance-comment/200409/msg00007.html
1477
else if ("Variables__84633".equals(id)
1478
|| "Variables__84634".equals(id)
1479
|| "Variables__84697".equals(id)
1480
|| "Variables__84710".equals(id)
1482
// An error. See 11.4
1483
// but are processors allowed to recover?
1484
// Hmm according to section 17, the
1485
// processor must signal these errors
1486
// and may but need not recover from them.
1487
// Xalan recovers. Microsoft doesn't.
1489
else if ("Output__78176".equals(id)) {
1490
// I think the test case is wrong; I see
1491
// nothing in the spec that says this is
1494
else if (id.startsWith("XSLTFunctions__100")) {
1495
// I think these test cases are all wrong
1496
// except perhaps XSLTFunctions__10026; I
1497
// see nothing in the spec that says this
1498
// is an error. These are all about the
1499
// unparsed-entity-uri function.
1501
else if ("Namespace__78027".equals(id)) {
1502
// Test case is incorrect. This should
1503
// operate in forwards compatible mode.
1504
// Xalan gets this right.
1506
else if ("Output_Output_UseStandAloneAttributeWithMultipleRoots".equals(id)) {
1507
// Error only appears when document is serialized;
1510
else { // transform should have failed
1511
fail("Transformed " + style + "\n id: "
1512
+ testcase.getAttributeValue("id"));
1517
if ("Attributes_xsl_attribute_dup_attr_with_namespace_conflict".equals(id)
1518
|| "BVTs_bvt057".equals(id)) {
1519
// This test case requires namespace prefix rewriting,
1520
// so the output won't be exactly the same between processors
1523
else if ("Comment_DisableOutputEscaping_XslTextInXslComment".equals(id)) {
1524
// Test case output is wrong
1527
else if ("Output__77927".equals(id)
1528
|| "Output__77928".equals(id)
1529
|| "Output__84304".equals(id)
1530
|| "Output__84305".equals(id)
1531
|| "Output__84312".equals(id)
1532
|| "Output__84619".equals(id)
1533
|| "Output__84620".equals(id)
1534
|| "Output_EntityRefInAttribHtml".equals(id)
1536
// These test cases have incorrect line
1537
// breaks in the reference output.
1540
else if ("Output_Modified84433".equals(id)) {
1541
// This test case uses disable output escaping
1542
// so the results don't match up
1545
else if ("Sorting_Sort_SortTextWithNonTextCharacters".equals(id)) {
1546
// Xalan and MSXML don't sort non alphabetic characters
1547
// exactly the same, but that's legal
1550
else if ("Text_DoeWithCdataInText".equals(id)) {
1551
// Requires disable-output-escaping
1554
else if ("Whitespaces__91443".equals(id)
1555
|| "Whitespaces__91444".equals(id)) {
1557
// See http://issues.apache.org/jira/browse/XALANJ-1969
1560
else if ("AVTs__77591".equals(id)) {
1561
// test suite bug; doesn't escape tabs in output. See
1562
// http://lists.oasis-open.org/archives/xslt-conformance-comment/200409/msg00017.html
1564
else if ("Keys_MultipltKeysInclude".equals(id) ) {
1566
// http://issues.apache.org/jira/browse/XALANJ-1956
1568
/* else if ("Keys_PerfRepro3".equals(id) ) {
1569
// Suspected Xalan bug
1570
// http://issues.apache.org/jira/browse/XALANJ-1955
1572
else if ("Number__84683".equals(id)) {
1575
else if ("Number__84687".equals(id)) {
1578
else if ("Number__84692".equals(id)) {
1581
else if ("Number__84694".equals(id)) {
1582
// Test suite expects Roman number for zero
1583
// to be the empty string while Xalan uses 0
1585
else if ("Number__84699".equals(id)) {
1588
else if ("Number__84700".equals(id)) {
1589
// Xalan bug; extra whitespace. Possibly
1592
else if ("Number__84716".equals(id)) {
1593
// Xalan doesn't support Russian
1594
// number formatting
1596
else if ("Number__84717".equals(id)) {
1597
// Xalan supports more Japanese than the
1600
else if ("Number__84722".equals(id)
1601
|| "Number__84723".equals(id)
1602
|| "Number__84724".equals(id)
1603
|| "Number__84725".equals(id)
1605
// Acceptable locale support differences
1607
else if ("Number_NaNOrInvalidValue".equals(id)) {
1608
// Double bug! Test case is wrong and
1609
// Xalan gets this wrong!
1611
else if ("Number_ValueAsNodesetTest1".equals(id)
1612
|| "Number_ValueAsEmptyNodeset".equals(id)) {
1613
// Another double bug! Test case is wrong and
1614
// Xalan gets this wrong!
1616
else if (id.equals("XSLTFunctions_BooleanFunction")) {
1617
// I think the test case is wrong; or perhaps unspecified
1619
else if (id.equals("XSLTFunctions_TestIdFuncInComplexStruct")) {
1620
// I think the Xalan output white space is wrong;
1621
// http://issues.apache.org/jira/browse/XALANJ-1947
1623
else if (id.equals("XSLTFunctions__testOn-0.00")) {
1624
// Possible test suite bug. See
1625
// http://issues.apache.org/jira/browse/XALANJ-2226
1628
Document expectedResult;
1629
if (strip) expectedResult = strippingBuilder.build(output);
1630
else expectedResult = builder.build(output);
1631
Document actualResult = XSLTransform.toDocument(result);
1632
assertEquals("Mismatch with " + id,
1633
expectedResult, actualResult);
1636
catch (ParsingException ex) {
1637
// a few of the test cases generate
1638
// text or HTML output rather than
1639
// well-formed XML. For the moment, I
1643
catch (IllegalAddException ex) {
1644
// A few of the test cases generate
1645
// incomplete documents so we can't
1646
// compare output. Perhaps I could
1647
// wrap in an element, then get children
1648
// to build a Node object rather than a
1649
// Document???? i.e. a fragment parser?
1650
// Could use a SequenceInputStream to hack this
1655
catch (MalformedURIException ex) {
1658
catch (FileNotFoundException ex) {
1659
// The catalog doesn't always match what's on disk
1661
catch (UnknownHostException ex) {
1662
// A few tests like ProcessingInstruction__78197
1663
// point to external DTD subsets that can't be loaded
1665
catch (ParsingException ex) {
1666
// several stylesheets use relative namespace URIs XOM
1667
// does not support; skip the test
1668
if (ex.getCause() instanceof MalformedURIException) {
1672
String operation = scenario.getAttributeValue("operation");
1673
if (!"execution-error".equals(operation)) {
1674
if ("Namespace_XPath_PredefinedPrefix_XML".equals(id)) {
1675
// uses relative namespace URIs
1677
else if ("Sorting__78191".equals(id)
1678
|| "Text__78245".equals(id)
1679
|| "Text__78273".equals(id)
1680
|| "Text__78281".equals(id)
1682
// binds XML namespace to prefix other than xml
1685
System.err.println(id + ": " + ex.getMessage());
1690
catch (XSLException ex) {
1691
// If the output was null the transformation
1692
// was expected to fail
1693
if (output != null) {
1694
Throwable cause = ex.getCause();
1695
if ("Attributes__81487".equals(id)
1696
|| "Attributes__81551".equals(id)) {
1697
// spec inconsistency; see
1698
// http://lists.w3.org/Archives/Public/xsl-editors/2004JulSep/0003.html
1701
else if (cause instanceof MissingResourceException) {
1703
// http://issues.apache.org/jira/secure/ManageAttachments.jspa?id=27366
1705
else if ("Include_Include_IncludedStylesheetShouldHaveDifferentBaseUri".equals(id)) {
1706
// This test case is wrong; Uses backslash in URI
1708
else if ("Elements__89070".equals(id)) {
1709
// bug fixed in later versions of Xalan
1711
else if ("Namespace-alias_Namespace-Alias_NSAliasForDefaultWithExcludeResPref".equals(id)) {
1712
// This test case is wrong; it uses a backslash in a URI
1714
else if ("Variables_VariableWithinVariable".equals(id)) {
1715
// Xalan does not recover from this one
1717
else if ("BVTs_bvt054".equals(id)) {
1719
// http://issues.apache.org/jira/browse/XALANJ-1952
1722
else if ("BVTs_bvt094".equals(id)) {
1724
// http://issues.apache.org/jira/browse/XALANJ-1953
1727
else if ("Output__78177".equals(id)
1728
|| "Output__84009".equals(id)) {
1729
// Xalan does not recover from this error
1730
// which involves duplicate and possibly conflicting xsl:output elements
1733
else if ("Comment_Comment_CDATAWithSingleHyphen".equals(id)
1734
|| "Comment_Comment_DoubleHypenEntitywithDelCharacter".equals(id)
1735
|| "Comment_Comment_LineOfAllHyphens".equals(id)
1736
|| "Comment_Comment_SingleHyphenOnly".equals(id)
1737
|| "Comment_Comment_DoubleHyphenONLY".equals(id)) {
1738
// Begins comment data with hyphen, which XOM doesn't allow
1741
else if ("ProcessingInstruction_ValueOfandTextWithDoeInProcInstr".equals(id)) {
1742
// Begins processing instruction data with white space, which XOM doesn't allow
1745
else if ("Elements__89716".equals(id)
1746
|| "Elements__89717".equals(id)
1747
|| "Elements__89718".equals(id)
1748
|| "Output__84309".equals(id)
1749
|| "Namespace__77670".equals(id))
1751
// Xalan doesn't recover from these, though recovery is allowed
1754
else if ("Output__84306".equals(id)) {
1756
// http://issues.apache.org/jira/browse/XALANJ-1954
1759
else if ("Output__84014".equals(id)) {
1760
// Fixed in later version of Xalan than is bundled with JDK
1763
else if (cause instanceof MalformedURIException) {
1764
// Some of the tests generate relative namespace URIs
1765
// XOM doesn't support
1769
System.out.println(id + ": " + ex.getMessage());
1770
System.out.println("in " + style);
1771
if (cause != null) {
1772
System.out.println("cause: " + cause.getMessage());
1778
catch (XMLException ex) {
1779
if ("Text_modified78309".equals(id)) {
1780
// output is not a full document
1783
System.err.println(id);
1795
public void testKeysPerfRepro3()
1796
throws IOException, ParsingException, XSLException {
1798
Builder builder = new Builder();
1799
NodeFactory stripper = new StrippingFactory();
1800
File base = new File("data");
1801
base = new File(base, "oasis-xslt-testsuite");
1802
base = new File(base, "TESTS");
1804
File input = new File(base, "MSFT_CONFORMANCE_TESTS/KEYS/input.xml");
1805
File style = new File(base, "MSFT_CONFORMANCE_TESTS/KEYS/input.xsl");
1806
File output = new File(base, "MSFT_CONFORMANCE_TESTS/KEYS/out/PerfRepro3.txt");;
1808
Document styleDoc = builder.build(style);
1809
Document inputDoc = builder.build(input);
1810
XSLTransform xform = new XSLTransform(styleDoc);
1811
Nodes result = xform.transform(inputDoc);
1812
Document expectedResult = builder.build(output);
1813
Document actualResult = XSLTransform.toDocument(result);
1814
assertEquals(expectedResult, actualResult);
1819
public void testSorting__89749()
1820
throws IOException, ParsingException, XSLException {
1822
Builder builder = new Builder();
1823
NodeFactory stripper = new StrippingFactory();
1824
File base = new File("data");
1825
base = new File(base, "oasis-xslt-testsuite");
1826
base = new File(base, "TESTS");
1828
File input = new File(base, "MSFT_CONFORMANCE_TESTS/Sorting/sorttest.xml");
1829
File style = new File(base, "MSFT_CONFORMANCE_TESTS/Sorting/2_5_13_repeat.xsl");
1830
File output = new File(base, "MSFT_CONFORMANCE_TESTS/Sorting/out/89749.txt");;
1832
Document styleDoc = builder.build(style);
1833
Document inputDoc = builder.build(input);
1834
XSLTransform xform = new XSLTransform(styleDoc);
1835
Nodes result = xform.transform(inputDoc);
1836
/*Document expectedResult = builder.build(output);
1837
Document actualResult = XSLTransform.toDocument(result);
1838
assertEquals(expectedResult, actualResult); */
1843
public void testToDocumentWithEmptyNodes() {
1846
XSLTransform.toDocument(new Nodes());
1847
fail("Converted empty nodes to document");
1849
catch (XMLException success) {
1850
assertNotNull(success.getMessage());
1856
public void testToDocumentWithNoRoot() {
1858
Nodes input = new Nodes();
1859
input.append(new Comment("data"));
1861
XSLTransform.toDocument(new Nodes());
1862
fail("Converted comment to document");
1864
catch (XMLException success) {
1865
assertNotNull(success.getMessage());
1871
public void testToDocumentWithText() {
1873
Nodes input = new Nodes();
1874
Element root = new Element("root");
1875
Comment comment = new Comment("data");
1876
ProcessingInstruction pi = new ProcessingInstruction("target", "data");
1877
input.append(comment);
1880
input.append(new Text("text"));
1882
XSLTransform.toDocument(new Nodes());
1883
fail("Converted text to document");
1885
catch (XMLException success) {
1886
assertNotNull(success.getMessage());
1892
public void testToDocumentWithAttribute() {
1894
Nodes input = new Nodes();
1895
Element root = new Element("root");
1896
Comment comment = new Comment("data");
1897
ProcessingInstruction pi = new ProcessingInstruction("target", "data");
1898
input.append(comment);
1901
input.append(new Attribute("name", "text"));
1903
XSLTransform.toDocument(new Nodes());
1904
fail("Converted text to document");
1906
catch (XMLException success) {
1907
assertNotNull(success.getMessage());
1913
public void testToDocumentWithDocType() {
1915
Nodes input = new Nodes();
1916
Element root = new Element("root");
1917
DocType doctype = new DocType("root");
1918
Comment comment = new Comment("data");
1919
ProcessingInstruction pi = new ProcessingInstruction("target", "data");
1920
input.append(comment);
1921
input.append(doctype);
1924
Document output = XSLTransform.toDocument(input);
1925
assertEquals(root, output.getRootElement());
1926
assertEquals(comment, output.getChild(0));
1927
assertEquals(doctype, output.getChild(1));
1928
assertEquals(pi, output.getChild(3));
1929
assertEquals(input.size(), output.getChildCount());
1934
public void testToDocumentWithDocTypeInEpilog() {
1936
Nodes input = new Nodes();
1937
Element root = new Element("root");
1938
DocType doctype = new DocType("root");
1939
Comment comment = new Comment("data");
1940
ProcessingInstruction pi = new ProcessingInstruction("target", "data");
1941
input.append(comment);
1943
input.append(doctype);
1946
XSLTransform.toDocument(input);
1947
fail("Allowed doctype in epilog");
1949
catch (XMLException success) {
1950
assertNotNull(success.getMessage());
1956
public void testToDocumentWithDoubleRoot() {
1958
Nodes input = new Nodes();
1959
Element root = new Element("root");
1960
Comment comment = new Comment("data");
1961
input.append(comment);
1963
input.append(new Element("root2"));
1965
XSLTransform.toDocument(input);
1966
fail("Allowed two root elements");
1968
catch (XMLException success) {
1969
assertNotNull(success.getMessage());
1975
public void testToDocumentWithSingleRoot() {
1977
Nodes input = new Nodes();
1978
Element root = new Element("root");
1980
Document output = XSLTransform.toDocument(input);
1981
assertEquals(root, output.getRootElement());
1982
assertEquals(input.size(), output.getChildCount());
1987
public void testToDocumentWithPrologAndEpilog() {
1989
Nodes input = new Nodes();
1990
Element root = new Element("root");
1991
Comment comment = new Comment("data");
1992
ProcessingInstruction pi = new ProcessingInstruction("target", "data");
1993
input.append(comment);
1996
Document output = XSLTransform.toDocument(input);
1997
assertEquals(root, output.getRootElement());
1998
assertEquals(comment, output.getChild(0));
1999
assertEquals(pi, output.getChild(2));
2000
assertEquals(input.size(), output.getChildCount());