Project

Profile

Help

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

he / src / main / c / samples / cppTests / testXSLT30.cpp @ 01d6fdb6

1

    
2
#include <sstream>
3
#include <stdio.h>
4

    
5
#include "../../Saxon.C.API/SaxonProcessor.h"
6
#include "../../Saxon.C.API/XdmValue.h"
7
#include "../../Saxon.C.API/XdmItem.h"
8
#include "../../Saxon.C.API/XdmNode.h"
9
#include "../../Saxon.C.API/XdmFunctionItem.h"
10
#include "../../Saxon.C.API/XdmMap.h"
11
#include "../../Saxon.C.API/XdmArray.h"
12
#include "cppExtensionFunction.h"
13
#include "CppTestUtils.h"
14
#include <string>
15
#include <thread>
16

    
17

    
18

    
19

    
20
using namespace std;
21

    
22

    
23
#ifdef MEM_DEBUG
24
#define new new(__FILE__, __LINE__)
25
#endif
26

    
27

    
28

    
29

    
30

    
31
char fname[] = "_nativeCall";
32
char funcParameters[] = "(Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;)Ljava/lang/Object;";
33

    
34
JNINativeMethod cppMethods[] =
35
        {
36
                {
37
                        fname,
38
                        funcParameters,
39
                        (void *) &cppExtensionFunction::cppNativeCall
40
                }
41
        };
42

    
43

    
44
/*
45
* Test transform to String. Source and stylesheet supplied as arguments
46
*/
47
void testApplyTemplatesString1(Xslt30Processor *trans, sResultCount *sresult) {
48

    
49
    cout << "Test: testApplyTemplatesString1:" << endl;
50
//    trans->setupXslMessage(false);
51

    
52
    XsltExecutable *executable = trans->compileFromFile("test.xsl");
53

    
54
    if(executable== nullptr) {
55
        sresult->failure++;
56
        sresult->failureList.push_back("testApplyTemplatesString1");
57
        cerr << "testApplyTemplatesString1 NULL found" << endl;
58
        if (trans->exceptionOccurred()) {
59
            cerr <<"testApplyTemplatesString1 error: "<< trans->getErrorMessage() << endl;
60
        }
61
        return;
62
    }
63
    executable->setInitialMatchSelectionAsFile("cat.xml");
64

    
65
    const char *output = executable->applyTemplatesReturningString();
66
    if (output == nullptr) {
67
        printf("result is null ====== FAIL ====== \n");
68
        sresult->failure++;
69
        fflush(stdout);
70
        sresult->failureList.push_back("testApplyTemplatesString1-0");
71
    } else if (string(output).find(string("<out>text2</out>")) != std::string::npos) {
72
        printf("%s", output);
73
        printf("result is OK \n");
74
        sresult->success++;
75
        delete output;
76
    } else {
77
        printf("result is null ====== FAIL ====== \n");
78
        sresult->failure++;
79
        sresult->failureList.push_back("testApplyTemplatesString1-1");
80
            std::cout<<"output="<<output<<std::endl;
81
        delete output;
82
    }
83
    fflush(stdout);
84
    delete executable;
85

    
86
}
87

    
88
/*
89
* Test transform to String. Source and stylesheet supplied as arguments
90
*/
91
void testTransformToStringExtensionFunc(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
92

    
93
    cout << endl << "Test: TransformToStringExtensionFunc:" << endl;
94
    bool nativeFound = processor->registerNativeMethods(SaxonProcessor::sxn_environ->env,
95
                                                        "com/saxonica/functions/extfn/cpp/NativeCall",
96
                                                        cppMethods, sizeof(cppMethods) / sizeof(cppMethods[0]));
97

    
98
    XsltExecutable * executable = trans->compileFromFile("testExtension.xsl");
99

    
100
    if(executable== nullptr) {
101
        sresult->failure++;
102
        sresult->failureList.push_back("TransformToStringExtensionFunc");
103
        cerr << "TransformToStringExtensionFunc NULL found" << endl;
104
        if (trans->exceptionOccurred()) {
105
            cerr <<"TransformToStringExtensionFunc error: "<< trans->getErrorMessage() << endl;
106
        }
107
        return;
108
    }
109

    
110
    executable->setProperty("extc", "home/ond1/work/svn/latest9.9-saxonc/samples/cppTests/cppExtensionFunction");
111

    
112
    if (nativeFound) {
113
        const char *output = executable->transformFileToString("cat.xml");
114

    
115
        if (output == nullptr) {
116
            SaxonProcessor::sxn_environ->env->ExceptionDescribe();
117
            printf("result is null ====== FAIL ======  \n");
118
            sresult->failure++;
119
            sresult->failureList.push_back("testTransformToStringExnteionFunc");
120
        } else {
121
            sresult->success++;
122
            printf("result is OK \n");
123
        }
124
        fflush(stdout);
125
        delete output;
126
    } else {
127
        printf("native Class not foun ====== FAIL ====== ");
128
        sresult->failure++;
129
        sresult->failureList.push_back("testTransformToStringExtensionFunc");
130
    }
131
    delete executable;
132
}
133

    
134

    
135
/*
136
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
137
*/
138
void testApplyTemplatesString2(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
139

    
140
    cout << "Test: testApplyTemplatesString2:" << endl;
141

    
142

    
143
    XdmNode *input = processor->parseXmlFromFile("cat.xml");
144

    
145
    if (input== nullptr) {
146
        cout << "Source document is null." << endl;
147
        if(processor->exceptionOccurred()) {
148
            cerr<<processor->getErrorMessage()<<endl;
149
        }
150
        sresult->failure++;
151
        sresult->failureList.push_back("testApplyTemplatesString2");
152
        return;
153

    
154
    }
155
    XsltExecutable * executable = trans->compileFromFile("test.xsl");
156
    if(executable == nullptr) {
157
        sresult->failure++;
158
        sresult->failureList.push_back("testApplyTemplatesString2");
159
        if (trans->exceptionOccurred()) {
160
            cerr <<"testApplyTemplatesString2 error: "<< trans->getErrorMessage() << endl;
161
        }
162
        delete input;
163
        return;
164
    }
165

    
166
    executable->setInitialMatchSelection((XdmValue *) input);
167

    
168
    const char *output = executable->applyTemplatesReturningString();
169
    if (output== nullptr) {
170
        printf("result is null ====== FAIL ======  \n");
171
        sresult->failureList.push_back("testApplyTemplatesString2");
172
        sresult->failure++;
173
    } else {
174
        sresult->success++;
175
        printf("%s", output);
176
        printf("result is OK \n");
177
        delete output;
178
    }
179
    fflush(stdout);
180
    delete input;
181
    delete executable;
182
}
183

    
184
/*
185
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
186
Should be error. Stylesheet file does not exist
187
*/
188
void testApplyTemplates2a_Error(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
189

    
190
    cout << "Test: TransformToString2a_Error:" << endl;
191

    
192
    XdmNode *input = processor->parseXmlFromFile("cat.xml");
193

    
194
    if (input == nullptr) {
195
        cout << "Source document is null. ====== FAIL ======" << endl;
196
        if(processor->exceptionOccurred()) {
197
            cerr<<processor->getErrorMessage()<<endl;
198
        }
199
        sresult->failure++;
200
        sresult->failureList.push_back("testApplyTemplates2a_Error");
201
        trans->exceptionClear();
202
        return;
203
    }
204
    XsltExecutable * executable = trans->compileFromFile("test-error.xsl");
205

    
206
    if(executable== nullptr) {
207
        printf("Expected result is null \n");
208
        sresult->success++;
209

    
210
        if (trans->exceptionOccurred()) {
211
            cerr <<"testApplyTemplates2a_Error error: "<< trans->getErrorMessage() << endl;
212
        }
213
        delete input;
214
        trans->exceptionClear();
215
        return;
216
    }
217

    
218
    sresult->failure++;
219
    sresult->failureList.push_back("testApplyTemplates2a_Error");
220
    fflush(stdout);
221

    
222
    delete executable;
223

    
224
}
225

    
226
/*
227
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
228
Should be error. Source file does not exist
229
*/
230
void testTransformToString2b(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
231

    
232
    cout << endl << "Test: TransformToString2b:" << endl;
233

    
234
    const char * result  = trans->transformFileToString("cat-error.xml", "test-error.xsl");
235

    
236
    if(result== nullptr) {
237
        if(trans->exceptionOccurred()) {
238
            cerr<<"Expected failure = "<<trans->getErrorMessage()<<endl;
239
        }
240
        sresult->success++;
241
        trans->exceptionClear();
242
        return;
243
    }else {
244
        delete result;
245
        sresult->failure++;
246
        sresult->failureList.push_back("testTransformToString2b");
247
        cerr << "testTransformToString2b NULL found" << endl;
248
        if (trans->exceptionOccurred()) {
249
            cerr <<"Error: "<< trans->getErrorMessage() << endl;
250
        }
251
        trans->exceptionClear();
252
        return;
253
    }
254

    
255

    
256
}
257

    
258

    
259
/*
260
* Test transform to String. stylesheet supplied as argument. Source supplied as xml string
261
and integer parmater created and supplied
262
*/
263
void testTransformToString3(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
264

    
265
    cout << endl << "Test: testTransformToString3" << endl;
266

    
267
    XdmNode *inputi = processor->parseXmlFromString(
268
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
269

    
270
    if (inputi== nullptr) {
271
        cout << "Source document inputi is null." << endl;
272
        if (trans->exceptionOccurred()) {
273
            cerr <<"testTransformToString3 error: "<< trans->getErrorMessage() << endl;
274
        }
275
        sresult->failure++;
276
        sresult->failureList.push_back("testTransformToString3");
277

    
278
        return;
279
    }
280

    
281
    XdmAtomicValue *value1 = processor->makeIntegerValue(10);
282

    
283
    if (value1== nullptr) {
284
        cout << "value1 is null." << endl;
285
        if (trans->exceptionOccurred()) {
286
            cerr <<"testTransformToString3 error: "<< trans->getErrorMessage() << endl;
287
        }
288
        sresult->failure++;
289
        sresult->failureList.push_back("testTransformToString3");
290
        delete inputi;
291
        return;
292
    }
293

    
294
    XsltExecutable * executable = trans->compileFromFile("test.xsl");
295

    
296
    if(executable== nullptr) {
297
        sresult->failure++;
298
        sresult->failureList.push_back("testTransformToString3");
299
        cerr << "testTransformToString3 NULL found" << endl;
300
        if (trans->exceptionOccurred()) {
301
            cerr <<"testTransformToString3 error: "<< trans->getErrorMessage() << endl;
302
        }
303
        delete value1;
304
        return;
305
    }
306

    
307

    
308
    executable->setParameter("numParam", (XdmValue *) value1);
309

    
310
    executable->setInitialMatchSelection((XdmNode *) inputi);
311
    const char *output = executable->applyTemplatesReturningString();
312
    if (output == nullptr) {
313
        printf("result is null ====== FAIL ====== \n");
314
        sresult->failureList.push_back("testTransformToString3");
315
    } else {
316
        printf("%s", output);
317
        printf("result is OK \n");
318
        delete output;
319
    }
320
    fflush(stdout);
321

    
322
    delete value1;
323
    delete inputi;
324
    inputi = nullptr;
325
    delete executable;
326

    
327
}
328

    
329
/*
330
* Test transform to String. stylesheet supplied as argument. Source supplied as xml string
331
and integer parmater created and supplied
332
*/
333
void testTransformToString4(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
334

    
335
    cout <<  "Test: testTransformToString4:" << endl;
336

    
337
    XdmNode *input = processor->parseXmlFromString(
338
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
339

    
340
    if (input== nullptr) {
341
        cout << "Source document is null. ====== FAIL ====== " << endl;
342
        sresult->failure++;
343
        sresult->failureList.push_back("testTransformToString4");
344
    }
345

    
346

    
347
    XdmValue *values = new XdmValue();
348
    XdmAtomicValue * a1 = processor->makeIntegerValue(10);
349
    XdmAtomicValue * a2 = processor->makeIntegerValue(5);
350
    XdmAtomicValue * a3 = processor->makeIntegerValue(6);
351
    XdmAtomicValue * a4 = processor->makeIntegerValue(7);
352
    values->addXdmItem(a1);
353
    values->addXdmItem(a2);
354
    values->addXdmItem(a3);
355
    values->addXdmItem(a4);
356

    
357

    
358
    XdmNode *sheet = processor->parseXmlFromFile("test2.xsl");
359

    
360
    XsltExecutable *executable = trans->compileFromXdmNode(sheet);
361

    
362
    if(executable== nullptr) {
363
        sresult->failure++;
364
        sresult->failureList.push_back("testTransformToString4");
365
        cerr << "testTransformToString4 NULL found" << endl;
366
        if (trans->exceptionOccurred()) {
367
            cerr <<"testTransformToString4 error: "<< trans->getErrorMessage() << endl;
368
        }
369
        delete values;
370
        return;
371
    }
372

    
373
    executable->setParameter("values", (XdmValue *) values);
374
    executable->setInitialMatchSelection((XdmNode *) input);
375
    const char *output = executable->applyTemplatesReturningString();
376
    if (output== nullptr) {
377
        printf("result is null \n");
378
        sresult->failure++;
379
        sresult->failureList.push_back("testTransformToString4");
380
    } else {
381
        printf("%s", output);
382
        printf("result is OK \n");
383
        delete output;
384
    }
385
    fflush(stdout);
386
    delete sheet;
387
    delete executable;
388
    delete input;
389
    delete values;
390

    
391
}
392

    
393
void testTransformFromstring(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
394
    cout << endl << "Test: testTransfromFromstring: "<< endl;
395

    
396
    if(processor == nullptr) {
397
        cout<<" processor is null"<<endl;
398
        return;
399

    
400
    }
401
    XdmNode *input = processor->parseXmlFromString(
402
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
403

    
404
    if(input == nullptr) {
405
        sresult->failure++;
406
        sresult->failureList.push_back("testTransformFromstring");
407
        cerr << "testTransformFromstring NULL found" << endl;
408
        if (trans->exceptionOccurred()) {
409
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
410
        }
411
        return;
412
    }
413

    
414
    XsltExecutable * executable = trans->compileFromString(
415
            "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>       <xsl:param name='values' select='(2,3,4)' /><xsl:output method='xml' indent='yes' /><xsl:template match='*'><output><xsl:for-each select='$values' ><out><xsl:value-of select='. * 3'/></out></xsl:for-each></output></xsl:template></xsl:stylesheet>");
416

    
417
    if(executable== nullptr) {
418
        sresult->failure++;
419
        sresult->failureList.push_back("testTransformFromstring");
420
        cerr << "testTransformFromstring NULL found" << endl;
421
        if (trans->exceptionOccurred()) {
422
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
423
        }
424
        delete input;
425
        return;
426
    }
427

    
428
    const char *output = executable->transformToString((XdmNode *) input);
429
    if (output== nullptr) {
430
        printf("result is null ====== FAIL ====== \n");
431
        sresult->failure++;
432
        sresult->failureList.push_back("testTransformFromString");
433
        if (trans->exceptionOccurred()) {
434
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
435
        }
436
    } else {
437
        printf("%s", output);
438
        printf("result is OK \n");
439
        sresult->success++;
440
        delete output;
441
    }
442
    fflush(stdout);
443
    delete input;
444
    delete executable;
445

    
446

    
447
}
448

    
449
//Test case has error in the stylesheet
450
void testTransformFromstring2Err(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
451
    cout << endl << "Test: testTransfromFromstring2-Error:" << endl;
452

    
453
    XsltExecutable * executable = trans->compileFromString(
454
            "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>       <xsl:param name='values' select='(2,3,4)' /><xsl:output method='xml' indent='yes' /><xsl:template match='*'><output><xsl:for-each select='$values' ><out><xsl:value-of select='. * 3'/></out><xsl:for-each></output></xsl:template><xsl:stylesheet>");
455

    
456
    if(executable== nullptr) {
457
        sresult->success++;
458

    
459
        if (trans->exceptionOccurred()) {
460
            cerr <<"Error expected: "<< trans->getErrorMessage() << endl;
461
        }
462
        trans->exceptionClear();
463
        return;
464
    }
465

    
466

    
467
    sresult->failure++;
468
    sresult->failureList.push_back("testTransfromFromstring2-Error");
469
    delete executable;
470

    
471
}
472

    
473
void testTrackingOfValueReference(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
474

    
475
    cout << endl << "Test: TrackingOfValueReference:" << endl;
476
    ostringstream test;
477
    ostringstream valStr;
478
    ostringstream name;
479
    for (int i = 0; i < 10; i++) {
480
        test << "v" << i;
481
        valStr << "<out><person>text1</person><person>text2</person><person>text3</person><value>" << test.str()
482
               << "</value></out>";
483
        name << "value" << i;
484

    
485
        XdmValue *values = (XdmValue *) processor->parseXmlFromString(valStr.str().c_str());
486
        //cout<<"Name:"<<name.str()<<", Value:"<<values->getHead()->getStringValue()<<endl;
487
        trans->setParameter(name.str().c_str(), values);
488
        test.str("");
489
        valStr.str("");
490
        name.str("");
491

    
492
        if(values == nullptr) {
493
            cerr<<"TrackingOfValueReference failed to create XdmNode object" <<endl;
494
            sresult->failure++;
495
            sresult->failureList.push_back("TrackingOfValueReference");
496

    
497
        }
498
    }
499

    
500
    std::map<std::string, XdmValue *> parMap = trans->getParameters();
501
    if (parMap.size() > 0) {
502
        //cout << "Parameter size: " << parMap.size() << endl;
503
        //cout << "Parameter size: " << parMap.size()<< endl;//", Value:"<<trans->getParameters()["value0"]->getHead()->getStringValue()<<endl;
504
        ostringstream name1;
505
        for (int i = 0; i < 10; i++) {
506
            name1 << "sparam:value" << i;
507
            cout << " i:" << i << " Map size:" << parMap.size() << ", ";
508
            XdmValue *valuei = parMap[name1.str()];
509
            if (valuei != nullptr) {
510
                cout << name1.str();
511
                if (valuei->itemAt(0) != nullptr)
512
                    cout << "= " << valuei->itemAt(0)->getStringValue();
513
                cout << endl;
514

    
515
            } else {
516
                sresult->failure++;
517
                std::cerr << "trackingValueReference ====== FAIL ======" << std::endl;
518
                sresult->failureList.push_back("testTrackingOfValueReference");
519
                return;
520
            }
521
            name1.str("");
522
        }
523
    }
524
    if(parMap.size() > 0 ) {
525
        cerr<<"Deleting map"<<endl;
526
        trans->clearParameters(true);
527
    }
528
    sresult->success++;
529
}
530

    
531
/*Test case should be error.*/
532
void testTrackingOfValueReferenceError(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
533
    trans->clearParameters();
534

    
535
    cout << endl << "Test: TrackingOfValueReference-Error:" << endl;
536
    cout << "Parameter Map size: " << (trans->getParameters().size()) << endl;
537
    ostringstream test;
538
    ostringstream valStr;
539
    ostringstream name;
540
    for (int i = 0; i < 2; i++) {
541
        test << "v" << i;
542
        valStr << "<out><person>text1</person><person>text2</person><person>text3</person><value>" << test.str()
543
               << "<value></out>";
544
        name << "value" << i;
545

    
546
        XdmValue *values = (XdmValue *) processor->parseXmlFromString(valStr.str().c_str());
547
        if(values == nullptr) {
548
            cerr<<"values NULL ====== FAIL ======="<<endl;
549
            processor->exceptionClear();
550
        } else {
551
            trans->setParameter(name.str().c_str(), values);
552
        }
553
        test.str("");
554
        valStr.str("");
555
        name.str("");
556
    }
557
    std::map<std::string, XdmValue *> parMap = trans->getParameters();
558
    cout << "Parameter Map size: " << parMap.size() << endl;
559

    
560
    ostringstream name1;
561
    bool errorFound = false;
562
    for (int i = 0; i < 2; i++) {
563
        name1 << "sparam:value" << i;
564
        cout << " i:" << i << " Map size:" << parMap.size() << ", ";
565
        try {
566

    
567
            XdmValue *valuei = parMap.at(name1.str());
568
            if (valuei != NULL) {
569
                cout << name1.str();
570
                if (valuei->itemAt(0) != NULL)
571
                    cout << "= " << valuei->itemAt(0)->getStringValue();
572
                cout << endl;
573
            }
574
        } catch (const std::out_of_range &oor) {
575
            cout << "Out of range exception occurred. Exception " << endl;
576
            if (!errorFound) {
577
                sresult->success++;
578
                errorFound = true;
579

    
580
                return;
581
            }
582
        }
583
        name1.str("");
584
    }
585
    sresult->failure++;
586
    sresult->failureList.push_back("testTrackingOfValueReferenceError");
587

    
588
}
589

    
590
void testValidation(Xslt30Processor *trans, sResultCount *sresult) {
591
    trans->clearParameters();
592

    
593

    
594

    
595
    XsltExecutable * executable = trans->compileFromString(
596
            "<?xml version='1.0'?><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'                 xmlns:xs='http://www.w3.org/2001/XMLSchema' version='3.0' exclude-result-prefixes='#all'>     <xsl:import-schema><xs:schema><xs:element name='x' type='xs:int'/></xs:schema></xsl:import-schema> <xsl:template name='main'>          <xsl:result-document validation='strict'> <x>3</x>   </xsl:result-document>    </xsl:template>    </xsl:stylesheet>");
597

    
598
    const char *rootValue = executable->callTemplateReturningString("main");
599

    
600

    
601
    if (rootValue== nullptr) {
602
        std::cout << "NULL found" << std::endl;
603
        sresult->failure++;
604
        sresult->failureList.push_back("testValidation");
605
        return;
606

    
607
    } else {
608
        std::cout << "Result=" << rootValue << endl;
609
        sresult->success++;
610
    }
611
    delete executable;
612
}
613

    
614

    
615
void testXdmNodeOutput(Xslt30Processor *trans, sResultCount *sresult) {
616

    
617
    std::cout << "testXdmNodeOutput" << std::endl;
618
    XsltExecutable * executable = trans->compileFromString(
619
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template name='go'><a/></xsl:template></xsl:stylesheet>");
620
    XdmValue *rootValue = executable->callTemplateReturningValue("go");
621
    if (rootValue== nullptr) {
622
        cout << "Result is null ====== FAIL ====== " << endl;
623
        sresult->failure++;
624
        sresult->failureList.push_back("testXdmNodeOutput-0.0");
625
        return;
626
    }
627
    XdmItem *rootItem = rootValue->getHead();
628
    if (rootItem== nullptr) {
629

    
630
        cout << "Result is null ====== FAIL ====== " << endl;
631
        sresult->failure++;
632
        sresult->failureList.push_back("testXdmNodeOutput-0");
633
        delete rootValue;
634
        delete executable;
635
        return;
636
    }
637
    XdmNode *root = (XdmNode *) rootItem;
638
    if (root->getNodeKind() == DOCUMENT) {
639
        cout << "Result is a Document" << endl;
640
    } else {
641
        cout << "Node is of kind:" << root->getNodeKind() << endl;
642
    }
643
    const char *result = executable->callTemplateReturningString("go");
644
    if (string(result).find(string("<a/>")) != std::string::npos) {
645
        sresult->success++;
646
        delete result;
647
    } else {
648
        //TODO - this test case prints the XML declaration. Check if this correct
649
        sresult->failure++;
650
        cout << "testXdmNodeOutputAndString ======= FAIL========" << endl;
651
        sresult->failureList.push_back("testXdmNodeOutput");
652
    }
653
    delete rootValue;
654
    delete executable;
655

    
656
}
657

    
658
void exampleSimple1(Xslt30Processor *proc, sResultCount *sresult) {
659
    cout << "ExampleSimple1 taken from PHP:" << endl;
660

    
661
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
662
    if(executable== nullptr) {
663
        sresult->failure++;
664
        sresult->failureList.push_back("exampleSimple1");
665
        cerr << "exampleSimple1 NULL found" << endl;
666
        if (proc->exceptionOccurred()) {
667
            cerr <<"exampleSimple1 error: "<< proc->getErrorMessage() << endl;
668
        }
669
        return;
670
    }
671
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
672
    const char *result = executable->applyTemplatesReturningString();
673
    if (result != NULL) {
674
        cout << result << endl;
675
        sresult->success++;
676
        delete result;
677
    } else {
678
        cout << "Result is null ====== FAIL ====== " << endl;
679
        sresult->failure++;
680
    }
681
    proc->clearParameters();
682
    delete executable;
683
}
684

    
685
void exampleSimple1Err(Xslt30Processor *proc, sResultCount *sresult) {
686
    cout << "ExampleSimple1Err taken from PHP:" << endl;
687

    
688
    XsltExecutable * executable = proc->compileFromFile("err.xsl");
689
    if(executable == nullptr || proc->exceptionOccurred()) {
690
        if( proc->exceptionOccurred()) {
691
            cout << "Error Message= "<< proc->getErrorMessage() << endl;
692
        }
693
        cout << "Result expected as null " << endl;
694
        proc->exceptionClear();
695
        sresult->success++;
696
        return;
697
    } else {
698
        sresult->failure++;
699
        sresult->failureList.push_back("exampleSimple1Err");
700

    
701
        delete executable;
702
    }
703

    
704
}
705

    
706

    
707
void exampleSimple2(Xslt30Processor *proc, sResultCount *sresult) {
708
    cout << "<b>exampleSimple2:</b><br/>" << endl;
709

    
710
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
711
    if(executable== nullptr) {
712
        sresult->failure++;
713
        sresult->failureList.push_back("exampleSimple2");
714
        cerr << "exampleSimple2 NULL found" << endl;
715
        if (proc->exceptionOccurred()) {
716
            cerr <<"exampleSimple2 error: "<< proc->getErrorMessage() << endl;
717
        }
718
        return;
719
    }
720

    
721
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
722
    const char *filename = "output1.xml";
723
    executable->setOutputFile(filename);
724
    executable->applyTemplatesReturningFile("output1.xml");
725

    
726
    if (CppTestUtils::exists("output1.xml")) {
727
        cout << "The file $filename exists" << endl;
728
        remove("output1.xml");
729
        sresult->success++;
730
    } else {
731
        cout << "The file " << filename << " does not exist" << endl;
732
        if (executable->exceptionOccurred()) {
733
            cout << proc->getErrorMessage() << endl;
734
        }
735
        sresult->failure++;
736
        sresult->failureList.push_back("exampleSimple2");
737
    }
738

    
739
    delete executable;
740

    
741
}
742

    
743
void exampleSimple3(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
744
    cout << "<b>exampleSimple3:</b><br/>" << endl;
745
    proc->clearParameters();
746

    
747
    XdmNode *xdmNode = saxonProc->parseXmlFromString("<doc><b>text value of out</b></doc>");
748
    if (xdmNode== nullptr) {
749
        cout << "Error: xdmNode is null'" << endl;
750
        if(saxonProc->exceptionOccurred()) {
751
            cout<<"Error message="<<saxonProc->getErrorMessage()<<endl;
752
        }
753
        sresult->failure++;
754
        sresult->failureList.push_back("exampleSimple3");
755
        return;
756
    }
757

    
758
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
759

    
760
   if(executable == nullptr) {
761
        cout << "executable is NULL" <<endl;
762
        if(proc->exceptionOccurred()) {
763
                cout << proc->getErrorMessage() <<endl;
764
                
765
        }
766
        sresult->failure++;
767
        sresult->failureList.push_back("exampleSimple3");
768
        return;
769
   }
770

    
771
    executable->setInitialMatchSelection((XdmNode *) xdmNode);
772
    sresult->success++;
773
    delete xdmNode;
774
    delete executable;
775
}
776

    
777
void exampleSimple3aError(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
778
    cout << "<b>exampleSimple3aError:</b><br/>" << endl;
779

    
780
    XsltExecutable * executable = proc->compileFromFile(nullptr);
781

    
782

    
783
    if (executable == nullptr) {
784

    
785

    
786
        cout << "Expected failure of test exampleSimple3aError:" << endl;
787
        if(proc->exceptionOccurred()) {
788
            cout<<proc->getErrorMessage()<<endl;
789
        }
790

    
791
        sresult->success++;
792
        proc->exceptionClear();
793
        return;
794
    }
795

    
796
    cout << "Error: executable is not nullptr'" << endl;
797
    sresult->failure++;
798
    sresult->failureList.push_back("exampleSimple3aError");
799
    delete executable;
800
    return;
801

    
802

    
803
}
804

    
805
void exampleParam(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
806
    cout << "Test: ExampleParam" << endl;
807

    
808
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
809

    
810

    
811
    if(executable == nullptr) {
812
        cout << "executable is NULL" <<endl;
813
        if(proc->exceptionOccurred()) {
814
            cout << proc->getErrorMessage() <<endl;
815

    
816
        }
817
        sresult->failure++;
818
        sresult->failureList.push_back("exampleParam");
819
        return;
820

    
821
    }
822

    
823
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
824

    
825
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
826
    if (xdmvalue != nullptr) {
827

    
828
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
829

    
830
    } else {
831
        cout << "Xdmvalue is NULL - ====== FAIL =====" << endl;
832
        sresult->failure++;
833
        sresult->failureList.push_back("exampleParam");
834
        if(executable->exceptionOccurred()) {
835
            SaxonApiException * exception = executable->getException();
836
            cerr<<"Error: " << exception->getMessage() <<endl;
837
            delete exception;
838
        }
839
        delete executable;
840
        return;
841
    }
842
    const char *result = executable->applyTemplatesReturningString();
843
    if (result != nullptr) {
844
        cout << "Output:" << result << endl;
845
        sresult->success++;
846
        delete result;
847
    } else {
848
        cout << "Result is NULL<br/>  ======= fail =====" << endl;
849
        sresult->failure++;
850
        sresult->failureList.push_back("exampleParam");
851
        if(executable->exceptionOccurred()) {
852
            SaxonApiException * exception = executable->getException();
853
            cerr<<"Error: " << exception->getMessage() <<endl;
854
            delete exception;
855
        }
856
        delete executable;
857
        return;
858
    }
859

    
860
    //proc->clearParameters();
861
    //unset($result);
862
    //echo 'again with a no parameter value<br/>';
863

    
864
    executable->setProperty("!indent", "yes");
865
    const char *result2 = executable->applyTemplatesReturningString();
866

    
867

    
868
    if (result2 != nullptr) {
869
        cout << "Result2 output= " << result2 << endl;
870
        sresult->success++;
871
        delete result2;
872
    } else {
873
        cout << "Result2 is NULL<br/>  ======= fail =====" << endl;
874
        sresult->failure++;
875
        sresult->failureList.push_back("exampleParam");
876
        if(executable->exceptionOccurred()) {
877
            SaxonApiException * exception = executable->getException();
878
            cerr<<"Error: " << exception->getMessage() <<endl;
879
            delete exception;
880
        }
881
        delete xdmvalue;
882
        delete executable;
883
        return;
884
    }
885

    
886
    //  unset($result);
887
    // echo 'again with no parameter and no properties value set. This should fail as no contextItem set<br/>';
888
    //delete xdmvalue;
889
    //executable->clearParameters();
890

    
891
    XdmAtomicValue *xdmValue2 = saxonProc->makeStringValue("goodbye to you");
892

    
893
    if (xdmValue2 == nullptr) {
894
        cout << "Xdmvalue is NULL - ====== FAIL =====" << endl;
895
        sresult->failure++;
896
        sresult->failureList.push_back("exampleParam");
897
        if(executable->exceptionOccurred()) {
898
            SaxonApiException * exception = executable->getException();
899
            cerr<<"Error: " << exception->getMessage() <<endl;
900
            delete exception;
901
        }
902
        delete executable;
903
        delete xdmvalue;
904
        return;
905
    }
906

    
907
    executable->setParameter("a-param", (XdmValue *) xdmValue2);
908
    delete xdmvalue;
909

    
910

    
911
    const char *result3 = executable->applyTemplatesReturningString();
912
    if (result3 != nullptr) {
913
        cout << "Output =" << result3 << endl;
914

    
915
        sresult->success++;
916
        delete result3;
917

    
918
    } else {
919
        cout << "Error in result ===== FAIL =======" << endl;
920
        sresult->failure++;
921
        sresult->failureList.push_back("exampleParam");
922
        if(executable->exceptionOccurred()) {
923
            SaxonApiException * exception = executable->getException();
924
            cerr<<"Error: " << exception->getMessage() <<endl;
925
            delete exception;
926
        }
927

    
928
    }
929
    delete xdmValue2;
930
    delete executable;
931

    
932

    
933

    
934
}
935

    
936
// test parameter and properties maps where we update key, value pair.
937
void exampleParam2(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
938
    cout << "\nExampleParam:</b><br/>" << endl;
939
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
940
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
941

    
942

    
943
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
944
    XdmAtomicValue *xdmvalue2i = saxonProc->makeStringValue("Hello from me");
945
    if (xdmvalue != NULL) {
946

    
947
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
948
        executable->setParameter("a-param", (XdmValue *) xdmvalue2i);
949

    
950
    } else {
951
        cout << "Xdmvalue is null - ====== FAIL =====" << endl;
952
        sresult->failure++;
953
        sresult->failureList.push_back("exampleParam-1");
954
    }
955
    const char *result = executable->applyTemplatesReturningString();
956
    if (result != NULL) {
957
        string sresulti = string(result);
958
        if (sresulti.compare("Hello from me") == 0) {
959
            cout << "Output:" << result << endl;
960
            sresult->success++;
961
        } else {
962
            cout << "Result is " << result << " <br/> ======= fail ===== " << endl;
963
            sresult->failure++;
964
            sresult->failureList.push_back("exampleParam-2");
965
        }
966
    } else {
967
        cout<<"Result is NULL<br/>  ======= fail ====="<<endl;
968
        sresult->failure++;
969
        sresult->failureList.push_back("exampleParam-2");
970
    }
971

    
972
//proc->clearParameters();
973
//unset($result);
974
//echo 'again with a no parameter value<br/>';
975

    
976
    executable->setProperty("!indent", "no");
977
    executable->setProperty("!indent", "yes");
978
    const char *result2 = executable->applyTemplatesReturningString();
979

    
980
    executable->clearProperties();
981

    
982
    if(result2 != NULL) {
983
        cout<<result2<<endl;
984
        sresult->success++;
985

    
986
    }
987

    
988
    //  unset($result);
989
    // echo 'again with no parameter and no properties value set. This should fail as no contextItem set<br/>';
990
    XdmAtomicValue *xdmValue2 = saxonProc->makeStringValue("goodbye to you");
991
    executable->setParameter("a-param", (XdmValue*)xdmValue2);
992

    
993
    const char *result3 = executable->applyTemplatesReturningString();
994
    if(result3 != NULL) {
995
        cout<<"Output ="<<result3<<endl;
996

    
997
        sresult->success++;
998
    } else {
999
        cout<<"Error in result ===== FAIL ======="<<endl;
1000
        sresult->failure++;
1001
        sresult->failureList.push_back("exampleParam");
1002
    }
1003

    
1004
}
1005

    
1006
/* XMarkbench mark test q12.xsl with just-in-time=true*/
1007
void xmarkTest1(Xslt30Processor *proc, sResultCount *sresult) {
1008
    cout << "Test: xmarkTest1 - XMarkbench mark test q12.xsl (JIT=true):" << endl;
1009

    
1010
    proc->setJustInTimeCompilation(true);
1011

    
1012
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1013
    if (result != NULL) {
1014
        cout << "XdmNode returned" << endl;
1015
        sresult->success++;
1016
        delete result;
1017
    } else {
1018
        printf("result is null \nCheck For errors:");
1019
        sresult->failure++;
1020
        sresult->failureList.push_back("xmarkTest1");
1021
        cerr << "xmarkTest1 cp0" << endl;
1022
        if (proc->exceptionOccurred()) {
1023
            cerr << "xmarkTest1 cp1" << endl;
1024
            const char * message = proc->getErrorMessage();
1025
            cerr << "xmarkTest1 cp2" << endl;
1026
            if(message != nullptr) {
1027
                cerr << "xmarkTest1 cp2-1" << endl;
1028
                cerr << proc->getErrorMessage() << endl;
1029
            } else {
1030
                cerr << "Message is nullptr" << endl;
1031
                SaxonProcessor::sxn_environ->env->ExceptionDescribe();
1032
            }
1033
        }
1034
        cerr << "xmarkTest1 cp3" << endl;
1035
    }
1036

    
1037

    
1038
}
1039

    
1040

    
1041
/* XMarkbench mark test q12.xsl with just-in-time=true*/
1042
void xmarkTest2(Xslt30Processor *proc, sResultCount *sresult) {
1043
    cout << "Test: xmarkTest2 - XMarkbench mark test q12.xsl (JIT=true):" << endl;
1044

    
1045

    
1046
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1047
    if (result != NULL) {
1048
        cout << "XdmNode returned" << endl;
1049
        sresult->success++;
1050
        delete result;
1051

    
1052
    } else {
1053
        printf("result is null \nCheck For errors:");
1054
        sresult->failure++;
1055
        sresult->failureList.push_back("xmarkTest2");
1056
        if (proc->exceptionOccurred() > 0) {
1057
            SaxonApiException * exception = proc->getException();
1058
            if(exception != nullptr) {
1059
                const char *message = exception->getMessage();
1060
                if (message != nullptr) {
1061
                    cerr << "xmarkTest2 cp2-1" << endl;
1062
                    cerr << message << endl;
1063
                }
1064
                delete exception;
1065
            } else {
1066
                cerr << "Exception object is nullptr" << endl;
1067
                SaxonProcessor::sxn_environ->env->ExceptionDescribe();
1068
            }
1069
        }
1070
    }
1071

    
1072

    
1073
}
1074

    
1075
/* XMarkbench mark test q12.xsl with just-in-time=true*/
1076
void exampleSimple_xmark(Xslt30Processor *proc, sResultCount *sresult) {
1077
    cout << "exampleSimple_xmark test - test q12.xsl:" << endl;
1078

    
1079
    proc->setJustInTimeCompilation(true);
1080

    
1081
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1082
    if (result != NULL) {
1083
        cout << "XdmNode returned" << endl;
1084
        sresult->success++;
1085
        delete result;
1086
    } else {
1087
        printf("Result is null \nCheck For errors:");
1088
        if (proc->exceptionOccurred() > 0) {
1089
            cout << proc->getErrorMessage() << endl;
1090
        }
1091
        sresult->failure++;
1092
        sresult->failureList.push_back("exampleSimple_xmark");
1093
        proc->exceptionClear();
1094
    }
1095
    proc->clearParameters();
1096

    
1097
}
1098

    
1099
/*
1100
* Test saving nd loading a Xslt package
1101
*/
1102
void testPackage1(Xslt30Processor *trans, sResultCount *sresult) {
1103

    
1104
    cout << endl << "Test: testPackage1 - Saving and loading Packages:" << endl;
1105
    trans->clearParameters();
1106

    
1107

    
1108
    trans->compileFromFileAndSave("test.xsl", "test1.sef");
1109
    const char *output = trans->transformFileToString("cat.xml", "test1.sef");
1110

    
1111
    if (output== nullptr) {
1112
        printf("result is null \n");
1113
        const char *message = trans->getErrorMessage();
1114
        if (message != NULL) {
1115
            cout << "Error message =" << message << endl;
1116
        }
1117
        sresult->failure++;
1118
        sresult->failureList.push_back("testPackage1");
1119

    
1120
    } else {
1121
        printf("%s", output);
1122
        printf("result is OK \n");
1123
        sresult->success++;
1124
    }
1125
    fflush(stdout);
1126
    delete output;
1127
}
1128

    
1129

    
1130
/*
1131
* Test saving and loading a Xslt package
1132
*/
1133
void testPackage1a(Xslt30Processor *trans, sResultCount *sresult) {
1134

    
1135
    cout << endl << "Test: testPackage1a" << endl;
1136
    trans->clearParameters();
1137

    
1138
    trans->compileFromFileAndSave("test.xsl", "test1a.sef");
1139

    
1140
    XsltExecutable * executable = trans->compileFromFile("test1.sef");
1141
    executable->setInitialMatchSelectionAsFile("cat.xml");
1142
    const char * output = executable->applyTemplatesReturningString();
1143
    if (output== nullptr) {
1144
        printf("result is null \n");
1145
        const char *message = trans->getErrorMessage();
1146
        if (message != NULL) {
1147
            cout << "Error message =" << message << endl;
1148
        }
1149
        sresult->failure++;
1150
        sresult->failureList.push_back("testPackage1a");
1151
        trans->exceptionClear();
1152

    
1153
    } else {
1154
        printf("%s", output);
1155
        printf("result is OK \n");
1156
        sresult->success++;
1157
        delete output;
1158
    }
1159
    fflush(stdout);
1160
    delete executable;
1161
}
1162

    
1163

    
1164
/*
1165
* Test saving and loading a Xslt package
1166
*/
1167
void testPackage2_Error(Xslt30Processor *trans, sResultCount *sresult) {
1168

    
1169
    cout << endl << "Test: testPackage2_Error:" << endl;
1170

    
1171
    const char *stylesheet = "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>       <xsl:param name='values' select='(2,3,4)' /><xsl:output method='xml' indent='yes' /><xsl:template match='*'><output><xsl:for-each select='$values' ><out><xsl:value-of select='. * 3'/></out><xsl:for-each></output></xsl:template><xsl:stylesheet>";
1172

    
1173
    trans->compileFromStringAndSave(stylesheet, "test2.sef");
1174

    
1175
    if(trans->exceptionOccurred()) {
1176
        const char *message = trans->getErrorMessage();
1177
        if (message != NULL) {
1178
            cout << "Error message =" << message << endl;
1179
        }
1180
        sresult->success++;
1181
        trans->exceptionClear();
1182
        return;
1183

    
1184
    }
1185

    
1186
    sresult->failure++;
1187
    sresult->failureList.push_back("testPackage2_Error");
1188

    
1189
}
1190

    
1191
void testCallFunction(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1192

    
1193
    const char *source = "<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:f='http://localhost/' version='3.0'> <xsl:function name='f:add' visibility='public'>    <xsl:param name='a'/><xsl:param name='b'/> <xsl:sequence select='$a + $b'/></xsl:function></xsl:stylesheet>";
1194
    cout << endl << "Test: testCallFunction:" << endl;
1195
    XsltExecutable * executable = trans->compileFromString(source);
1196

    
1197
    if(executable == nullptr) {
1198
        if(trans->exceptionOccurred()) {
1199
            SaxonApiException * exception = trans->getException();
1200
            if(exception != nullptr) {
1201
                cerr << "Error: " << exception->getMessage() << endl;
1202
                delete exception;
1203
            }
1204

    
1205
        }
1206
        sresult->failure++;
1207
        sresult->failureList.push_back("testCallFunction");
1208
        trans->exceptionClear();
1209
        return;
1210

    
1211

    
1212
    }
1213

    
1214
    XdmValue **valueArray = new XdmValue *[2];
1215

    
1216
    valueArray[0] = (XdmValue *) (proc->makeIntegerValue(2));
1217
    valueArray[1] = (XdmValue *) (proc->makeIntegerValue(3));
1218
    XdmValue *v = executable->callFunctionReturningValue("{http://localhost/}add", valueArray, 2);
1219

    
1220
    if (v != NULL && (v->getHead())->isAtomic() && ((XdmAtomicValue *) (v->getHead()))->getLongValue() == 5) {
1221
        sresult->success++;
1222
        delete v;
1223
    } else {
1224
        if (v != NULL && !(v->getHead())->isAtomic()) {
1225
            cout << "Value in callFunction is not atomic - but expected as atomic value" << endl;
1226
        }
1227
        cout << "testCallFunction ======= FAIL ======" << endl;
1228
        if(executable->exceptionOccurred()) {
1229
            SaxonApiException * exception = executable->getException();
1230
            if(exception != nullptr) {
1231
                cerr << "Error: " << exception->getMessage() << endl;
1232
                delete exception;
1233
            }
1234
        }
1235
        sresult->failure++;
1236
        sresult->failureList.push_back("testCallFunction");
1237
        executable->exceptionClear();
1238
        delete v;
1239
    }
1240
    delete valueArray[0];
1241
    delete valueArray[1];
1242
    delete [] valueArray;
1243
    delete executable;
1244
}
1245

    
1246
void testInitialTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1247

    
1248

    
1249
    const char *source = "<?xml version='1.0'?>  <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'  xmlns:xs='http://www.w3.org/2001/XMLSchema'  version='3.0'>  <xsl:template match='*'>     <xsl:param name='a' as='xs:double'/>     <xsl:param name='b' as='xs:float'/>     <xsl:sequence select='., $a + $b'/>  </xsl:template>  </xsl:stylesheet>";
1250
    cout << endl << "Test:testInitialTemplate" << endl;
1251
    XsltExecutable * executable = trans->compileFromString(source);
1252
    if(executable == nullptr) {
1253
        if(trans->exceptionOccurred()) {
1254
            cout << "Error: "<< trans->getErrorMessage() << endl;
1255
        }
1256
        return;
1257
    }
1258
    XdmNode * node = proc->parseXmlFromString("<e/>");
1259

    
1260
    executable->setResultAsRawValue(false);
1261
    std::map<std::string, XdmValue *> parameterValues;
1262

    
1263
    XdmAtomicValue * a1 = proc->makeIntegerValue(12);
1264
    XdmAtomicValue * a2 = proc->makeIntegerValue(5);;
1265
    parameterValues["a"] = a1;
1266
    parameterValues["b"] = a2;
1267
    executable->setInitialTemplateParameters(parameterValues, false);
1268
    executable->setInitialMatchSelection(node);
1269
    XdmValue *result = executable->applyTemplatesReturningValue();
1270
    if (result != NULL) {
1271
        sresult->success++;
1272
        cout << "Result=" << result->getHead()->getStringValue() << endl;
1273
        delete result;
1274
    } else {
1275
        sresult->failure++;
1276
    }
1277

    
1278
    delete executable;
1279
    delete a1;
1280
    delete a2;
1281
    delete node;
1282
    parameterValues.clear();
1283
}
1284

    
1285
void testResolveUri(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1286
    cout << endl << "Test: testResolveUri:" << endl;
1287

    
1288
    XsltExecutable * executable = trans->compileFromString(
1289
            "<xsl:stylesheet version='3.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:err='http://www.w3.org/2005/xqt-errors'><xsl:template name='go'><xsl:try><xsl:variable name='uri' as='xs:anyURI' select=\"resolve-uri('notice trailing space /out.xml')\"/> <xsl:message select='$uri'/><xsl:result-document href='{$uri}'><out/></xsl:result-document><xsl:catch><xsl:sequence select=\"'$err:code: ' || $err:code  || ', $err:description: ' || $err:description\"/></xsl:catch></xsl:try></xsl:template></xsl:stylesheet>");
1290

    
1291
    if(executable == nullptr) {
1292
        sresult->failure++;
1293
        sresult->failureList.push_back("testResolveUri");
1294
        if(trans->exceptionOccurred()) {
1295
            const char * message = trans->getErrorMessage();
1296
            if(message != nullptr) {
1297
                cout << "Error: " << trans->getErrorMessage() << endl;
1298
            }
1299
        }
1300
        trans->exceptionClear();
1301
        return;
1302
    }
1303
    XdmValue *value = executable->callTemplateReturningValue("go");
1304

    
1305

    
1306
    if (value== nullptr) {
1307

    
1308
        sresult->failure++;
1309
        sresult->failureList.push_back("testResolveUri");
1310
    } else {
1311

    
1312
        const char *svalue = value->itemAt(0)->getStringValue();
1313
        cout << "testResolveUri = " << svalue << endl;
1314
        sresult->success++;
1315
        delete value;
1316
    }
1317

    
1318
    delete executable;
1319
}
1320

    
1321
void testContextNotRoot(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1322
    cout << endl << "Test: testContextNotRoot" << endl;
1323

    
1324
    XdmNode *node = proc->parseXmlFromString("<doc><e>text</e></doc>");
1325

    
1326
    XsltExecutable *executable = trans->compileFromString(
1327
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:variable name='x' select='.'/><xsl:template match='/'>errorA</xsl:template><xsl:template match='e'>[<xsl:value-of select='name($x)'/>]</xsl:template></xsl:stylesheet>");
1328

    
1329
    if (executable == nullptr) {
1330
        sresult->failure++;
1331
        sresult->failureList.push_back("testContextNotRoot");
1332
        if (trans->exceptionOccurred()) {
1333
            cout << "Error: " << trans->getErrorMessage() << endl;
1334
        }
1335
        return;
1336
    }
1337

    
1338
    executable->setGlobalContextItem(node);
1339
    if (node->getChildCount() > 0) {
1340
        XdmNode **eNodeL1 = node->getChildren();
1341
        if (eNodeL1 != nullptr) {
1342
            XdmNode ** eNodeChildren = eNodeL1[0]->getChildren();
1343
            if (eNodeChildren != nullptr) {
1344
                XdmNode *eNode = eNodeChildren[0];
1345
                cout << "Node content = " << eNode->toString() << endl;
1346
                executable->setInitialMatchSelection(eNode);
1347
                const char *result = executable->applyTemplatesReturningString();
1348

    
1349
                if (result == nullptr) {
1350

    
1351
                    cout << "testContextNotRoot ======= FAIL ======" << endl;
1352
                    if (executable->exceptionOccurred()) {
1353
                        SaxonApiException *exception = executable->getException();
1354
                        cerr << "Error: " << exception->getMessage() << endl;
1355
                        delete exception;
1356
                    }
1357
                    sresult->failure++;
1358
                    sresult->failureList.push_back("testContextNotRoot");
1359

    
1360

    
1361
                } else {
1362

    
1363
                    cout << "testContextNotRoot = " << result << endl;
1364
                    sresult->success++;
1365
                    delete result;
1366

    
1367
                }
1368

    
1369
            }
1370
        }
1371
    } else {
1372
        sresult->failure++;
1373
        sresult->failureList.push_back("testContextNotRoot");
1374
    }
1375

    
1376
    delete executable;
1377
    delete node;
1378
}
1379

    
1380

    
1381
void testContextNotRootNamedTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1382
    cout << endl << "Test: testContextNotRootNamedTemplate" << endl;
1383

    
1384
    XdmNode *node = proc->parseXmlFromString("<doc><e>text</e></doc>");
1385

    
1386
    if(node == nullptr) {
1387
        if(proc->exceptionOccurred()) {
1388
            const char * message = proc->getErrorMessage();
1389
            if(message != nullptr) {
1390
                cerr << "Error: " << message << endl;
1391
            }
1392
            proc->exceptionClear();
1393
        }
1394
        sresult->failure++;
1395
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1396
        return;
1397
    }
1398

    
1399
    XsltExecutable * executable = trans->compileFromString(
1400
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:variable name='x' select='.'/><xsl:template match='/'>errorA</xsl:template><xsl:template name='main'>[<xsl:value-of select='name($x)'/>]</xsl:template></xsl:stylesheet>");
1401

    
1402
    if(executable == nullptr) {
1403
        sresult->failure++;
1404
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1405
        if(trans->exceptionOccurred()) {
1406
            cout << "Error: "<< trans->getErrorMessage() << endl;
1407
        }
1408
        delete node;
1409
        return;
1410
    }
1411

    
1412
    executable->setGlobalContextItem(node);
1413
    const char *result = executable->callTemplateReturningString("main");
1414

    
1415
    if (result== nullptr) {
1416

    
1417
        cout << "testContextNotRootNameTemplate ======= FAIL ======" << endl;
1418
        if(executable->exceptionOccurred()) {
1419
            SaxonApiException * exception = executable->getException();
1420
            cerr<<"Error: " << exception->getMessage() <<endl;
1421
            delete exception;
1422
        }
1423
        sresult->failure++;
1424
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1425
    } else {
1426

    
1427
        cout << "testContextNotRoot = " << result << endl;
1428
        sresult->success++;
1429
        delete result;
1430
    }
1431
    delete node;
1432
    delete executable;
1433

    
1434
}
1435

    
1436

    
1437
void testContextNotRootNamedTemplateValue(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1438
    cout << endl << "Test: testContextNotRootNamedTemplateValue" << endl;
1439

    
1440
    XdmNode *node = proc->parseXmlFromString("<doc><e>text</e></doc>");
1441

    
1442
    if(node == nullptr) {
1443
        if(proc->exceptionOccurred()) {
1444
            const char * message = proc->getErrorMessage();
1445
            if(message != nullptr) {
1446
                cerr << "Error: " << message << endl;
1447
            }
1448
            proc->exceptionClear();
1449
        }
1450
        sresult->failure++;
1451
        sresult->failureList.push_back("testContextNotRootNamedTemplateValue");
1452
        return;
1453
    }
1454

    
1455
    XsltExecutable * executable = trans->compileFromString(
1456
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:variable name='x' select='.'/><xsl:template match='/'>errorA</xsl:template><xsl:template name='main'>[<xsl:value-of select='name($x)'/>]</xsl:template></xsl:stylesheet>");
1457

    
1458
    if(executable == nullptr) {
1459
        sresult->failure++;
1460
        sresult->failureList.push_back("testContentNotRootNamedTemplateValue");
1461
        if(trans->exceptionOccurred()) {
1462
            cout << "Error: "<< trans->getErrorMessage() << endl;
1463
        }
1464
        delete node;
1465
        return;
1466
    }
1467

    
1468
    executable->setGlobalContextItem(node);
1469
    XdmValue *result = executable->callTemplateReturningValue("main");
1470

    
1471
    if (result == nullptr) {
1472

    
1473
        cout << "testCallFunction ======= FAIL ======" << endl;
1474
        if(executable->exceptionOccurred()) {
1475
            SaxonApiException * exception = executable->getException();
1476
            cerr<<"Error: " << exception->getMessage() <<endl;
1477
            delete exception;
1478
        }
1479
        sresult->failure++;
1480
        sresult->failureList.push_back("testContextNotRootNamedTemplateValue");
1481
    } else {
1482

    
1483
        cout << "testContextNotRoot = " << result->getHead()->getStringValue() << endl;
1484
        sresult->success++;
1485
        delete result;
1486
        result = nullptr;
1487
    }
1488

    
1489
    delete node;
1490
    delete executable;
1491

    
1492

    
1493
}
1494

    
1495
void testCallSystemFunction(SaxonProcessor *proc, sResultCount *sresult) {
1496

    
1497
    XdmFunctionItem *fi = XdmFunctionItem::getSystemFunction(proc, "{http://www.w3.org/2005/xpath-functions}parse-json",
1498
                                                            1);
1499
    if (fi== nullptr) {
1500
        sresult->failure++;
1501
        sresult->failureList.push_back("testCallSystemFunction");
1502
        return;
1503
    }
1504

    
1505
    XdmValue ** xdmValue = new XdmValue*[1];
1506
    xdmValue[0] = new XdmValue();
1507
    xdmValue[0]->addXdmItem(proc->makeStringValue("[1,2,3]"));
1508
    XdmValue *result = fi->call(xdmValue, 1);
1509

    
1510
    std::cerr << "Result = " << result->toString() << endl;
1511
    if(result->size() == 3) {
1512

    
1513
        cout << "testCallSystemFunction = " << result->getHead()->getStringValue() << endl;
1514
        sresult->success++;
1515

    
1516
    } else {
1517
        sresult->failure++;
1518
        sresult->failureList.push_back("testCallSystemFunction");
1519

    
1520
    }
1521

    
1522
}
1523

    
1524

    
1525
void testPipeline(SaxonProcessor *proc, sResultCount *sresult) {
1526
    cout << endl << "Test: testPipeline" << endl;
1527

    
1528
    Xslt30Processor * trans = proc->newXslt30Processor();
1529
    if(trans == nullptr) {
1530
        cout << "Error: Xslt30Processor is null - maybe unclean state of JNI" << endl;
1531
        sresult->failure++;
1532
        sresult->failureList.push_back("testPipeline");
1533
        return;
1534
    }
1535
    XsltExecutable * stage1 = trans->compileFromString(
1536
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='/'><a><xsl:copy-of select='.'/></a></xsl:template></xsl:stylesheet>");
1537

    
1538
    if(stage1 == nullptr) {
1539
        sresult->failure++;
1540
        sresult->failureList.push_back("testPipeline");
1541
        if(trans->exceptionOccurred()) {
1542
            const char * message = trans->getErrorMessage();
1543
            cout << "stage 1 Error: " << message << endl;
1544

    
1545
        }
1546
        cout << "Stage 1 Error - exit method " << endl;
1547
        delete trans;
1548
        return;
1549
    }
1550

    
1551
    XdmNode *inn = proc->parseXmlFromString("<z/>");
1552

    
1553
    XsltExecutable *stage2 = trans->compileFromString(
1554
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='/'><a><xsl:copy-of select='.'/></a></xsl:template></xsl:stylesheet>");
1555

    
1556
    if(stage2 == nullptr) {
1557
        sresult->failure++;
1558
        sresult->failureList.push_back("testPipeline");
1559
        if(trans->exceptionOccurred()) {
1560
            const char * message = trans->getErrorMessage();
1561
            cout << "stage 2 Error: " << message << endl;
1562

    
1563
        }
1564
        cout << "Stage 2 Error - exit method " << endl;
1565
        delete stage1;
1566
        delete trans;
1567
        delete inn;
1568
        return;
1569
    }
1570

    
1571

    
1572
    XsltExecutable *stage3 = trans->compileFromString(
1573
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='/'><a><xsl:copy-of select='.'/></a></xsl:template></xsl:stylesheet>");
1574

    
1575
    if(stage3 == nullptr) {
1576
        sresult->failure++;
1577
        sresult->failureList.push_back("testPipeline");
1578
        if(trans->exceptionOccurred()) {
1579
            const char * message = trans->getErrorMessage();
1580
            cout << "stage 3 Error: " << message << endl;
1581

    
1582
        } else {
1583
            cout << "Stage 3 Error: testPipeline failed with exception" << endl;
1584
        }
1585
        cout << "Stage 3 Error - exit method " << endl;
1586
        delete stage1;
1587
        delete stage2;
1588
        delete trans;
1589
        delete inn;
1590
        return;
1591
    }
1592

    
1593
    XsltExecutable *stage4 = trans->compileFromString(
1594
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='/'><a><xsl:copy-of select='.'/></a></xsl:template></xsl:stylesheet>");
1595

    
1596
    if(stage4 == nullptr) {
1597
        sresult->failure++;
1598
        sresult->failureList.push_back("testPipeline");
1599
        if(trans->exceptionOccurred()) {
1600
            const char * message = trans->getErrorMessage();
1601
            cout << "stage 4 Error: " << message << endl;
1602

    
1603
        } else {
1604
            cout << "Stage 4 Error: testPipeline failed with exception" << endl;
1605
        }
1606
        cout << "Stage 4 Error - exit method " << endl;
1607
        delete stage1;
1608
        delete stage2;
1609
        delete stage3;
1610
        delete trans;
1611
        delete inn;
1612
        return;
1613
    }
1614

    
1615
    XsltExecutable *stage5 = trans->compileFromString(
1616
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='/'><a><xsl:copy-of select='.'/></a></xsl:template></xsl:stylesheet>");
1617

    
1618
    if(stage5 == nullptr) {
1619
        sresult->failure++;
1620
        sresult->failureList.push_back("testPipeline");
1621
        if(trans->exceptionOccurred()) {
1622
            const char * message = trans->getErrorMessage();
1623
            cout << "stage 5 Error: " << message << endl;
1624

    
1625
        } else {
1626
            cout << "Stage 5 Error: testPipeline failed with exception" << endl;
1627
        }
1628
        cout << "Stage 5     Error - exit method " << endl;
1629
        delete stage1;
1630
        delete stage2;
1631
        delete stage3;
1632
        delete stage4;
1633
        delete trans;
1634
        delete inn;
1635
        return;
1636
    }
1637

    
1638
    stage1->setProperty("!indent", "no");
1639
    stage1->setInitialMatchSelection(inn);
1640

    
1641
    XdmValue *d1 = stage1->applyTemplatesReturningValue();
1642
    if(d1 == nullptr) {
1643
        if (stage1->exceptionOccurred()) {
1644
            sresult->failure++;
1645
            sresult->failureList.push_back("testPipeline");
1646
            if (stage1->exceptionOccurred()) {
1647
                SaxonApiException *exception = stage1->getException();
1648
                if (exception != nullptr) {
1649
                    cerr << "Error: " << exception->getMessage() << endl;
1650
                    delete exception;
1651
                    exception = nullptr;
1652
                }
1653
            }
1654
            cout << "Stage d1 Error - exit method " << endl;
1655
            delete stage1;
1656
            delete stage2;
1657
            delete stage3;
1658
            delete stage4;
1659
            delete stage5;
1660
            delete trans;
1661
            delete inn;
1662
            return;
1663
        }
1664
    }
1665

    
1666
    XdmItem *d11 = d1->getHead();
1667
    if (d11== nullptr) {
1668

    
1669
        cout << "d11 is NULL\n" << endl;
1670
        sresult->failure++;
1671
        sresult->failureList.push_back("testPipeline-1");
1672
        delete stage1;
1673
        delete stage2;
1674
        delete stage3;
1675
        delete stage4;
1676
        delete stage5;
1677
        delete d1;
1678
        delete inn;
1679
        delete trans;
1680
        return;
1681
    }
1682
    const char *data = d11->toString();
1683

    
1684
    if (data != NULL) {
1685
        cout << "d1 result=" << data << endl;
1686
    } else {
1687

    
1688
        cout << "d1 result Error - toString is NULL" << endl;
1689
        delete stage1;
1690
        delete stage2;
1691
        delete stage3;
1692
        delete stage4;
1693
        delete stage5;
1694
        delete d1;
1695
        delete inn;
1696
        delete trans;
1697
        return;
1698
    }
1699
    stage2->setProperty("!indent", "no");
1700
    stage2->setInitialMatchSelection(d11);
1701
    XdmValue *d2 = stage2->applyTemplatesReturningValue();
1702
    if (d2== nullptr) {
1703
        cout << "ERROR-11\n" << endl;
1704
        sresult->failure++;
1705
        sresult->failureList.push_back("testPipeline-2");
1706
        if (stage2->exceptionOccurred()) {
1707
            SaxonApiException *exception = stage2->getException();
1708
            if (exception != nullptr) {
1709
                cerr << "Error: " << exception->getMessage() << endl;
1710
                delete exception;
1711
                exception = nullptr;
1712
            }
1713
        }
1714
        cout << "Stage d2 Error - exit method " << endl;
1715
        delete d1;
1716
        delete inn;
1717
        delete stage1;
1718
        delete stage2;
1719
        delete stage3;
1720
        delete stage4;
1721
        delete stage5;
1722
        delete trans;
1723
        return;
1724
    }
1725
    stage3->setProperty("!indent", "no");
1726
    stage3->setInitialMatchSelection(d2);
1727
    XdmValue * d3 = stage3->applyTemplatesReturningValue();
1728
    if(d3 == nullptr){
1729
        sresult->failure++;
1730
        sresult->failureList.push_back("testPipeline-3");
1731
        if (stage3->exceptionOccurred()) {
1732
            SaxonApiException *exception = stage3->getException();
1733
            if (exception != nullptr) {
1734
                cerr << "Error: " << exception->getMessage() << endl;
1735
                delete exception;
1736
                exception = nullptr;
1737
            }
1738
        }
1739
        cout << "Stage d3 Error - exit method " << endl;
1740
        delete d1;
1741
        delete d2;
1742
        delete inn;
1743

    
1744
        delete stage1;
1745
        delete stage2;
1746
        delete stage3;
1747
        delete stage4;
1748
        delete stage5;
1749
        delete trans;
1750
        return;
1751
      }
1752
    stage4->setProperty("!indent", "no");
1753
    stage4->setInitialMatchSelection(d3);
1754
    XdmValue * d4 = stage4->applyTemplatesReturningValue();
1755
    if(d4== nullptr){
1756
        sresult->failure++;
1757
        sresult->failureList.push_back("testPipeline-4");
1758
        if (stage4->exceptionOccurred()) {
1759
            SaxonApiException *exception = stage4->getException();
1760
            if (exception != nullptr) {
1761
                cerr << "Error: " << exception->getMessage() << endl;
1762
                delete exception;
1763
                exception = nullptr;
1764
            }
1765
        }
1766
        cout << "Stage d4 Error - exit method " << endl;
1767
        delete d3;
1768
        delete d2;
1769
        delete d1;
1770
        delete inn;
1771
        delete stage1;
1772
        delete stage2;
1773
        delete stage3;
1774
        delete stage4;
1775
        delete stage5;
1776
        delete trans;
1777
        return;
1778
      }
1779
    stage5->setProperty("!indent", "no");
1780
    stage5->setInitialMatchSelection(d4);
1781
    const char * sw = stage5->applyTemplatesReturningString();
1782
    if(sw == nullptr){
1783
        sresult->failure++;
1784
        sresult->failureList.push_back("testPipeline-5");
1785
        if (stage5->exceptionOccurred()) {
1786
            SaxonApiException *exception = stage5->getException();
1787
            if (exception != nullptr) {
1788
                cerr << "Error: " << exception->getMessage() << endl;
1789
                delete exception;
1790
            }
1791
        }
1792
        cout << "Stage sw Error - exit method " << endl;
1793
        delete stage1;
1794
        delete stage2;
1795
        delete stage3;
1796
        delete stage4;
1797
        delete stage5;
1798
        delete trans;
1799
        delete d4;
1800
        delete d3;
1801
        delete d2;
1802
        delete d1;
1803
        delete inn;
1804
        return;
1805
      }
1806
    cout<<sw<<endl;
1807
    cout << "testPipeline = " << sw << endl;
1808
    sresult->success++;
1809
    delete stage1;
1810
    stage1 = nullptr;
1811

    
1812
    delete stage2;
1813
    stage2 = nullptr;
1814

    
1815

    
1816
    delete stage3;
1817
    stage3 = nullptr;
1818

    
1819
    delete stage4;
1820
    stage4 = nullptr;
1821

    
1822
    delete stage5;
1823
    stage5 = nullptr;
1824

    
1825
    delete trans;
1826
    trans = nullptr;
1827

    
1828
    delete sw;
1829
    sw = nullptr;
1830

    
1831
    delete d4;
1832
    d4 = nullptr;
1833

    
1834
    delete d3;
1835
    d3 = nullptr;
1836

    
1837
    delete d2;
1838
    d2 = nullptr;
1839

    
1840
    delete d1;
1841
    d1 = nullptr;
1842

    
1843
    delete inn;
1844
    inn = nullptr;
1845

    
1846
}
1847

    
1848

    
1849
void testCatalog(const char * cwd, SaxonProcessor * proc, sResultCount *sresult) {
1850

    
1851

    
1852
    cout << endl << "Test: testCatalog" << endl;
1853
    bool trace = false;
1854
    proc->setcwd(cwd);
1855
    proc->setCatalog("../php/catalog-test/catalog.xml", trace);
1856

    
1857
    Xslt30Processor * trans = proc->newXslt30Processor();
1858

    
1859
    XsltExecutable  * executable = trans->compileFromFile("../php/catalog-test/test1.xsl");
1860

    
1861
    if(executable == nullptr) {
1862

    
1863
        if (trans->exceptionOccurred()) {
1864

    
1865
            if(trans->getErrorMessage() != nullptr) {
1866
                const char *message = trans->getErrorMessage();
1867
                cerr << "exception=" << message << endl;
1868
            } else {
1869
                const char *message = proc->getErrorMessage();
1870
                if(message != nullptr) {
1871
                    cerr << "exception-proc=" << message << endl;
1872
                }
1873
            }
1874
        }
1875
        sresult->failure++;
1876
        sresult->failureList.push_back("testCatalog");
1877
        trans->exceptionClear();
1878
        delete trans;
1879
        trans= nullptr;
1880
        return;
1881
    }
1882

    
1883
    executable->setInitialMatchSelectionAsFile("../php/catalog-test/example.xml");
1884

    
1885
    const char *result = executable->applyTemplatesReturningString();
1886

    
1887
    if(result != NULL) {
1888
        std::cerr << "testCatalog result= " << result << std::endl;
1889
        delete result;
1890
        sresult->success++; // TODO - check the results more carefully
1891
    } else {
1892
        sresult->failure++;
1893
        sresult->failureList.push_back("testCatalog");
1894
    }
1895

    
1896

    
1897
    delete executable;
1898
    executable = nullptr;
1899
    delete trans;
1900
    trans = nullptr;
1901

    
1902
}
1903

    
1904
static int NUM_THREADS = 10;
1905

    
1906
void RunThread(XsltExecutable * executable, int tid, const std::string filename) {
1907
    JavaVMAttachArgs att_arg;
1908
    att_arg.version = JNI_VERSION_1_2;
1909
    att_arg.name = NULL;
1910
    att_arg.group = NULL;
1911

    
1912
   // SaxonProcessor::sxn_environ->jvm->AttachCurrentThread((void**)&SaxonProcessor::sxn_environ->env, &att_arg);
1913
    cerr<<endl<<"RunThread cp0,  THEAD ID="<<tid<<endl;
1914
    fflush(stderr);
1915
    fflush(stdout);
1916

    
1917
    if(executable != nullptr) {
1918
       executable->setInitialMatchSelectionAsFile(filename.c_str());
1919

    
1920
        cerr << "RunThread cp1" << endl;
1921

    
1922
       const char *result = nullptr;
1923
        result = executable->applyTemplatesReturningString();
1924
        if (result != nullptr) {
1925
            cout << " Result from THREAD ID: " << tid << ", " << result << endl;
1926
            delete result;
1927
        } else {
1928
            cerr << " ===== Failed in THREAD ID: " << tid << endl;
1929
           /* if(executable->exceptionOccurred()) {
1930
                SaxonApiException *exception = executable->getException();
1931

1932
                if (exception != nullptr) {
1933
                    const char *message = exception->getMessage();
1934
                    if (message != nullptr) {
1935
                        cerr << "Error = " << message << endl;
1936
                    }
1937
                    delete exception;
1938
                }
1939
            } else {
1940
            cerr << "No exception found - result is nullptr" << endl;
1941
            }*/
1942

    
1943
        }
1944
    delete executable;
1945
    } else {
1946
        cerr << "XsltExecutable is nullptr" << endl;
1947
    }
1948

    
1949
   // (   SaxonProcessor::sxn_environ->jvm)->DetachCurrentThread();
1950

    
1951
}
1952

    
1953
void testThreads (SaxonProcessor * processor, Xslt30Processor * trans, sResultCount *sresult) {
1954

    
1955
    std::vector<std::string> s = {
1956
            "../data/xmark100k.xml",
1957
            "../data/xmark1.xml",
1958
            "../data/xmark4.xml",
1959
            "../data/xmark10.xml"
1960
    };
1961

    
1962
    std::vector<std::thread> threads;
1963

    
1964
    for (int i = 0; i < s.size(); i++) {
1965

    
1966
        XsltExecutable * executable = trans->compileFromFile("q12.xsl");
1967
            threads.push_back(std::thread(RunThread, executable, i, s[i]));
1968

    
1969
    }
1970
    cerr<<"testThreads cp0"<<endl;
1971
    for (auto &th : threads) {
1972
        th.join();
1973
        cerr<<"testThreads cp1 - loop"<<endl;
1974
    }
1975
}
1976

    
1977

    
1978

    
1979
int main(int argc, char* argv[]) {
1980

    
1981

    
1982
    const char * cwd = nullptr;
1983
    if(argc > 0) {
1984
        cwd = argv[1];
1985
    }
1986

    
1987
    SaxonProcessor *processor = new SaxonProcessor(true);
1988
    processor->setConfigurationProperty("http://saxon.sf.net/feature/licenseFileLocation", "/usr/local/lib/saxon-license.lic");
1989
    cout << "Test: Xslt30Processor with Saxon version=" << processor->version() << endl << endl;
1990
    
1991
    char buff[FILENAME_MAX]; //create string buffer to hold path
1992
    GetCurrentDir( buff, FILENAME_MAX );
1993
    cout<<"CWD = "<< buff<<endl;
1994
    if(cwd != nullptr) {
1995
            processor->setcwd(cwd);//"/Users/ond1/work/development/git/saxon-dev/saxondev/src/main/c/samples/cppTests"); //set to the current working directory
1996
    } else {
1997
        processor->setcwd(buff);
1998
    }
1999
    if (processor->isSchemaAwareProcessor()) {
2000

    
2001
        std::cerr << "Processor is Schema Aware" << std::endl;
2002
    } else {
2003
        std::cerr << "Processor is not Schema Aware" << std::endl;
2004
    }
2005

    
2006
    sResultCount *sresult = new sResultCount();
2007
    Xslt30Processor *trans = processor->newXslt30Processor();
2008

    
2009
    if( trans == nullptr) {
2010
        cout<< "Error creating Xslt30Processor"<<endl;
2011
        if(processor->exceptionOccurred()) {
2012
            cout<< "Error message: " <<processor->getErrorMessage()<<endl;
2013

    
2014
        }
2015
        return -1;
2016
    }
2017
    //testValidation(trans,sresult);
2018

    
2019
    testInitialTemplate(processor, trans, sresult);
2020

    
2021
    cout<<endl<<"============================================================="<<endl<<endl;
2022

    
2023
    exampleSimple1Err(trans, sresult);
2024

    
2025
    cout<<endl<<"============================================================="<<endl<<endl;
2026

    
2027
    exampleSimple1(trans, sresult);
2028

    
2029
    cout<<endl<<"============================================================="<<endl<<endl;
2030

    
2031
    exampleSimple_xmark(trans, sresult);
2032

    
2033
    cout<<endl<<"============================================================="<<endl<<endl;
2034

    
2035
    exampleSimple2(trans, sresult);
2036

    
2037
    cout<<endl<<"============================================================="<<endl<<endl;
2038

    
2039
    exampleSimple3(processor, trans, sresult);
2040

    
2041
    cout<<endl<<"============================================================="<<endl<<endl;
2042

    
2043
    exampleSimple3aError(processor, trans, sresult);
2044

    
2045
    cout<<endl<<"============================================================="<<endl<<endl;
2046

    
2047
    testApplyTemplatesString1(trans, sresult);
2048

    
2049
    cout<<endl<<"============================================================="<<endl<<endl;
2050

    
2051
    testApplyTemplatesString2(processor, trans, sresult);
2052

    
2053
    cout<<endl<<"============================================================="<<endl<<endl;
2054

    
2055

    
2056
    testApplyTemplates2a_Error(processor, trans, sresult);
2057

    
2058
    cout<<endl<<"============================================================="<<endl<<endl;
2059

    
2060
    testTransformToString4(processor, trans, sresult);
2061

    
2062
    cout<<endl<<"============================================================="<<endl<<endl;
2063

    
2064
    testCatalog(cwd, processor,  sresult);
2065

    
2066
    cout<<endl<<"============================================================="<<endl<<endl;
2067

    
2068
    testTransformToString2b(processor, trans, sresult);
2069

    
2070
    cout<<endl<<"============================================================="<<endl<<endl;
2071

    
2072
    testTransformToString3(processor, trans, sresult);
2073

    
2074
    cout<<endl<<"============================================================="<<endl<<endl;
2075
        
2076
    testTransformFromstring(processor, trans, sresult);
2077

    
2078

    
2079
    cout<<endl<<"============================================================="<<endl<<endl;
2080

    
2081
    testTransformFromstring2Err(processor, trans, sresult);
2082

    
2083
    cout<<endl<<"============================================================="<<endl<<endl;
2084

    
2085
    testTrackingOfValueReference(processor, trans, sresult);
2086

    
2087
    cout<<endl<<"============================================================="<<endl<<endl;
2088

    
2089
    testTrackingOfValueReferenceError(processor, trans, sresult);
2090

    
2091
    cout<<endl<<"============================================================="<<endl<<endl;
2092

    
2093
    testPackage1(trans, sresult);
2094

    
2095
    cout<<endl<<"============================================================="<<endl<<endl;
2096

    
2097
    testPackage1a(trans, sresult);
2098

    
2099
    cout<<endl<<"============================================================="<<endl<<endl;
2100

    
2101
    testPackage2_Error(trans, sresult);
2102

    
2103
    cout<<endl<<"============================================================="<<endl<<endl;
2104

    
2105
    testXdmNodeOutput(trans, sresult);
2106

    
2107
    cout<<endl<<"============================================================="<<endl<<endl;
2108

    
2109
    exampleParam(processor, trans, sresult);
2110

    
2111
    cout<<endl<<"============================================================="<<endl<<endl;
2112

    
2113
    xmarkTest1(trans, sresult);
2114

    
2115
    cout<<endl<<"============================================================="<<endl<<endl;
2116

    
2117
    xmarkTest2(trans, sresult);
2118

    
2119
    cout<<endl<<"============================================================="<<endl<<endl;
2120

    
2121
    testCallFunction(processor, trans, sresult);
2122

    
2123
    cout<<endl<<"============================================================="<<endl<<endl;
2124

    
2125
    testResolveUri(processor, trans, sresult);
2126

    
2127
    cout<<endl<<"============================================================="<<endl<<endl;
2128

    
2129
    testContextNotRoot(processor, trans, sresult);
2130

    
2131
    cout<<endl<<"============================================================="<<endl<<endl;
2132

    
2133
    testContextNotRootNamedTemplate(processor, trans, sresult);
2134

    
2135
    cout<<endl<<"============================================================="<<endl<<endl;
2136

    
2137
    testContextNotRootNamedTemplateValue(processor, trans, sresult);
2138

    
2139
    cout<<endl<<"============================================================="<<endl<<endl;
2140

    
2141
   testPipeline(processor, sresult);
2142

    
2143

    
2144
    cout<<endl<<"============================================================="<<endl<<endl;
2145

    
2146
    fflush(stdout);
2147

    
2148

    
2149
    //Available in PE and EE
2150
    //testTransformToStringExtensionFunc(processor, trans);
2151

    
2152

    
2153

    
2154
    //processor->release();
2155
    //return 0;
2156

    
2157
    fflush(stdout);
2158

    
2159

    
2160
    //set to the current working directory
2161
    if(cwd != nullptr) {
2162
        processor->setcwd(cwd);
2163
    } else {
2164
        processor->setcwd(buff);
2165
    }
2166
    Xslt30Processor *trans2 = processor->newXslt30Processor();
2167
    cerr<<" after trans2 cp0"<<endl;
2168
    testApplyTemplatesString1(trans2, sresult);
2169
    cout<<endl<<"============================================================="<<endl<<endl;
2170

    
2171

    
2172

    
2173
    //testThreads (processor, trans2, sresult);
2174

    
2175

    
2176
    delete trans;
2177
    delete trans2;
2178
    delete processor;
2179
    processor->release();
2180

    
2181

    
2182
    cout<<endl<<"======================== Test Results ========================"<<endl<<endl;
2183

    
2184
    std::cout << "\nTest Results - Number of tests= " << (sresult->success + sresult->failure) << ", Successes = "
2185
              << sresult->success << ",  Failures= " << sresult->failure << std::endl;
2186

    
2187
    std::list<std::string>::iterator it;
2188
    std::cout << "Failed tests:" << std::endl;
2189
    // Make iterate point to beginning and increment it one by one until it reaches the end of list.
2190
    for (it = sresult->failureList.begin(); it != sresult->failureList.end(); it++) {
2191
        //Print the contents
2192
        std::cout << it->c_str() << std::endl;
2193

    
2194
    }
2195

    
2196
    delete sresult;
2197

    
2198
#ifdef MEM_DEBUG
2199
    SaxonProcessor::getInfo();
2200
#endif
2201

    
2202
    return 0;
2203
}
(27-27/28)