Project

Profile

Help

How to connect?
Download (30.7 KB) Statistics
| Branch: | Tag: | Revision:

he / tags / 9.6.0.7 / hen / csource / samples / XsltTestSuiteDriver.cs @ aa733b18

1
using System;
2
using System.IO;
3
using System.Collections;
4
using System.Xml;
5
using Saxon.Api;
6
using TestRunner;
7

    
8
/// <summary>
9
/// This is the test suite driver for running the W3C XSLT test suite against Saxon on .NET.
10
/// Note that the W3C XSLT test suite at the time of writing is available to W3C members only.
11
/// </summary>
12
/// <remarks>
13
/// <para>Before running, carry out the following steps:</para>
14
/// <para>1. Create a subdirectory SaxonResults.net within TestSuiteStagingArea</para>
15
/// <para>2. Copy compare.xsl from samples/styles into that subdirectory</para>
16
/// <para>3. Create a subdirectory SaxonResults within TestSuiteStagingArea</para>
17
/// <para>4. Create a file exceptions.xml within that subdirectory listing tests that
18
/// are not to be run (with a reason). Specimen format
19
/// as follows:</para>
20
/// <para><![CDATA[
21
/// <testcase-exceptions xmlns="http://www.w3.org/2005/05/xslt20-test-catalog">
22
///   <exception>
23
///     <testcase name="atrs24">
24
///       <comment>Expected result wrong, bug 700</comment>
25
///     </testcase>
26
///   </exception>
27
/// </testcase-exceptions>
28
/// ]]></para>
29
/// <para>TODO: use a different exceptions file for .NET, to exclude tests that rely on
30
/// features such as id() support.</para>
31
/// </remarks>
32

    
33

    
34
public class XsltTestSuiteDriver
35
{
36
    static void MainXXX(string[] args)
37
    {
38
        if (args.Length == 0 || args[0].Equals("-?"))
39
        {
40
            Console.WriteLine("XsltTestSuiteDriver testsuiteDir testName?");
41
        }
42

    
43
        new XsltTestSuiteDriver().go(args);
44
    }
45

    
46
    String testSuiteDir;
47
    Processor processor = new Processor();    
48
    Processor schemaAwareProcessor; /* = new Processor(true); */
49
    IFeedbackListener feedback;
50
    FileComparer fileComparer;
51

    
52
    string testPattern = null;
53
    bool xml11 = false;
54

    
55
    StreamWriter results;
56
    StreamWriter log;
57

    
58
    
59

    
60
    /**
61
     * Some tests use schemas that conflict with others, so they can't use the common schema cache.
62
     * These tests are run in a Configuration of their own. (Ideally we would put this list in a
63
     * catalogue file of some kind).
64
     */
65
    
66
        static Hashtable noCacheTests = new Hashtable(30); // HashSet is available in .NET 3.5 and later
67
        static XsltTestSuiteDriver() {
68
            noCacheTests.Add("schemaas20_002_01", true);
69
            noCacheTests.Add("schemaas20_011_02", true);
70
            noCacheTests.Add("schemaas20_025_01", true);
71
            noCacheTests.Add("schemaas20_028_02", true);
72
            noCacheTests.Add("schemaas20_038_02", true);
73
            noCacheTests.Add("schemaas20_041_02", true);
74
            noCacheTests.Add("schemaas20_044_02", true);
75
            noCacheTests.Add("schemaas20_050_02", true);
76
            noCacheTests.Add("schemaas20_109_01", true);
77
            noCacheTests.Add("schemaas20_111_01", true);
78
            noCacheTests.Add("schemaas20_115_01", true);
79
            noCacheTests.Add("schemaas20_116_01", true);
80
            noCacheTests.Add("schemaas20_121_01", true);
81
            noCacheTests.Add("schemaas20_125_01", true);
82
            noCacheTests.Add("schemaas20_157_01", true);
83
            noCacheTests.Add("schemaas20_161_01", true);
84
            noCacheTests.Add("schemaas20_209_01", true);
85
            noCacheTests.Add("schemaas20_221_01", true);
86
            noCacheTests.Add("schemaas20_225_01", true);
87
            noCacheTests.Add("schemaas20_226_01", true);
88
            noCacheTests.Add("schemaas20_228_01", true);
89
            noCacheTests.Add("schemaas20_242_01", true);
90
            noCacheTests.Add("schemaas20_255_01", true);
91
            noCacheTests.Add("schemaas20_260_01", true);
92
            noCacheTests.Add("schemaas20_274_01", true);
93
            noCacheTests.Add("schemaas20_292_01", true);
94
            noCacheTests.Add("schemaas20_332_01", true);
95
            noCacheTests.Add("schema092", true);
96
            noCacheTests.Add("schemainline20_005_01", true);
97
            noCacheTests.Add("schemamatch20_001_01", true);
98
            noCacheTests.Add("schemamatch20_003_01", true);
99
            noCacheTests.Add("schemamatch20_005_01", true);
100
            noCacheTests.Add("schemamatch20_007_01", true);
101
            noCacheTests.Add("schemamatch20_036_01", true);
102
            noCacheTests.Add("schemamatch20_038_01", true);
103
            noCacheTests.Add("schemamatch20_061_01", true);
104
            noCacheTests.Add("schemamatch20_079_01", true);
105
            noCacheTests.Add("schemamatch20_092_01", true);
106
            noCacheTests.Add("schemamatch20_123_01", true);
107
            noCacheTests.Add("schemamatch20_140_01", true);
108
            noCacheTests.Add("schemanodetest20_001_01", true);
109
            noCacheTests.Add("schemanodetest20_023_01", true);
110
            noCacheTests.Add("schvalid001", true);
111
            noCacheTests.Add("schvalid009", true);
112
            noCacheTests.Add("schvalid014", true);
113
            noCacheTests.Add("schvalid015", true);
114
            noCacheTests.Add("schvalid020", true);
115
            noCacheTests.Add("striptype20_003_01", true);
116
            noCacheTests.Add("striptype20_006_01", true);
117
            noCacheTests.Add("striptype20_008_01", true);
118
            noCacheTests.Add("striptype20_011_01", true);
119
            noCacheTests.Add("striptype20_012_01", true);
120
            noCacheTests.Add("striptype20_028_01", true);
121
            noCacheTests.Add("striptype20_039_01", true);
122

    
123
        }
124
    
125

    
126
    public void setFeedbackListener(IFeedbackListener f) {
127
        feedback = f;
128
    }
129

    
130
    private XdmNode getChildElement(XdmNode parent, QName child)
131
    {
132
        IEnumerator e = parent.EnumerateAxis(XdmAxis.Child, child);
133
        return e.MoveNext() ? (XdmNode)e.Current : null;
134
    }
135

    
136

    
137
    public void go(String[] args)
138
    {
139
        int passed = 0;
140
        int failed = 0;
141
        int total = 0;
142

    
143
        
144
        testSuiteDir = args[0];
145
        if (testSuiteDir.EndsWith("/"))
146
        {
147
            testSuiteDir = testSuiteDir.Substring(0, testSuiteDir.Length - 1);
148
        }
149
        Hashtable exceptions = new Hashtable();
150

    
151
        if (args.Length > 1)
152
        {
153
            testPattern = (args[1]); 
154
        }
155

    
156
        for (int i = 0; i < args.Length; i++)
157
        {
158
            if (args[i].Equals("-w"))
159
            {
160
                //showWarnings = true;
161
            }
162
        }
163

    
164
        try
165
        {
166
            schemaAwareProcessor = new Processor(true);
167
        }
168
        catch (Exception)
169
        {
170
            Console.WriteLine("Cannot load Saxon-SA: continuing without it");
171
        }
172

    
173
        processor.SetProperty("http://saxon.sf.net/feature/preferJaxpParser", "true");
174

    
175
        if (schemaAwareProcessor != null)
176
        {
177
            schemaAwareProcessor.SetProperty("http://saxon.sf.net/feature/preferJaxpParser", "true");
178
        }
179
        fileComparer = new FileComparer(processor, testSuiteDir);
180

    
181
        String testURI = "http://www.w3.org/2005/05/xslt20-test-catalog";
182

    
183
        QName testCaseNT = new QName(testURI, "testcase");
184
        QName nameNT = new QName(testURI, "name");
185
        QName inputNT = new QName(testURI, "input");
186
        QName outputNT = new QName(testURI, "output");
187
        QName stylesheetNT = new QName(testURI, "stylesheet");
188
        QName schemaNT = new QName(testURI, "schema");
189
        QName initialModeNT = new QName(testURI, "initial-mode");
190
        QName entryNamedTemplateNT = new QName(testURI, "entry-named-template");
191
        QName sourceDocumentNT = new QName(testURI, "source-document");
192
        QName stylesheetParametersNT = new QName(testURI, "stylesheet-parameters");
193
        QName paramNT = new QName(testURI, "param");
194
        QName resultDocumentNT = new QName(testURI, "result-document");
195
        QName errorNT = new QName(testURI, "error");
196
        QName validationNT = new QName(testURI, "validation");
197
        QName discretionaryItemsNT = new QName(testURI, "discretionary-items");
198
        QName discretionaryFeatureNT = new QName(testURI, "discretionary-feature");
199
        QName discretionaryChoiceNT = new QName(testURI, "discretionary-choice");
200
        QName discretionaryVersionNT = new QName(testURI, "discretionary-version");
201
        QName initialContextNodeNT = new QName(testURI, "initial-context-node");
202

    
203

    
204
        QName fileAtt = new QName("", "file");
205
        QName errorIdAtt = new QName("", "error-id");
206
        QName typeAtt = new QName("", "type");
207
        QName nameAtt = new QName("", "name");
208
        QName behaviorAtt = new QName("", "behavior");
209
        QName specAtt = new QName("", "spec");
210
        QName qnameAtt = new QName("", "qname");
211
        QName modeAtt = new QName("", "mode");
212
        QName validatesAtt = new QName("", "validates");
213
        QName roleAtt = new QName("", "role");
214

    
215
        DocumentBuilder builder = processor.NewDocumentBuilder();
216
        XdmNode exceptionsDoc = builder.Build(new Uri(testSuiteDir + '/' + getResultDirectoryName() + "/exceptions.xml"));
217

    
218
        IEnumerator exceptionTestCases = exceptionsDoc.EnumerateAxis(XdmAxis.Descendant, testCaseNT);
219
        while (exceptionTestCases.MoveNext())
220
        {
221
            XdmNode n = (XdmNode)exceptionTestCases.Current;
222
            String nameAttVal = n.GetAttributeValue(nameAtt);
223
            char[] seps = { ' ', '\n', '\t' };
224
            String[] parts = nameAttVal.Split(seps);
225
            foreach (string p in parts)
226
            {
227
                if (!exceptions.ContainsKey(p))
228
                {
229
                    exceptions.Add(p, "Kilroy");
230
                }
231
            }
232
        }
233

    
234
        XdmNode catalog = builder.Build(new Uri(testSuiteDir + "/catalog.xml"));
235

    
236
        results = new StreamWriter(testSuiteDir + "/SaxonResults.net/results"
237
                    + processor.ProductVersion + ".xml");
238
        log = new StreamWriter(testSuiteDir + "/SaxonResults.net/log"
239
                    + processor.ProductVersion + "n.xml");
240
        log.WriteLine("Testing Saxon " + processor.ProductVersion);
241

    
242
        results.WriteLine("<test-suite-result>");
243
        results.WriteLine(" <implementation name='Saxon-SA' version='" + processor.ProductVersion +
244
                "' anonymous-result-column='false'>");
245
        results.WriteLine("  <organization name='http://www.saxonica.com/' anonymous='false'/>");
246
        results.WriteLine("  <submitter name='Michael Kay' email='mike@saxonica.com'/>");
247
        outputDiscretionaryItems();
248
        results.WriteLine(" </implementation>");
249

    
250
        total = 0;
251
        IEnumerator testCases = catalog.EnumerateAxis(XdmAxis.Descendant, testCaseNT);
252
        while (testCases.MoveNext())
253
        {
254
            total++;
255
        }
256

    
257
        testCases = catalog.EnumerateAxis(XdmAxis.Descendant, testCaseNT);
258
        while (testCases.MoveNext())
259
        {
260
            bool useAssociated = false;
261
            XdmNode testCase = (XdmNode)testCases.Current;
262

    
263
            String testName = getChildElement(testCase, nameNT).StringValue;
264
            if (testPattern != null && !testName.StartsWith(testPattern))
265
            {
266
                continue;
267
            }
268
            if (exceptions.ContainsKey(testName))
269
            {
270
                continue;
271
            }
272
            if (isExcluded(testName))
273
            {
274
                continue;
275
            }
276
            log.WriteLine("Test " + testName);
277
            XdmNode testInput = getChildElement(testCase, inputNT);
278

    
279
            XdmNode stylesheet = getChildElement(testInput, stylesheetNT);
280
            String absXSLName = null;
281
            if (stylesheet == null)
282
            {
283
                useAssociated = true;
284
            }
285
            else
286
            {
287
                absXSLName = testSuiteDir + "/TestInputs/" + stylesheet.GetAttributeValue(fileAtt);
288
            }
289

    
290
            XdmNode sourceDocument = getChildElement(testInput, sourceDocumentNT);
291
            String absXMLName = null;
292
            if (sourceDocument != null)
293
            {
294
                absXMLName = testSuiteDir + "/TestInputs/" + sourceDocument.GetAttributeValue(fileAtt);
295
            }
296

    
297
            bool schemaAware = false;
298
            bool recoverRecoverable = true;
299
            bool backwardsCompatibility = true;
300
            bool supportsDOE = true;
301
            bool recoverSESU0007 = false;
302
            bool useXSLT30 = false;
303
            XdmNode discretionaryItems = getChildElement(testCase, discretionaryItemsNT);
304
            if (discretionaryItems != null)
305
            {
306
                IEnumerator features = discretionaryItems.EnumerateAxis(XdmAxis.Child, discretionaryFeatureNT);
307
                while (features.MoveNext())
308
                {
309
                    XdmNode feature = (XdmNode)features.Current;
310
                    String featureName = feature.GetAttributeValue(nameAtt);
311
                    if ("schema_aware".Equals(featureName) || "Saxon-PE".Equals(featureName))   // TODO: test Saxon-PE properly
312
                    {
313
                        schemaAware = "on".Equals(feature.GetAttributeValue(behaviorAtt));
314
                    }
315
                    else if ("XML_1.1".Equals(featureName))
316
                    {
317
                        xml11 = "on".Equals(feature.GetAttributeValue(behaviorAtt));
318
                    }
319
                    else if ("backwards_compatibility".Equals(featureName))
320
                    {
321
                        backwardsCompatibility = "on".Equals(feature.GetAttributeValue(behaviorAtt));
322
                    }
323
                    else if ("disabling_output_escaping".Equals(featureName))
324
                    {
325
                        supportsDOE = "on".Equals(feature.GetAttributeValue(behaviorAtt));
326
                    }
327
                }
328
                IEnumerator choices = discretionaryItems.EnumerateAxis(
329
                        XdmAxis.Child, discretionaryChoiceNT);
330
                while (choices.MoveNext())
331
                {
332
                    XdmNode choice = (XdmNode)choices.Current;
333
                    String featureName = choice.GetAttributeValue(nameAtt);
334
                    if ("error".Equals(choice.GetAttributeValue(behaviorAtt)))
335
                    {
336
                        recoverRecoverable = false;
337
                    }
338
                    else if ("SESU0007".Equals(featureName))
339
                    {
340
                        recoverSESU0007 = "recovery".Equals(choice.GetAttributeValue(behaviorAtt));
341
                    }
342
                }
343
                IEnumerator versions = discretionaryItems.EnumerateAxis(
344
                        XdmAxis.Child, discretionaryVersionNT);
345
                while (versions.MoveNext())
346
                {
347
                    XdmNode dv = (XdmNode)versions.Current;
348
                    useXSLT30 = ("XSLT30" == dv.GetAttributeValue(specAtt));
349
                }
350
            }
351

    
352
            if (!backwardsCompatibility)
353
            {
354
                // Saxon cannot run with BC switched off
355
                results.WriteLine(" <testcase name='" + testName + "' result='not run' comment='requires backwards-compatibility=off'/>");
356
                continue;
357
            }
358

    
359
            if (!supportsDOE)
360
            {
361
                // Saxon cannot run with DOE switched off
362
                results.WriteLine(" <testcase name='" + testName + "' result='not run' comment='requires disable-output-escaping=off'/>");
363
                continue;
364
            }
365

    
366
            if (recoverSESU0007)
367
            {
368
                // Saxon cannot recover from error SESU0007
369
                results.WriteLine(" <testcase name='" + testName + "' result='not run' comment='requires recovery from error SESU0007'/>");
370
                continue;
371
            }
372

    
373
            XdmNode initialMode = getChildElement(testInput, initialModeNT);
374
            QName initialModeName = null;
375
            if (initialMode != null)
376
            {
377
                String ini = initialMode.GetAttributeValue(qnameAtt);
378
                initialModeName = makeQName(ini, initialMode);
379
            }
380

    
381
            XdmNode initialTemplate = getChildElement(testInput, entryNamedTemplateNT);
382
            QName initialTemplateName = null;
383
            if (initialTemplate != null)
384
            {
385
                String ini = initialTemplate.GetAttributeValue(qnameAtt);
386
                initialTemplateName = makeQName(ini, initialTemplate);
387
            }
388

    
389
            XdmNode initialContextNode = getChildElement(testInput, initialContextNodeNT);
390
            String initialContextPath = null;
391
            if (initialContextNode != null)
392
            {
393
                initialContextPath = initialContextNode.StringValue;
394
            }
395

    
396
            XdmNode validation = getChildElement(testInput, validationNT);
397
            String validationMode = null;
398
            if (validation != null)
399
            {
400
                validationMode = validation.GetAttributeValue(modeAtt);
401
            }
402

    
403
            Hashtable paramTable = null;
404
            XdmNode paramList = getChildElement(testInput, stylesheetParametersNT);
405
            if (paramList != null)
406
            {
407
                paramTable = new Hashtable(5);
408
                IEnumerator paramIter = paramList.EnumerateAxis(XdmAxis.Child, paramNT);
409
                while (paramIter.MoveNext())
410
                {
411
                    XdmNode param = (XdmNode)paramIter.Current;
412
                    QName name = makeQName(param.GetAttributeValue(qnameAtt), param);
413
                    String value = param.StringValue;
414
                    paramTable.Add(name, value);
415
                }
416
            }
417

    
418
            IEnumerator schemas = testInput.EnumerateAxis(XdmAxis.Child, schemaNT);
419
            while (schemas.MoveNext())
420
            {
421
                XdmNode schema = (XdmNode)schemas.Current;
422
                if (schema == null)
423
                {
424
                    break;
425
                }
426
                schemaAware = true;
427
                String role = schema.GetAttributeValue(roleAtt);
428
                if (("source-validator".Equals(role) || "source-reference".Equals(role))
429
                    /* && schema.GetAttributeValue(validatesAtt) != null */)
430
                {
431
                    validationMode = "strict";
432
                    // TODO: control which source documents are validated...
433
                }
434
            }
435
            XdmNode testOutput = getChildElement(testCase, outputNT);
436
            XdmNode resultDocument = getChildElement(testOutput, resultDocumentNT);
437
            // TODO: handle alternative result documents
438
            String refFileName = null;
439
            String outFileName;
440
            String comparator = "xml";
441
            if (resultDocument != null)
442
            {
443
                String relativePath = resultDocument.GetAttributeValue(fileAtt);
444
                int slash = relativePath.IndexOf('/');
445
                if (slash > 0)
446
                {
447
                    String relativeDir = relativePath.Substring(0, slash);
448
                    String fullDir = testSuiteDir + '/' + getResultDirectoryName() + "/" + relativeDir;
449
                    if (!Directory.Exists(fullDir))
450
                    {
451
                        Directory.CreateDirectory(fullDir);
452
                    }
453
                }
454
                refFileName = testSuiteDir + "/ExpectedTestResults/" + relativePath;
455
                outFileName = testSuiteDir + '/' + getResultDirectoryName() + "/" + relativePath;
456
                comparator = resultDocument.GetAttributeValue(typeAtt);
457
            }
458
            else
459
            {
460
                outFileName = testSuiteDir + '/' + getResultDirectoryName() + "/temp.out";
461
            }
462
            XdmNode error = getChildElement(testOutput, errorNT);
463
            String expectedError = null;
464
            if (error != null)
465
            {
466
                expectedError = error.GetAttributeValue(errorIdAtt);
467
            }
468
            bool success;
469
            Exception xsltOutcome =
470
                runXSLT(testName, absXMLName, absXSLName, initialModeName, initialTemplateName,
471
                    outFileName, paramTable, initialContextPath,
472
                    useAssociated, schemaAware, validationMode, recoverRecoverable, useXSLT30);
473
            if (xsltOutcome == null)
474
            {
475
                success = true;
476
                if (expectedError != null && resultDocument == null)
477
                {
478
                    log.WriteLine("Test failed. Expected error " + expectedError + ", got success");
479
                    feedback.Feedback(passed, failed++, total);
480
                    success = false;
481
                    results.WriteLine(" <testcase name='" + testName +
482
                            "' result='differ' comment='Expected error " +
483
                            expectedError + ", got success'/>");
484
                }
485
                else
486
                {
487
                    feedback.Feedback(passed++, failed, total);
488
                }
489
            }
490
            else
491
            {
492
                String errorCode = null;
493
                if (xsltOutcome is StaticError)
494
                {
495
                    errorCode = ((StaticError)xsltOutcome).ErrorCode.LocalName;
496
                }
497
                else if (xsltOutcome is DynamicError)
498
                {
499
                    errorCode = ((DynamicError)xsltOutcome).ErrorCode.LocalName;
500
                }
501
                if (expectedError != null && errorCode != null && errorCode.Equals(expectedError))
502
                {
503
                    feedback.Feedback(passed++, failed, total);
504
                    log.WriteLine("Test succeeded (" + expectedError + ')');
505
                    results.WriteLine(" <testcase name='" + testName +
506
                            "' result='full' comment='Error " +
507
                            expectedError + " as expected'/>");
508
                }
509
                else if (expectedError != null)
510
                {
511
                    feedback.Feedback(passed++, failed, total);
512
                    log.WriteLine("Test succeeded (??) (expected " + expectedError + ", got " + errorCode + ')');
513
                    results.WriteLine(" <testcase name='" + testName +
514
                            "' result='different-error' comment='Expected " +
515
                            expectedError + " got " + errorCode + "'/>");
516
                }
517
                else
518
                {
519
                    feedback.Feedback(passed, failed++, total);
520
                    log.WriteLine("Test failed. Expected success, got " + errorCode);
521
                    results.WriteLine(" <testcase name='" + testName +
522
                            "' result='differ' comment='Expected success, got " +
523
                            errorCode + "'/>");
524
                    results.WriteLine(" <!--" + xsltOutcome.Message + "-->");
525
                }
526
                success = false;
527
                continue;
528
            }
529

    
530

    
531
            if (success)
532
            {
533
                String outcome = fileComparer.compare(outFileName, refFileName, comparator);
534
                if (outcome == "OK")
535
                {
536
                    results.WriteLine(" <testcase name='" + testName + "' result='full'/>");
537
                }
538
                else if (outcome.StartsWith("#"))
539
                {
540
                    results.WriteLine(" <testcase name='" + testName + "' result='full' + comments='" + outcome.Substring(1) + "/>");
541
                }
542
                else
543
                {
544
                    results.WriteLine(" <testcase name='" + testName + "' result='differ' comments='" + outcome + "'/>");
545
                }
546
            }
547

    
548
        }
549

    
550
        results.WriteLine("</test-suite-result>");
551
        results.Close();
552
        log.Close();
553

    
554
        //} 
555
    }
556

    
557
    private static QName makeQName(String lexical, XdmNode element)
558
    {
559
        if (lexical.IndexOf(":") >= 0)
560
        {
561
            return new QName(lexical, element);
562
        }
563
        else
564
        {
565
            return new QName("", lexical);
566
        }
567
    }
568

    
569
    protected string getResultDirectoryName()
570
    {
571
        return "SaxonResults.Net";
572
    }
573

    
574
    protected bool isExcluded(String testName)
575
    {
576
        return false;
577
    }
578

    
579
    /// <summary>
580
    /// Run the transformation
581
    /// </summary>
582
    /// <param name="testName"></param>
583
    /// <param name="xml"></param>
584
    /// <param name="xsl"></param>
585
    /// <param name="initialMode"></param>
586
    /// <param name="initialTemplate"></param>
587
    /// <param name="outfile"></param>
588
    /// <param name="paramTable"></param>
589
    /// <param name="initialContextPath"></param>
590
    /// <param name="useAssociated"></param>
591
    /// <param name="schemaAware"></param>
592
    /// <param name="validationMode"></param>
593
    /// <param name="recoverRecoverable"></param>
594
    /// <returns>Either null, indicating success, or an Exception object with information about the failure</returns>
595

    
596
    protected Exception runXSLT(String testName, String xml, String xsl, QName initialMode,
597
                           QName initialTemplate, String outfile, Hashtable paramTable, String initialContextPath,
598
                           bool useAssociated, bool schemaAware,
599
                           String validationMode, bool recoverRecoverable, bool useXSLT30)
600
    {
601
        Serializer sr = new Serializer();
602
        sr.SetOutputFile(outfile);
603
        Processor f;
604
        if (noCacheTests.ContainsKey(testName)) {
605
            //create a custom Processor to avoid schema caching
606
            f = new Processor(true);
607
        }
608
        else if (schemaAware)
609
        {
610
            f = schemaAwareProcessor;
611
            if (f == null)
612
            {
613
                return new DynamicError("Saxon-SA not available");
614
            }
615
        }
616
        else if (xml11)
617
        {
618
            f = processor;
619
            // Use an Xml 1.1 processor
620
        }
621
        else
622
        {
623
            f = processor;
624
        }
625
        
626

    
627
        XdmNode source = null;
628

    
629
        IList errors = new ArrayList();
630
        XsltCompiler compiler = f.NewXsltCompiler();
631
        compiler.SchemaAware = schemaAware;
632
        compiler.ErrorList = errors;
633
        if (useXSLT30)
634
        {
635
            compiler.XsltLanguageVersion = "3.0";
636
        }
637
        XsltExecutable sheet = null;
638
        XsltTransformer inst;
639

    
640
        if (useAssociated)
641
        {
642
            try
643
            {
644
                source = buildSource(f.NewDocumentBuilder(), xml, validationMode);
645
            }
646
            catch (Exception e)
647
            {
648
                log.WriteLine("Failed to build source document: " + e.Message);
649
                return e;
650
            }
651
            try
652
            {
653
                sheet = compiler.CompileAssociatedStylesheet(source);
654
            }
655
            catch (Exception e)
656
            {
657
                log.WriteLine("Failed to compile stylesheet: " + e.Message);
658
                if (errors.Count > 0)
659
                {
660
                    return (Exception)errors[0];
661
                    //QName code = ((StaticError)errors[0]).ErrorCode;
662
                    //(code == null ? "Failed to compile stylesheet: " + e.Message : code.LocalName);
663
                }
664
                else
665
                {
666
                    return e;
667
                }
668
            }
669
        }
670
        else
671
        {
672
            Stream stream = new FileStream(xsl, FileMode.Open, FileAccess.Read);
673
            compiler.BaseUri = new Uri(xsl);
674
            try
675
            {
676
                sheet = compiler.Compile(stream);
677
            }
678
            catch (StaticError e)
679
            {
680
                if (errors.Count > 0)
681
                {
682
                    return ((StaticError)errors[0]);
683
                }
684
                else
685
                {
686
                    log.WriteLine(e.Message);
687
                    return e;
688
                }
689
            }
690
            catch (Exception e2)
691
            {
692
                log.WriteLine("Unexpected CRASH: " + e2.Message);
693
                log.WriteLine(e2.StackTrace);
694
                return e2;
695
            }
696
            finally
697
            {
698
                stream.Close();
699
            }
700
        }
701
        if (initialContextPath != null)
702
        {
703
            if (source == null && xml != null)
704
            {
705
                try
706
                {
707
                    source = buildSource(f.NewDocumentBuilder(), xml, validationMode);
708
                }
709
                catch (Exception e)
710
                {
711
                    log.WriteLine("Failed to build source document: " + e.Message);
712
                    return e;
713
                }
714
            }
715

    
716
            XPathCompiler xc = f.NewXPathCompiler();
717
            XPathExecutable exp = xc.Compile(initialContextPath);
718
            XPathSelector xpe = exp.Load();
719
            xpe.ContextItem = source;
720
            XdmNode node = (XdmNode)xpe.EvaluateSingle();
721
            source = node;
722

    
723
        }
724

    
725
        inst = sheet.Load();
726
        if (source != null) {
727
            inst.InitialContextNode = source;
728
        }
729

    
730
        if (source == null && xml != null)
731
        {
732
            Stream stream = new FileStream(xml, FileMode.Open, FileAccess.Read);
733
            inst.SetInputStream(stream, new Uri(xml));
734
        }
735

    
736
        
737
        if (initialMode != null)
738
        {
739
            inst.InitialMode = initialMode;
740
        }
741
        if (initialTemplate != null)
742
        {
743
            try
744
            {
745
                inst.InitialTemplate = initialTemplate;
746
            }
747
            catch (DynamicError e)
748
            {
749
                return e;
750
            }
751
        }
752
        if (paramTable != null)
753
        {
754
            foreach (DictionaryEntry de in paramTable)
755
            {
756
                inst.SetParameter((QName)de.Key, new XdmAtomicValue(de.Value.ToString()));
757
            }
758
        }
759
        
760
        inst.BaseOutputUri = new Uri(outfile);
761
        inst.RecoveryPolicy = recoverRecoverable ? RecoveryPolicy.RecoverSilently : RecoveryPolicy.DoNotRecover;
762

    
763
        if ("strict" == validationMode)
764
        {
765
            inst.SchemaValidationMode = SchemaValidationMode.Strict;
766
        }
767
        else
768
        {
769
            inst.SchemaValidationMode = SchemaValidationMode.None;
770
        }
771

    
772
        //inst.setURIResolver(factory.getURIResolver());
773
        //inst.setErrorListener(errorListener);
774
        //((Controller)inst).setRecoveryPolicy(recoverRecoverable ? Configuration.RECOVER_SILENTLY : Configuration.DO_NOT_RECOVER);
775
        // To avoid test results being dependent on the date and time (and timezone), set a fixed
776
        // date and time for the run
777
        //((Controller)inst).setCurrentDateTime(new DateTimeValue("2005-01-01T12:49:30.5+01:00"));
778

    
779
        try
780
        {
781
            inst.Run(sr);
782
        }
783
        catch (DynamicError e)
784
        {
785
            log.WriteLine(e.Message);
786
            return e;
787
        }
788
        catch (Exception e2)
789
        {
790
            log.WriteLine("Unexpected CRASH: " + e2.Message);
791
            log.WriteLine(e2.StackTrace);
792
            return e2;
793
        }
794
        return null;    // indicating success
795
    }
796

    
797
    /**
798
     * Construct source object. This method allows subclassing e.g. to build a DOM or XOM source.
799
     * @param xml
800
     * @return
801
     * @throws XPathException
802
     */
803

    
804
    protected XdmNode buildSource(DocumentBuilder builder, String xml, String validationMode)
805
    {
806
        if ("strict".Equals(validationMode))
807
        {
808
            builder.SchemaValidationMode = SchemaValidationMode.Strict;
809
        }
810
        else
811
        {
812
            builder.SchemaValidationMode = SchemaValidationMode.None;
813
        }
814
        return builder.Build(new Uri(xml));
815
    }
816

    
817

    
818
    private void outputDiscretionaryItems()
819
    {
820
        results.WriteLine("  <discretionary-items/>");
821
    }
822

    
823

    
824

    
825

    
826
}
827

    
828

    
(24-24/24)