Project

Profile

Help

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

he / src / main / c / samples / cppTests / testXSLT30.cpp @ 00082f6f

1
#include <sstream>
2
#include <stdio.h>
3
#include "../../Saxon.C.API/SaxonProcessor.h"
4
#include "../../Saxon.C.API/XdmValue.h"
5
#include "../../Saxon.C.API/XdmItem.h"
6
#include "../../Saxon.C.API/XdmNode.h"
7
#include "../../Saxon.C.API/XdmFunctionItem.h"
8
#include "cppExtensionFunction.h"
9
#include "CppTestUtils.h"
10
#include <string>
11

    
12
// TODO: write test case for checking parameters which are null
13

    
14

    
15
using namespace std;
16
char fname[] = "_nativeCall";
17
char funcParameters[] = "(Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;)Ljava/lang/Object;";
18

    
19
JNINativeMethod cppMethods[] =
20
        {
21
                {
22
                        fname,
23
                        funcParameters,
24
                        (void *) &cppExtensionFunction::cppNativeCall
25
                }
26
        };
27

    
28

    
29
/*
30
* Test transform to String. Source and stylesheet supplied as arguments
31
*/
32
void testApplyTemplatesString1(Xslt30Processor *trans, sResultCount *sresult) {
33

    
34
    cout << endl << "Test: testApplyTemplatesString1:" << endl;
35
//    trans->setupXslMessage(false);
36

    
37
    XsltExecutable *executable = trans->compileFromFile("test.xsl");
38

    
39
    if(executable== nullptr) {
40
        sresult->failure++;
41
        sresult->failureList.push_back("testApplyTemplatesString1");
42
        cerr << "testApplyTemplatesString1 NULL found" << endl;
43
        if (trans->exceptionOccurred()) {
44
            cerr <<"testApplyTemplatesString1 error: "<< trans->getErrorMessage() << endl;
45
        }
46
        return;
47
    }
48
    executable->setInitialMatchSelectionAsFile("cat.xml");
49

    
50
    const char *output = executable->applyTemplatesReturningString();
51
    if (output == nullptr) {
52
        printf("result is null ====== FAIL ====== \n");
53
        sresult->failure++;
54
        fflush(stdout);
55
        sresult->failureList.push_back("testApplyTemplatesString1-0");
56
    } else if (string(output).find(string("<out>text2</out>")) != std::string::npos) {
57
        printf("%s", output);
58
        printf("result is OK \n");
59
        sresult->success++;
60
        delete output;
61
    } else {
62
        printf("result is null ====== FAIL ====== \n");
63
        sresult->failure++;
64
        sresult->failureList.push_back("testApplyTemplatesString1-1");
65
            std::cout<<"output="<<output<<std::endl;
66
        delete output;
67
    }
68
    fflush(stdout);
69
    delete executable;
70

    
71
}
72

    
73
/*
74
* Test transform to String. Source and stylesheet supplied as arguments
75
*/
76
void testTransformToStringExtensionFunc(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
77

    
78
    cout << endl << "Test: TransformToStringExtensionFunc:" << endl;
79
    bool nativeFound = processor->registerNativeMethods(SaxonProcessor::sxn_environ->env,
80
                                                        "com/saxonica/functions/extfn/cpp/NativeCall",
81
                                                        cppMethods, sizeof(cppMethods) / sizeof(cppMethods[0]));
82

    
83
    XsltExecutable * executable = trans->compileFromFile("testExtension.xsl");
84

    
85
    if(executable== nullptr) {
86
        sresult->failure++;
87
        sresult->failureList.push_back("TransformToStringExtensionFunc");
88
        cerr << "TransformToStringExtensionFunc NULL found" << endl;
89
        if (trans->exceptionOccurred()) {
90
            cerr <<"TransformToStringExtensionFunc error: "<< trans->getErrorMessage() << endl;
91
        }
92
        return;
93
    }
94

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

    
97
    if (nativeFound) {
98
        const char *output = executable->transformFileToString("cat.xml");
99

    
100
        if (output == nullptr) {
101
            SaxonProcessor::sxn_environ->env->ExceptionDescribe();
102
            printf("result is null ====== FAIL ======  \n");
103
            sresult->failureList.push_back("testTransformToStringExnteionFunc");
104
        } else {
105
            sresult->success++;
106
            printf("result is OK \n");
107
        }
108
        fflush(stdout);
109
        delete output;
110
    } else {
111
        printf("native Class not foun ====== FAIL ====== ");
112
        sresult->failureList.push_back("testTransformToStringExtensionFunc");
113
    }
114
    delete executable;
115
}
116

    
117

    
118
/*
119
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
120
*/
121
void testApplyTemplatesString2(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
122

    
123
    cout << endl << "Test: testApplyTemplatesString2:" << endl;
124
    trans->clearParameters(true);
125

    
126
    XdmNode *input = processor->parseXmlFromFile("cat.xml");
127

    
128
    if (input== nullptr) {
129
        cout << "Source document is null." << endl;
130
        sresult->failure++;
131
        sresult->failureList.push_back("testApplyTemplatesString2");
132
        return;
133

    
134
    }
135
    XsltExecutable * executable = trans->compileFromFile("test.xsl");
136
    if(executable == nullptr) {
137
        sresult->failure++;
138
        sresult->failureList.push_back("testApplyTemplatesString2");
139
        if (trans->exceptionOccurred()) {
140
            cerr <<"testApplyTemplatesString2 error: "<< trans->getErrorMessage() << endl;
141
        }
142
        delete input;
143
        return;
144
    }
145

    
146
    executable->setInitialMatchSelection((XdmValue *) input);
147
    const char *output = executable->applyTemplatesReturningString();
148
    if (output== nullptr) {
149
        printf("result is null ====== FAIL ======  \n");
150
        sresult->failureList.push_back("testApplyTemplatesString2");
151
    } else {
152
        sresult->success++;
153
        printf("%s", output);
154
        printf("result is OK \n");
155
        delete output;
156
    }
157
    fflush(stdout);
158
    delete executable;
159
}
160

    
161
/*
162
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
163
Should be error. Stylesheet file does not exist
164
*/
165
void testApplyTemplates2a(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
166

    
167
    cout << endl << "Test: TransformToString2a:" << endl;
168
    trans->clearParameters(true);
169
    XdmNode *input = processor->parseXmlFromFile("cat.xml");
170

    
171
    if (input == nullptr) {
172
        cout << "Source document is null. ====== FAIL ======" << endl;
173
        sresult->failure++;
174
        sresult->failureList.push_back("testApplyTemplates2a");
175
        return;
176
    }
177
    XsltExecutable * executable = trans->compileFromFile("test-error.xsl");
178

    
179
    if(executable== nullptr) {
180
        printf("result is null \n");
181
        sresult->success++;
182

    
183
        if (trans->exceptionOccurred()) {
184
            cerr <<"testApplyTemplates2a error: "<< trans->getErrorMessage() << endl;
185
        }
186
        delete input;
187
        return;
188
    }
189

    
190

    
191
    sresult->failure++;
192
    cout << endl << "cpp1" << endl;
193
    sresult->failureList.push_back("testApplyTemplates2a");
194
    fflush(stdout);
195
    delete executable;
196

    
197
}
198

    
199
/*
200
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
201
Should be error. Source file does not exist
202
*/
203
void testTransformToString2b(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
204

    
205
    cout << endl << "Test: TransformToString2b:" << endl;
206
    trans->clearParameters(true);
207

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

    
210
    if(result== nullptr) {
211
        sresult->success++;
212
        return;
213
    }else {
214
        delete result;
215
        sresult->failure++;
216
        sresult->failureList.push_back("testTransformToString2b");
217
        cerr << "testTransformToString2b NULL found" << endl;
218
        if (trans->exceptionOccurred()) {
219
            cerr <<"testTransformToString2b error: "<< trans->getErrorMessage() << endl;
220
        }
221
        trans->exceptionClear();
222
        return;
223
    }
224

    
225

    
226
}
227

    
228

    
229
/*
230
* Test transform to String. stylesheet supplied as argument. Source supplied as xml string
231
and integer parmater created and supplied
232
*/
233
void testTransformToString3(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
234

    
235
    cout << endl << "Test: TransformToString3: param size=" << trans->getParameters().size() << endl;
236
    trans->clearParameters(true);
237

    
238
    XdmNode *inputi = processor->parseXmlFromString(
239
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
240

    
241
    XdmAtomicValue *value1 = processor->makeIntegerValue(10);
242

    
243
    trans->setParameter("numParam", (XdmValue *) value1);
244

    
245
    if (inputi== nullptr) {
246
        cout << "Source document inputi is null. ====== FAIL ====== " << endl;
247
        sresult->failureList.push_back("testTransformToString3");
248
        return;
249
    }
250

    
251
    XsltExecutable * executable = trans->compileFromFile("test.xsl");
252

    
253
    if(executable== nullptr) {
254
        sresult->failure++;
255
        sresult->failureList.push_back("testTransformToString3");
256
        cerr << "testTransformToString3 NULL found" << endl;
257
        if (trans->exceptionOccurred()) {
258
            cerr <<"testTransformToString3 error: "<< trans->getErrorMessage() << endl;
259
        }
260
        delete value1;
261
        return;
262
    }
263

    
264
    executable->setInitialMatchSelection((XdmNode *) inputi);
265
    const char *output = executable->applyTemplatesReturningString();
266
    if (output == nullptr) {
267
        printf("result is null ====== FAIL ====== \n");
268
        sresult->failureList.push_back("testTransformToString3");
269
    } else {
270
        printf("%s", output);
271
        printf("result is OK \n");
272
        delete output;
273
    }
274
    fflush(stdout);
275

    
276
    delete value1;
277
    delete executable;
278

    
279
}
280

    
281
/*
282
* Test transform to String. stylesheet supplied as argument. Source supplied as xml string
283
and integer parmater created and supplied
284
*/
285
void testTransformToString4(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
286

    
287
    cout << endl << "Test: testTransformToString4:" << endl;
288
    cout << endl << "cpp0-1" << endl;
289
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
290

    
291
    trans->clearParameters(true);
292
    cout << endl << "cpp0-2" << endl;
293
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
294

    
295
    XdmNode *input = processor->parseXmlFromString(
296
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
297

    
298
    if (input== nullptr) {
299
        sresult->failure++;
300
        cout << "Source document is null. ====== FAIL ====== " << endl;
301
        sresult->failureList.push_back("testTransformToString4");
302
    }
303

    
304

    
305
    XdmValue *values = new XdmValue();
306
    values->addXdmItem((XdmItem *) processor->makeIntegerValue(10));
307
    values->addXdmItem((XdmItem *) processor->makeIntegerValue(5));
308
    values->addXdmItem((XdmItem *) processor->makeIntegerValue(6));
309
    values->addXdmItem((XdmItem *) processor->makeIntegerValue(7));
310

    
311
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
312

    
313
   /* XdmNode *sheet = processor->parseXmlFromFile("test2.xsl");
314

315
    XsltExecutable *executable = trans->compileFromXdmNode(sheet);
316

317
    if(executable== nullptr) {
318
        sresult->failure++;
319
        sresult->failureList.push_back("testTransformToString4");
320
        cerr << "testTransformToString4 NULL found" << endl;
321
        if (trans->exceptionOccurred()) {
322
            cerr <<"testTransformToString4 error: "<< trans->getErrorMessage() << endl;
323
        }
324
        delete values;
325
        return;
326
    }
327

328
    executable->setParameter("values", (XdmValue *) values);
329
    executable->setInitialMatchSelection((XdmNode *) input);
330
    const char *output = executable->applyTemplatesReturningString();
331
    if (output== nullptr) {
332
        printf("result is null \n");
333
        sresult->failureList.push_back("testTransformToString4");
334
    } else {
335
        printf("%s", output);
336
        printf("result is OK \n");
337
        delete output;
338
    }
339
    fflush(stdout);
340
    delete sheet;
341
    delete executable;*/
342
    delete input;
343
    delete values;
344

    
345
}
346

    
347
void testTransformFromstring(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
348
    cout << endl << "Test: testTransfromFromstring: param size="<< trans->getParameters().size() << endl;
349
    trans->clearParameters(true);
350
    if(processor == nullptr) {
351
        cout<<" processor is null"<<endl;
352
        return;
353

    
354
    }
355
    XdmNode *input = processor->parseXmlFromString(
356
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
357

    
358
    XsltExecutable * executable = trans->compileFromString(
359
            "<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>");
360

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

    
371
    cout<<" checkpoint 00"<<endl;
372

    
373
    const char *output = executable->transformToString((XdmNode *) input);
374
    if (output== nullptr) {
375
        printf("result is null ====== FAIL ====== \n");
376
        sresult->failureList.push_back("testTransformFromString");
377
        if (trans->exceptionOccurred()) {
378
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
379
        }
380
    } else {
381
        printf("%s", output);
382
        printf("result is OK \n");
383
        delete output;
384
    }
385
    fflush(stdout);
386

    
387
    delete input;
388
    delete executable;
389

    
390

    
391
}
392

    
393
//Test case has error in the stylesheet
394
void testTransformFromstring2Err(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
395
    cout << endl << "Test: testTransfromFromstring2-Error:" << endl;
396
    trans->clearParameters(true);
397

    
398
    XdmNode *input = processor->parseXmlFromString(
399
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
400

    
401
    XsltExecutable * executable = trans->compileFromString(
402
            "<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>");
403

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

    
414
    executable->setInitialMatchSelection((XdmValue *) input);
415
    const char *output = executable->applyTemplatesReturningString();
416
    if (output== nullptr) {
417
        printf("result is null ====== FAIL ======  \n\nCheck For errors:\n");
418
        sresult->failureList.push_back("testTransformFromstring2Err");
419
        if (executable->exceptionOccurred()) {
420
            cout << "Error message=" << executable->getErrorMessage() << endl;
421
        }
422
    } else {
423
        printf("%s", output);
424
        printf("result is OK \n");
425
    }
426
    fflush(stdout);
427
    delete output;
428

    
429
    trans->exceptionClear();
430
    delete input;
431
    delete executable;
432

    
433
}
434

    
435
void testTrackingOfValueReference(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
436
    trans->clearParameters(true);
437

    
438
    cout << endl << "Test: TrackingOfValueReference:" << endl;
439
    ostringstream test;
440
    ostringstream valStr;
441
    ostringstream name;
442
    for (int i = 0; i < 10; i++) {
443
        test << "v" << i;
444
        valStr << "<out><person>text1</person><person>text2</person><person>text3</person><value>" << test.str()
445
               << "</value></out>";
446
        name << "value" << i;
447

    
448
        XdmValue *values = (XdmValue *) processor->parseXmlFromString(valStr.str().c_str());
449
        //cout<<"Name:"<<name.str()<<", Value:"<<values->getHead()->getStringValue()<<endl;
450
        trans->setParameter(name.str().c_str(), values);
451
        test.str("");
452
        valStr.str("");
453
        name.str("");
454
    }
455

    
456
    std::map<std::string, XdmValue *> parMap = trans->getParameters();
457
    if (parMap.size() > 0) {
458
        //cout << "Parameter size: " << parMap.size() << endl;
459
        //cout << "Parameter size: " << parMap.size()<< endl;//", Value:"<<trans->getParameters()["value0"]->getHead()->getStringValue()<<endl;
460
        ostringstream name1;
461
        for (int i = 0; i < 10; i++) {
462
            name1 << "param:value" << i;
463
            cout << " i:" << i << " Map size:" << parMap.size() << ", ";
464
            XdmValue *valuei = parMap[name1.str()];
465
            if (valuei != NULL) {
466
                cout << name1.str();
467
                if (valuei->itemAt(0) != NULL)
468
                    cout << "= " << valuei->itemAt(0)->getStringValue();
469
                cout << endl;
470
            } else {
471
                sresult->failure++;
472
                std::cerr << "trackingValueReference ====== FAIL ======" << std::endl;
473
                sresult->failureList.push_back("testTrackingOfValueReference");
474
                return;
475
            }
476
            name1.str("");
477
        }
478
    }
479
    sresult->success++;
480
}
481

    
482
/*Test case should be error.*/
483
void testTrackingOfValueReferenceError(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
484
    trans->clearParameters(true);
485

    
486
    cout << endl << "Test: TrackingOfValueReference-Error:" << endl;
487
    cout << "Parameter Map size: " << (trans->getParameters().size()) << endl;
488
    ostringstream test;
489
    ostringstream valStr;
490
    ostringstream name;
491
    for (int i = 0; i < 10; i++) {
492
        test << "v" << i;
493
        valStr << "<out><person>text1</person><person>text2</person><person>text3</person><value>" << test.str()
494
               << "<value></out>";
495
        name << "value" << i;
496

    
497
        XdmValue *values = (XdmValue *) processor->parseXmlFromString(valStr.str().c_str());
498
        trans->setParameter(name.str().c_str(), values);
499
        test.str("");
500
        valStr.str("");
501
        name.str("");
502
    }
503
    std::map<std::string, XdmValue *> parMap = trans->getParameters();
504
    cout << "Parameter Map size: " << parMap.size() << endl;
505

    
506
    ostringstream name1;
507
    bool errorFound = false;
508
    for (int i = 0; i < 10; i++) {
509
        name1 << "param:value" << i;
510
        cout << " i:" << i << " Map size:" << parMap.size() << ", ";
511
        try {
512
            XdmValue *valuei = parMap.at(name1.str());
513
            if (valuei != NULL) {
514
                cout << name1.str();
515
                if (valuei->itemAt(0) != NULL)
516
                    cout << "= " << valuei->itemAt(0)->getStringValue();
517
                cout << endl;
518
            }
519
        } catch (const std::out_of_range &oor) {
520
            cout << "Out of range exception occurred. Exception no. " << endl;
521
            if (!errorFound) {
522
                sresult->success++;
523
                errorFound = true;
524

    
525
                return;
526
            }
527
        }
528
        name1.str("");
529
    }
530
    sresult->failure++;
531
    sresult->failureList.push_back("testTrackingOfValueReferenceError");
532

    
533
}
534

    
535
void testValidation(Xslt30Processor *trans, sResultCount *sresult) {
536
    trans->clearParameters(true);
537

    
538

    
539

    
540
    XsltExecutable * executable = trans->compileFromString(
541
            "<?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>");
542

    
543
    const char *rootValue = executable->callTemplateReturningString("main");
544

    
545

    
546
    if (rootValue== nullptr) {
547
        std::cout << "NULL found" << std::endl;
548
        sresult->failure++;
549
        sresult->failureList.push_back("testValidation");
550
        return;
551

    
552
    } else {
553
        std::cout << "Result=" << rootValue << endl;
554
        sresult->success++;
555
    }
556
    delete executable;
557
}
558

    
559

    
560
void testXdmNodeOutput(Xslt30Processor *trans, sResultCount *sresult) {
561
    trans->clearParameters(true);
562

    
563

    
564
    std::cout << "testXdmNodeOutput" << std::endl;
565
    XsltExecutable * executable = trans->compileFromString(
566
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template name='go'><a/></xsl:template></xsl:stylesheet>");
567
    XdmValue *rootValue = executable->callTemplateReturningValue("go");
568
    if (rootValue== nullptr) {
569
        cout << "Result is null ====== FAIL ====== " << endl;
570
        sresult->failure++;
571
        sresult->failureList.push_back("testXdmNodeOutput-0.0");
572
        return;
573
    }
574
    XdmItem *rootItem = rootValue->getHead();
575
    if (rootItem== nullptr) {
576

    
577
        cout << "Result is null ====== FAIL ====== " << endl;
578
        sresult->failure++;
579
        sresult->failureList.push_back("testXdmNodeOutput-0");
580
        return;
581
    }
582
    XdmNode *root = (XdmNode *) rootItem;
583
    if (root->getNodeKind() == DOCUMENT) {
584
        cout << "Result is a Document" << endl;
585
    } else {
586
        cout << "Node is of kind:" << root->getNodeKind() << endl;
587
    }
588
    const char *result = executable->callTemplateReturningString("go");
589
    if (string(result).find(string("<a/>")) != std::string::npos) {
590
        sresult->success++;
591
    } else {
592
        //TODO - this test case prints the XML declaration. Check if this correct
593
        sresult->failure++;
594
        cout << "testXdmNodeOutputAndString ======= FAIL========" << endl;
595
        sresult->failureList.push_back("testXdmNodeOutput");
596
    }
597
    delete executable;
598
    delete result;
599

    
600
}
601

    
602
void exampleSimple1(Xslt30Processor *proc, sResultCount *sresult) {
603
    cout << "ExampleSimple1 taken from PHP:" << endl;
604

    
605
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
606
    if(executable== nullptr) {
607
        sresult->failure++;
608
        sresult->failureList.push_back("exampleSimple1");
609
        cerr << "exampleSimple1 NULL found" << endl;
610
        if (proc->exceptionOccurred()) {
611
            cerr <<"exampleSimple1 error: "<< proc->getErrorMessage() << endl;
612
        }
613
        return;
614
    }
615
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
616
    const char *result = executable->applyTemplatesReturningString();
617
    if (result != NULL) {
618
        cout << result << endl;
619
        sresult->success++;
620
        delete result;
621
    } else {
622
        cout << "Result is null ====== FAIL ====== " << endl;
623
        sresult->failure++;
624
    }
625
    proc->clearParameters(true);
626
    delete executable;
627

    
628
}
629

    
630
void exampleSimple1Err(Xslt30Processor *proc, sResultCount *sresult) {
631
    cout << "ExampleSimple1Err taken from PHP:" << endl;
632

    
633
    XsltExecutable * executable = proc->compileFromFile("err.xsl");
634
    if(executable == nullptr || proc->exceptionOccurred()) {
635
        if( proc->exceptionOccurred()) {
636
            cout << "Error Message= "<< proc->getErrorMessage() << endl;
637
        }
638
        cout << "Result expected as null " << endl;
639
        sresult->success++;
640
        return;
641
    } else {
642
        sresult->failure++;
643
        sresult->failureList.push_back("exampleSimple1Err");
644

    
645
        delete executable;
646
    }
647
    proc->clearParameters(true);
648

    
649
}
650

    
651

    
652
void exampleSimple2(Xslt30Processor *proc, sResultCount *sresult) {
653
    cout << "<b>exampleSimple2:</b><br/>" << endl;
654
    proc->setcwd("/Users/ond1/work/development/git/saxon-dev/saxondev/src/main/c/samples/cppTests");
655

    
656
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
657
    if(executable== nullptr) {
658
        sresult->failure++;
659
        sresult->failureList.push_back("exampleSimple2");
660
        cerr << "exampleSimple2 NULL found" << endl;
661
        if (proc->exceptionOccurred()) {
662
            cerr <<"exampleSimple2 error: "<< proc->getErrorMessage() << endl;
663
        }
664
        return;
665
    }
666

    
667
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
668
    const char *filename = "output1.xml";
669
    executable->setOutputFile(filename);
670
    executable->applyTemplatesReturningFile("output1.xml");
671

    
672
    if (CppTestUtils::exists("output1.xml")) {
673
        cout << "The file $filename exists" << endl;
674
        remove("output1.xml");
675
        sresult->success++;
676
    } else {
677
        cout << "The file " << filename << " does not exist" << endl;
678
        if (executable->exceptionOccurred()) {
679
            cout << proc->getErrorMessage() << endl;
680
        }
681
        sresult->failure++;
682
        sresult->failureList.push_back("exampleSimple2");
683
    }
684
    proc->clearParameters(true);
685
    executable->clearParameters(true);
686
    delete executable;
687

    
688
}
689

    
690
void exampleSimple3(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
691
    cout << "<b>exampleSimple3:</b><br/>" << endl;
692
    proc->clearParameters(true);
693

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

    
696
   if(executable== nullptr) {
697
        cout << "executable is NULL" <<endl;
698
        if(proc->exceptionOccurred()) {
699
                cout << proc->getErrorMessage() <<endl;
700
                
701
        }
702
        return;
703

    
704
   }
705

    
706
    XdmNode *xdmNode = saxonProc->parseXmlFromString("<doc><b>text value of out</b></doc>");
707
    if (xdmNode== nullptr) {
708
        cout << "Error: xdmNode is null'" << endl;
709
        sresult->failure++;
710
        sresult->failureList.push_back("exampleSimple3");
711
        return;
712
    }
713
    executable->setInitialMatchSelection((XdmNode *) xdmNode);
714
    cout << "end of exampleSimple3" << endl;
715
    executable->clearParameters(true);
716

    
717
    sresult->success++;
718
    delete executable;
719

    
720
}
721

    
722
void exampleSimple3a(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
723
    cout << "<b>exampleSimple3:</b><br/>" << endl;
724
    proc->clearParameters(true);
725

    
726
    XsltExecutable * executable = proc->compileFromFile(nullptr);
727

    
728

    
729
    if (executable == nullptr) {
730

    
731

    
732
        cout << "end of exampleSimple3a" << endl;
733
        proc->clearParameters(true);
734

    
735
        sresult->success++;
736
        return;
737
    }
738

    
739
    cout << "Error: executable is not nullptr'" << endl;
740
    sresult->failure++;
741
    sresult->failureList.push_back("exampleSimple3a");
742
    delete executable;
743
    return;
744

    
745

    
746
}
747

    
748
void exampleParam(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
749
    cout << "\nExampleParam:</b><br/>" << endl;
750

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

    
753
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
754

    
755
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
756
    if (xdmvalue != NULL) {
757

    
758
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
759

    
760
    } else {
761
        cout << "Xdmvalue is null - ====== FAIL =====" << endl;
762
        sresult->failure++;
763
        sresult->failureList.push_back("exampleParam-1");
764
    }
765
    const char *result = executable->applyTemplatesReturningString();
766
    if (result != NULL) {
767
        cout << "Output:" << result << endl;
768
        sresult->success++;
769
    } else {
770
        cout << "Result is NULL<br/>  ======= fail =====" << endl;
771
        sresult->failure++;
772
        sresult->failureList.push_back("exampleParam-2");
773
    }
774

    
775
    //proc->clearParameters();
776
    //unset($result);
777
    //echo 'again with a no parameter value<br/>';
778

    
779
    executable->setProperty("!indent", "yes");
780
    const char *result2 = executable->applyTemplatesReturningString();
781

    
782

    
783
    if (result2 != NULL) {
784
        cout << result2 << endl;
785
        sresult->success++;
786
    }
787

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

    
793
    const char *result3 = executable->applyTemplatesReturningString();
794
    if (result3 != NULL) {
795
        cout << "Output =" << result3 << endl;
796
        sresult->success++;
797
    } else {
798
        cout << "Error in result ===== FAIL =======" << endl;
799
        sresult->failure++;
800
        sresult->failureList.push_back("exampleParam");
801
    }
802
    proc->clearParameters();
803

    
804

    
805
}
806

    
807
// test parameter and properties maps where we update key, value pair.
808
void exampleParam2(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
809
    cout << "\nExampleParam:</b><br/>" << endl;
810
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
811
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
812

    
813

    
814
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
815
    XdmAtomicValue *xdmvalue2i = saxonProc->makeStringValue("Hello from me");
816
    if (xdmvalue != NULL) {
817

    
818
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
819
        executable->setParameter("a-param", (XdmValue *) xdmvalue2i);
820

    
821
    } else {
822
        cout << "Xdmvalue is null - ====== FAIL =====" << endl;
823
        sresult->failure++;
824
        sresult->failureList.push_back("exampleParam-1");
825
    }
826
    const char *result = executable->applyTemplatesReturningString();
827
    if (result != NULL) {
828
        string sresulti = string(result);
829
        if (sresulti.compare("Hello from me") == 0) {
830
            cout << "Output:" << result << endl;
831
            sresult->success++;
832
        } else {
833
            cout << "Result is " << result << " <br/> ======= fail ===== " << endl;
834
            sresult->failure++;
835
            sresult->failureList.push_back("exampleParam-2");
836
        }
837
    } else {
838
        cout<<"Result is NULL<br/>  ======= fail ====="<<endl;
839
        sresult->failure++;
840
        sresult->failureList.push_back("exampleParam-2");
841
    }
842

    
843
//proc->clearParameters();
844
//unset($result);
845
//echo 'again with a no parameter value<br/>';
846

    
847
    executable->setProperty("!indent", "no");
848
    executable->setProperty("!indent", "yes");
849
    const char *result2 = executable->applyTemplatesReturningString();
850

    
851
    executable->clearProperties();
852

    
853
    if(result2 != NULL) {
854
        cout<<result2<<endl;
855
        sresult->success++;
856

    
857
    }
858

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

    
864
    const char *result3 = executable->applyTemplatesReturningString();
865
    if(result3 != NULL) {
866
        cout<<"Output ="<<result3<<endl;
867

    
868
        sresult->success++;
869
    } else {
870
        cout<<"Error in result ===== FAIL ======="<<endl;
871
        sresult->failure++;
872
        sresult->failureList.push_back("exampleParam");
873
    }
874

    
875
}
876

    
877
/* XMarkbench mark test q12.xsl with just-in-time=true*/
878
void xmarkTest1(Xslt30Processor *proc, sResultCount *sresult) {
879
    cout << "XMarkbench mark test q12.xsl (JIT=true):" << endl;
880

    
881
    proc->setJustInTimeCompilation(true);
882

    
883
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
884
    if (result != NULL) {
885
        cout << "XdmNode returned" << endl;
886
        sresult->success++;
887
    } else {
888
        printf("result is null \nCheck For errors:");
889
        sresult->failure++;
890
        sresult->failureList.push_back("xmarkTest1");
891
        if (proc->exceptionOccurred() > 0) {
892
            cout << proc->getErrorMessage() << endl;
893
        }
894
    }
895
    proc->clearParameters(true);
896

    
897

    
898
}
899

    
900

    
901
/* XMarkbench mark test q12.xsl with just-in-time=true*/
902
void xmarkTest2(Xslt30Processor *proc, sResultCount *sresult) {
903
    cout << "XMarkbench mark test q12.xsl (JIT=true):" << endl;
904

    
905

    
906
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
907
    if (result != NULL) {
908
        cout << "XdmNode returned" << endl;
909
    } else {
910
        printf("result is null \nCheck For errors:");
911
        sresult->failure++;
912
        sresult->failureList.push_back("xmarkTest2");
913
        if (proc->exceptionOccurred() > 0) {
914
            cout << proc->getErrorMessage() << endl;
915
        }
916
    }
917
    proc->clearParameters(true);
918

    
919

    
920
}
921

    
922
/* XMarkbench mark test q12.xsl with just-in-time=true*/
923
void exampleSimple_xmark(Xslt30Processor *proc, sResultCount *sresult) {
924
    cout << "XMarkbench mark test q12.xsl:" << endl;
925

    
926
    proc->setJustInTimeCompilation(true);
927

    
928
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
929
    if (result != NULL) {
930
        cout << "XdmNode returned" << endl;
931
        delete result;
932
    } else {
933
        printf("result is null \nCheck For errors:");
934
        if (proc->exceptionOccurred() > 0) {
935
            cout << proc->getErrorMessage() << endl;
936
        }
937
    }
938
    proc->clearParameters(true);
939

    
940
}
941

    
942

    
943
/*
944
* Test saving nd loading a Xslt package
945
*/
946
void testPackage1(Xslt30Processor *trans, sResultCount *sresult) {
947

    
948
    cout << endl << "Test: Saving and loading Packages:" << endl;
949
    trans->clearParameters(true);
950

    
951

    
952
    trans->compileFromFileAndSave("test.xsl", "test1.sef");
953
    const char *output = trans->transformFileToString("cat.xml", "test1.sef");
954
    if (output== nullptr) {
955
        printf("result is null \n");
956
        const char *message = trans->getErrorMessage();
957
        if (message != NULL) {
958
            cout << "Error message =" << message << endl;
959
        }
960
        sresult->failure++;
961
        sresult->failureList.push_back("testPackage1");
962

    
963
    } else {
964
        printf("%s", output);
965
        printf("result is OK \n");
966
        sresult->success++;
967
    }
968
    fflush(stdout);
969
    delete output;
970
}
971

    
972

    
973
/*
974
* Test saving and loading a Xslt package
975
*/
976
void testPackage2(Xslt30Processor *trans, sResultCount *sresult) {
977

    
978
    cout << endl << "Test: Saving and loading Packages2 - Error:" << endl;
979
    trans->clearParameters(true);
980

    
981

    
982
    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>";
983

    
984
    trans->compileFromStringAndSave(stylesheet, "test2.sef");
985
    const char *output = trans->transformFileToString("cat.xml", "test2.sef");
986
    if (output== nullptr) {
987
        printf("result is null as expected \n");
988
        const char *message = trans->getErrorMessage();
989
        if (message != NULL) {
990
            cout << "Error message =" << message << endl;
991
        }
992
        sresult->success++;
993
    } else {
994
        printf("%s", output);
995
        printf("result is OK \n");
996
        sresult->failure++;
997
        sresult->failureList.push_back("testPackage2");
998
    }
999
    fflush(stdout);
1000
    delete output;
1001
}
1002

    
1003
void testCallFunction(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1004

    
1005
    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>";
1006
    cout << endl << "Test: testCallFunction:" << endl;
1007
    XsltExecutable * executable = trans->compileFromString(source);
1008
    XdmValue **valueArray = new XdmValue *[2];
1009

    
1010
    valueArray[0] = (XdmValue *) (proc->makeIntegerValue(2));
1011
    valueArray[1] = (XdmValue *) (proc->makeIntegerValue(3));
1012
    XdmValue *v = executable->callFunctionReturningValue("{http://localhost/}add", valueArray, 2);
1013

    
1014
    if (v != NULL && (v->getHead())->isAtomic() && ((XdmAtomicValue *) (v->getHead()))->getLongValue() == 5) {
1015
        sresult->success++;
1016
    } else {
1017
        if (v != NULL && !(v->getHead())->isAtomic()) {
1018
            cout << "Value in callFunction is not atomic - but expected as atomic value" << endl;
1019
        }
1020
        cout << "testCallFunction ======= FAIL ======" << endl;
1021
        const char *message = executable->getErrorMessage();
1022
        if (message != NULL) {
1023
            cout << "Error message =" << message << endl;
1024
        }
1025
        sresult->failure++;
1026
        sresult->failureList.push_back("testCallFunction");
1027
    }
1028
    delete valueArray[0];
1029
    delete valueArray[1];
1030
    delete [] valueArray;
1031
}
1032

    
1033
void testInitialTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1034

    
1035

    
1036
    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>";
1037
    cout << endl << "Test:testInitialTemplate" << endl;
1038
    XsltExecutable * executable = trans->compileFromString(source);
1039
    if(executable == nullptr) {
1040
        if(trans->exceptionOccurred()) {
1041
            cout << "Error: "<< trans->getErrorMessage() << endl;
1042
        }
1043
        return;
1044
    }
1045
    XdmNode *node = proc->parseXmlFromString("<e/>");
1046

    
1047
    executable->setResultAsRawValue(false);
1048
    std::map<std::string, XdmValue *> parameterValues;
1049
    parameterValues["a"] = proc->makeIntegerValue(12);
1050
    parameterValues["b"] = proc->makeIntegerValue(5);
1051
    executable->setInitialTemplateParameters(parameterValues, false);
1052
    executable->setInitialMatchSelection(node);
1053
    XdmValue *result = executable->applyTemplatesReturningValue();
1054
    if (result != NULL) {
1055
        sresult->success++;
1056
        cout << "Result=" << result->getHead()->getStringValue() << endl;
1057
    } else {
1058
        sresult->failure++;
1059
    }
1060

    
1061
    executable->clearParameters(true);
1062
    delete result;
1063
    delete executable;
1064

    
1065
}
1066

    
1067
void testResolveUri(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1068
    cout << endl << "Test: testResolveUri:" << endl;
1069

    
1070
    XsltExecutable * executable = trans->compileFromString(
1071
            "<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>");
1072

    
1073
    if(executable == nullptr) {
1074
        sresult->failure++;
1075
        sresult->failureList.push_back("testResolveUri");
1076
        if(trans->exceptionOccurred()) {
1077
            cout << "Error: "<< trans->getErrorMessage() << endl;
1078
        }
1079
        return;
1080
    }
1081
    XdmValue *value = executable->callTemplateReturningValue("go");
1082

    
1083

    
1084
    if (value== nullptr) {
1085

    
1086
        sresult->failure++;
1087
        sresult->failureList.push_back("testResolveUri");
1088
    } else {
1089

    
1090
        const char *svalue = value->itemAt(0)->getStringValue();
1091
        cout << "testResolveUri = " << svalue << endl;
1092
        sresult->success++;
1093
    }
1094

    
1095

    
1096
}
1097

    
1098
void testContextNotRoot(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1099
    cout << endl << "Test: testContextNotRoot" << endl;
1100

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

    
1103
    XsltExecutable * executable = trans->compileFromString(
1104
            "<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>");
1105

    
1106
    if(executable == nullptr) {
1107
        sresult->failure++;
1108
        sresult->failureList.push_back("testContextNotRoot");
1109
        if(trans->exceptionOccurred()) {
1110
            cout << "Error: "<< trans->getErrorMessage() << endl;
1111
        }
1112
        return;
1113
    }
1114

    
1115
    executable->setGlobalContextItem(node);
1116
    if (node->getChildCount() > 0) {
1117
        XdmNode *eNode = node->getChildren()[0]->getChildren()[0];
1118
        cout << "Node content = " << eNode->toString() << endl;
1119
        executable->setInitialMatchSelection(eNode);
1120
        const char *result = executable->applyTemplatesReturningString();
1121

    
1122
        if (result== nullptr) {
1123

    
1124
            cout << "testCallFunction ======= FAIL ======" << endl;
1125
            const char *message = executable->getErrorMessage();
1126
            if (message != NULL) {
1127
                cout << "Error message =" << message << endl;
1128
            }
1129
            sresult->failure++;
1130
            sresult->failureList.push_back("testContextNotRoot");
1131
        } else {
1132

    
1133
            cout << "testContextNotRoot = " << result << endl;
1134
            sresult->success++;
1135
        }
1136
    }
1137
}
1138

    
1139

    
1140
void testContextNotRootNamedTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1141
    cout << endl << "Test: testContextNotRootNamedTemplate" << endl;
1142

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

    
1145
    XsltExecutable * executable = trans->compileFromString(
1146
            "<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>");
1147

    
1148
    if(executable == nullptr) {
1149
        sresult->failure++;
1150
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1151
        if(trans->exceptionOccurred()) {
1152
            cout << "Error: "<< trans->getErrorMessage() << endl;
1153
        }
1154
        return;
1155
    }
1156

    
1157
    executable->setGlobalContextItem(node);
1158
    const char *result = executable->callTemplateReturningString("main");
1159

    
1160
    if (result== nullptr) {
1161

    
1162
        cout << "testCallFunction ======= FAIL ======" << endl;
1163
        const char *message = executable->getErrorMessage();
1164
        if (message != NULL) {
1165
            cout << "Error message =" << message << endl;
1166
        }
1167
        sresult->failure++;
1168
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1169
    } else {
1170

    
1171
        cout << "testContextNotRoot = " << result << endl;
1172
        sresult->success++;
1173
    }
1174

    
1175
}
1176

    
1177

    
1178
void testContextNotRootNamedTemplateValue(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1179
    cout << endl << "Test: testContextNotRootNamedTemplateValue" << endl;
1180

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

    
1183
    XsltExecutable * executable = trans->compileFromString(
1184
            "<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>");
1185

    
1186
    if(executable == nullptr) {
1187
        sresult->failure++;
1188
        sresult->failureList.push_back("testContentNotRootNamedTemplateValue");
1189
        if(trans->exceptionOccurred()) {
1190
            cout << "Error: "<< trans->getErrorMessage() << endl;
1191
        }
1192
        return;
1193
    }
1194

    
1195
    executable->setGlobalContextItem(node);
1196
    XdmValue *result = executable->callTemplateReturningValue("main");
1197

    
1198
    if (result== nullptr) {
1199

    
1200
        cout << "testCallFunction ======= FAIL ======" << endl;
1201
        const char *message = executable->getErrorMessage();
1202
        if (message != NULL) {
1203
            cout << "Error message =" << message << endl;
1204
        }
1205
        sresult->failure++;
1206
        sresult->failureList.push_back("testContextNotRootNamedTemplateValue");
1207
    } else {
1208

    
1209
        cout << "testContextNotRoot = " << result->getHead()->getStringValue() << endl;
1210
        sresult->success++;
1211
    }
1212

    
1213
}
1214

    
1215
void testCallSystemFunction(SaxonProcessor *proc, sResultCount *sresult) {
1216

    
1217
    XdmFunctionItem *fi = XdmFunctionItem::getSystemFunction(proc, "{http://www.w3.org/2005/xpath-functions}parse-json",
1218
                                                            1);
1219
    if (fi== nullptr) {
1220
        sresult->failure++;
1221
        sresult->failureList.push_back("testCallSystemFunction");
1222
        return;
1223
    }
1224

    
1225
    XdmValue ** xdmValue = new XdmValue*[1];
1226
    xdmValue[0] = new XdmValue();
1227
    xdmValue[0]->addXdmItem(proc->makeStringValue("[1,2,3]"));
1228
    XdmValue *result = fi->call(xdmValue, 1);
1229

    
1230
    std::cerr << "Result = " << result->toString() << endl;
1231
    if(result->size() == 3) {
1232

    
1233
        cout << "testCallSystemFunction = " << result->getHead()->getStringValue() << endl;
1234
        sresult->success++;
1235

    
1236
    } else {
1237
        sresult->failure++;
1238
        sresult->failureList.push_back("testCallSystemFunction");
1239

    
1240
    }
1241

    
1242
}
1243

    
1244

    
1245
void testPipeline(SaxonProcessor *proc, sResultCount *sresult) {
1246
    cout << endl << "Test: testPipeline" << endl;
1247

    
1248
    Xslt30Processor * trans = proc->newXslt30Processor();
1249
    XsltExecutable * stage1 = trans->compileFromString(
1250
            "<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>");
1251

    
1252
    if(stage1 == nullptr) {
1253
        sresult->failure++;
1254
        sresult->failureList.push_back("testPipeline");
1255
        if(trans->exceptionOccurred()) {
1256
            cout << "stage 1 Error: "<< trans->getErrorMessage() << endl;
1257
        }
1258
        return;
1259
    }
1260

    
1261
    XdmNode *inn = proc->parseXmlFromString("<z/>");
1262

    
1263
    XsltExecutable *stage2 = trans->compileFromString(
1264
            "<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>");
1265

    
1266
    if(stage2 == nullptr) {
1267
        sresult->failure++;
1268
        sresult->failureList.push_back("testPipeline");
1269
        if(trans->exceptionOccurred()) {
1270
            cout << "stage 2 Error: "<< trans->getErrorMessage() << endl;
1271
        }
1272
        return;
1273
    }
1274
    XsltExecutable *stage3 = trans->compileFromString(
1275
            "<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>");
1276

    
1277
    if(stage3 == nullptr) {
1278
        sresult->failure++;
1279
        sresult->failureList.push_back("testPipeline");
1280
        if(trans->exceptionOccurred()) {
1281
            cout << "stage 3 Error: "<< trans->getErrorMessage() << endl;
1282
        }
1283
        return;
1284
    }
1285
    XsltExecutable *stage4 = trans->compileFromString(
1286
            "<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>");
1287

    
1288
    if(stage3 == nullptr) {
1289
        sresult->failure++;
1290
        sresult->failureList.push_back("testPipeline");
1291
        if(trans->exceptionOccurred()) {
1292
            cout << "stage 4 Error: "<< trans->getErrorMessage() << endl;
1293
        }
1294
        return;
1295
    }
1296

    
1297
    XsltExecutable *stage5 = trans->compileFromString(
1298
            "<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>");
1299

    
1300
    if(stage5 == nullptr) {
1301
        sresult->failure++;
1302
        sresult->failureList.push_back("testPipeline");
1303
        if(trans->exceptionOccurred()) {
1304
            cout << "stage 5 Error: "<< trans->getErrorMessage() << endl;
1305
        }
1306
        return;
1307
    }
1308

    
1309
    stage1->setProperty("!indent", "no");
1310
    stage1->setInitialMatchSelection(inn);
1311

    
1312
    XdmValue *d1 = stage1->applyTemplatesReturningValue();
1313
    if (stage1->exceptionOccurred()) {
1314
        sresult->failure++;
1315
        sresult->failureList.push_back("testPipeline");
1316
        cout << "Exception message=" << (stage1->getErrorMessage()) << endl;
1317
        return;
1318
    }
1319

    
1320
    if (d1 == nullptr) {
1321
        cout << "ERROR1 :"<< stage1->getErrorMessage() << endl;;
1322
        sresult->failure++;
1323
        sresult->failureList.push_back("testPipeline-0");
1324
        return;
1325
    }
1326
    XdmItem *d11 = d1->getHead();
1327
    if (d11== nullptr) {
1328

    
1329
        cout << "d11 is NULL\n" << endl;
1330
    }
1331
    const char *data = d1->getHead()->toString();
1332

    
1333
    if (data != NULL) {
1334
        cout << "d1 result=" << data << endl;
1335
    } else {
1336
        sresult->failure++;
1337
        sresult->failureList.push_back("testPipeline-1");
1338

    
1339
        return;
1340
    }
1341
    stage2->setProperty("!indent", "no");
1342
    stage2->setInitialMatchSelection(d1->getHead());
1343
    XdmValue *d2 = stage2->applyTemplatesReturningValue();
1344
    if (d2== nullptr) {
1345
        cout << "ERROR-11\n" << endl;
1346
        if (stage2->exceptionOccurred()) {
1347
            const char *message = stage2->getErrorMessage();
1348
            cout << "exception=" << message << endl;
1349
        }
1350
        sresult->failure++;
1351
        sresult->failureList.push_back("testPipeline-2");
1352
        return;
1353
    }
1354
    stage3->setProperty("!indent", "no");
1355
    stage3->setInitialMatchSelection(d2);
1356
    XdmValue * d3 = stage3->applyTemplatesReturningValue();
1357
    if(d3 == nullptr){
1358
        sresult->failure++;
1359
        sresult->failureList.push_back("testPipeline-3");
1360
        if (stage3->exceptionOccurred()) {
1361
            const char *message = stage3->getErrorMessage();
1362
            cout << "exception=" << message << endl;
1363
        }
1364
      }
1365
    stage4->setProperty("!indent", "no");
1366
    stage4->setInitialMatchSelection(d3);
1367
    XdmValue * d4 = stage4->applyTemplatesReturningValue();
1368
    if(d3== nullptr){
1369
        sresult->failure++;
1370
        sresult->failureList.push_back("testPipeline-1");
1371
        if (stage4->exceptionOccurred()) {
1372
            const char *message = stage4->getErrorMessage();
1373
            cout << "exception=" << message << endl;
1374
        }
1375
        return;
1376
      }
1377
    stage5->setProperty("!indent", "no");
1378
    stage5->setInitialMatchSelection(d4);
1379
    const char * sw = stage5->applyTemplatesReturningString();
1380
    if(sw==NULL){
1381
        sresult->failure++;
1382
        sresult->failureList.push_back("testPipeline-1");
1383
        if (stage5->exceptionOccurred()) {
1384
            const char *message = stage5->getErrorMessage();
1385
            cout << "exception=" << message << endl;
1386
        }
1387
        return;
1388
      }
1389
    cout<<sw<<endl;
1390
    cout << "testPipeline = " << sw << endl;
1391
    sresult->success++;
1392

    
1393

    
1394
}
1395

    
1396

    
1397
void testCatalog(const char * cwd, SaxonProcessor * proc, sResultCount *sresult) {
1398

    
1399

    
1400
    cout << endl << "Test: testCatalog" << endl;
1401
    bool trace = false;
1402
    proc->setcwd(cwd);
1403
    proc->setCatalog("../php/catalog-test/catalog.xml", trace);
1404

    
1405
    Xslt30Processor * trans = proc->newXslt30Processor();
1406

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

    
1409
    if(executable == nullptr) {
1410

    
1411
        if (trans->exceptionOccurred()) {
1412

    
1413
            if(trans->getErrorMessage() != nullptr) {
1414
                const char *message = trans->getErrorMessage();
1415
                cout << "exception=" << message << endl;
1416
            } else {
1417
                const char *message = proc->getErrorMessage();
1418
                if(message != nullptr) {
1419
                    cout << "exception=" << message << endl;
1420
                }
1421
            }
1422
            }
1423
        return;
1424
    }
1425

    
1426
    executable->setInitialMatchSelectionAsFile("../php/catalog-test/example.xml");
1427
//trans->compileFromFile("/home/ond1/work/svn/latest9.9-saxonc/samples/php/catalog-test/test1.xsl");
1428
    const char *result = executable->applyTemplatesReturningString();
1429

    
1430
    if(result != NULL) {
1431
        std::cerr << "testCatalog result= " << result << std::endl;
1432
    }
1433

    
1434
}
1435

    
1436

    
1437
/*
1438
 *
1439
 *void *RunThread(void *args) {
1440

1441
    struct arg_struct *argsi = (struct arg_struct *)args;
1442
    int threadid = argsi->id;
1443
    Xslt30Processor * trans = argsi->trans;
1444
    long tid;
1445
    tid = (long)threadid;
1446

1447
    trans->attachThread();
1448

1449
   trans->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
1450

1451
    const char *result = trans->applyTemplatesReturningString();
1452
    cout<<" Result from THREAD ID: "<< tid << ", " << result<<endl;
1453
    delete result;
1454
    //pthread_exit(NULL);
1455
    trans->detachThread();
1456
}
1457

1458
void testThreads (SaxonProcessor * processor) {
1459
    pthread_t threads[NUM_THREADS];
1460
    int rc;
1461
    int i;
1462

1463
    Xslt30Processor *  trans = processor->newXslt30Processor();
1464

1465
    trans->compileFromFile("../php/xsl/foo.xsl");
1466
    struct arg_struct args;
1467
    args.trans = trans;
1468

1469
    for( i = 0; i < NUM_THREADS; i++ ) {
1470
        cout << "main() : creating thread, " << i << endl;
1471
        args.id = i;
1472
        rc = pthread_create(&threads[i], NULL, RunThread, (void *)&args);
1473

1474
        if (rc) {
1475
            cout << "Error:unable to create thread," << rc << endl;
1476
            exit(-1);
1477
        }
1478
        (void) pthread_join(threads[i], NULL);
1479
    }
1480
  //  pthread_exit(NULL);
1481
}
1482

1483
 *
1484
 */
1485

    
1486

    
1487
int main(int argc, char* argv[]) {
1488

    
1489

    
1490
    const char * cwd = nullptr;
1491
    if(argc > 0) {
1492
        cwd = argv[1];
1493
    }
1494

    
1495
    SaxonProcessor *processor = new SaxonProcessor(true);
1496
    processor->setConfigurationProperty("http://saxon.sf.net/feature/licenseFileLocation", "/usr/local/lib/saxon-license.lic");
1497
    cout << "Test: Xslt30Processor with Saxon version=" << processor->version() << endl << endl;
1498
    
1499
    char buff[FILENAME_MAX]; //create string buffer to hold path
1500
    GetCurrentDir( buff, FILENAME_MAX );
1501
    cout<<"CWD = "<< buff<<endl;
1502
    if(cwd != nullptr) {
1503
            processor->setcwd(cwd);//"/Users/ond1/work/development/git/saxon-dev/saxondev/src/main/c/samples/cppTests"); //set to the current working directory
1504
    }
1505
    if (processor->isSchemaAwareProcessor()) {
1506

    
1507
        std::cerr << "Processor is Schema Aware" << std::endl;
1508
    } else {
1509
        std::cerr << "Processor is not Schema Aware" << std::endl;
1510
    }
1511

    
1512
    sResultCount *sresult = new sResultCount();
1513
    Xslt30Processor *trans = processor->newXslt30Processor();
1514

    
1515
    if( trans == nullptr) {
1516
        cout<< "Error creating Xslt30Processor"<<endl;
1517
        if(processor->exceptionOccurred()) {
1518
            cout<< "Error message: " <<processor->getErrorMessage()<<endl;
1519

    
1520
        }
1521
        return -1;
1522
    }
1523
    //testValidation(trans,sresult);
1524

    
1525
    testInitialTemplate(processor, trans, sresult);
1526

    
1527
    exampleSimple1Err(trans, sresult);
1528

    
1529
    exampleSimple1(trans, sresult);
1530

    
1531
    exampleSimple_xmark(trans, sresult);
1532

    
1533
    exampleSimple2(trans, sresult);
1534

    
1535
    exampleSimple3(processor, trans, sresult);
1536

    
1537
    exampleSimple3a(processor, trans, sresult);
1538

    
1539
    testApplyTemplatesString1(trans, sresult);
1540

    
1541
    testApplyTemplatesString2(processor, trans, sresult);
1542

    
1543
    testApplyTemplates2a(processor, trans, sresult);
1544

    
1545
    testTransformToString4(processor, trans, sresult);
1546

    
1547

    
1548
    testCatalog(cwd, processor,  sresult);
1549
    processor->release();
1550
    return 0;
1551

    
1552
    testTransformToString2b(processor, trans, sresult);
1553

    
1554
    testTransformToString3(processor, trans, sresult);
1555
        
1556
    testTransformFromstring(processor, trans, sresult);
1557

    
1558
    testTransformFromstring2Err(processor, trans, sresult);
1559

    
1560
    testTrackingOfValueReference(processor, trans, sresult);
1561

    
1562
    testTrackingOfValueReferenceError(processor, trans, sresult);
1563

    
1564
    testPackage1(trans, sresult);
1565

    
1566
    testPackage2(trans, sresult);
1567

    
1568
    testXdmNodeOutput(trans, sresult);
1569

    
1570
    exampleParam(processor, trans, sresult);
1571

    
1572
    xmarkTest1(trans, sresult);
1573

    
1574
    xmarkTest2(trans, sresult);
1575
    testCallFunction(processor, trans, sresult);
1576
    testResolveUri(processor, trans, sresult);
1577
    testContextNotRoot(processor, trans, sresult);
1578
    testContextNotRootNamedTemplate(processor, trans, sresult);
1579
    testContextNotRootNamedTemplateValue(processor, trans, sresult);
1580
    testPipeline(processor, sresult);
1581

    
1582
    //Available in PE and EE
1583
    //testTransformToStringExtensionFunc(processor, trans);
1584

    
1585
    delete trans;
1586
    delete processor;
1587
    //processor->release();
1588

    
1589

    
1590
    SaxonProcessor *processor2 = new SaxonProcessor(true);
1591

    
1592
    Xslt30Processor *trans2 = processor2->newXslt30Processor();
1593
    testApplyTemplatesString1(trans2, sresult);
1594
    delete trans2;
1595
    processor2->release();
1596

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

    
1600
    std::list<std::string>::iterator it;
1601
    std::cout << "Failed tests:" << std::endl;
1602
// Make iterate point to begining and incerement it one by one till it reaches the end of list.
1603
    for (it = sresult->failureList.begin(); it != sresult->failureList.end(); it++) {
1604
        //Print the contents
1605
        std::cout << it->c_str() << std::endl;
1606

    
1607
    }
1608

    
1609

    
1610
    return 0;
1611
}
(27-27/28)