~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/tests/XSLTransformTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2002-2005 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;
 
24
import java.io.File;
 
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;
 
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.Namespace;
 
43
import nu.xom.Node;
 
44
import nu.xom.NodeFactory;
 
45
import nu.xom.Nodes;
 
46
import nu.xom.ParentNode;
 
47
import nu.xom.ParsingException;
 
48
import nu.xom.ProcessingInstruction;
 
49
import nu.xom.Serializer;
 
50
import nu.xom.Text;
 
51
import nu.xom.XMLException;
 
52
import nu.xom.xslt.XSLException;
 
53
import nu.xom.xslt.XSLTransform;
 
54
 
 
55
/**
 
56
 * <p>
 
57
 * Unit tests for the XSLT engine.
 
58
 * </p>
 
59
 * 
 
60
 * <p>
 
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.
 
64
 * </p>
 
65
 * 
 
66
 * @author Elliotte Rusty Harold
 
67
 * @version 1.1b6
 
68
 *
 
69
 */
 
70
public class XSLTransformTest extends XOMTestCase {
 
71
 
 
72
    
 
73
    public XSLTransformTest(String name) {
 
74
        super(name);   
 
75
    }
 
76
 
 
77
    
 
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\"?>" 
 
82
     + "<!-- test -->"
 
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&apos;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>"
 
91
     + "</test>\r\n"
 
92
     + "<!--epilog-->";
 
93
    
 
94
    
 
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;
 
99
    
 
100
    private File inputDir;
 
101
    
 
102
    protected void setUp() {
 
103
        
 
104
        System.setErr(new PrintStream(new ByteArrayOutputStream()));
 
105
        
 
106
        inputDir = new File("data");
 
107
        inputDir = new File(inputDir, "xslt");
 
108
        inputDir = new File(inputDir, "input");
 
109
        
 
110
    } 
 
111
    
 
112
    
 
113
    protected void tearDown() {
 
114
        System.setErr(systemErr);
 
115
    }
 
116
    
 
117
    // Primarily this makes sure the XSLTHandler can handle various
 
118
    // edge cases
 
119
    public void testIdentityTransform() 
 
120
      throws ParsingException, IOException, XSLException {
 
121
        
 
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));
 
134
        
 
135
    }
 
136
 
 
137
    
 
138
    public void testPrefixMappingIssues() 
 
139
      throws XSLException, ParsingException, IOException {
 
140
        
 
141
        String doc = "<test>"
 
142
           + "<span xmlns:a='http://www.example.com'/>"
 
143
           + "<span xmlns:b='http://www.example.net'/>"
 
144
           + "</test>"; 
 
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));
 
152
        
 
153
    }
 
154
    
 
155
    
 
156
    public void testDocumentConstructor() 
 
157
      throws ParsingException, IOException {
 
158
        
 
159
        try {
 
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");
 
167
        }
 
168
        catch (XSLException success) { 
 
169
            assertNotNull(success.getMessage());
 
170
        }
 
171
        
 
172
    }
 
173
 
 
174
 
 
175
    public void testLiteralResultElementUsedAsStylesheet() 
 
176
      throws ParsingException, IOException, XSLException {
 
177
 
 
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"
 
182
        + "  <head>\n"
 
183
        + "    <title>Expense Report Summary</title>\n"
 
184
        + "  </head>\n"
 
185
        + "  <body>\n"
 
186
        + "    <p>Total Amount: <xsl:value-of select='expense-report/total'/></p>\n"
 
187
        + "  </body>\n"
 
188
        + "</html>\n";
 
189
    
 
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());
 
200
        
 
201
    }
 
202
 
 
203
 
 
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>
 
208
    <element4/>
 
209
    <xsl:comment>test</xsl:comment>
 
210
    <xsl:processing-instruction name="test">PIs are not treated as literals in XSLT?</xsl:processing-instruction>
 
211
  </xsl:template> */
 
212
  
 
213
    public void testCreateDocumentFragment() 
 
214
      throws ParsingException, IOException, XSLException {
 
215
        
 
216
        Element element1 = new Element("element1");
 
217
        element1.appendChild("some data and ");
 
218
        element1.appendChild(new Element("content"));
 
219
        element1.appendChild(" for a test");
 
220
        
 
221
        Element element2 = new Element("element2");
 
222
        element2.appendChild(
 
223
          "Remember, the XSLT processor is going to strip out the literal white space"
 
224
        );
 
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));
 
239
        
 
240
    }
 
241
 
 
242
    
 
243
    public void testTransform() 
 
244
      throws ParsingException, IOException, XSLException {
 
245
        
 
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)));
 
254
 
 
255
        Document expected = builder.build("data/xslt/output/8-8.xml");
 
256
        assertEquals(expected, result);
 
257
        
 
258
    }
 
259
 
 
260
    
 
261
    public void testSetParameter() 
 
262
      throws ParsingException, IOException, XSLException {
 
263
        
 
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());
 
274
        
 
275
    }
 
276
 
 
277
    
 
278
    public void testSetParameterWithNamespace() 
 
279
      throws ParsingException, IOException, XSLException {
 
280
        
 
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>", 
 
291
            result.toXML());
 
292
        
 
293
    }
 
294
 
 
295
    
 
296
    public void testRemoveParameterWithNamespace() 
 
297
      throws ParsingException, IOException, XSLException {
 
298
        
 
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>", 
 
310
            result.toXML());
 
311
        
 
312
    }
 
313
 
 
314
    
 
315
    public void testRemoveParameter() 
 
316
      throws ParsingException, IOException, XSLException {
 
317
        
 
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());
 
329
        
 
330
    }
 
331
 
 
332
    
 
333
    public void testTransformWithCFilter() 
 
334
      throws ParsingException, IOException, XSLException {
 
335
        
 
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());
 
342
        
 
343
        Nodes output = xform.transform(builder.build(doc));
 
344
        assertEquals(1, output.size());
 
345
        Document result = new Document((Element) (output.get(0)));
 
346
 
 
347
        Document expected = builder.build("data/xslt/output/8-8c.xml");
 
348
        assertEquals(expected, result);
 
349
        
 
350
    }
 
351
 
 
352
    
 
353
    public void testCreateDocumentFragmentWithCommentFilter() 
 
354
      throws ParsingException, IOException, XSLException {
 
355
        
 
356
        Element element1 = new Element("element1");
 
357
        element1.appendChild("some data and ");
 
358
        element1.appendChild(new Element("content"));
 
359
        element1.appendChild(" for a test");
 
360
        
 
361
        Element element2 = new Element("element2");
 
362
        element2.appendChild(
 
363
          "Remember, the XSLT processor is going to strip out the literal white space"
 
364
        );
 
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());
 
371
        
 
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));
 
380
        
 
381
    }    
 
382
    
 
383
    
 
384
    public void testCreateDocumentFragmentWithProcessingInstructionFilter() 
 
385
      throws ParsingException, IOException, XSLException {
 
386
        
 
387
        Element element1 = new Element("element1");
 
388
        element1.appendChild("some data and ");
 
389
        element1.appendChild(new Element("content"));
 
390
        element1.appendChild(" for a test");
 
391
        
 
392
        Element element2 = new Element("element2");
 
393
        element2.appendChild(
 
394
          "Remember, the XSLT processor is going to strip out the literal white space"
 
395
        );
 
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());
 
402
        
 
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));
 
410
        
 
411
    }    
 
412
    
 
413
    
 
414
    public void testCreateDocumentFragmentWithUncommentFilter() 
 
415
      throws ParsingException, IOException, XSLException {
 
416
        
 
417
        Element element1 = new Element("element1");
 
418
        element1.appendChild("some data and ");
 
419
        element1.appendChild(new Element("content"));
 
420
        element1.appendChild(" for a test");
 
421
        
 
422
        Element element2 = new Element("element2");
 
423
        element2.appendChild(
 
424
          "Remember, the XSLT processor is going to strip out the literal white space"
 
425
        );
 
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());
 
432
        
 
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));
 
442
        
 
443
    }    
 
444
    
 
445
    
 
446
    public void testTransform2() 
 
447
      throws ParsingException, IOException, XSLException {
 
448
        
 
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)));
 
457
 
 
458
        Document expected = builder.build("data/xslt/output/8-12.xml");
 
459
        assertEquals(expected, result);
 
460
        
 
461
    }
 
462
 
 
463
    
 
464
    
 
465
    // For debugging
 
466
    private static void dumpResult(Document result, String filename) 
 
467
      throws IOException {
 
468
        
 
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);
 
475
        serializer.flush();
 
476
        out.close();
 
477
        
 
478
    }
 
479
 
 
480
    
 
481
    public void testTransformFromDocument() 
 
482
      throws ParsingException, IOException, XSLException {
 
483
        
 
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)));
 
491
 
 
492
        Document expected = builder.build("data/xslt/output/8-12.xml");
 
493
        assertEquals(expected, result);
 
494
        
 
495
    }
 
496
 
 
497
    
 
498
    public void testTransformFromSystemID() 
 
499
      throws ParsingException, IOException, XSLException {
 
500
        
 
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)));
 
509
 
 
510
        Document expected = builder.build("data/xslt/output/8-12.xml");
 
511
        assertEquals(expected, result);
 
512
        
 
513
    }
 
514
 
 
515
 
 
516
    public void testTransformWithNamespaces() 
 
517
      throws ParsingException, IOException, XSLException {
 
518
        
 
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)));
 
528
 
 
529
        Document expected = builder.build("data/xslt/output/8-15.xml");
 
530
        assertEquals(expected, result);
 
531
        
 
532
    }
 
533
 
 
534
    
 
535
    public void testSingleTextNode() 
 
536
      throws ParsingException, IOException, XSLException {
 
537
        
 
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());
 
548
        
 
549
    }
 
550
    
 
551
    
 
552
    public void testToString() 
 
553
      throws XSLException, ParsingException, IOException {
 
554
        
 
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: "));
 
560
        
 
561
    }    
 
562
    
 
563
 
 
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 {
 
569
        
 
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);
 
584
        
 
585
    }
 
586
 
 
587
    
 
588
    public void testCommentWithParent() 
 
589
      throws XSLException, ParsingException, IOException {
 
590
        
 
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());
 
603
        
 
604
    }
 
605
 
 
606
    
 
607
    public void testProcessingInstructionWithParent() 
 
608
      throws XSLException, ParsingException, IOException {
 
609
        
 
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());
 
623
        
 
624
    } 
 
625
 
 
626
    
 
627
    public void testTransformNodes()
 
628
      throws XSLException, ParsingException, IOException {
 
629
        
 
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());
 
643
        
 
644
    } 
 
645
    
 
646
    
 
647
    public void testCopyXMLBaseAttribute()
 
648
      throws XSLException, ParsingException, IOException {
 
649
        
 
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());
 
665
        
 
666
    } 
 
667
    
 
668
    
 
669
    public void testCopyXMLLangAttribute()
 
670
      throws XSLException, ParsingException, IOException {
 
671
        
 
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());
 
686
        
 
687
    } 
 
688
    
 
689
    
 
690
    public void testTriple() 
 
691
      throws IOException, ParsingException, XSLException {
 
692
        
 
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());
 
698
 
 
699
        String data = "<a><b><c/></b></a>";
 
700
        Document doc = builder.build(data, "http://www.example.org/");
 
701
        
 
702
        Nodes result = xform.transform(doc);
 
703
        
 
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());
 
714
        
 
715
    }
 
716
   
 
717
    
 
718
    public void testPassingNullSetsDefaultFactory() 
 
719
      throws IOException, ParsingException, XSLException { 
 
720
        
 
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);
 
725
 
 
726
        String data = "<a><b><c/></b></a>";
 
727
        Document doc = builder.build(data, "http://www.example.org/");
 
728
      
 
729
        Nodes result = xform.transform(doc);
 
730
        
 
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());
 
739
        
 
740
    }
 
741
    
 
742
    
 
743
    public void testTransformEmptyNodesList() 
 
744
      throws IOException, ParsingException, XSLException {  
 
745
        
 
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);
 
750
       
 
751
        Nodes result = xform.transform(new Nodes());
 
752
        
 
753
        assertEquals(0, result.size());
 
754
        
 
755
    }
 
756
    
 
757
    
 
758
    public void testMinimizingFactory() 
 
759
      throws XSLException, ParsingException, IOException {
 
760
        
 
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());
 
766
        
 
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());
 
772
        
 
773
    } 
 
774
    
 
775
    
 
776
    public void testIllegalTransform() 
 
777
      throws XSLException, ParsingException, IOException {
 
778
        
 
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);
 
785
        try {
 
786
            xform.transform(input);
 
787
            fail("Allowed illegal transform");
 
788
        }
 
789
        catch (XSLException ex) {
 
790
            assertNotNull(ex.getMessage());
 
791
        }
 
792
        
 
793
    }
 
794
 
 
795
    
 
796
    public void testRemapPrefixToSameURI() 
 
797
      throws IOException, ParsingException, XSLException { 
 
798
        
 
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);
 
803
 
 
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/");
 
807
        
 
808
        Nodes result = xform.transform(doc);
 
809
        
 
810
        assertEquals(doc.getRootElement(), result.get(0));
 
811
        
 
812
    }
 
813
 
 
814
    
 
815
    public void testElementsToAttributes() 
 
816
      throws IOException, ParsingException, XSLException {  
 
817
        
 
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());
 
823
 
 
824
        String data = "<a><b>in B<c>in C</c></b></a>";
 
825
        Document doc = builder.build(data, "http://www.example.org/");
 
826
        
 
827
        Nodes result = xform.transform(doc);
 
828
        
 
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());
 
835
        
 
836
    }
 
837
 
 
838
    
 
839
    private static class AttributeFactory extends NodeFactory {
 
840
 
 
841
        public Nodes finishMakingElement(Element element) {
 
842
            ParentNode parent = element.getParent();
 
843
            if (parent == null || parent instanceof Document) {
 
844
                return new Nodes(element);
 
845
            }        
 
846
            return new Nodes(new Attribute(element.getQualifiedName(), 
 
847
                    element.getNamespaceURI(), element.getValue()));
 
848
        }     
 
849
        
 
850
    }
 
851
    
 
852
 
 
853
    public void testAttributesToElements() 
 
854
      throws IOException, ParsingException, XSLException {  
 
855
        
 
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());
 
861
 
 
862
        String data = "<a name='value'><b x='y' a='b'/></a>";
 
863
        Document doc = builder.build(data, "http://www.example.org/");
 
864
        
 
865
        Nodes result = xform.transform(doc);
 
866
        
 
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());
 
880
        
 
881
    }
 
882
 
 
883
    
 
884
    private static class AttributesToElements extends NodeFactory {
 
885
 
 
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);
 
891
        }   
 
892
        
 
893
    }
 
894
 
 
895
 
 
896
    public void testCommentsAreTransformed() 
 
897
      throws IOException, ParsingException, XSLException {
 
898
        
 
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);
 
903
 
 
904
        String data = "<a><!--test--></a>";
 
905
        Document doc = builder.build(data, "http://www.example.org/");
 
906
        
 
907
        Nodes result = xform.transform(doc);
 
908
        
 
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"));
 
917
        
 
918
    }
 
919
    
 
920
    
 
921
    public void testCommentToAttribute() 
 
922
      throws IOException, ParsingException, XSLException {
 
923
        
 
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, 
 
928
          new NodeFactory() {
 
929
            public Nodes makeComment(String text) {
 
930
                return new Nodes(new Attribute("comment", text));   
 
931
            }
 
932
        });
 
933
 
 
934
        String data = "<a><!--test--></a>";
 
935
        Document doc = builder.build(data, "http://www.example.org/");
 
936
        
 
937
        Nodes result = xform.transform(doc);
 
938
        
 
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());
 
947
        
 
948
    }
 
949
 
 
950
    
 
951
    public void testAdditionalDefaultNamespace() 
 
952
      throws IOException, ParsingException, XSLException {  
 
953
        
 
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);
 
958
 
 
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/");
 
962
        
 
963
        Nodes result = xform.transform(doc);
 
964
        
 
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());
 
973
        
 
974
    }
 
975
    
 
976
    
 
977
 
 
978
    private static boolean indentYes(Document styleDoc) {
 
979
        
 
980
        Element output = styleDoc
 
981
          .getRootElement()
 
982
          .getFirstChildElement("output", 
 
983
             "http://www.w3.org/1999/XSL/Transform");
 
984
        if (output == null) return false;
 
985
        
 
986
        String indent = output.getAttributeValue("indent");
 
987
        if ("yes".equals(indent)) {
 
988
            return true;
 
989
        }
 
990
        else return false;
 
991
        
 
992
    }
 
993
    
 
994
    
 
995
    private static class StrippingFactory extends NodeFactory {
 
996
    
 
997
        public Nodes makeText(String s) {
 
998
            
 
999
            String stripped = stripSpace(s);
 
1000
            if (stripped.length() == 0) return new Nodes();
 
1001
            Text result = new Text(stripped);
 
1002
            return new Nodes(result);
 
1003
        }
 
1004
        
 
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));
 
1008
        }        
 
1009
        
 
1010
        private String stripSpace(String s) {
 
1011
            
 
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));
 
1016
                }
 
1017
            }
 
1018
            
 
1019
            return sb.toString();
 
1020
            
 
1021
        }
 
1022
        
 
1023
    }
 
1024
 
 
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 {
 
1029
        
 
1030
        Builder builder = new Builder();
 
1031
        NodeFactory stripper = new StrippingFactory();
 
1032
        Builder strippingBuilder = new Builder(stripper);
 
1033
        
 
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");
 
1039
        
 
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++) {
 
1050
                    
 
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 
 
1056
                        // relevant to XOM
 
1057
                        continue;
 
1058
                    }
 
1059
                    File root = new File(base, testcase.getFirstChildElement("file-path").getValue());
 
1060
                    File input = null;
 
1061
                    File style = null;
 
1062
                    File output = null;
 
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());
 
1070
                        }
 
1071
                        else if ("principal-stylesheet".equals(role)) {
 
1072
                            style = new File(root, file.getValue());
 
1073
                        }
 
1074
                    }
 
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);
 
1086
                        }
 
1087
                    }
 
1088
                    
 
1089
                    try {
 
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
 
1094
                        XSLTransform xform;
 
1095
                        if (indentYes(styleDoc)) {
 
1096
                            xform = new XSLTransform(styleDoc, stripper);
 
1097
                        }
 
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);
 
1103
                        }
 
1104
                        else { 
 
1105
                            try {
 
1106
                                Document expectedResult;
 
1107
                                if (indentYes(styleDoc)) {
 
1108
                                    expectedResult = strippingBuilder.build(output);
 
1109
                                }
 
1110
                                else {
 
1111
                                    expectedResult = builder.build(output);
 
1112
                                }
 
1113
                                Document actualResult = XSLTransform.toDocument(result);
 
1114
                                
 
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.
 
1120
                                    continue;
 
1121
                                }
 
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
 
1125
                                }
 
1126
                                else if (id.equals("copy_copy56") 
 
1127
                                  || id.equals("copy_copy58")
 
1128
                                  || id.equals("copy_copy60")
 
1129
                                  || id.equals("copy_copy59")) {
 
1130
                                    // Xalan bug;
 
1131
                                    // See http://issues.apache.org/jira/browse/XALANJ-1081
 
1132
                                    // Also see erratum E27 to the XSLT spec.
 
1133
                                } 
 
1134
                                else if (id.equals("expression_expression02")) {
 
1135
                                    // requires unparsed entities XOM doesn't support
 
1136
                                } 
 
1137
                                else if (id.equals("idkey_idkey31")) {
 
1138
                                    // Known Xalan bug
 
1139
                                    // See http://issues.apache.org/jira/browse/XALANJ-1325
 
1140
                                } 
 
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
 
1146
                                } 
 
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
 
1150
                                }
 
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
 
1156
                                }
 
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
 
1161
                                }
 
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
 
1165
                                }
 
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
 
1169
                                }
 
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
 
1175
                                } 
 
1176
                                else {
 
1177
                                    assertEquals("Problem with " + id,
 
1178
                                      expectedResult, actualResult);
 
1179
                                }
 
1180
                            }
 
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 
 
1185
                                // just skip these.
 
1186
                                continue;
 
1187
                            }
 
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
 
1196
                            }
 
1197
                        }
 
1198
                        
 
1199
                    }
 
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;
 
1204
                        throw ex;
 
1205
                    }
 
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) {
 
1214
                                continue;
 
1215
                            }
 
1216
                            
 
1217
                            if ("impincl_impincl27".equals(id)) {  
 
1218
                                // Test case uses file: URI XOM doesn't support
 
1219
                                continue;
 
1220
                            }
 
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.
 
1229
                                continue;
 
1230
                            }
 
1231
                            
 
1232
                            System.out.println(id);
 
1233
                            System.out.println(ex.getMessage());
 
1234
                            throw ex;
 
1235
                        }
 
1236
                    }
 
1237
                    
 
1238
                }
 
1239
            } 
 
1240
            
 
1241
        }
 
1242
     
 
1243
    } 
 
1244
    
 
1245
    public void testOASISMicrosoftConformanceSuite()  
 
1246
      throws IOException, ParsingException, XSLException {
 
1247
        
 
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");
 
1255
        
 
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());
 
1269
                File input = null;
 
1270
                File style = null;
 
1271
                File output = null;
 
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());
 
1279
                    }
 
1280
                    else if ("principal-stylesheet".equals(role)) {
 
1281
                        style = new File(root, file.getValue());
 
1282
                    }
 
1283
                }  // end for 
 
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);
 
1295
                    }
 
1296
                }  // end for 
 
1297
                
 
1298
                try {
 
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
 
1305
                        continue;
 
1306
                    } 
 
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
 
1311
                        continue;
 
1312
                    } 
 
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.
 
1317
                        continue;
 
1318
                    } 
 
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
 
1324
                        continue;
 
1325
                    } 
 
1326
                    else if ("Namespace_XPath_CopyNamespaceNodeToOutput".equals(id)) {
 
1327
                        // Xalan bug
 
1328
                        // http://issues.apache.org/jira/browse/XALANJ-1959
 
1329
                        continue;
 
1330
                    } 
 
1331
                    else if ("Namespace-alias_Namespace-Alias_WithinRTF".equals(id)) {
 
1332
                        // Xalan bug
 
1333
                        // http://issues.apache.org/jira/browse/XALANJ-1960
 
1334
                        continue;
 
1335
                    } 
 
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)
 
1341
                      ) {
 
1342
                        // a recoverable error Xalan doesn't recover from
 
1343
                        continue;
 
1344
                    } 
 
1345
                    else if ("Output__84008".equals(id)) {
 
1346
                        // a recoverable error Xalan doesn't recover from
 
1347
                        continue;
 
1348
                    } 
 
1349
                    else if ("XSLTFunctions_ElementAvailFunctionFalseTest".equals(id)) {
 
1350
                        // Xalan bug
 
1351
                        // http://issues.apache.org/jira/browse/XALANJ-1961
 
1352
                        continue;
 
1353
                    } 
 
1354
                    else if ("XSLTFunctions_GenereateIdAppliedToNamespaceNodesOnDifferentElements".equals(id)) {
 
1355
                        // Xalan bug
 
1356
                        // http://issues.apache.org/jira/browse/XALANJ-1962
 
1357
                        continue;
 
1358
                    } 
 
1359
                    else if ("XSLTFunctions__specialCharInPattern".equals(id)) {
 
1360
                        // a recoverable error Xalan doesn't recover from
 
1361
                        continue;                        
 
1362
                    }
 
1363
                    else if ("XSLTFunctions_DocumentFunctionWithAbsoluteArgument".equals(id)) {
 
1364
                        // test case bug; bad URL passed to document function
 
1365
                        continue;
 
1366
                    }
 
1367
                    else if ("BVTs_bvt052".equals(id) || "Keys_PerfRepro2".equals(id)) {
 
1368
                        // Requires a non-standard extension function
 
1369
                        continue;
 
1370
                    } 
 
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
 
1374
                        continue;
 
1375
                    } 
 
1376
                    else if ("BVTs_bvt039".equals(id)) {
 
1377
                        // Xalan bug
 
1378
                        continue;
 
1379
                    } 
 
1380
                    else if ("BVTs_bvt033".equals(id) || "BVTs_bvt034".equals(id)) {
 
1381
                        // Test suite bug; 2.0 is not unrecognized
 
1382
                        continue;
 
1383
                    } 
 
1384
                    else if ("Text__78274".equals(id) || "Text__78276".equals(id)) {
 
1385
                        // Test suite bug; no xsl:preserve-space attribute
 
1386
                        continue;                           
 
1387
                    }
 
1388
                    else if ("XSLTFunctions__minimumValue".equals(id)
 
1389
                     || "XSLTFunctions__minimalValue".equals(id)) {
 
1390
                        // test suite bug
 
1391
                        continue;
 
1392
                    } 
 
1393
                    else if ("Errors_err073".equals(id)) {
 
1394
                        // Xalan bug: StackOverflowError
 
1395
                        continue;
 
1396
                    } 
 
1397
                    else if ("Sorting_SortExprWithCurrentInsideForEach1".equals(id)) {
 
1398
                        // Xalan bug
 
1399
                        // http://issues.apache.org/jira/browse/XALANJ-1970
 
1400
                        continue;
 
1401
                    }
 
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
 
1407
                        // 2.6.0 does
 
1408
                        continue;
 
1409
                    } 
 
1410
                    Document inputDoc = builder.build(input);
 
1411
                    XSLTransform xform;
 
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
 
1419
                            // this problem.
 
1420
                            assertEquals(0, result.size());
 
1421
                        }
 
1422
                        else if ("Attributes__89464".equals(id)) {
 
1423
                            // Processors are allowed to recover from
 
1424
                            // this problem.
 
1425
                            assertEquals(0, ((Element) result.get(0)).getAttributeCount());
 
1426
                        }
 
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)
 
1434
                          ) {
 
1435
                            // Processors are allowed to recover from
 
1436
                            // this problem.
 
1437
                            continue;
 
1438
                        }
 
1439
                        else if (id.startsWith("Errors_")) {
 
1440
                            // Processors are allowed to recover from
 
1441
                            // most of these problems.
 
1442
                        }
 
1443
                        else if (id.startsWith("FormatNumber")) {
 
1444
                            // Processors are allowed to recover from
 
1445
                            // most of these problems.
 
1446
                        }
 
1447
                        else if ("BVTs_bvt074".equals(id)) {
 
1448
                            // Processors are allowed to recover from
 
1449
                            // this problem.
 
1450
                            assertEquals(0, result.get(0).getChildCount());
 
1451
                        }
 
1452
                        else if ("XSLTFunctions__currency".equals(id)
 
1453
                          || "XSLTFunctions__mixingInvalids".equals(id)) {
 
1454
                            // Processors are allowed to recover from
 
1455
                            // this problem.
 
1456
                            continue;
 
1457
                        }
 
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)
 
1462
                          ) {
 
1463
                            // test follows namespace errata we don't accept
 
1464
                        }
 
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
 
1468
                            // an error.
 
1469
                        }
 
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
 
1474
                            // an error. See
 
1475
                            // http://lists.oasis-open.org/archives/xslt-conformance-comment/200409/msg00007.html
 
1476
                        }
 
1477
                        else if ("Variables__84633".equals(id)
 
1478
                          || "Variables__84634".equals(id)
 
1479
                          || "Variables__84697".equals(id)
 
1480
                          || "Variables__84710".equals(id)
 
1481
                          ) {
 
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.
 
1488
                        }
 
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
 
1492
                            // an error.
 
1493
                        }
 
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.
 
1500
                        }
 
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.
 
1505
                        } 
 
1506
                        else if ("Output_Output_UseStandAloneAttributeWithMultipleRoots".equals(id)) {
 
1507
                            // Error only appears when document is serialized;
 
1508
                            // not before
 
1509
                        }
 
1510
                        else { // transform should have failed
 
1511
                            fail("Transformed " + style + "\n id: "
 
1512
                              + testcase.getAttributeValue("id"));
 
1513
                        }
 
1514
                    }
 
1515
                    else { 
 
1516
                        try { 
 
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
 
1521
                                continue;
 
1522
                            }
 
1523
                            else if ("Comment_DisableOutputEscaping_XslTextInXslComment".equals(id)) {
 
1524
                               // Test case output is wrong
 
1525
                                continue;
 
1526
                            } 
 
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)
 
1535
                            ) {
 
1536
                                // These test cases have incorrect line 
 
1537
                                //  breaks in the reference output.
 
1538
                                continue;
 
1539
                            }
 
1540
                            else if ("Output_Modified84433".equals(id)) {
 
1541
                                // This test case uses disable output escaping
 
1542
                                // so the results don't match up
 
1543
                                continue;
 
1544
                            }
 
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
 
1548
                                continue;
 
1549
                            }
 
1550
                            else if ("Text_DoeWithCdataInText".equals(id)) {
 
1551
                               // Requires disable-output-escaping 
 
1552
                                continue;
 
1553
                            } 
 
1554
                            else if ("Whitespaces__91443".equals(id)
 
1555
                              || "Whitespaces__91444".equals(id)) { 
 
1556
                                // Xalan bug
 
1557
                                // See http://issues.apache.org/jira/browse/XALANJ-1969 
 
1558
                                continue;
 
1559
                            } 
 
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
 
1563
                            }
 
1564
                            else if ("Keys_MultipltKeysInclude".equals(id) ) {
 
1565
                               // Xalan bug
 
1566
                               // http://issues.apache.org/jira/browse/XALANJ-1956
 
1567
                            } 
 
1568
                            /* else if ("Keys_PerfRepro3".equals(id) ) {
 
1569
                               // Suspected Xalan bug 
 
1570
                               // http://issues.apache.org/jira/browse/XALANJ-1955
 
1571
                            } */
 
1572
                            else if ("Number__84683".equals(id)) {
 
1573
                               // test suite bug
 
1574
                            }
 
1575
                            else if ("Number__84687".equals(id)) {
 
1576
                               // test suite bug
 
1577
                            }
 
1578
                            else if ("Number__84692".equals(id)) {
 
1579
                               // test suite bug
 
1580
                            }
 
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
 
1584
                            }
 
1585
                            else if ("Number__84699".equals(id)) {
 
1586
                               // Xalan bug
 
1587
                            }
 
1588
                            else if ("Number__84700".equals(id)) {
 
1589
                               // Xalan bug; extra whitespace. Possibly
 
1590
                               // the same as 
 
1591
                            }
 
1592
                            else if ("Number__84716".equals(id)) {
 
1593
                               // Xalan doesn't support Russian
 
1594
                                // number formatting
 
1595
                            }
 
1596
                            else if ("Number__84717".equals(id)) {
 
1597
                               // Xalan supports more Japanese than the
 
1598
                               // test case does
 
1599
                            }
 
1600
                            else if ("Number__84722".equals(id)
 
1601
                              || "Number__84723".equals(id)
 
1602
                              || "Number__84724".equals(id)
 
1603
                              || "Number__84725".equals(id)
 
1604
                            ) {
 
1605
                                // Acceptable locale support differences
 
1606
                            }
 
1607
                            else if ("Number_NaNOrInvalidValue".equals(id)) {
 
1608
                                // Double bug! Test case is wrong and 
 
1609
                                // Xalan gets this wrong!
 
1610
                            }
 
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!
 
1615
                            }
 
1616
                            else if (id.equals("XSLTFunctions_BooleanFunction")) {
 
1617
                                // I think the test case is wrong; or perhaps unspecified
 
1618
                            } 
 
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
 
1622
                            }
 
1623
                            else if (id.equals("XSLTFunctions__testOn-0.00")) {
 
1624
                                // Possible test suite bug. See
 
1625
                                // http://issues.apache.org/jira/browse/XALANJ-2226
 
1626
                            }
 
1627
                            else {
 
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);
 
1634
                            }
 
1635
                        } // end try
 
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 
 
1640
                            // just skip these.
 
1641
                            continue;
 
1642
                        }
 
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
 
1651
                        }
 
1652
                    } // end else
 
1653
                    
 
1654
                } // end try
 
1655
                catch (MalformedURIException ex) {
 
1656
                    
 
1657
                }
 
1658
                catch (FileNotFoundException ex) {
 
1659
                    // The catalog doesn't always match what's on disk
 
1660
                }
 
1661
                catch (UnknownHostException ex) {
 
1662
                    // A few tests like ProcessingInstruction__78197 
 
1663
                    // point to external DTD subsets that can't be loaded
 
1664
                }
 
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) {
 
1669
                        continue;
 
1670
                    }
 
1671
                    
 
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
 
1676
                        }
 
1677
                        else if ("Sorting__78191".equals(id)
 
1678
                          || "Text__78245".equals(id)
 
1679
                          || "Text__78273".equals(id)
 
1680
                          || "Text__78281".equals(id)
 
1681
                        ) {
 
1682
                            // binds XML namespace to prefix other than xml
 
1683
                        }
 
1684
                        else {
 
1685
                            System.err.println(id + ": " + ex.getMessage());
 
1686
                            throw ex;
 
1687
                        }
 
1688
                    }
 
1689
                }
 
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
 
1699
                            continue;
 
1700
                        }
 
1701
                        else if (cause instanceof MissingResourceException) {
 
1702
                            // Xalan bug;
 
1703
                            // http://issues.apache.org/jira/secure/ManageAttachments.jspa?id=27366
 
1704
                        } 
 
1705
                        else if ("Include_Include_IncludedStylesheetShouldHaveDifferentBaseUri".equals(id)) {
 
1706
                           // This test case is wrong; Uses backslash in URI
 
1707
                        }
 
1708
                        else if ("Elements__89070".equals(id)) {
 
1709
                            // bug fixed in later versions of Xalan
 
1710
                        }
 
1711
                        else if ("Namespace-alias_Namespace-Alias_NSAliasForDefaultWithExcludeResPref".equals(id)) {
 
1712
                           // This test case is wrong; it uses a backslash in a URI 
 
1713
                        }
 
1714
                        else if ("Variables_VariableWithinVariable".equals(id)) {
 
1715
                            // Xalan does not recover from this one
 
1716
                        }
 
1717
                        else if ("BVTs_bvt054".equals(id)) {
 
1718
                            // Xalan bug 
 
1719
                            // http://issues.apache.org/jira/browse/XALANJ-1952 
 
1720
                            continue;
 
1721
                        } 
 
1722
                        else if ("BVTs_bvt094".equals(id)) {
 
1723
                            // Xalan bug 
 
1724
                            // http://issues.apache.org/jira/browse/XALANJ-1953 
 
1725
                            continue;
 
1726
                        } 
 
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
 
1731
                            continue;
 
1732
                        }
 
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 
 
1739
                            continue;
 
1740
                        }
 
1741
                        else if ("ProcessingInstruction_ValueOfandTextWithDoeInProcInstr".equals(id)) {
 
1742
                           // Begins processing instruction data with white space, which XOM doesn't allow   
 
1743
                            continue;
 
1744
                        }
 
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))
 
1750
                          {
 
1751
                           // Xalan doesn't recover from these, though recovery is allowed   
 
1752
                            continue;
 
1753
                        }
 
1754
                        else if ("Output__84306".equals(id)) {
 
1755
                            // Xalan bug
 
1756
                            // http://issues.apache.org/jira/browse/XALANJ-1954
 
1757
                            continue;
 
1758
                        }
 
1759
                        else if ("Output__84014".equals(id)) {
 
1760
                            // Fixed in later version of Xalan than is bundled with JDK  
 
1761
                            continue;
 
1762
                        } 
 
1763
                        else if (cause instanceof MalformedURIException) {
 
1764
                            // Some of the tests generate relative namespace URIs
 
1765
                            // XOM doesn't support
 
1766
                            continue;
 
1767
                        }
 
1768
                        else {
 
1769
                            System.out.println(id + ": " + ex.getMessage());
 
1770
                            System.out.println("in " + style);
 
1771
                            if (cause != null) {
 
1772
                                System.out.println("cause: " + cause.getMessage());                                
 
1773
                            }
 
1774
                            throw ex;
 
1775
                        }
 
1776
                    }
 
1777
                } // end catch
 
1778
                catch (XMLException ex) {
 
1779
                    if ("Text_modified78309".equals(id)) {
 
1780
                       // output is not a full document   
 
1781
                    }
 
1782
                    else {
 
1783
                        System.err.println(id);
 
1784
                        throw ex;
 
1785
                    }
 
1786
                }
 
1787
                
 
1788
            } // end for 
 
1789
            
 
1790
        } // end if 
 
1791
     
 
1792
    } 
 
1793
    
 
1794
    
 
1795
    public void testKeysPerfRepro3()  
 
1796
      throws IOException, ParsingException, XSLException {
 
1797
        
 
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");
 
1803
 
 
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");;
 
1807
 
 
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);
 
1815
     
 
1816
    } 
 
1817
    
 
1818
    
 
1819
    public void testSorting__89749()  
 
1820
      throws IOException, ParsingException, XSLException {
 
1821
        
 
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");
 
1827
 
 
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");;
 
1831
 
 
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); */
 
1839
     
 
1840
    } 
 
1841
    
 
1842
    
 
1843
    public void testToDocumentWithEmptyNodes() {
 
1844
     
 
1845
        try {
 
1846
            XSLTransform.toDocument(new Nodes());
 
1847
            fail("Converted empty nodes to document");
 
1848
        }
 
1849
        catch (XMLException success) {
 
1850
            assertNotNull(success.getMessage());
 
1851
        }
 
1852
        
 
1853
    }
 
1854
    
 
1855
    
 
1856
    public void testToDocumentWithNoRoot() {
 
1857
     
 
1858
        Nodes input = new Nodes();
 
1859
        input.append(new Comment("data"));
 
1860
        try {
 
1861
            XSLTransform.toDocument(new Nodes());
 
1862
            fail("Converted comment to document");
 
1863
        }
 
1864
        catch (XMLException success) {
 
1865
            assertNotNull(success.getMessage());
 
1866
        }
 
1867
        
 
1868
    }
 
1869
    
 
1870
    
 
1871
    public void testToDocumentWithText() {
 
1872
     
 
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);
 
1878
        input.append(root);
 
1879
        input.append(pi);
 
1880
        input.append(new Text("text"));
 
1881
        try {
 
1882
            XSLTransform.toDocument(new Nodes());
 
1883
            fail("Converted text to document");
 
1884
        }
 
1885
        catch (XMLException success) {
 
1886
            assertNotNull(success.getMessage());
 
1887
        }
 
1888
        
 
1889
    }
 
1890
    
 
1891
    
 
1892
    public void testToDocumentWithAttribute() {
 
1893
     
 
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);
 
1899
        input.append(root);
 
1900
        input.append(pi);
 
1901
        input.append(new Attribute("name", "text"));
 
1902
        try {
 
1903
            XSLTransform.toDocument(new Nodes());
 
1904
            fail("Converted text to document");
 
1905
        }
 
1906
        catch (XMLException success) {
 
1907
            assertNotNull(success.getMessage());
 
1908
        }
 
1909
        
 
1910
    }
 
1911
    
 
1912
    
 
1913
    public void testToDocumentWithDocType() {
 
1914
     
 
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);
 
1922
        input.append(root);
 
1923
        input.append(pi);
 
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());
 
1930
        
 
1931
    }
 
1932
    
 
1933
    
 
1934
    public void testToDocumentWithDocTypeInEpilog() {
 
1935
     
 
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);
 
1942
        input.append(root);
 
1943
        input.append(doctype);
 
1944
        input.append(pi);
 
1945
        try {
 
1946
            XSLTransform.toDocument(input);
 
1947
            fail("Allowed doctype in epilog");
 
1948
        }
 
1949
        catch (XMLException success) {
 
1950
            assertNotNull(success.getMessage());
 
1951
        }
 
1952
        
 
1953
    }
 
1954
    
 
1955
    
 
1956
    public void testToDocumentWithDoubleRoot() {
 
1957
     
 
1958
        Nodes input = new Nodes();
 
1959
        Element root = new Element("root");
 
1960
        Comment comment = new Comment("data");
 
1961
        input.append(comment);
 
1962
        input.append(root);
 
1963
        input.append(new Element("root2"));
 
1964
        try {
 
1965
            XSLTransform.toDocument(input);
 
1966
            fail("Allowed two root elements");
 
1967
        }
 
1968
        catch (XMLException success) {
 
1969
            assertNotNull(success.getMessage());
 
1970
        }
 
1971
        
 
1972
    }
 
1973
    
 
1974
    
 
1975
    public void testToDocumentWithSingleRoot() {
 
1976
     
 
1977
        Nodes input = new Nodes();
 
1978
        Element root = new Element("root");
 
1979
        input.append(root);
 
1980
        Document output = XSLTransform.toDocument(input);
 
1981
        assertEquals(root, output.getRootElement());
 
1982
        assertEquals(input.size(), output.getChildCount());
 
1983
        
 
1984
    }
 
1985
    
 
1986
 
 
1987
    public void testToDocumentWithPrologAndEpilog() {
 
1988
     
 
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);
 
1994
        input.append(root);
 
1995
        input.append(pi);
 
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());
 
2001
        
 
2002
    }
 
2003
    
 
2004
    
 
2005
}