Project

Profile

Help

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

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

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
    sresult->failureList.push_back("testApplyTemplates2a");
193
    fflush(stdout);
194
    delete executable;
195

    
196
}
197

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

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

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

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

    
224

    
225
}
226

    
227

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

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

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

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

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

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

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

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

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

    
275
    delete value1;
276
    delete executable;
277

    
278
}
279

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

    
286
    cout << endl << "Test: testTransformToString4:" << endl;
287
    trans->clearParameters(true);
288

    
289
    XdmNode *input = processor->parseXmlFromString(
290
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
291

    
292
    if (input== nullptr) {
293
        sresult->failure++;
294
        cout << "Source document is null. ====== FAIL ====== " << endl;
295
        sresult->failureList.push_back("testTransformToString4");
296
    }
297

    
298

    
299
    XdmValue *values = new XdmValue();
300
    values->addXdmItem((XdmItem *) processor->makeIntegerValue(10));
301
    values->addXdmItem((XdmItem *) processor->makeIntegerValue(5));
302
    values->addXdmItem((XdmItem *) processor->makeIntegerValue(6));
303
    values->addXdmItem((XdmItem *) processor->makeIntegerValue(7));
304

    
305

    
306

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

309
    XsltExecutable *executable = trans->compileFromXdmNode(sheet);
310

311
    if(executable== nullptr) {
312
        sresult->failure++;
313
        sresult->failureList.push_back("testTransformToString4");
314
        cerr << "testTransformToString4 NULL found" << endl;
315
        if (trans->exceptionOccurred()) {
316
            cerr <<"testTransformToString4 error: "<< trans->getErrorMessage() << endl;
317
        }
318
        delete values;
319
        return;
320
    }
321

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

    
339
}
340

    
341
void testTransformFromstring(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
342
    cout << endl << "Test: testTransfromFromstring: param size="<< trans->getParameters().size() << endl;
343
    trans->clearParameters(true);
344
    if(processor == nullptr) {
345
        cout<<" processor is null"<<endl;
346
        return;
347

    
348
    }
349
    XdmNode *input = processor->parseXmlFromString(
350
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
351

    
352
    XsltExecutable * executable = trans->compileFromString(
353
            "<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>");
354

    
355
    if(executable== nullptr || input == nullptr) {
356
        sresult->failure++;
357
        sresult->failureList.push_back("testTransformFromstring");
358
        cerr << "testTransformFromstring NULL found" << endl;
359
        if (trans->exceptionOccurred()) {
360
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
361
        }
362
        return;
363
    }
364

    
365
    cout<<" checkpoint 00"<<endl;
366

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

    
381
    delete input;
382
    delete executable;
383

    
384

    
385
}
386

    
387
//Test case has error in the stylesheet
388
void testTransformFromstring2Err(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
389
    cout << endl << "Test: testTransfromFromstring2-Error:" << endl;
390
    trans->clearParameters(true);
391

    
392
    XdmNode *input = processor->parseXmlFromString(
393
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
394

    
395
    XsltExecutable * executable = trans->compileFromString(
396
            "<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>");
397

    
398
    if(executable== nullptr) {
399
        sresult->failure++;
400
        sresult->failureList.push_back("testTransformFromstring2Err");
401
        cerr << "testTransformFromstring2Err NULL found" << endl;
402
        if (trans->exceptionOccurred()) {
403
            cerr <<"testTransformFromstring2Err error: "<< trans->getErrorMessage() << endl;
404
        }
405
        return;
406
    }
407

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

    
423
    trans->exceptionClear();
424
    delete input;
425
    delete executable;
426

    
427
}
428

    
429
void testTrackingOfValueReference(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
430
    trans->clearParameters(true);
431

    
432
    cout << endl << "Test: TrackingOfValueReference:" << endl;
433
    ostringstream test;
434
    ostringstream valStr;
435
    ostringstream name;
436
    for (int i = 0; i < 10; i++) {
437
        test << "v" << i;
438
        valStr << "<out><person>text1</person><person>text2</person><person>text3</person><value>" << test.str()
439
               << "</value></out>";
440
        name << "value" << i;
441

    
442
        XdmValue *values = (XdmValue *) processor->parseXmlFromString(valStr.str().c_str());
443
        //cout<<"Name:"<<name.str()<<", Value:"<<values->getHead()->getStringValue()<<endl;
444
        trans->setParameter(name.str().c_str(), values);
445
        test.str("");
446
        valStr.str("");
447
        name.str("");
448
    }
449

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

    
476
/*Test case should be error.*/
477
void testTrackingOfValueReferenceError(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
478
    trans->clearParameters(true);
479

    
480
    cout << endl << "Test: TrackingOfValueReference-Error:" << endl;
481
    cout << "Parameter Map size: " << (trans->getParameters().size()) << endl;
482
    ostringstream test;
483
    ostringstream valStr;
484
    ostringstream name;
485
    for (int i = 0; i < 10; i++) {
486
        test << "v" << i;
487
        valStr << "<out><person>text1</person><person>text2</person><person>text3</person><value>" << test.str()
488
               << "<value></out>";
489
        name << "value" << i;
490

    
491
        XdmValue *values = (XdmValue *) processor->parseXmlFromString(valStr.str().c_str());
492
        trans->setParameter(name.str().c_str(), values);
493
        test.str("");
494
        valStr.str("");
495
        name.str("");
496
    }
497
    std::map<std::string, XdmValue *> parMap = trans->getParameters();
498
    cout << "Parameter Map size: " << parMap.size() << endl;
499

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

    
519
                return;
520
            }
521
        }
522
        name1.str("");
523
    }
524
    sresult->failure++;
525
    sresult->failureList.push_back("testTrackingOfValueReferenceError");
526

    
527
}
528

    
529
void testValidation(Xslt30Processor *trans, sResultCount *sresult) {
530
    trans->clearParameters(true);
531

    
532

    
533

    
534
    XsltExecutable * executable = trans->compileFromString(
535
            "<?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>");
536

    
537
    const char *rootValue = executable->callTemplateReturningString("main");
538

    
539

    
540
    if (rootValue== nullptr) {
541
        std::cout << "NULL found" << std::endl;
542
        sresult->failure++;
543
        sresult->failureList.push_back("testValidation");
544
        return;
545

    
546
    } else {
547
        std::cout << "Result=" << rootValue << endl;
548
        sresult->success++;
549
    }
550
    delete executable;
551
}
552

    
553

    
554
void testXdmNodeOutput(Xslt30Processor *trans, sResultCount *sresult) {
555
    trans->clearParameters(true);
556

    
557

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

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

    
594
}
595

    
596
void exampleSimple1(Xslt30Processor *proc, sResultCount *sresult) {
597
    cout << "ExampleSimple1 taken from PHP:" << endl;
598

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

    
622
}
623

    
624
void exampleSimple1Err(Xslt30Processor *proc, sResultCount *sresult) {
625
    cout << "ExampleSimple1Err taken from PHP:" << endl;
626

    
627
    XsltExecutable * executable = proc->compileFromFile("err.xsl");
628
    if(executable == nullptr || proc->exceptionOccurred()) {
629
        if( proc->exceptionOccurred()) {
630
            cout << "Error Message= "<< proc->getErrorMessage() << endl;
631
        }
632
        cout << "Result expected as null " << endl;
633
        sresult->success++;
634
        return;
635
    } else {
636
        sresult->failure++;
637
        sresult->failureList.push_back("exampleSimple1Err");
638

    
639
        delete executable;
640
    }
641
    proc->clearParameters(true);
642

    
643
}
644

    
645

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

    
650
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
651
    if(executable== nullptr) {
652
        sresult->failure++;
653
        sresult->failureList.push_back("exampleSimple2");
654
        cerr << "exampleSimple2 NULL found" << endl;
655
        if (proc->exceptionOccurred()) {
656
            cerr <<"exampleSimple2 error: "<< proc->getErrorMessage() << endl;
657
        }
658
        return;
659
    }
660

    
661
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
662
    const char *filename = "output1.xml";
663
    executable->setOutputFile(filename);
664
    executable->applyTemplatesReturningFile("output1.xml");
665

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

    
682
}
683

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

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

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

    
698
   }
699

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

    
711
    sresult->success++;
712
    delete executable;
713

    
714
}
715

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

    
720
    XsltExecutable * executable = proc->compileFromFile(nullptr);
721

    
722

    
723
    if (executable == nullptr) {
724

    
725

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

    
729
        sresult->success++;
730
        return;
731
    }
732

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

    
739

    
740
}
741

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

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

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

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

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

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

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

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

    
776

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

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

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

    
798

    
799
}
800

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

    
807

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

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

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

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

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

    
845
    executable->clearProperties();
846

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

    
851
    }
852

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

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

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

    
869
}
870

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

    
875
    proc->setJustInTimeCompilation(true);
876

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

    
891

    
892
}
893

    
894

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

    
899

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

    
913

    
914
}
915

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

    
920
    proc->setJustInTimeCompilation(true);
921

    
922
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
923
    if (result != NULL) {
924
        cout << "XdmNode returned" << endl;
925
        delete result;
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
    executable->clearParameters(true);
1056
    delete result;
1057
    delete executable;
1058

    
1059
}
1060

    
1061
void testResolveUri(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1062
    cout << endl << "Test: testResolveUri:" << endl;
1063

    
1064
    XsltExecutable * executable = trans->compileFromString(
1065
            "<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>");
1066

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

    
1077

    
1078
    if (value== nullptr) {
1079

    
1080
        sresult->failure++;
1081
        sresult->failureList.push_back("testResolveUri");
1082
    } else {
1083

    
1084
        const char *svalue = value->itemAt(0)->getStringValue();
1085
        cout << "testResolveUri = " << svalue << endl;
1086
        sresult->success++;
1087
    }
1088

    
1089

    
1090
}
1091

    
1092
void testContextNotRoot(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1093
    cout << endl << "Test: testContextNotRoot" << endl;
1094

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

    
1097
    XsltExecutable * executable = trans->compileFromString(
1098
            "<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>");
1099

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

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

    
1116
        if (result== nullptr) {
1117

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

    
1127
            cout << "testContextNotRoot = " << result << endl;
1128
            sresult->success++;
1129
        }
1130
    }
1131
}
1132

    
1133

    
1134
void testContextNotRootNamedTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1135
    cout << endl << "Test: testContextNotRootNamedTemplate" << endl;
1136

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

    
1139
    XsltExecutable * executable = trans->compileFromString(
1140
            "<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>");
1141

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

    
1151
    executable->setGlobalContextItem(node);
1152
    const char *result = executable->callTemplateReturningString("main");
1153

    
1154
    if (result== nullptr) {
1155

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

    
1165
        cout << "testContextNotRoot = " << result << endl;
1166
        sresult->success++;
1167
    }
1168

    
1169
}
1170

    
1171

    
1172
void testContextNotRootNamedTemplateValue(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1173
    cout << endl << "Test: testContextNotRootNamedTemplateValue" << endl;
1174

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

    
1177
    XsltExecutable * executable = trans->compileFromString(
1178
            "<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>");
1179

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

    
1189
    executable->setGlobalContextItem(node);
1190
    XdmValue *result = executable->callTemplateReturningValue("main");
1191

    
1192
    if (result== nullptr) {
1193

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

    
1203
        cout << "testContextNotRoot = " << result->getHead()->getStringValue() << endl;
1204
        sresult->success++;
1205
    }
1206

    
1207
}
1208

    
1209
void testCallSystemFunction(SaxonProcessor *proc, sResultCount *sresult) {
1210

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

    
1219
    XdmValue ** xdmValue = new XdmValue*[1];
1220
    xdmValue[0] = new XdmValue();
1221
    xdmValue[0]->addXdmItem(proc->makeStringValue("[1,2,3]"));
1222
    XdmValue *result = fi->call(xdmValue, 1);
1223

    
1224
    std::cerr << "Result = " << result->toString() << endl;
1225
    if(result->size() == 3) {
1226

    
1227
        cout << "testCallSystemFunction = " << result->getHead()->getStringValue() << endl;
1228
        sresult->success++;
1229

    
1230
    } else {
1231
        sresult->failure++;
1232
        sresult->failureList.push_back("testCallSystemFunction");
1233

    
1234
    }
1235

    
1236
}
1237

    
1238

    
1239
void testPipeline(SaxonProcessor *proc, sResultCount *sresult) {
1240
    cout << endl << "Test: testPipeline" << endl;
1241

    
1242
    Xslt30Processor * trans = proc->newXslt30Processor();
1243
    XsltExecutable * stage1 = trans->compileFromString(
1244
            "<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>");
1245

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

    
1255
    XdmNode *inn = proc->parseXmlFromString("<z/>");
1256

    
1257
    XsltExecutable *stage2 = trans->compileFromString(
1258
            "<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>");
1259

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

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

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

    
1291
    XsltExecutable *stage5 = trans->compileFromString(
1292
            "<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>");
1293

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

    
1303
    stage1->setProperty("!indent", "no");
1304
    stage1->setInitialMatchSelection(inn);
1305

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

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

    
1323
        cout << "d11 is NULL\n" << endl;
1324
    }
1325
    const char *data = d1->getHead()->toString();
1326

    
1327
    if (data != NULL) {
1328
        cout << "d1 result=" << data << endl;
1329
    } else {
1330
        sresult->failure++;
1331
        sresult->failureList.push_back("testPipeline-1");
1332

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

    
1387

    
1388
}
1389

    
1390

    
1391
void testCatalog(const char * cwd, SaxonProcessor * proc, sResultCount *sresult) {
1392

    
1393
    cout << endl << "Test: testCatalog" << endl;
1394
    bool trace = false;
1395
    proc->setcwd(cwd);
1396
    proc->setCatalog("../php/catalog-test/catalog.xml", trace);
1397

    
1398
    Xslt30Processor * trans = proc->newXslt30Processor();
1399

    
1400

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

    
1403
    if(executable == nullptr) {
1404
        if (trans->exceptionOccurred()) {
1405
            const char *message = trans->getErrorMessage();
1406
            cout << "exception=" << message << endl;
1407
        }
1408
        return;
1409
    }
1410

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

    
1415
    if(result != NULL) {
1416
        std::cerr << "testCatalog result= " << result << std::endl;
1417
    }
1418

    
1419
}
1420

    
1421

    
1422
/*
1423
 *
1424
 *void *RunThread(void *args) {
1425

1426
    struct arg_struct *argsi = (struct arg_struct *)args;
1427
    int threadid = argsi->id;
1428
    Xslt30Processor * trans = argsi->trans;
1429
    long tid;
1430
    tid = (long)threadid;
1431

1432
    trans->attachThread();
1433

1434
   trans->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
1435

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

1443
void testThreads (SaxonProcessor * processor) {
1444
    pthread_t threads[NUM_THREADS];
1445
    int rc;
1446
    int i;
1447

1448
    Xslt30Processor *  trans = processor->newXslt30Processor();
1449

1450
    trans->compileFromFile("../php/xsl/foo.xsl");
1451
    struct arg_struct args;
1452
    args.trans = trans;
1453

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

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

1468
 *
1469
 */
1470

    
1471

    
1472
int main(int argc, char* argv[]) {
1473

    
1474

    
1475
    const char * cwd = nullptr;
1476
    if(argc > 0) {
1477
        cwd = argv[1];
1478
    }
1479

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

    
1492
        std::cerr << "Processor is Schema Aware" << std::endl;
1493
    } else {
1494
        std::cerr << "Processor is not Schema Aware" << std::endl;
1495
    }
1496

    
1497
    sResultCount *sresult = new sResultCount();
1498
    Xslt30Processor *trans = processor->newXslt30Processor();
1499

    
1500
    if( trans == nullptr) {
1501
        cout<< "Error creating Xslt30Processor"<<endl;
1502
        if(processor->exceptionOccurred()) {
1503
            cout<< "Error message: " <<processor->getErrorMessage()<<endl;
1504

    
1505
        }
1506
        return -1;
1507
    }
1508
    //testValidation(trans,sresult);
1509

    
1510
    testInitialTemplate(processor, trans, sresult);
1511

    
1512
    exampleSimple1Err(trans, sresult);
1513

    
1514
    exampleSimple1(trans, sresult);
1515

    
1516
    exampleSimple_xmark(trans, sresult);
1517

    
1518
    exampleSimple2(trans, sresult);
1519

    
1520
    exampleSimple3(processor, trans, sresult);
1521

    
1522
    exampleSimple3a(processor, trans, sresult);
1523

    
1524
    testApplyTemplatesString1(trans, sresult);
1525

    
1526
    testApplyTemplatesString2(processor, trans, sresult);
1527

    
1528
    testApplyTemplates2a(processor, trans, sresult);
1529

    
1530
    testTransformToString4(processor, trans, sresult);
1531
    processor->release();
1532
    return 0;
1533

    
1534
    testCatalog(cwd, processor,  sresult);
1535

    
1536
    testTransformToString2b(processor, trans, sresult);
1537

    
1538
    testTransformToString3(processor, trans, sresult);
1539
        
1540
    testTransformFromstring(processor, trans, sresult);
1541

    
1542
    testTransformFromstring2Err(processor, trans, sresult);
1543

    
1544
    testTrackingOfValueReference(processor, trans, sresult);
1545

    
1546
    testTrackingOfValueReferenceError(processor, trans, sresult);
1547

    
1548
    testPackage1(trans, sresult);
1549

    
1550
    testPackage2(trans, sresult);
1551

    
1552
    testXdmNodeOutput(trans, sresult);
1553

    
1554
    exampleParam(processor, trans, sresult);
1555

    
1556
    xmarkTest1(trans, sresult);
1557

    
1558
    xmarkTest2(trans, sresult);
1559
    testCallFunction(processor, trans, sresult);
1560
    testResolveUri(processor, trans, sresult);
1561
    testContextNotRoot(processor, trans, sresult);
1562
    testContextNotRootNamedTemplate(processor, trans, sresult);
1563
    testContextNotRootNamedTemplateValue(processor, trans, sresult);
1564
    testPipeline(processor, sresult);
1565

    
1566
    //Available in PE and EE
1567
    //testTransformToStringExtensionFunc(processor, trans);
1568

    
1569
    delete trans;
1570
    delete processor;
1571
    //processor->release();
1572

    
1573

    
1574
    SaxonProcessor *processor2 = new SaxonProcessor(true);
1575

    
1576
    Xslt30Processor *trans2 = processor2->newXslt30Processor();
1577
    testApplyTemplatesString1(trans2, sresult);
1578
    delete trans2;
1579
    processor2->release();
1580

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

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

    
1591
    }
1592

    
1593

    
1594
    return 0;
1595
}
(27-27/28)