KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > tests > XSLTransformTest


1 /* Copyright 2002-2004 Elliotte Rusty Harold
2    
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.
6    
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.
11    
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
16    
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/
20 */

21 package nu.xom.tests;
22
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.PrintStream JavaDoc;
30 import java.net.UnknownHostException JavaDoc;
31 import java.util.MissingResourceException JavaDoc;
32
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.Node;
43 import nu.xom.NodeFactory;
44 import nu.xom.Nodes;
45 import nu.xom.ParentNode;
46 import nu.xom.ParsingException;
47 import nu.xom.ProcessingInstruction;
48 import nu.xom.Serializer;
49 import nu.xom.Text;
50 import nu.xom.XMLException;
51 import nu.xom.xslt.XSLException;
52 import nu.xom.xslt.XSLTransform;
53
54 /**
55  * <p>
56  * Unit tests for the XSLT engine.
57  * </p>
58  *
59  * <p>
60  * Many of the tests in this suite use an identity transformation.
61  * This is often done to make sure I get a particular content into
62  * the output tree in order to test the XSLTHandler.
63  * </p>
64  *
65  * @author Elliotte Rusty Harold
66  * @version 1.0
67  *
68  */

69 public class XSLTransformTest extends XOMTestCase {
70
71     
72     public XSLTransformTest(String JavaDoc name) {
73         super(name);
74     }
75
76     
77     // not a literal result element as stylesheet
78
// because it's missing the xsl:version attribute
79
private String JavaDoc notAStyleSheet =
80      "<?xml-stylesheet HREF=\"file.css\" type=\"text/css\"?>"
81      + "<!-- test -->"
82      + "<test xmlns:xlink='http://www.w3.org/TR/1999/xlink'>Hello dear"
83      + "\r\n<em id=\"p1\" xmlns:none=\"http://www.example.com\">"
84      + "very important</em>"
85      + "<span xlink:type='simple'>here&apos;s the link</span>\r\n"
86      + "<svg:svg xmlns:svg='http://www.w3.org/TR/2000/svg'>"
87      + "<svg:text>text in a namespace</svg:text></svg:svg>\r\n"
88      + "<svg xmlns='http://www.w3.org/TR/2000/svg'>"
89      + "<text>text in a namespace</text></svg>"
90      + "</test>\r\n"
91      + "<!--epilog-->";
92     
93     
94     // This class tests a lot error conditions, which
95
// Xalan annoyingly logs to System.err. This hides System.err
96
// before each test and restores it after each test.
97
private PrintStream JavaDoc systemErr = System.err;
98     
99     private File JavaDoc inputDir;
100     
101     protected void setUp() {
102         
103         System.setErr(new PrintStream JavaDoc(new ByteArrayOutputStream JavaDoc()));
104         
105         inputDir = new File JavaDoc("data");
106         inputDir = new File JavaDoc(inputDir, "xslt");
107         inputDir = new File JavaDoc(inputDir, "input");
108         
109     }
110     
111     
112     protected void tearDown() {
113         System.setErr(systemErr);
114     }
115     
116     // primarily this makes sure the XSLTHandler can handle various
117
// edge cases
118
public void testIdentityTransform()
119       throws ParsingException, IOException JavaDoc, XSLException {
120         
121         
122         File JavaDoc stylesheet = new File JavaDoc(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));
134         
135     }
136
137     
138     public void testPrefixMappingIssues()
139       throws XSLException, ParsingException, IOException JavaDoc {
140         
141         String JavaDoc doc = "<test>"
142            + "<span xmlns:a='http://www.example.com'/>"
143            + "<span xmlns:b='http://www.example.net'/>"
144            + "</test>";
145         File JavaDoc stylesheet = new File JavaDoc(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));
152         
153     }
154     
155     
156     public void testDocumentConstructor()
157       throws ParsingException, IOException JavaDoc {
158         
159         try {
160             Builder builder = new Builder();
161             Document doc = builder.build(notAStyleSheet,
162               "http://www.example.com");
163             new XSLTransform(doc);
164             fail("Compiled non-stylesheet");
165         }
166         catch (XSLException success) {
167             assertNotNull(success.getMessage());
168         }
169         
170     }
171
172
173     public void testLiteralResultElementUsedAsStylesheet()
174       throws ParsingException, IOException JavaDoc, XSLException {
175
176         String JavaDoc literalResultElementAsStylesheet =
177         "<html xsl:version='1.0'\n"
178         + " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'\n"
179         + " xmlns='http://www.w3.org/TR/xhtml1/strict'>\n"
180         + " <head>\n"
181         + " <title>Expense Report Summary</title>\n"
182         + " </head>\n"
183         + " <body>\n"
184         + " <p>Total Amount: <xsl:value-of select='expense-report/total'/></p>\n"
185         + " </body>\n"
186         + "</html>\n";
187     
188         Builder builder = new Builder();
189         Document stylesheet = builder.build(literalResultElementAsStylesheet,
190           "http://www.example.com");
191         XSLTransform transform = new XSLTransform(stylesheet);
192         Document doc = builder.build(notAStyleSheet,
193               "http://www.example.com");
194         Nodes result = transform.transform(doc);
195         Element root = (Element) (result.get(0));
196         assertEquals("html", root.getQualifiedName());
197         assertEquals(2, root.getChildCount());
198         
199     }
200
201
202 /* <xsl:template match="/">
203     <element1>some data and <content/> for a test</element1>
204     <element2>Remember, the XSLT processor is going to strip out the literal white space</element2>
205     <element3>some data and <content/> for a <!--test--></element3>
206     <element4/>
207     <xsl:comment>test</xsl:comment>
208     <xsl:processing-instruction name="test">PIs are not treated as literals in XSLT?</xsl:processing-instruction>
209   </xsl:template> */

210   
211     public void testCreateDocumentFragment()
212       throws ParsingException, IOException JavaDoc, XSLException {
213         
214         Element element1 = new Element("element1");
215         element1.appendChild("some data and ");
216         element1.appendChild(new Element("content"));
217         element1.appendChild(" for a test");
218         
219         Element element2 = new Element("element2");
220         element2.appendChild(
221           "Remember, the XSLT processor is going to strip out the literal white space"
222         );
223         File JavaDoc doc = new File JavaDoc(inputDir, "8-14.xml");
224         File JavaDoc stylesheet = new File JavaDoc(inputDir, "fragment.xsl");
225         Builder builder = new Builder();
226         Document stylesheetDoc = builder.build(stylesheet);
227         XSLTransform xform = new XSLTransform(stylesheetDoc);
228         Document input = builder.build(doc);
229         Nodes output = xform.transform(input);
230         assertEquals(6, output.size());
231         assertEquals(element1, output.get(0));
232         assertEquals(element2, output.get(1));
233         assertEquals(new Element("element4"), output.get(3));
234         assertEquals(new Comment("test"), output.get(4));
235         assertEquals(new ProcessingInstruction("test",
236           "PIs are not treated as literals in XSLT?"), output.get(5));
237         
238     }
239
240     
241     public void testTransform()
242       throws ParsingException, IOException JavaDoc, XSLException {
243         
244         File JavaDoc doc = new File JavaDoc(inputDir, "8-1.xml");
245         File JavaDoc stylesheet = new File JavaDoc(inputDir, "8-8.xsl");
246         Builder builder = new Builder();
247         Document stylesheetDoc = builder.build(stylesheet);
248         XSLTransform xform = new XSLTransform(stylesheetDoc);
249         Nodes output = xform.transform(builder.build(doc));
250         assertEquals(1, output.size());
251         Document result = new Document((Element) (output.get(0)));
252
253         Document expected = builder.build("data/xslt/output/8-8.xml");
254         assertEquals(expected, result);
255         
256     }
257
258     
259     public void testTransformWithCFilter()
260       throws ParsingException, IOException JavaDoc, XSLException {
261         
262         File JavaDoc doc = new File JavaDoc(inputDir, "8-1.xml");
263         File JavaDoc stylesheet = new File JavaDoc(inputDir, "8-8.xsl");
264         Builder builder = new Builder();
265         Document stylesheetDoc = builder.build(stylesheet);
266         XSLTransform xform = new XSLTransform(
267           stylesheetDoc, new NodeFactoryTest.CFactory());
268         
269         Nodes output = xform.transform(builder.build(doc));
270         assertEquals(1, output.size());
271         Document result = new Document((Element) (output.get(0)));
272
273         Document expected = builder.build("data/xslt/output/8-8c.xml");
274         assertEquals(expected, result);
275         
276     }
277
278     
279     public void testCreateDocumentFragmentWithCommentFilter()
280       throws ParsingException, IOException JavaDoc, XSLException {
281         
282         Element element1 = new Element("element1");
283         element1.appendChild("some data and ");
284         element1.appendChild(new Element("content"));
285         element1.appendChild(" for a test");
286         
287         Element element2 = new Element("element2");
288         element2.appendChild(
289           "Remember, the XSLT processor is going to strip out the literal white space"
290         );
291         File JavaDoc doc = new File JavaDoc(inputDir, "8-14.xml");
292         File JavaDoc stylesheet = new File JavaDoc(inputDir, "fragment.xsl");
293         Builder builder = new Builder();
294         Document stylesheetDoc = builder.build(stylesheet);
295         XSLTransform xform = new XSLTransform(
296           stylesheetDoc, new NodeFactoryTest.CommentFilter());
297         
298         Document input = builder.build(doc);
299         Nodes output = xform.transform(input);
300         assertEquals(5, output.size());
301         assertEquals(element1, output.get(0));
302         assertEquals(element2, output.get(1));
303         assertEquals(new Element("element4"), output.get(3));
304         assertEquals(new ProcessingInstruction("test",
305           "PIs are not treated as literals in XSLT?"), output.get(4));
306         
307     }
308     
309     
310     public void testCreateDocumentFragmentWithProcessingInstructionFilter()
311       throws ParsingException, IOException JavaDoc, XSLException {
312         
313         Element element1 = new Element("element1");
314         element1.appendChild("some data and ");
315         element1.appendChild(new Element("content"));
316         element1.appendChild(" for a test");
317         
318         Element element2 = new Element("element2");
319         element2.appendChild(
320           "Remember, the XSLT processor is going to strip out the literal white space"
321         );
322         File JavaDoc doc = new File JavaDoc(inputDir, "8-14.xml");
323         File JavaDoc stylesheet = new File JavaDoc(inputDir, "fragment.xsl");
324         Builder builder = new Builder();
325         Document stylesheetDoc = builder.build(stylesheet);
326         XSLTransform xform = new XSLTransform(stylesheetDoc,
327           new NodeFactoryTest.ProcessingInstructionFilter());
328         
329         Document input = builder.build(doc);
330         Nodes output = xform.transform(input);
331         assertEquals(5, output.size());
332         assertEquals(element1, output.get(0));
333         assertEquals(element2, output.get(1));
334         assertEquals(new Element("element4"), output.get(3));
335         assertEquals(new Comment("test"), output.get(4));
336         
337     }
338     
339     
340     public void testCreateDocumentFragmentWithUncommentFilter()
341       throws ParsingException, IOException JavaDoc, XSLException {
342         
343         Element element1 = new Element("element1");
344         element1.appendChild("some data and ");
345         element1.appendChild(new Element("content"));
346         element1.appendChild(" for a test");
347         
348         Element element2 = new Element("element2");
349         element2.appendChild(
350           "Remember, the XSLT processor is going to strip out the literal white space"
351         );
352         File JavaDoc doc = new File JavaDoc(inputDir, "8-14.xml");
353         File JavaDoc stylesheet = new File JavaDoc(inputDir, "fragment.xsl");
354         Builder builder = new Builder();
355         Document stylesheetDoc = builder.build(stylesheet);
356         XSLTransform xform = new XSLTransform(stylesheetDoc,
357           new NodeFactoryTest.UncommentFilter());
358         
359         Document input = builder.build(doc);
360         Nodes output = xform.transform(input);
361         assertEquals(6, output.size());
362         assertEquals(element1, output.get(0));
363         assertEquals(element2, output.get(1));
364         assertEquals(new Element("element4"), output.get(3));
365         assertEquals(new Text("test"), output.get(4));
366         assertEquals(new ProcessingInstruction("test",
367           "PIs are not treated as literals in XSLT?"), output.get(5));
368         
369     }
370     
371     
372     public void testTransform2()
373       throws ParsingException, IOException JavaDoc, XSLException {
374         
375         File JavaDoc doc = new File JavaDoc(inputDir, "8-1.xml");
376         File JavaDoc stylesheet = new File JavaDoc(inputDir, "8-12.xsl");
377         Builder builder = new Builder();
378         Document stylesheetDoc = builder.build(stylesheet);
379         XSLTransform xform = new XSLTransform(stylesheetDoc);
380         Nodes output = xform.transform(builder.build(doc));
381         assertEquals(1, output.size());
382         Document result = new Document((Element) (output.get(0)));
383
384         Document expected = builder.build("data/xslt/output/8-12.xml");
385         assertEquals(expected, result);
386         
387     }
388
389     
390     
391     // For debugging
392
private static void dumpResult(Document result, String JavaDoc filename)
393       throws IOException JavaDoc {
394         
395         File JavaDoc debug = new File JavaDoc("data");
396         debug = new File JavaDoc(debug, "xslt");
397         debug = new File JavaDoc(debug, "debug/" + filename);
398         OutputStream JavaDoc out = new FileOutputStream JavaDoc(debug);
399         Serializer serializer = new Serializer(out);
400         serializer.write(result);
401         serializer.flush();
402         out.close();
403         
404     }
405
406     
407     public void testTransformFromDocument()
408       throws ParsingException, IOException JavaDoc, XSLException {
409         
410         File JavaDoc doc = new File JavaDoc(inputDir, "8-1.xml");
411         Builder builder = new Builder();
412         Document stylesheet = builder.build("data/xslt/input/8-12.xsl");
413         XSLTransform xform = new XSLTransform(stylesheet);
414         Nodes output = xform.transform(builder.build(doc));
415         assertEquals(1, output.size());
416         Document result = new Document((Element) (output.get(0)));
417
418         Document expected = builder.build("data/xslt/output/8-12.xml");
419         assertEquals(expected, result);
420         
421     }
422
423     
424     public void testTransformFromSystemID()
425       throws ParsingException, IOException JavaDoc, XSLException {
426         
427         File JavaDoc doc = new File JavaDoc(inputDir, "8-1.xml");
428         Builder builder = new Builder();
429         String JavaDoc stylesheet = "data/xslt/input/8-12.xsl";
430         Document stylesheetDoc = builder.build(stylesheet);
431         XSLTransform xform = new XSLTransform(stylesheetDoc);
432         Nodes output = xform.transform(builder.build(doc));
433         assertEquals(1, output.size());
434         Document result = new Document((Element) (output.get(0)));
435
436         Document expected = builder.build("data/xslt/output/8-12.xml");
437         assertEquals(expected, result);
438         
439     }
440
441
442     public void testTransformWithNamespaces()
443       throws ParsingException, IOException JavaDoc, XSLException {
444         
445         File JavaDoc doc = new File JavaDoc(inputDir, "8-14.xml");
446         File JavaDoc stylesheet = new File JavaDoc(inputDir, "8-15.xsl");
447         Builder builder = new Builder();
448         Document stylesheetDoc = builder.build(stylesheet);
449         XSLTransform xform = new XSLTransform(stylesheetDoc);
450         Document input = builder.build(doc);
451         Nodes output = xform.transform(input);
452         assertEquals(1, output.size());
453         Document result = new Document((Element) (output.get(0)));
454
455         Document expected = builder.build("data/xslt/output/8-15.xml");
456         assertEquals(expected, result);
457         
458     }
459
460     
461     public void testSingleTextNode()
462       throws ParsingException, IOException JavaDoc, XSLException {
463         
464         File JavaDoc doc = new File JavaDoc(inputDir, "8-14.xml");
465         File JavaDoc stylesheet = new File JavaDoc(inputDir, "singlestring.xsl");
466         Builder builder = new Builder();
467         Document stylesheetDoc = builder.build(stylesheet);
468         XSLTransform xform = new XSLTransform(stylesheetDoc);
469         Document input = builder.build(doc);
470         Nodes output = xform.transform(input);
471         assertEquals(1, output.size());
472         Text data = (Text) (output.get(0));
473         assertEquals("Data", data.getValue());
474         
475     }
476     
477     
478     public void testToString()
479       throws XSLException, ParsingException, IOException JavaDoc {
480         
481         File JavaDoc stylesheet = new File JavaDoc(inputDir, "singlestring.xsl");
482         Builder builder = new Builder();
483         Document stylesheetDoc = builder.build(stylesheet);
484         XSLTransform xform = new XSLTransform(stylesheetDoc);
485         assertTrue(xform.toString().startsWith("[nu.xom.xslt.XSLTransform: "));
486         
487     }
488     
489
490     // Make sure that method="text" doesn't affect what we get
491
// since this is not a serialized transform
492
// See http://nagoya.apache.org/bugzilla/show_bug.cgi?id=30197
493
public void testTextMethod()
494       throws ParsingException, IOException JavaDoc, XSLException {
495         
496         File JavaDoc doc = new File JavaDoc(inputDir, "8-14.xml");
497         File JavaDoc stylesheet = new File JavaDoc(inputDir, "textmethod.xsl");
498         Builder builder = new Builder();
499         Document stylesheetDoc = builder.build(stylesheet);
500         XSLTransform xform = new XSLTransform(stylesheetDoc);
501         Document input = builder.build(doc);
502         Nodes output = xform.transform(input);
503         assertEquals(6, output.size());
504         assertEquals("12345", output.get(0).getValue());
505         assertEquals("67890", output.get(1).getValue());
506         assertEquals("", output.get(2).getValue());
507         assertEquals("0987654321", output.get(3).getValue());
508         assertTrue(output.get(4) instanceof Comment);
509         assertTrue(output.get(5) instanceof ProcessingInstruction);
510         
511     }
512
513     
514     public void testCommentWithParent()
515       throws XSLException, ParsingException, IOException JavaDoc {
516         
517         Builder builder = new Builder();
518         File JavaDoc stylesheet = new File JavaDoc(inputDir, "commentwithparent.xsl");
519         Document stylesheetDoc = builder.build(stylesheet);
520         XSLTransform xform = new XSLTransform(stylesheetDoc);
521         Document input = new Document(new Element("root"));
522         Nodes output = xform.transform(input);
523         assertEquals(1, output.size());
524         assertEquals("", output.get(0).getValue());
525         Element root = (Element) output.get(0);
526         assertEquals(1, root.getChildCount());
527         Comment child = (Comment) root.getChild(0);
528         assertEquals("test", child.getValue());
529         
530     }
531
532     
533     public void testProcessingInstructionWithParent()
534       throws XSLException, ParsingException, IOException JavaDoc {
535         
536         Builder builder = new Builder();
537         File JavaDoc stylesheet = new File JavaDoc(inputDir, "piwithparent.xsl");
538         Document stylesheetDoc = builder.build(stylesheet);
539         XSLTransform xform = new XSLTransform(stylesheetDoc);
540         Document input = new Document(new Element("root"));
541         Nodes output = xform.transform(input);
542         assertEquals(1, output.size());
543         assertEquals("", output.get(0).getValue());
544         Element root = (Element) output.get(0);
545         assertEquals(1, root.getChildCount());
546         ProcessingInstruction child = (ProcessingInstruction) root.getChild(0);
547         assertEquals("target", child.getTarget());
548         assertEquals("test", child.getValue());
549         
550     }
551
552     
553     public void testTransformNodes()
554       throws XSLException, ParsingException, IOException JavaDoc {
555         
556         File JavaDoc stylesheet = new File JavaDoc(inputDir, "piwithparent.xsl");
557         Builder builder = new Builder();
558         Nodes input = new Nodes(new Element("root"));
559         Document stylesheetDoc = builder.build(stylesheet);
560         XSLTransform xform = new XSLTransform(stylesheetDoc);
561         Nodes output = xform.transform(input);
562         assertEquals(1, output.size());
563         assertEquals("", output.get(0).getValue());
564         Element root = (Element) output.get(0);
565         assertEquals(1, root.getChildCount());
566         ProcessingInstruction child = (ProcessingInstruction) root.getChild(0);
567         assertEquals("target", child.getTarget());
568         assertEquals("test", child.getValue());
569         
570     }
571     
572     
573     public void testTriple()
574       throws IOException JavaDoc, ParsingException, XSLException {
575         
576         File JavaDoc stylesheet = new File JavaDoc(inputDir, "identity.xsl");
577         Builder builder = new Builder();
578         Document stylesheetDoc = builder.build(stylesheet);
579         XSLTransform xform = new XSLTransform(stylesheetDoc,
580           new NodeFactoryTest.TripleElementFilter());
581
582         String JavaDoc data = "<a><b><c/></b></a>";
583         Document doc = builder.build(data, "http://www.example.org/");
584         
585         Nodes result = xform.transform(doc);
586         
587         assertEquals(3, result.size());
588         assertEquals(result.get(0), result.get(1));
589         assertEquals(result.get(1), result.get(2));
590         Element a = (Element) result.get(2);
591         assertEquals("a", a.getLocalName());
592         assertEquals(3, a.getChildCount());
593         assertEquals(0, a.getAttributeCount());
594         Element b = (Element) a.getChild(1);
595         assertEquals(3, b.getChildCount());
596         assertEquals("b", b.getLocalName());
597         
598     }
599    
600     
601     public void testPassingNullSetsDefaultFactory()
602       throws IOException JavaDoc, ParsingException, XSLException {
603         
604         File JavaDoc stylesheet = new File JavaDoc(inputDir, "identity.xsl");
605         Builder builder = new Builder();
606         Document stylesheetDoc = builder.build(stylesheet);
607         XSLTransform xform = new XSLTransform(stylesheetDoc, null);
608
609         String JavaDoc data = "<a><b><c/></b></a>";
610         Document doc = builder.build(data, "http://www.example.org/");
611       
612         Nodes result = xform.transform(doc);
613         
614         assertEquals(1, result.size());
615         Element a = (Element) result.get(0);
616         assertEquals("a", a.getLocalName());
617         assertEquals(1, a.getChildCount());
618         assertEquals(0, a.getAttributeCount());
619         Element b = (Element) a.getChild(0);
620         assertEquals(1, b.getChildCount());
621         assertEquals("b", b.getLocalName());
622         
623     }
624     
625     
626     public void testTransformEmptyNodesList()
627       throws IOException JavaDoc, ParsingException, XSLException {
628         
629         File JavaDoc stylesheet = new File JavaDoc(inputDir, "identity.xsl");
630         Builder builder = new Builder();
631         Document stylesheetDoc = builder.build(stylesheet);
632         XSLTransform xform = new XSLTransform(stylesheetDoc);
633        
634         Nodes result = xform.transform(new Nodes());
635         
636         assertEquals(0, result.size());
637         
638     }
639     
640     
641     public void testMinimizingFactory()
642       throws XSLException, ParsingException, IOException JavaDoc {
643         
644         File JavaDoc stylesheet = new File JavaDoc(inputDir, "identity.xsl");
645         Builder builder = new Builder();
646         Document stylesheetDoc = builder.build(stylesheet);
647         XSLTransform xform = new XSLTransform(stylesheetDoc,
648           new NodeFactoryTest.MinimizingFactory());
649         
650         Document input = builder.build("<!-- test--><test>" +
651                 "<em>data</em>\r\n<span>test</span></test>" +
652                 "<?target data?>", "http://example.org/");
653         Nodes output = xform.transform(input);
654         assertEquals(0, output.size());
655         
656     }
657     
658     
659     public void testIllegalTransform()
660       throws XSLException, ParsingException, IOException JavaDoc {
661         
662         File JavaDoc stylesheet = new File JavaDoc(inputDir, "illegaltransform.xsl");
663         Builder builder = new Builder();
664         Document stylesheetDoc = builder.build(stylesheet);
665         XSLTransform xform = new XSLTransform(stylesheetDoc);
666         Element root = new Element("root", "http://www.example.org");
667         Document input = new Document(root);
668         try {
669             xform.transform(input);
670             fail("Allowed illegal transform");
671         }
672         catch (XSLException ex) {
673             assertNotNull(ex.getMessage());
674         }
675         
676     }
677
678     
679     public void testRemapPrefixToSameURI()
680       throws IOException JavaDoc, ParsingException, XSLException {
681         
682         File JavaDoc stylesheet = new File JavaDoc(inputDir, "identity.xsl");
683         Builder builder = new Builder();
684         Document stylesheetDoc = builder.build(stylesheet);
685         XSLTransform xform = new XSLTransform(stylesheetDoc);
686
687         String JavaDoc data = "<a xmlns:pre='http://www.example.org/'>" +
688                 "<b xmlns:pre='http://www.example.org/'>in B</b></a>";
689         Document doc = builder.build(data, "http://www.example.org/");
690         
691         Nodes result = xform.transform(doc);
692         
693         assertEquals(doc.getRootElement(), result.get(0));
694         
695     }
696  
697     
698     public void testElementsToAttributes()
699       throws IOException JavaDoc, ParsingException, XSLException {
700         
701         File JavaDoc stylesheet = new File JavaDoc(inputDir, "identity.xsl");
702         Builder builder = new Builder();
703         Document stylesheetDoc = builder.build(stylesheet);
704         XSLTransform xform = new XSLTransform(stylesheetDoc,
705           new AttributeFactory());
706
707         String JavaDoc data = "<a><b>in B<c>in C</c></b></a>";
708         Document doc = builder.build(data, "http://www.example.org/");
709         
710         Nodes result = xform.transform(doc);
711         
712         assertEquals(1, result.size());
713         Element a = (Element) result.get(0);
714         assertEquals("a", a.getLocalName());
715         assertEquals(0, a.getChildCount());
716         assertEquals(1, a.getAttributeCount());
717         assertEquals("in B", a.getAttribute("b").getValue());
718         
719     }
720  
721     
722     private static class AttributeFactory extends NodeFactory {
723
724         public Nodes finishMakingElement(Element element) {
725             ParentNode parent = element.getParent();
726             if (parent == null || parent instanceof Document) {
727                 return new Nodes(element);
728             }
729             return new Nodes(new Attribute(element.getQualifiedName(),
730                     element.getNamespaceURI(), element.getValue()));
731         }
732         
733     }
734     
735
736     public void testAttributesToElements()
737       throws IOException JavaDoc, ParsingException, XSLException {
738         
739         File JavaDoc stylesheet = new File JavaDoc(inputDir, "identity.xsl");
740         Builder builder = new Builder();
741         Document stylesheetDoc = builder.build(stylesheet);
742         XSLTransform xform = new XSLTransform(stylesheetDoc,
743           new AttributesToElements());
744
745         String JavaDoc data = "<a name='value'><b x='y' a='b'/></a>";
746         Document doc = builder.build(data, "http://www.example.org/");
747         
748         Nodes result = xform.transform(doc);
749         
750         assertEquals(1, result.size());
751         Element a = (Element) result.get(0);
752         assertEquals("a", a.getLocalName());
753         assertEquals(2, a.getChildCount());
754         assertEquals(0, a.getAttributeCount());
755         Element name = (Element) a.getChild(0);
756         assertEquals("name", name.getLocalName());
757         assertEquals("value", name.getValue());
758         Element b = (Element) a.getChild(1);
759         assertEquals("b", b.getLocalName());
760         assertEquals(2, b.getChildCount());
761         assertEquals("y", b.getFirstChildElement("x").getValue());
762         assertEquals("b", b.getFirstChildElement("a").getValue());
763         
764     }
765  
766     
767     private static class AttributesToElements extends NodeFactory {
768
769         public Nodes makeAttribute(String JavaDoc name, String JavaDoc URI,
770           String JavaDoc value, Attribute.Type type) {
771             Element element = new Element(name, URI);
772             element.appendChild(value);
773             return new Nodes(element);
774         }
775         
776     }
777
778
779     public void testCommentsAreTransformed()
780       throws IOException JavaDoc, ParsingException, XSLException {
781         
782         File JavaDoc stylesheet = new File JavaDoc(inputDir, "identity.xsl");
783         Builder builder = new Builder();
784         Document stylesheetDoc = builder.build(stylesheet);
785         XSLTransform xform = new XSLTransform(stylesheetDoc);
786
787         String JavaDoc data = "<a><!--test--></a>";
788         Document doc = builder.build(data, "http://www.example.org/");
789         
790         Nodes result = xform.transform(doc);
791         
792         assertEquals(1, result.size());
793         Element a = (Element) result.get(0);
794         assertEquals("a", a.getLocalName());
795         assertEquals(1, a.getChildCount());
796         assertEquals(0, a.getAttributeCount());
797         Node child = a.getChild(0);
798         assertTrue(child instanceof Comment);
799         assertTrue(child.getValue().equals("test"));
800         
801     }
802     
803     
804     public void testCommentToAttribute()
805       throws IOException JavaDoc, ParsingException, XSLException {
806         
807         File JavaDoc stylesheet = new File JavaDoc(inputDir, "identity.xsl");
808         Builder builder = new Builder();
809         Document stylesheetDoc = builder.build(stylesheet);
810         XSLTransform xform = new XSLTransform(stylesheetDoc,
811           new NodeFactory() {
812             public Nodes makeComment(String JavaDoc text) {
813                 return new Nodes(new Attribute("comment", text));
814             }
815         });
816
817         String JavaDoc data = "<a><!--test--></a>";
818         Document doc = builder.build(data, "http://www.example.org/");
819         
820         Nodes result = xform.transform(doc);
821         
822         assertEquals(1, result.size());
823         Element a = (Element) result.get(0);
824         assertEquals("a", a.getLocalName());
825         assertEquals(0, a.getChildCount());
826         assertEquals(1, a.getAttributeCount());
827         Attribute comment = a.getAttribute(0);
828         assertEquals("comment", comment.getLocalName());
829         assertEquals("test", comment.getValue());
830         
831     }
832
833     
834     public void testAdditionalDefaultNamespace()
835       throws IOException JavaDoc, ParsingException, XSLException {
836         
837         File JavaDoc stylesheet = new File JavaDoc(inputDir, "identity.xsl");
838         Builder builder = new Builder();
839         Document stylesheetDoc = builder.build(stylesheet);
840         XSLTransform xform = new XSLTransform(stylesheetDoc);
841
842         String JavaDoc data = "<pre:a xmlns:pre='http://www.example.org' " +
843                 "xmlns='http://www.example.net'>data</pre:a>";
844         Document doc = builder.build(data, "http://www.example.org/");
845         
846         Nodes result = xform.transform(doc);
847         
848         assertEquals(1, result.size());
849         Element a = (Element) result.get(0);
850         assertEquals("a", a.getLocalName());
851         assertEquals("pre:a", a.getQualifiedName());
852         assertEquals("data", a.getValue());
853         assertEquals("http://www.example.org", a.getNamespaceURI("pre"));
854         assertEquals("http://www.example.net", a.getNamespaceURI(""));
855         assertEquals(2, a.getNamespaceDeclarationCount());
856         
857     }
858     
859     
860
861     private static boolean indentYes(Document styleDoc) {
862         
863         Element output = styleDoc
864           .getRootElement()
865           .getFirstChildElement("output",
866              "http://www.w3.org/1999/XSL/Transform");
867         if (output == null) return false;
868         
869         String JavaDoc indent = output.getAttributeValue("indent");
870         if ("yes".equals(indent)) {
871             return true;
872         }
873         else return false;
874         
875     }
876     
877     
878     private static class StrippingFactory extends NodeFactory {
879     
880         public Nodes makeText(String JavaDoc s) {
881             
882             String JavaDoc stripped = stripSpace(s);
883             if (stripped.length() == 0) return new Nodes();
884             Text result = new Text(stripped);
885             return new Nodes(result);
886         }
887         
888         public Nodes makeAttribute(String JavaDoc name, String JavaDoc URI,
889           String JavaDoc value, Attribute.Type type) {
890             return new Nodes(new Attribute(name, URI, stripSpace(value), type));
891         }
892         
893         private String JavaDoc stripSpace(String JavaDoc s) {
894             
895             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
896             for (int i = 0; i < s.length(); i++) {
897                 if (!Character.isWhitespace(s.charAt(i))) {
898                     sb.append(s.charAt(i));
899                 }
900             }
901             
902             return sb.toString();
903             
904         }
905         
906     }
907
908
909     public void testOASISXalanConformanceSuite()
910       throws IOException JavaDoc, ParsingException, XSLException {
911         
912         Builder builder = new Builder();
913         NodeFactory stripper = new StrippingFactory();
914         Builder strippingBuilder = new Builder(stripper);
915         
916         File JavaDoc base = new File JavaDoc("data");
917         base = new File JavaDoc(base, "oasis-xslt-testsuite");
918         base = new File JavaDoc(base, "TESTS");
919         base = new File JavaDoc("Xalan_Conformance_Tests");
920         File JavaDoc catalog = new File JavaDoc(base, "catalog.xml");
921         
922         // The test suite need to be installed separately. If we can't
923
// find the catalog, we just don't run these tests.
924
if (catalog.exists()) {
925             Document doc = builder.build(catalog);
926             Element testsuite = doc.getRootElement();
927             Elements submitters = testsuite.getChildElements("test-catalog");
928             for (int i = 0; i < submitters.size(); i++) {
929                 Element submitter = submitters.get(i);
930                 Elements testcases = submitter.getChildElements("test-case");
931                 for (int j = 0; j < testcases.size(); j++) {
932                     Element testcase = testcases.get(j);
933                     String JavaDoc id = testcase.getAttributeValue("id");
934                     if (id.startsWith("output_")) {
935                         // These test cases are mostly about producing
936
// HTML and plain text output that isn't
937
// relevant to XOM
938
continue;
939                     }
940                     File JavaDoc root = new File JavaDoc(base, testcase.getFirstChildElement("file-path").getValue());
941                     File JavaDoc input = null;
942                     File JavaDoc style = null;
943                     File JavaDoc output = null;
944                     Element scenario = testcase.getFirstChildElement("scenario");
945                     Elements inputs = scenario.getChildElements("input-file");
946                     for (int k = 0; k < inputs.size(); k++) {
947                         Element file = inputs.get(k);
948                         String JavaDoc role = file.getAttributeValue("role");
949                         if ("principal-data".equals(role)) {
950                             input = new File JavaDoc(root, file.getValue());
951                         }
952                         else if ("principal-stylesheet".equals(role)) {
953                             style = new File JavaDoc(root, file.getValue());
954                         }
955                     }
956                     Elements outputs = scenario.getChildElements("output-file");
957                     for (int k = 0; k < outputs.size(); k++) {
958                         Element file = outputs.get(k);
959                         String JavaDoc role = file.getAttributeValue("role");
960                         if ("principal".equals(role)) {
961                             // Fix up OASIS catalog bugs
962
File JavaDoc parent = new File JavaDoc(root.getParent());
963                             parent = new File JavaDoc(parent, "REF_OUT");
964                             parent = new File JavaDoc(parent, root.getName());
965                             String JavaDoc outputFileName = file.getValue();
966                             output = new File JavaDoc(parent, outputFileName);
967                         }
968                     }
969                     
970                     try {
971                         Document inputDoc = builder.build(input);
972                         Document styleDoc = builder.build(style);
973                         // If the transform specifies indent="yes".
974
// we remove all white space before comparing
975
XSLTransform xform;
976                         if (indentYes(styleDoc)) {
977                             xform = new XSLTransform(styleDoc, stripper);
978                         }
979                         else xform = new XSLTransform(styleDoc);
980                         Nodes result = xform.transform(inputDoc);
981                         if (output == null) {
982                             // transform should have failed
983
fail("Transformed " + id);
984                         }
985                         else {
986                             try {
987                                 Document expectedResult;
988                                 if (indentYes(styleDoc)) {
989                                     expectedResult = strippingBuilder.build(output);
990                                 }
991                                 else {
992                                     expectedResult = builder.build(output);
993                                 }
994                                 Document actualResult = XSLTransform.toDocument(result);
995                                 
996                                 if (id.equals("attribset_attribset40")) {
997                                     // This test does not necessarily
998
// produce an identical infoset due
999
// to necessary remapping of
1000
// namespace prefixes.
1001
continue;
1002                                }
1003                                else if (id.equals("axes_axes129")) {
1004                                    // Xalan bug. Fixed in more recent
1005
// version than bundled with the JDK 1.4.2_05
1006
}
1007                                else if (id.equals("copy_copy56")
1008                                  || id.equals("copy_copy58")
1009                                  || id.equals("copy_copy60")
1010                                  || id.equals("copy_copy59")) {
1011                                    // Xalan bug;
1012
// See http://nagoya.apache.org/jira/browse/XALANJ-1081
1013
// Also see erratum E27 to the XSLT spec.
1014
}
1015                                else if (id.equals("expression_expression02")) {
1016                                    // requires unparsed entities XOM doesn't support
1017
}
1018                                else if (id.equals("idkey_idkey31")) {
1019                                    // Known Xalan bug
1020
// See http://nagoya.apache.org/jira/browse/XALANJ-1325
1021
}
1022                                else if (id.equals("idkey_idkey61")
1023                                  || id.equals("idkey_idkey62")) {
1024                                    // Xalan bug. Fixed in more recent
1025
// version than bundled with the JDK 1.4.2_05
1026
// See http://nagoya.apache.org/jira/browse/XALANJ-1318
1027
}
1028                                else if (id.equals("impincl_impincl11")) {
1029                                    // Test case bug; reported 2004-09-18
1030
// See http://lists.oasis-open.org/archives/xslt-conformance-comment/200409/msg00001.html
1031
}
1032                                else if (id.equals("math_math110")
1033                                  || id.equals("math_math111")) {
1034                                    // Xalan bug. Fixed in more recent
1035
// version than bundled with the JDK 1.4.2_05
1036
// See http://nagoya.apache.org/jira/browse/XALANJ-1278
1037
}
1038                                else if (id.equals("position_position104")) {
1039                                    // Xalan bug. Fixed in more recent
1040
// version than bundled with the JDK 1.4.2_05
1041
}
1042                                else if (id.equals("position_position106")) {
1043                                    // Xalan bug. Fixed in more recent
1044
// version than bundled with the JDK 1.4.2_05
1045
}
1046                                else if (id.equals("position_position107")
1047                                  || id.equals("position_position109")) {
1048                                    // Xalan bug. Fixed in more recent
1049
// version than bundled with the JDK 1.4.2_05
1050
// See http://nagoya.apache.org/jira/browse/XALANJ-1289
1051
}
1052                                else {
1053                                    assertEquals("Problem with " + id,
1054                                      expectedResult, actualResult);
1055                                }
1056                            }
1057                            catch (ParsingException ex) {
1058                                // a few of the test cases generate
1059
// text or HTML output rather than
1060
// well-formed XML. For the moment, I
1061
// just skip these.
1062
continue;
1063                            }
1064                            catch (IllegalAddException ex) {
1065                                // A few of the test cases generate
1066
// incomplete documents so we can't
1067
// compare output. Perhaps I could
1068
// wrap in an element, then get children
1069
// to build a Nodes object rather than a
1070
// Document???? i.e. a fragment parser?
1071
// Could use a SequenceInputStream to hack this
1072
}
1073                        }
1074                        
1075                    }
1076                    catch (MalformedURIException ex) {
1077                        // Some of the test cases contain relative
1078
// namespace URIs XOM does not support
1079
}
1080                    catch (XSLException ex) {
1081                        // If the output was null the transformation
1082
// was expected to fail
1083
if (output != null) {
1084                            // a few of the test cases use relative namespace URIs
1085
// XOM doesn't support
1086
Throwable JavaDoc cause = ex.getCause();
1087                            if (cause instanceof MalformedURIException) {
1088                                continue;
1089                            }
1090                            
1091                            if ("axes_axes62".equals(id)) {
1092                                // Bug 12690
1093
// http://nagoya.apache.org/bugzilla/show_bug.cgi?id=12690
1094
continue;
1095                            }
1096                            else if ("impincl_impincl27".equals(id)) {
1097                                // Test case uses file: URI XOM doesn't support
1098
continue;
1099                            }
1100                            else if ("select_select85".equals(id)) {
1101                                // This has been fixed in Xalan 2.6.0.
1102
// However, it's a bug in earlier versions of Xalan
1103
// including the one bundled with the JDK 1.4.2_05
1104
continue;
1105                            }
1106                            else if ("numberformat_numberformat45".equals(id)
1107                              || "numberformat_numberformat46".equals(id)) {
1108                                // This has been fixed in Xalan 2.5.2.
1109
// However, it's a bug in earlier versions of Xalan
1110
// including the one bundled with the JDK 1.4.2_05
1111
// See http://nagoya.apache.org/jira/browse/XALANJ-805
1112
continue;
1113                            }
1114                            
1115                            System.err.println(id);
1116                            System.err.println(ex.getMessage());
1117                            throw ex;
1118                        }
1119                    }
1120                    
1121                }
1122            }
1123            
1124        }
1125     
1126    }
1127    
1128   
1129    public void testOASISMicrosoftConformanceSuite()
1130      throws IOException JavaDoc, ParsingException, XSLException {
1131        
1132        Builder builder = new Builder();
1133        NodeFactory stripper = new StrippingFactory();
1134        Builder strippingBuilder = new Builder(stripper);
1135        File JavaDoc base = new File JavaDoc("data");
1136        base = new File JavaDoc(base, "oasis-xslt-testsuite");
1137        base = new File JavaDoc("TESTS");
1138        File JavaDoc catalog = new File JavaDoc(base, "catalog.xml");
1139        
1140        // The test suite need to be installed separately. If we can't
1141
// find the catalog, we just don't run these tests.
1142
if (catalog.exists()) {
1143            Document doc = builder.build(catalog);
1144            Element testsuite = doc.getRootElement();
1145            Elements submitters = testsuite.getChildElements("test-catalog");
1146            Element submitter = submitters.get(1);
1147            Elements testcases = submitter.getChildElements("test-case");
1148            for (int j = 0; j < testcases.size(); j++) {
1149                Element testcase = testcases.get(j);
1150                String JavaDoc id = testcase.getAttributeValue("id");
1151                File JavaDoc root = new File JavaDoc(base, "MSFT_Conformance_Tests");
1152                root = new File JavaDoc(root, testcase.getFirstChildElement("file-path").getValue());
1153                File JavaDoc input = null;
1154                File JavaDoc style = null;
1155                File JavaDoc output = null;
1156                Element scenario = testcase.getFirstChildElement("scenario");
1157                Elements inputs = scenario.getChildElements("input-file");
1158                for (int k = 0; k < inputs.size(); k++) {
1159                    Element file = inputs.get(k);
1160                    String JavaDoc role = file.getAttributeValue("role");
1161                    if ("principal-data".equals(role)) {
1162                        input = new File JavaDoc(root, file.getValue());
1163                    }
1164                    else if ("principal-stylesheet".equals(role)) {
1165                        style = new File JavaDoc(root, file.getValue());
1166                    }
1167                } // end for
1168
Elements outputs = scenario.getChildElements("output-file");
1169                for (int k = 0; k < outputs.size(); k++) {
1170                    Element file = outputs.get(k);
1171                    String JavaDoc role = file.getAttributeValue("role");
1172                    if ("principal".equals(role)) {
1173                        // Fix up OASIS catalog bugs
1174
File JavaDoc parent = new File JavaDoc(root.getParent());
1175                        parent = new File JavaDoc(parent, "REF_OUT");
1176                        parent = new File JavaDoc(parent, root.getName());
1177                        String JavaDoc outputFileName = file.getValue();
1178                        output = new File JavaDoc(parent, outputFileName);
1179                    }
1180                } // end for
1181

1182                try {
1183                    Document styleDoc = builder.build(style);
1184                    boolean strip = indentYes(styleDoc);
1185                    if ("BVTs_bvt002".equals(id) || "BVTs_bvt077".equals(id)) {
1186                        // This has been fixed at least as of Xalan 2.6.0.
1187
// However, it's a bug in earlier versions of Xalan
1188
// including the one bundled with the JDK 1.4.2_05
1189
continue;
1190                    }
1191                    else if ("XSLTFunctions_Bug76984".equals(id)) {
1192                        // This has been fixed at least as of Xalan 2.6.0.
1193
// However, it's a bug in earlier versions of Xalan
1194
// including the one bundled with the JDK 1.4.2_05
1195
continue;
1196                    }
1197                    else if ("BVTs_bvt020".equals(id) || "BVTs_bvt022".equals(id)
1198                      || "BVTs_bvt024".equals(id) || "BVTs_bvt058".equals(id)) {
1199                        // Either a test suite bug, or a recoverable
1200
// error Xalan doesn't recover from.
1201
continue;
1202                    }
1203                    else if ("BVTs_bvt038".equals(id)
1204                      || "Namespace-alias__91785".equals(id)
1205                      || "Namespace-alias__91786".equals(id)) {
1206                        // a recoverable error Xalan doesn't recover from properly
1207
// http://nagoya.apache.org/jira/browse/XALANJ-1957
1208
continue;
1209                    }
1210                    else if ("Namespace_XPath_CopyNamespaceNodeToOutput".equals(id)) {
1211                        // Xalan bug
1212
// http://nagoya.apache.org/jira/browse/XALANJ-1959
1213
continue;
1214                    }
1215                    else if ("Namespace-alias_Namespace-Alias_WithinRTF".equals(id)) {
1216                        // Xalan bug
1217
// http://nagoya.apache.org/jira/browse/XALANJ-1960
1218
continue;
1219                    }
1220                    else if ("Completeness__84361".equals(id)
1221                      || "Namespace-alias__91781".equals(id)
1222                      || "Namespace-alias__91782".equals(id)
1223                      || "Namespace-alias_Namespace-Alias_Test1".equals(id)
1224                      || "Namespace-alias_Namespace-Alias_Test2".equals(id)
1225                      ) {
1226                        // a recoverable error Xalan doesn't recover from
1227
continue;
1228                    }
1229                    else if ("Output__84008".equals(id)) {
1230                        // a recoverable error Xalan doesn't recover from
1231
continue;
1232                    }
1233                    else if ("XSLTFunctions_ElementAvailFunctionFalseTest".equals(id)) {
1234                        // Xalan bug
1235
// http://nagoya.apache.org/jira/browse/XALANJ-1961
1236
continue;
1237                    }
1238                    else if ("XSLTFunctions_GenereateIdAppliedToNamespaceNodesOnDifferentElements".equals(id)) {
1239                        // Xalan bug
1240
// http://nagoya.apache.org/jira/browse/XALANJ-1962
1241
continue;
1242                    }
1243                    else if ("XSLTFunctions__specialCharInPattern".equals(id)) {
1244                        // a recoverable error Xalan doesn't recover from
1245
continue;
1246                    }
1247                    else if ("XSLTFunctions_DocumentFunctionWithAbsoluteArgument".equals(id)) {
1248                        // test case bug; bad URL passed to document function
1249
continue;
1250                    }
1251                    else if ("BVTs_bvt052".equals(id) || "Keys_PerfRepro2".equals(id)) {
1252                        // Requires a non-standard extension function
1253
continue;
1254                    }
1255                    else if ("BVTs_bvt044".equals(id)) {
1256                        // a recoverable error Xalan doesn't recover from
1257
// http://nagoya.apache.org/jira/browse/XALANJ-1957
1258
continue;
1259                    }
1260                    else if ("BVTs_bvt039".equals(id)) {
1261                        // Xalan bug
1262
continue;
1263                    }
1264                    else if ("BVTs_bvt033".equals(id) || "BVTs_bvt034".equals(id)) {
1265                        // Test suite bug; 2.0 is not unrecognized
1266
continue;
1267                    }
1268                    else if ("Text__78274".equals(id) || "Text__78276".equals(id)) {
1269                        // Test suite bug; no xsl:preserve-space attribute
1270
continue;
1271                    }
1272                    else if ("XSLTFunctions__minimumValue".equals(id)
1273                     || "XSLTFunctions__minimalValue".equals(id)) {
1274                        // test suite bug
1275
continue;
1276                    }
1277                    else if ("Errors_err073".equals(id)) {
1278                        // Xalan bug: StackOverflowError
1279
continue;
1280                    }
1281                    else if ("Sorting_SortExprWithCurrentInsideForEach1".equals(id)) {
1282                        // Xalan bug
1283
// http://issues.apache.org/jira/browse/XALANJ-1970
1284
continue;
1285                    }
1286                    else if ("BVTs_bvt041".equals(id) || "BVTs_bvt063".equals(id)
1287                        || "BVTs_bvt070".equals(id)) {
1288                        // Xalan bundled with JDK 1.4.2_05 does not recover
1289
// from this error involving multiple conflicting
1290
// xsl:output at same import precedence, though
1291
// 2.6.0 does
1292
continue;
1293                    }
1294                    Document inputDoc = builder.build(input);
1295                    XSLTransform xform;
1296                    if (strip) xform = new XSLTransform(styleDoc, stripper);
1297                    else xform = new XSLTransform(styleDoc);
1298                    Nodes result = xform.transform(inputDoc);
1299                    if (output == null) {
1300                        if ("Attributes__89463".equals(id)
1301                          || "Attributes__89465".equals(id)) {
1302                            // Processors are allowed to recover from
1303
// this problem.
1304
assertEquals(0, result.size());
1305                        }
1306                        else if ("Attributes__89464".equals(id)) {
1307                            // Processors are allowed to recover from
1308
// this problem.
1309
assertEquals(0, ((Element) result.get(0)).getAttributeCount());
1310                        }
1311                        else if ("Namespace-alias__91772".equals(id)
1312                          || "Namespace-alias__91774".equals(id)
1313                          || "Namespace-alias__91780".equals(id)
1314                          || "Namespace-alias__91790".equals(id)
1315                          || "Namespace-alias__91791".equals(id)
1316                          || "Sorting__84006".equals(id)
1317                          || "Sorting__91754".equals(id)
1318                          ) {
1319                            // Processors are allowed to recover from
1320
// this problem.
1321
continue;
1322                        }
1323                        else if (id.startsWith("Errors_")) {
1324                            // Processors are allowed to recover from
1325
// most of these problems.
1326
}
1327                        else if (id.startsWith("FormatNumber")) {
1328                            // Processors are allowed to recover from
1329
// most of these problems.
1330
}
1331                        else if ("BVTs_bvt074".equals(id)) {
1332                            // Processors are allowed to recover from
1333
// this problem.
1334
assertEquals(0, result.get(0).getChildCount());
1335                        }
1336                        else if ("XSLTFunctions__currency".equals(id)
1337                          || "XSLTFunctions__mixingInvalids".equals(id)) {
1338                            // Processors are allowed to recover from
1339
// this problem.
1340
continue;
1341                        }
1342                        else if ("Attributes_Attribute_UseXmlnsNsAsNamespaceForAttribute".equals(id)
1343                          || "Attributes_Attribute_UseXmlnsAsNamespaceForAttributeImplicitly".equals(id)
1344                          || "Elements_Element_UseXslElementWithNameSpaceAttrEqualToXmlnsUri".equalsIgnoreCase(id)
1345                          || "Elements_Element_UseXslElementWithNameSpaceEqualToXmlnsUri".equalsIgnoreCase(id)
1346                          ) {
1347                            // test follows namespace errata we don't accept
1348
}
1349                        else if ("AttributeSets_RefToUndefinedAttributeSet".equals(id)) {
1350                            // I think the test case is wrong; I see
1351
// nothing in the spec that says this is
1352
// an error.
1353
}
1354                        else if ("Namespace__77665".equals(id)
1355                          || "Namespace__77675".equals(id)) {
1356                            // I think the test case is wrong; I see
1357
// nothing in the spec that says this is
1358
// an error. See
1359
// http://lists.oasis-open.org/archives/xslt-conformance-comment/200409/msg00007.html
1360
}
1361                        else if ("Variables__84633".equals(id)
1362                          || "Variables__84634".equals(id)
1363                          || "Variables__84697".equals(id)
1364                          || "Variables__84710".equals(id)
1365                          ) {
1366                            // An error. See 11.4
1367
// but are processors allowed to recover?
1368
// Hmm according to section 17, the
1369
// processor must signal these errors
1370
// and may but need not recover from them.
1371
// Xalan recovers. Microsoft doesn't.
1372
}
1373                        else if ("Output__78176".equals(id)) {
1374                            // I think the test case is wrong; I see
1375
// nothing in the spec that says this is
1376
// an error.
1377
}
1378                        else if (id.startsWith("XSLTFunctions__100")) {
1379                            // I think these test cases are all wrong
1380
// except perhaps XSLTFunctions__10026; I
1381
// see nothing in the spec that says this
1382
// is an error. These are all about the
1383
// unparsed-entity-uri function.
1384
}
1385                        else if ("Namespace__78027".equals(id)) {
1386                            // Test case is incorrect. This should
1387
// operate in forwards compatible mode.
1388
// Xalan gets this right.
1389
}
1390                        else if ("Output_Output_UseStandAloneAttributeWithMultipleRoots".equals(id)) {
1391                            // Error only appears when document is serialized;
1392
// not before
1393
}
1394                        else { // transform should have failed
1395
fail("Transformed " + style + "\n id: "
1396                              + testcase.getAttributeValue("id"));
1397                        }
1398                    }
1399                    else {
1400                        try {
1401                            if ("Attributes_xsl_attribute_dup_attr_with_namespace_conflict".equals(id)
1402                               || "BVTs_bvt057".equals(id)) {
1403                                // This test case requires namespace prefix rewriting,
1404
// so the output won't be exactly the same between processors
1405
continue;
1406                            }
1407                            else if ("Comment_DisableOutputEscaping_XslTextInXslComment".equals(id)) {
1408                               // Test case output is wrong
1409
continue;
1410                            }
1411                            else if ("Output__77927".equals(id)
1412                              || "Output__77928".equals(id)
1413                              || "Output__84304".equals(id)
1414                              || "Output__84305".equals(id)
1415                              || "Output__84312".equals(id)
1416                              || "Output__84619".equals(id)
1417                              || "Output__84620".equals(id)
1418                              || "Output_EntityRefInAttribHtml".equals(id)
1419                            ) {
1420                                // These test cases have incorrect line
1421
// breaks in the reference output.
1422
continue;
1423                            }
1424                            else if ("Output_Modified84433".equals(id)) {
1425                                // This test case uses disable output escaping
1426
// so the results don't match up
1427
continue;
1428                            }
1429                            else if ("Sorting_Sort_SortTextWithNonTextCharacters".equals(id)) {
1430                               // Xalan and MSXML don't sort non alphabetic characters
1431
// exactly the same, but that's legal
1432
continue;
1433                            }
1434                            else if ("Text_DoeWithCdataInText".equals(id)) {
1435                               // Requires disable-output-escaping
1436
continue;
1437                            }
1438                            else if ("Whitespaces__91443".equals(id)
1439                              || "Whitespaces__91444".equals(id)) {
1440                                // Xalan bug
1441
// See http://nagoya.apache.org/jira/browse/XALANJ-1969
1442
continue;
1443                            }
1444                            else if ("AVTs__77591".equals(id)) {
1445                                // test suite bug; doesn't escape tabs in output. See
1446
// http://lists.oasis-open.org/archives/xslt-conformance-comment/200409/msg00017.html
1447
}
1448                            else if ("Keys_MultipltKeysInclude".equals(id) ) {
1449                               // Xalan bug
1450
// http://nagoya.apache.org/jira/browse/XALANJ-1956
1451
}
1452                            else if ("Keys_PerfRepro3".equals(id) ) {
1453                               // Suspected Xalan bug
1454
// http://nagoya.apache.org/jira/browse/XALANJ-1955
1455
}
1456                            else if ("Number__84683".equals(id)) {
1457                               // test suite bug
1458
}
1459                            else if ("Number__84687".equals(id)) {
1460                               // test suite bug
1461
}
1462                            else if ("Number__84692".equals(id)) {
1463                               // test suite bug
1464
}
1465                            else if ("Number__84694".equals(id)) {
1466                               // Test suite expects Roman number for zero
1467
// to be the empty string while Xalan uses 0
1468
}
1469                            else if ("Number__84699".equals(id)) {
1470                               // Xalan bug
1471
}
1472                            else if ("Number__84700".equals(id)) {
1473                               // Xalan bug; extra whitespace. Possibly
1474
// the same as
1475
}
1476                            else if ("Number__84716".equals(id)) {
1477                               // Xalan doesn't support Russian
1478
// number formatting
1479
}
1480                            else if ("Number__84717".equals(id)) {
1481                               // Xalan supports more Japanese than the
1482
// test case does
1483
}
1484                            else if ("Number__84722".equals(id)
1485                              || "Number__84723".equals(id)
1486                              || "Number__84724".equals(id)
1487                              || "Number__84725".equals(id)
1488                            ) {
1489                                // Acceptable locale support differences
1490
}
1491                            else if ("Number_NaNOrInvalidValue".equals(id)) {
1492                                // Double bug! Test case is wrong and
1493
// Xalan gets this wrong!
1494
}
1495                            else if ("Number_ValueAsNodesetTest1".equals(id)
1496                              || "Number_ValueAsEmptyNodeset".equals(id)) {
1497                                // Another double bug! Test case is wrong and
1498
// Xalan gets this wrong!
1499
}
1500                            else if (id.equals("XSLTFunctions_BooleanFunction")) {
1501                                // I think the test case is wrong; or perhaps unspecified
1502
}
1503                            else if (id.equals("XSLTFunctions_TestIdFuncInComplexStruct")) {
1504                                // I think the Xalan output white space is wrong;
1505
// http://nagoya.apache.org/jira/browse/XALANJ-1947
1506
}
1507                            else {
1508                                Document expectedResult;
1509                                if (strip) expectedResult = strippingBuilder.build(output);
1510                                else expectedResult = builder.build(output);
1511                                Document actualResult = XSLTransform.toDocument(result);
1512                                assertEquals("Mismatch with " + id,
1513                                  expectedResult, actualResult);
1514                            }
1515                        } // end try
1516
catch (ParsingException ex) {
1517                            // a few of the test cases generate
1518
// text or HTML output rather than
1519
// well-formed XML. For the moment, I
1520
// just skip these.
1521
continue;
1522                        }
1523                        catch (IllegalAddException ex) {
1524                            // A few of the test cases generate
1525
// incomplete documents so we can't
1526
// compare output. Perhaps I could
1527
// wrap in an element, then get children
1528
// to build a Node object rather than a
1529
// Document???? i.e. a fragment parser?
1530
// Could use a SequenceInputStream to hack this
1531
}
1532                    } // end else
1533

1534                } // end try
1535
catch (MalformedURIException ex) {
1536                    // several stylesheets use relative namespace URIs XOM
1537
// does not support; skip the test
1538
}
1539                catch (FileNotFoundException JavaDoc ex) {
1540                    // The catalog doesn't always match what's on disk
1541
}
1542                catch (UnknownHostException JavaDoc ex) {
1543                    // A few tests like ProcessingInstruction__78197
1544
// point to external DTD subsets that can't be loaded
1545
}
1546                catch (ParsingException ex) {
1547                    String JavaDoc operation = scenario.getAttributeValue("operation");
1548                    if (!"execution-error".equals(operation)) {
1549                        if ("Namespace_XPath_PredefinedPrefix_XML".equals(id)) {
1550                            // uses relative namespace URIs
1551
}
1552                        else if ("Sorting__78191".equals(id)
1553                          || "Text__78245".equals(id)
1554                          || "Text__78273".equals(id)
1555                          || "Text__78281".equals(id)
1556                        ) {
1557                            // binds XML namespace to prefix other than xml
1558
}
1559                        else {
1560                            System.err.println(id + ": " + ex.getMessage());
1561                            throw ex;
1562                        }
1563                    }
1564                }
1565                catch (XSLException ex) {
1566                    // If the output was null the transformation
1567
// was expected to fail
1568
if (output != null) {
1569                        Throwable JavaDoc cause = ex.getCause();
1570                        if ("Attributes__81487".equals(id)
1571                          || "Attributes__81551".equals(id)) {
1572                            // spec inconsistency; see
1573
// http://lists.w3.org/Archives/Public/xsl-editors/2004JulSep/0003.html
1574
continue;
1575                        }
1576                        else if (cause instanceof MissingResourceException JavaDoc) {
1577                            // Xalan bug;
1578
// http://nagoya.apache.org/jira/secure/ManageAttachments.jspa?id=27366
1579
}
1580                        else if ("Include_Include_IncludedStylesheetShouldHaveDifferentBaseUri".equals(id)) {
1581                           // This test case is wrong; Uses backslash in URI
1582
}
1583                        else if ("Elements__89070".equals(id)) {
1584                            // bug fixed in later versions of Xalan
1585
}
1586                        else if ("Namespace-alias_Namespace-Alias_NSAliasForDefaultWithExcludeResPref".equals(id)) {
1587                           // This test case is wrong; it uses a backslash in a URI
1588
}
1589                        else if ("Variables_VariableWithinVariable".equals(id)) {
1590                            // Xalan does not recover from this one
1591
}
1592                        else if ("BVTs_bvt054".equals(id)) {
1593                            // Xalan bug
1594
// http://nagoya.apache.org/jira/browse/XALANJ-1952
1595
continue;
1596                        }
1597                        else if ("BVTs_bvt094".equals(id)) {
1598                            // Xalan bug
1599
// http://nagoya.apache.org/jira/browse/XALANJ-1953
1600
continue;
1601                        }
1602                        else if ("Output__78177".equals(id)
1603                          || "Output__84009".equals(id)) {
1604                           // Xalan does not recover from this error
1605
// which involves duplicate and possibly conflicting xsl:output elements
1606
continue;
1607                        }
1608                        else if ("Comment_Comment_CDATAWithSingleHyphen".equals(id)
1609                          || "Comment_Comment_DoubleHypenEntitywithDelCharacter".equals(id)
1610                          || "Comment_Comment_LineOfAllHyphens".equals(id)
1611                          || "Comment_Comment_SingleHyphenOnly".equals(id)
1612                          || "Comment_Comment_DoubleHyphenONLY".equals(id)) {
1613                           // Begins comment data with hyphen, which XOM doesn't allow
1614
continue;
1615                        }
1616                        else if ("ProcessingInstruction_ValueOfandTextWithDoeInProcInstr".equals(id)) {
1617                           // Begins processing instruction data with white space, which XOM doesn't allow
1618
continue;
1619                        }
1620                        else if ("Elements__89716".equals(id)
1621                          || "Elements__89717".equals(id)
1622                          || "Elements__89718".equals(id)
1623                          || "Output__84309".equals(id)
1624                          || "Namespace__77670".equals(id))
1625                          {
1626                           // Xalan doesn't recover from these, though recovery is allowed
1627
continue;
1628                        }
1629                        else if ("Output__84306".equals(id)) {
1630                            // Xalan bug
1631
// http://nagoya.apache.org/jira/browse/XALANJ-1954
1632
continue;
1633                        }
1634                        else if ("Output__84014".equals(id)) {
1635                            // Fixed in later version of Xalan than is bundled with JDK
1636
continue;
1637                        }
1638                        else if (cause != null
1639                          && cause instanceof MalformedURIException) {
1640                            // Some of the tests generate relative namespace URIs
1641
// XOM doesn't support
1642
continue;
1643                        }
1644                        else {
1645                            System.err.println(id + ": " + ex.getMessage());
1646                            System.err.println("in " + style);
1647                            if (cause != null) {
1648                                System.err.println("cause: " + cause.getMessage());
1649                            }
1650                            throw ex;
1651                        }
1652                    }
1653                } // end catch
1654
catch (XMLException ex) {
1655                    if ("Text_modified78309".equals(id)) {
1656                       // output is not a full document
1657
}
1658                    else {
1659                        System.err.println(id);
1660                        throw ex;
1661                    }
1662                }
1663                
1664            } // end for
1665

1666        } // end if
1667

1668    }
1669    
1670    
1671    public void testToDocumentWithEmptyNodes() {
1672     
1673        try {
1674            XSLTransform.toDocument(new Nodes());
1675            fail("Converted empty nodes to document");
1676        }
1677        catch (XMLException success) {
1678            assertNotNull(success.getMessage());
1679        }
1680        
1681    }
1682    
1683    
1684    public void testToDocumentWithNoRoot() {
1685     
1686        Nodes input = new Nodes();
1687        input.append(new Comment("data"));
1688        try {
1689            XSLTransform.toDocument(new Nodes());
1690            fail("Converted comment to document");
1691        }
1692        catch (XMLException success) {
1693            assertNotNull(success.getMessage());
1694        }
1695        
1696    }
1697    
1698    
1699    public void testToDocumentWithText() {
1700     
1701        Nodes input = new Nodes();
1702        Element root = new Element("root");
1703        Comment comment = new Comment("data");
1704        ProcessingInstruction pi = new ProcessingInstruction("target", "data");
1705        input.append(comment);
1706        input.append(root);
1707        input.append(pi);
1708        input.append(new Text("text"));
1709        try {
1710            XSLTransform.toDocument(new Nodes());
1711            fail("Converted text to document");
1712        }
1713        catch (XMLException success) {
1714            assertNotNull(success.getMessage());
1715        }
1716        
1717    }
1718    
1719    
1720    public void testToDocumentWithAttribute() {
1721     
1722        Nodes input = new Nodes();
1723        Element root = new Element("root");
1724        Comment comment = new Comment("data");
1725        ProcessingInstruction pi = new ProcessingInstruction("target", "data");
1726        input.append(comment);
1727        input.append(root);
1728        input.append(pi);
1729        input.append(new Attribute("name", "text"));
1730        try {
1731            XSLTransform.toDocument(new Nodes());
1732            fail("Converted text to document");
1733        }
1734        catch (XMLException success) {
1735            assertNotNull(success.getMessage());
1736        }
1737        
1738    }
1739    
1740    
1741    public void testToDocumentWithDocType() {
1742     
1743        Nodes input = new Nodes();
1744        Element root = new Element("root");
1745        DocType doctype = new DocType("root");
1746        Comment comment = new Comment("data");
1747        ProcessingInstruction pi = new ProcessingInstruction("target", "data");
1748        input.append(comment);
1749        input.append(doctype);
1750        input.append(root);
1751        input.append(pi);
1752        Document output = XSLTransform.toDocument(input);
1753        assertEquals(root, output.getRootElement());
1754        assertEquals(comment, output.getChild(0));
1755        assertEquals(doctype, output.getChild(1));
1756        assertEquals(pi, output.getChild(3));
1757        assertEquals(input.size(), output.getChildCount());
1758        
1759    }
1760    
1761    
1762    public void testToDocumentWithDocTypeInEpilog() {
1763     
1764        Nodes input = new Nodes();
1765        Element root = new Element("root");
1766        DocType doctype = new DocType("root");
1767        Comment comment = new Comment("data");
1768        ProcessingInstruction pi = new ProcessingInstruction("target", "data");
1769        input.append(comment);
1770        input.append(root);
1771        input.append(doctype);
1772        input.append(pi);
1773        try {
1774            XSLTransform.toDocument(input);
1775            fail("Allowed doctype in epilog");
1776        }
1777        catch (XMLException success) {
1778            assertNotNull(success.getMessage());
1779        }
1780        
1781    }
1782    
1783    
1784    public void testToDocumentWithDoubleRoot() {
1785     
1786        Nodes input = new Nodes();
1787        Element root = new Element("root");
1788        Comment comment = new Comment("data");
1789        input.append(comment);
1790        input.append(root);
1791        input.append(new Element("root2"));
1792        try {
1793            XSLTransform.toDocument(input);
1794            fail("Allowed two root elements");
1795        }
1796        catch (XMLException success) {
1797            assertNotNull(success.getMessage());
1798        }
1799        
1800    }
1801    
1802    
1803    public void testToDocumentWithSingleRoot() {
1804     
1805        Nodes input = new Nodes();
1806        Element root = new Element("root");
1807        input.append(root);
1808        Document output = XSLTransform.toDocument(input);
1809        assertEquals(root, output.getRootElement());
1810        assertEquals(input.size(), output.getChildCount());
1811        
1812    }
1813    
1814
1815    public void testToDocumentWithPrologAndEpilog() {
1816     
1817        Nodes input = new Nodes();
1818        Element root = new Element("root");
1819        Comment comment = new Comment("data");
1820        ProcessingInstruction pi = new ProcessingInstruction("target", "data");
1821        input.append(comment);
1822        input.append(root);
1823        input.append(pi);
1824        Document output = XSLTransform.toDocument(input);
1825        assertEquals(root, output.getRootElement());
1826        assertEquals(comment, output.getChild(0));
1827        assertEquals(pi, output.getChild(2));
1828        assertEquals(input.size(), output.getChildCount());
1829        
1830    }
1831    
1832    
1833}
1834
Popular Tags