Project

Profile

Help

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

he / src / main / c / samples / cppTests / testXSLT30.cpp @ d0e6f8be

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
        return;
57
    } else if (string(output).find(string("<out>text2</out>")) != std::string::npos) {
58
        printf("%s", output);
59
        printf("result is OK \n");
60
        sresult->success++;
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
    }
67
    fflush(stdout);
68
    cout<<"checkout XXX0"<<endl;
69
    delete output;
70
    cout<<"checkout XXX1"<<endl;
71
    delete executable;
72

    
73
}
74

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

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

    
85
    XsltExecutable * executable = trans->compileFromFile("testExtension.xsl");
86

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

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

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

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

    
119

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

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

    
128
    XdmNode *input = processor->parseXmlFromFile("cat.xml");
129

    
130
    if (input== nullptr) {
131
        cout << "Source document is null." << endl;
132

    
133
    }
134
    XsltExecutable * executable = trans->compileFromFile("test.xsl");
135
    if(executable== nullptr) {
136
        sresult->failure++;
137
        sresult->failureList.push_back("testApplyTemplatesString2");
138
        cerr << "testApplyTemplatesString2 NULL found" << endl;
139
        if (trans->exceptionOccurred()) {
140
            cerr <<"testApplyTemplatesString2 error: "<< trans->getErrorMessage() << endl;
141
        }
142
        return;
143
    }
144
    executable->setInitialMatchSelection((XdmValue *) input);
145
    const char *output = executable->applyTemplatesReturningString();
146
    if (output== nullptr) {
147
        printf("result is null ====== FAIL ======  \n");
148
        sresult->failureList.push_back("testApplyTemplatesString2");
149
    } else {
150
        printf("%s", output);
151
        printf("result is OK \n");
152
    }
153
    fflush(stdout);
154
    delete output;
155
    delete executable;
156
}
157

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

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

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

    
176
    if(executable== nullptr) {
177
        sresult->failure++;
178
        sresult->failureList.push_back("testApplyTemplates2a");
179
        cerr << "testApplyTemplates2a NULL found" << endl;
180
        if (trans->exceptionOccurred()) {
181
            cerr <<"testApplyTemplates2a error: "<< trans->getErrorMessage() << endl;
182
        }
183
        return;
184
    }
185

    
186
    executable->setInitialMatchSelection((XdmValue *) input);
187
    const char *output = executable->applyTemplatesReturningString();
188
    if (output== nullptr) {
189
        printf("result is null \n");
190
        sresult->success++;
191
    } else {
192
        printf("%s", output);
193
        printf("result is OK - ======= FAIL ======= \n");
194
        sresult->failure++;
195
        sresult->failureList.push_back("testApplyTemplates2a");
196
    }
197
    fflush(stdout);
198
    delete output;
199
    delete executable;
200

    
201

    
202
}
203

    
204
/*
205
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
206
Should be error. Source file does not exist
207
*/
208
void testTransformToString2b(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
209

    
210
    cout << endl << "Test: TransformToString2b:" << endl;
211
    trans->clearParameters(true);
212

    
213
    XdmNode *input = processor->parseXmlFromFile("cat-error.xml");
214

    
215
    if (input== nullptr) {
216
        cout << "Source document is null. ====== FAIL ====== " << endl;
217
        sresult->failureList.push_back("testTransformToString2b");
218
        return;
219
    }
220
    XsltExecutable * executable = trans->compileFromString("test-error.xsl");
221

    
222
    if(executable== nullptr) {
223
        sresult->failure++;
224
        sresult->failureList.push_back("testTransformToString2b");
225
        cerr << "testTransformToString2b NULL found" << endl;
226
        if (trans->exceptionOccurred()) {
227
            cerr <<"testTransformToString2b error: "<< trans->getErrorMessage() << endl;
228
        }
229
        return;
230
    }
231

    
232
    executable->setInitialMatchSelection((XdmNode *) input);
233
    const char *output = executable->transformFileToString(NULL);
234
    if (output== nullptr) {
235
        printf("result is null ====== FAIL ======  \nCheck For errors:");
236
        sresult->failureList.push_back("testTransformToString2b");
237
        if (executable->exceptionOccurred()) {
238
            cout << trans->getErrorMessage() << endl;
239
        }
240
    } else {
241
        printf("%s", output);
242
        printf("result is OK \n");
243
    }
244
    fflush(stdout);
245
    delete output;
246

    
247
    trans->exceptionClear();
248
    delete executable;
249

    
250
}
251

    
252

    
253
/*
254
* Test transform to String. stylesheet supplied as argument. Source supplied as xml string
255
and integer parmater created and supplied
256
*/
257
void testTransformToString3(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
258

    
259
    cout << endl << "Test: TransformToString3:" << endl;
260
    trans->clearParameters(true);
261

    
262
    XdmNode *inputi = processor->parseXmlFromString(
263
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
264

    
265
    XdmAtomicValue *value1 = processor->makeIntegerValue(10);
266

    
267
    trans->setParameter("numParam", (XdmValue *) value1);
268

    
269
    if (inputi== nullptr) {
270
        cout << "Source document inputi is null. ====== FAIL ====== " << endl;
271
        sresult->failureList.push_back("testTransformToString3");
272
        return;
273
    }
274

    
275
    XsltExecutable * executable = trans->compileFromFile("test.xsl");
276

    
277
    if(executable== nullptr) {
278
        sresult->failure++;
279
        sresult->failureList.push_back("testTransformToString3");
280
        cerr << "testTransformToString3 NULL found" << endl;
281
        if (trans->exceptionOccurred()) {
282
            cerr <<"testTransformToString3 error: "<< trans->getErrorMessage() << endl;
283
        }
284
        return;
285
    }
286

    
287
    executable->setInitialMatchSelection((XdmNode *) inputi);
288
    const char *output = executable->applyTemplatesReturningString();
289
    if (output== nullptr) {
290
        printf("result is null ====== FAIL ====== \n");
291
        sresult->failureList.push_back("testTransformToString3");
292
    } else {
293
        printf("%s", output);
294
        printf("result is OK \n");
295
    }
296
    fflush(stdout);
297
    delete output;
298
    delete value1;
299
    delete executable;
300

    
301
}
302

    
303
/*
304
* Test transform to String. stylesheet supplied as argument. Source supplied as xml string
305
and integer parmater created and supplied
306
*/
307
void testTransformToString4(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
308

    
309
    cout << endl << "Test: TransformToString4:" << endl;
310
    trans->clearParameters(true);
311

    
312
    XdmNode *input = processor->parseXmlFromString(
313
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
314

    
315
    XdmValue *values = new XdmValue();
316
    values->addXdmItem((XdmItem *) processor->makeIntegerValue(10));
317
    values->addXdmItem((XdmItem *) processor->makeIntegerValue(5));
318
    values->addXdmItem((XdmItem *) processor->makeIntegerValue(6));
319
    values->addXdmItem((XdmItem *) processor->makeIntegerValue(7));
320

    
321

    
322
    if (input== nullptr) {
323
        cout << "Source document is null. ====== FAIL ====== " << endl;
324
        sresult->failureList.push_back("testTransformToString4");
325
    }
326
    XdmNode *sheet = processor->parseXmlFromFile("test2.xsl");
327

    
328
    XsltExecutable *executable = trans->compileFromXdmNode(sheet);
329

    
330
    if(executable== nullptr) {
331
        sresult->failure++;
332
        sresult->failureList.push_back("testTransformToString4");
333
        cerr << "testTransformToString4 NULL found" << endl;
334
        if (trans->exceptionOccurred()) {
335
            cerr <<"testTransformToString4 error: "<< trans->getErrorMessage() << endl;
336
        }
337
        return;
338
    }
339

    
340
    executable->setParameter("values", (XdmValue *) values);
341
    executable->setInitialMatchSelection((XdmNode *) input);
342
    const char *output = executable->applyTemplatesReturningString(/*"test2.xsl"*/);
343
    if (output== nullptr) {
344
        printf("result is null \n");
345
        sresult->failureList.push_back("testTransformToString4");
346
    } else {
347
        printf("%s", output);
348
        printf("result is OK \n");
349
    }
350
    fflush(stdout);
351
    delete output;
352
    delete executable;
353
    delete input;
354
    delete values;
355

    
356
}
357

    
358
void testTransformFromstring(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
359
    cout << endl << "Test: testTransfromFromstring:" << endl;
360
    trans->clearParameters(true);
361

    
362
    XdmNode *input = processor->parseXmlFromString(
363
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
364

    
365
    XsltExecutable * executable = trans->compileFromString(
366
            "<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>");
367

    
368
    if(executable== nullptr) {
369
        sresult->failure++;
370
        sresult->failureList.push_back("testTransformFromstring");
371
        cerr << "testTransformFromstring NULL found" << endl;
372
        if (trans->exceptionOccurred()) {
373
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
374
        }
375
        return;
376
    }
377

    
378
    const char *output = executable->transformToString((XdmNode *) input);
379
    if (output== nullptr) {
380
        printf("result is null ====== FAIL ====== \n");
381
        sresult->failureList.push_back("testTransformFromString");
382
    } else {
383
        printf("%s", output);
384
        printf("result is OK \n");
385

    
386
    }
387
    fflush(stdout);
388
    delete output;
389
    delete input;
390
    delete executable;
391

    
392

    
393
}
394

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

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

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

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

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

    
431
    trans->exceptionClear();
432
    delete input;
433
    delete executable;
434

    
435
}
436

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

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

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

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

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

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

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

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

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

    
535
}
536

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

    
540

    
541

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

    
545
    const char *rootValue = executable->callTemplateReturningString("main");
546

    
547

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

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

    
561

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

    
565

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

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

    
602
}
603

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

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

    
629
}
630

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

    
634
    XsltExecutable * executable = proc->compileFromFile("err.xsl");
635
    if(proc->exceptionOccurred()) {
636
        cout << "Result expected as null " << endl;
637
        sresult->success++;
638
    } else {
639

    
640
        sresult->failure++;
641
        sresult->failureList.push_back("exampleSimple1Err");
642

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

    
648

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

    
653
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
654
    if(executable== nullptr) {
655
        sresult->failure++;
656
        sresult->failureList.push_back("exampleSimple2");
657
        cerr << "exampleSimple2 NULL found" << endl;
658
        if (proc->exceptionOccurred()) {
659
            cerr <<"exampleSimple2 error: "<< proc->getErrorMessage() << endl;
660
        }
661
        return;
662
    }
663
    cout << "exampleSimple2 cp0" << endl;
664
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
665
    const char *filename = "output1.xml";
666
    executable->setOutputFile(filename);
667
    executable->applyTemplatesReturningFile("output1.xml");
668

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

    
685
}
686

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

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

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

    
701
   }
702

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

    
714
    sresult->success++;
715

    
716
}
717

    
718
void exampleSimple3a(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
719
    cout << "<b>exampleSimple3:</b><br/>" << endl;
720
    proc->clearParameters(true);
721

    
722
    XsltExecutable * executable = proc->compileFromFile(nullptr);
723

    
724

    
725
    if (executable == nullptr) {
726

    
727

    
728
        cout << "end of exampleSimple3a" << endl;
729
        proc->clearParameters(true);
730

    
731
        sresult->success++;
732
        return;
733
    }
734

    
735
    cout << "Error: executable is not nullptr'" << endl;
736
    sresult->failure++;
737
    sresult->failureList.push_back("exampleSimple3a");
738
    return;
739

    
740

    
741
}
742

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

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

    
748
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
749

    
750
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
751
    if (xdmvalue != NULL) {
752

    
753
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
754

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

    
770
    //proc->clearParameters();
771
    //unset($result);
772
    //echo 'again with a no parameter value<br/>';
773

    
774
    executable->setProperty("!indent", "yes");
775
    const char *result2 = executable->applyTemplatesReturningString();
776

    
777

    
778
    if (result2 != NULL) {
779
        cout << result2 << endl;
780
        sresult->success++;
781
    }
782

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

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

    
799

    
800
}
801

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

    
808

    
809
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
810
    XdmAtomicValue *xdmvalue2i = saxonProc->makeStringValue("Hello from me");
811
    if (xdmvalue != NULL) {
812

    
813
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
814
        executable->setParameter("a-param", (XdmValue *) xdmvalue2i);
815

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

    
838
//proc->clearParameters();
839
//unset($result);
840
//echo 'again with a no parameter value<br/>';
841

    
842
    executable->setProperty("!indent", "no");
843
    executable->setProperty("!indent", "yes");
844
    const char *result2 = executable->applyTemplatesReturningString();
845

    
846
    executable->clearProperties();
847

    
848
    if(result2 != NULL) {
849
        cout<<result2<<endl;
850
        sresult->success++;
851

    
852
    }
853

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

    
859
    const char *result3 = executable->applyTemplatesReturningString();
860
    if(result3 != NULL) {
861
        cout<<"Output ="<<result3<<endl;
862

    
863
        sresult->success++;
864
    } else {
865
        cout<<"Error in result ===== FAIL ======="<<endl;
866
        sresult->failure++;
867
        sresult->failureList.push_back("exampleParam");
868
    }
869

    
870
}
871

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

    
876
    proc->setJustInTimeCompilation(true);
877

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

    
892

    
893
}
894

    
895

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

    
900

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

    
914

    
915
}
916

    
917
/* XMarkbench mark test q12.xsl with just-in-time=true*/
918
void exampleSimple_xmark(Xslt30Processor *proc, sResultCount *sresult) {
919
    cout << "XMarkbench mark test q12.xsl:" << endl;
920

    
921
    proc->setJustInTimeCompilation(true);
922

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

    
934
}
935

    
936

    
937
/*
938
* Test saving nd loading a Xslt package
939
*/
940
void testPackage1(Xslt30Processor *trans, sResultCount *sresult) {
941

    
942
    cout << endl << "Test: Saving and loading Packages:" << endl;
943
    trans->clearParameters(true);
944

    
945

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

    
957
    } else {
958
        printf("%s", output);
959
        printf("result is OK \n");
960
        sresult->success++;
961
    }
962
    fflush(stdout);
963
    delete output;
964
}
965

    
966

    
967
/*
968
* Test saving and loading a Xslt package
969
*/
970
void testPackage2(Xslt30Processor *trans, sResultCount *sresult) {
971

    
972
    cout << endl << "Test: Saving and loading Packages2 - Error:" << endl;
973
    trans->clearParameters(true);
974

    
975

    
976
    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>";
977

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

    
997
void testCallFunction(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
998

    
999
    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>";
1000
    cout << endl << "Test: testCallFunction:" << endl;
1001
    XsltExecutable * executable = trans->compileFromString(source);
1002
    XdmValue **valueArray = new XdmValue *[2];
1003

    
1004
    valueArray[0] = (XdmValue *) (proc->makeIntegerValue(2));
1005
    valueArray[1] = (XdmValue *) (proc->makeIntegerValue(3));
1006
    XdmValue *v = executable->callFunctionReturningValue("{http://localhost/}add", valueArray, 2);
1007

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

    
1027
void testInitialTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1028

    
1029

    
1030
    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>";
1031
    cout << endl << "Test:testInitialTemplate" << endl;
1032
    XsltExecutable * executable = trans->compileFromString(source);
1033
    if(executable == nullptr) {
1034
        if(trans->exceptionOccurred()) {
1035
            cout << "Error: "<< trans->getErrorMessage() << endl;
1036
        }
1037
        return;
1038
    }
1039
    XdmNode *node = proc->parseXmlFromString("<e/>");
1040

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

    
1055
}
1056

    
1057
void testResolveUri(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1058
    cout << endl << "Test: testResolveUri:" << endl;
1059

    
1060
    XsltExecutable * executable = trans->compileFromString(
1061
            "<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>");
1062

    
1063
    if(executable == nullptr) {
1064
        sresult->failure++;
1065
        sresult->failureList.push_back("testResolveUri");
1066
        if(trans->exceptionOccurred()) {
1067
            cout << "Error: "<< trans->getErrorMessage() << endl;
1068
        }
1069
        return;
1070
    }
1071
    XdmValue *value = executable->callTemplateReturningValue("go");
1072

    
1073

    
1074
    if (value== nullptr) {
1075

    
1076
        sresult->failure++;
1077
        sresult->failureList.push_back("testResolveUri");
1078
    } else {
1079

    
1080
        const char *svalue = value->itemAt(0)->getStringValue();
1081
        cout << "testResolveUri = " << svalue << endl;
1082
        sresult->success++;
1083
    }
1084

    
1085

    
1086
}
1087

    
1088
void testContextNotRoot(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1089
    cout << endl << "Test: testContextNotRoot" << endl;
1090

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

    
1093
    XsltExecutable * executable = trans->compileFromString(
1094
            "<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>");
1095

    
1096
    if(executable == nullptr) {
1097
        sresult->failure++;
1098
        sresult->failureList.push_back("testContextNotRoot");
1099
        if(trans->exceptionOccurred()) {
1100
            cout << "Error: "<< trans->getErrorMessage() << endl;
1101
        }
1102
        return;
1103
    }
1104

    
1105
    executable->setGlobalContextItem(node);
1106
    if (node->getChildCount() > 0) {
1107
        XdmNode *eNode = node->getChildren()[0]->getChildren()[0];
1108
        cout << "Node content = " << eNode->toString() << endl;
1109
        executable->setInitialMatchSelection(eNode);
1110
        const char *result = executable->applyTemplatesReturningString();
1111

    
1112
        if (result== nullptr) {
1113

    
1114
            cout << "testCallFunction ======= FAIL ======" << endl;
1115
            const char *message = executable->getErrorMessage();
1116
            if (message != NULL) {
1117
                cout << "Error message =" << message << endl;
1118
            }
1119
            sresult->failure++;
1120
            sresult->failureList.push_back("testContextNotRoot");
1121
        } else {
1122

    
1123
            cout << "testContextNotRoot = " << result << endl;
1124
            sresult->success++;
1125
        }
1126
    }
1127
}
1128

    
1129

    
1130
void testContextNotRootNamedTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1131
    cout << endl << "Test: testContextNotRootNamedTemplate" << endl;
1132

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

    
1135
    XsltExecutable * executable = trans->compileFromString(
1136
            "<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>");
1137

    
1138
    if(executable == nullptr) {
1139
        sresult->failure++;
1140
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1141
        if(trans->exceptionOccurred()) {
1142
            cout << "Error: "<< trans->getErrorMessage() << endl;
1143
        }
1144
        return;
1145
    }
1146

    
1147
    executable->setGlobalContextItem(node);
1148
    const char *result = executable->callTemplateReturningString("main");
1149

    
1150
    if (result== nullptr) {
1151

    
1152
        cout << "testCallFunction ======= FAIL ======" << endl;
1153
        const char *message = executable->getErrorMessage();
1154
        if (message != NULL) {
1155
            cout << "Error message =" << message << endl;
1156
        }
1157
        sresult->failure++;
1158
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1159
    } else {
1160

    
1161
        cout << "testContextNotRoot = " << result << endl;
1162
        sresult->success++;
1163
    }
1164

    
1165
}
1166

    
1167

    
1168
void testContextNotRootNamedTemplateValue(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1169
    cout << endl << "Test: testContextNotRootNamedTemplateValue" << endl;
1170

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

    
1173
    XsltExecutable * executable = trans->compileFromString(
1174
            "<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>");
1175

    
1176
    if(executable == nullptr) {
1177
        sresult->failure++;
1178
        sresult->failureList.push_back("testContentNotRootNamedTemplateValue");
1179
        if(trans->exceptionOccurred()) {
1180
            cout << "Error: "<< trans->getErrorMessage() << endl;
1181
        }
1182
        return;
1183
    }
1184

    
1185
    executable->setGlobalContextItem(node);
1186
    XdmValue *result = executable->callTemplateReturningValue("main");
1187

    
1188
    if (result== nullptr) {
1189

    
1190
        cout << "testCallFunction ======= FAIL ======" << endl;
1191
        const char *message = executable->getErrorMessage();
1192
        if (message != NULL) {
1193
            cout << "Error message =" << message << endl;
1194
        }
1195
        sresult->failure++;
1196
        sresult->failureList.push_back("testContextNotRootNamedTemplateValue");
1197
    } else {
1198

    
1199
        cout << "testContextNotRoot = " << result->getHead()->getStringValue() << endl;
1200
        sresult->success++;
1201
    }
1202

    
1203
}
1204

    
1205
void testCallSystemFunction(SaxonProcessor *proc, sResultCount *sresult) {
1206

    
1207
    XdmFunctionItem *fi = XdmFunctionItem::getSystemFunction(proc, "{http://www.w3.org/2005/xpath-functions}parse-json",
1208
                                                            1);
1209
    if (fi== nullptr) {
1210
        sresult->failure++;
1211
        sresult->failureList.push_back("testCallSystemFunction");
1212
        return;
1213
    }
1214

    
1215
    XdmValue ** xdmValue = new XdmValue*[1];
1216
    xdmValue[0] = new XdmValue();
1217
    xdmValue[0]->addXdmItem(proc->makeStringValue("[1,2,3]"));
1218
    XdmValue *result = fi->call(xdmValue, 1);
1219

    
1220
    std::cerr << "Result = " << result->toString() << endl;
1221
    if(result->size() == 3) {
1222

    
1223
        cout << "testCallSystemFunction = " << result->getHead()->getStringValue() << endl;
1224
        sresult->success++;
1225

    
1226
    } else {
1227
        sresult->failure++;
1228
        sresult->failureList.push_back("testCallSystemFunction");
1229

    
1230
    }
1231

    
1232
}
1233

    
1234

    
1235
void testPipeline(SaxonProcessor *proc, sResultCount *sresult) {
1236
    cout << endl << "Test: testPipeline" << endl;
1237

    
1238
    Xslt30Processor * trans = proc->newXslt30Processor();
1239
    XsltExecutable * stage1 = trans->compileFromString(
1240
            "<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>");
1241

    
1242
    if(stage1 == nullptr) {
1243
        sresult->failure++;
1244
        sresult->failureList.push_back("testPipeline");
1245
        if(trans->exceptionOccurred()) {
1246
            cout << "stage 1 Error: "<< trans->getErrorMessage() << endl;
1247
        }
1248
        return;
1249
    }
1250

    
1251
    XdmNode *inn = proc->parseXmlFromString("<z/>");
1252

    
1253
    XsltExecutable *stage2 = trans->compileFromString(
1254
            "<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>");
1255

    
1256
    if(stage2 == nullptr) {
1257
        sresult->failure++;
1258
        sresult->failureList.push_back("testPipeline");
1259
        if(trans->exceptionOccurred()) {
1260
            cout << "stage 2 Error: "<< trans->getErrorMessage() << endl;
1261
        }
1262
        return;
1263
    }
1264
    XsltExecutable *stage3 = trans->compileFromString(
1265
            "<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>");
1266

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

    
1278
    if(stage3 == nullptr) {
1279
        sresult->failure++;
1280
        sresult->failureList.push_back("testPipeline");
1281
        if(trans->exceptionOccurred()) {
1282
            cout << "stage 4 Error: "<< trans->getErrorMessage() << endl;
1283
        }
1284
        return;
1285
    }
1286

    
1287
    XsltExecutable *stage5 = trans->compileFromString(
1288
            "<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>");
1289

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

    
1299
    stage1->setProperty("!indent", "no");
1300
    stage1->setInitialMatchSelection(inn);
1301

    
1302
    XdmValue *d1 = stage1->applyTemplatesReturningValue();
1303
    if (stage1->exceptionOccurred()) {
1304
        sresult->failure++;
1305
        sresult->failureList.push_back("testPipeline");
1306
        cout << "Exception message=" << (stage1->getErrorMessage()) << endl;
1307
        return;
1308
    }
1309

    
1310
    if (d1 == nullptr) {
1311
        cout << "ERROR1 :"<< stage1->getErrorMessage() << endl;;
1312
        sresult->failure++;
1313
        sresult->failureList.push_back("testPipeline-0");
1314
        return;
1315
    }
1316
    XdmItem *d11 = d1->getHead();
1317
    if (d11== nullptr) {
1318

    
1319
        cout << "d11 is NULL\n" << endl;
1320
    }
1321
    const char *data = d1->getHead()->toString();
1322

    
1323
    if (data != NULL) {
1324
        cout << "d1 result=" << data << endl;
1325
    } else {
1326
        sresult->failure++;
1327
        sresult->failureList.push_back("testPipeline-1");
1328

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

    
1383

    
1384
}
1385

    
1386

    
1387
void testCatalog(const char * cwd, SaxonProcessor * proc, sResultCount *sresult) {
1388

    
1389
    cout << endl << "Test: testCatalog" << endl;
1390
    bool trace = false;
1391
    proc->setcwd(cwd);
1392
    proc->setCatalog("../php/catalog-test/catalog.xml", trace);
1393

    
1394
    Xslt30Processor * trans = proc->newXslt30Processor();
1395

    
1396

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

    
1399
    if(executable == nullptr) {
1400
        if (trans->exceptionOccurred()) {
1401
            const char *message = trans->getErrorMessage();
1402
            cout << "exception=" << message << endl;
1403
        }
1404
        return;
1405
    }
1406
    //trans->setSourceFromFile("/home/ond1/work/svn/latest9.9-saxonc/samples/php/catalog-test/example.xml");
1407
    executable->setInitialMatchSelectionAsFile("../php/catalog-test/example.xml");
1408
//trans->compileFromFile("/home/ond1/work/svn/latest9.9-saxonc/samples/php/catalog-test/test1.xsl");
1409
    const char *result = executable->applyTemplatesReturningString();
1410

    
1411
    if(result != NULL) {
1412
        std::cerr << "testCatalog result= " << result << std::endl;
1413
    }
1414

    
1415
}
1416

    
1417

    
1418
/*
1419
 *
1420
 *void *RunThread(void *args) {
1421

1422
    struct arg_struct *argsi = (struct arg_struct *)args;
1423
    int threadid = argsi->id;
1424
    Xslt30Processor * trans = argsi->trans;
1425
    long tid;
1426
    tid = (long)threadid;
1427

1428
    trans->attachThread();
1429

1430
   trans->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
1431

1432
    const char *result = trans->applyTemplatesReturningString();
1433
    cout<<" Result from THREAD ID: "<< tid << ", " << result<<endl;
1434
    delete result;
1435
    //pthread_exit(NULL);
1436
    trans->detachThread();
1437
}
1438

1439
void testThreads (SaxonProcessor * processor) {
1440
    pthread_t threads[NUM_THREADS];
1441
    int rc;
1442
    int i;
1443

1444
    Xslt30Processor *  trans = processor->newXslt30Processor();
1445

1446
    trans->compileFromFile("../php/xsl/foo.xsl");
1447
    struct arg_struct args;
1448
    args.trans = trans;
1449

1450
    for( i = 0; i < NUM_THREADS; i++ ) {
1451
        cout << "main() : creating thread, " << i << endl;
1452
        args.id = i;
1453
        rc = pthread_create(&threads[i], NULL, RunThread, (void *)&args);
1454

1455
        if (rc) {
1456
            cout << "Error:unable to create thread," << rc << endl;
1457
            exit(-1);
1458
        }
1459
        (void) pthread_join(threads[i], NULL);
1460
    }
1461
  //  pthread_exit(NULL);
1462
}
1463

1464
 *
1465
 */
1466

    
1467

    
1468
int main(int argc, char* argv[]) {
1469

    
1470

    
1471
    const char * cwd = nullptr;
1472
    if(argc > 0) {
1473
        cwd = argv[1];
1474
    }
1475

    
1476
    SaxonProcessor *processor = new SaxonProcessor(true);
1477
    processor->setConfigurationProperty("http://saxon.sf.net/feature/licenseFileLocation", "/usr/local/lib/saxon-license.lic");
1478
    cout << "Test: Xslt30Processor with Saxon version=" << processor->version() << endl << endl;
1479
    
1480
    char buff[FILENAME_MAX]; //create string buffer to hold path
1481
    GetCurrentDir( buff, FILENAME_MAX );
1482
    cout<<"CWD = "<< buff<<endl;
1483
    if(cwd != nullptr) {
1484
            processor->setcwd(cwd);//"/Users/ond1/work/development/git/saxon-dev/saxondev/src/main/c/samples/cppTests"); //set to the current working directory
1485
    }
1486
    if (processor->isSchemaAwareProcessor()) {
1487

    
1488
        std::cerr << "Processor is Schema Aware" << std::endl;
1489
    } else {
1490
        std::cerr << "Processor is not Schema Aware" << std::endl;
1491
    }
1492

    
1493
    sResultCount *sresult = new sResultCount();
1494
    Xslt30Processor *trans = processor->newXslt30Processor();
1495

    
1496
    if( trans == nullptr) {
1497
        cout<< "Error creating Xslt30Processor"<<endl;
1498
        if(processor->exceptionOccurred()) {
1499
            cout<< "Error message: " <<processor->getErrorMessage()<<endl;
1500

    
1501
        }
1502
        return -1;
1503
    }
1504
    //testValidation(trans,sresult);
1505
    std::cerr << "cp0" << std::endl;
1506
    testInitialTemplate(processor, trans, sresult);
1507
    std::cerr << "cp1" << std::endl;
1508
    exampleSimple1Err(trans, sresult);
1509
    std::cerr << "cp2" << std::endl;
1510
    exampleSimple1(trans, sresult);
1511
    std::cerr << "cp3" << std::endl;
1512

    
1513
    exampleSimple_xmark(trans, sresult);
1514
    std::cerr << "cp4" << std::endl;
1515
    exampleSimple2(trans, sresult);
1516
    std::cerr << "cp5" << std::endl;
1517
    exampleSimple3(processor, trans, sresult);
1518
    std::cerr << "cp6" << std::endl;
1519
    exampleSimple3a(processor, trans, sresult);
1520
    std::cerr << "cp7" << std::endl;
1521
    testApplyTemplatesString1(trans, sresult);
1522
    std::cerr << "cp8" << std::endl;
1523
    testApplyTemplatesString2(processor, trans, sresult);
1524

    
1525
    testApplyTemplates2a(processor, trans, sresult);
1526

    
1527
    testTransformToString4(processor, trans, sresult);
1528

    
1529
    testCatalog(cwd, processor,  sresult);
1530

    
1531
    /*testTransformToString2b(processor, trans, sresult);
1532

1533
    testTransformToString3(processor, trans, sresult);
1534
        
1535
    testTransformFromstring(processor, trans, sresult);
1536

1537
    testTransformFromstring2Err(processor, trans, sresult);      */
1538

    
1539
    testTrackingOfValueReference(processor, trans, sresult);
1540

    
1541
    testTrackingOfValueReferenceError(processor, trans, sresult);
1542

    
1543
    testPackage1(trans, sresult);
1544

    
1545
    testPackage2(trans, sresult);
1546

    
1547
    testXdmNodeOutput(trans, sresult);
1548

    
1549
    exampleParam(processor, trans, sresult);
1550

    
1551
    xmarkTest1(trans, sresult);
1552

    
1553
    xmarkTest2(trans, sresult);
1554
    testCallFunction(processor, trans, sresult);
1555
    testResolveUri(processor, trans, sresult);
1556
    testContextNotRoot(processor, trans, sresult);
1557
    testContextNotRootNamedTemplate(processor, trans, sresult);
1558
    testContextNotRootNamedTemplateValue(processor, trans, sresult);
1559
    testPipeline(processor, sresult);
1560

    
1561
    //Available in PE and EE
1562
    //testTransformToStringExtensionFunc(processor, trans);
1563

    
1564
    delete trans;
1565
    delete processor;
1566
    // processor->release();
1567

    
1568

    
1569
    SaxonProcessor *processor2 = new SaxonProcessor(true);
1570

    
1571
    Xslt30Processor *trans2 = processor2->newXslt30Processor();
1572
    testApplyTemplatesString1(trans2, sresult);
1573
    delete trans2;
1574
    processor2->release();
1575

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

    
1579
    std::list<std::string>::iterator it;
1580
    std::cout << "Failed tests:" << std::endl;
1581
// Make iterate point to begining and incerement it one by one till it reaches the end of list.
1582
    for (it = sresult->failureList.begin(); it != sresult->failureList.end(); it++) {
1583
        //Print the contents
1584
        std::cout << it->c_str() << std::endl;
1585

    
1586
    }
1587

    
1588

    
1589
    return 0;
1590
}
(27-27/28)