Project

Profile

Help

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

he / src / main / c / samples / cppTests / testXSLT30.cpp @ 85a9912e

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();
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();
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
        cerr<< "testApplyTemplates2a - cp0"<<endl;
188
        SaxonProcessor::sxn_environ->env->ExceptionDescribe();
189
        return;
190
    }
191

    
192

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

    
199
}
200

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

    
207
    cout << endl << "Test: TransformToString2b:" << endl;
208
    trans->clearParameters();
209

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

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

    
227

    
228
}
229

    
230

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

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

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

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

    
247
        return;
248
    }
249

    
250
    XdmAtomicValue *value1 = processor->makeIntegerValue(10);
251

    
252

    
253

    
254
    XsltExecutable * executable = trans->compileFromFile("test.xsl");
255

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

    
267

    
268

    
269
    trans->setParameter("numParam", (XdmValue *) value1);
270

    
271
    cerr << "testTransformToString3  test test " << endl;
272

    
273
    executable->setInitialMatchSelection((XdmNode *) inputi);
274
    const char *output = executable->applyTemplatesReturningString();
275
    if (output == nullptr) {
276
        printf("result is null ====== FAIL ====== \n");
277
        sresult->failureList.push_back("testTransformToString3");
278
    } else {
279
        printf("%s", output);
280
        printf("result is OK \n");
281
        delete output;
282
    }
283
    fflush(stdout);
284

    
285
    cout<<"value 1 object ref="<<(value1)<<endl;
286
    delete value1;
287
    delete executable;
288

    
289
}
290

    
291
/*
292
* Test transform to String. stylesheet supplied as argument. Source supplied as xml string
293
and integer parmater created and supplied
294
*/
295
void testTransformToString4(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
296

    
297
    cout << endl << "Test: testTransformToString4:" << endl;
298
    cout << endl << "cpp0-1" << endl;
299
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
300

    
301
    trans->clearParameters();
302
    cout << endl << "cpp0-2" << endl;
303
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
304

    
305
    XdmNode *input = processor->parseXmlFromString(
306
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
307

    
308
    if (input== nullptr) {
309
        sresult->failure++;
310
        cout << "Source document is null. ====== FAIL ====== " << endl;
311
        sresult->failureList.push_back("testTransformToString4");
312
    }
313

    
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
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
322

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

325
    XsltExecutable *executable = trans->compileFromXdmNode(sheet);
326

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

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

    
355
}
356

    
357
void testTransformFromstring(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
358
    cout << endl << "Test: testTransfromFromstring: param size="<< trans->getParameters().size() << endl;
359
    trans->clearParameters();
360
    if(processor == nullptr) {
361
        cout<<" processor is null"<<endl;
362
        return;
363

    
364
    }
365
    XdmNode *input = processor->parseXmlFromString(
366
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
367

    
368
    if(input == 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
    XsltExecutable * executable = trans->compileFromString(
379
            "<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>");
380

    
381
    if(executable== nullptr) {
382
        sresult->failure++;
383
        sresult->failureList.push_back("testTransformFromstring");
384
        cerr << "testTransformFromstring NULL found" << endl;
385
        if (trans->exceptionOccurred()) {
386
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
387
        }
388
        delete input;
389
        return;
390
    }
391

    
392
    cout<<" checkpoint 00"<<endl;
393

    
394
    const char *output = executable->transformToString((XdmNode *) input);
395
    if (output== nullptr) {
396
        printf("result is null ====== FAIL ====== \n");
397
        sresult->failureList.push_back("testTransformFromString");
398
        if (trans->exceptionOccurred()) {
399
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
400
        }
401
    } else {
402
        printf("%s", output);
403
        printf("result is OK \n");
404
        delete output;
405
    }
406
    fflush(stdout);
407

    
408
    delete input;
409
    delete executable;
410

    
411

    
412
}
413

    
414
//Test case has error in the stylesheet
415
void testTransformFromstring2Err(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
416
    cout << endl << "Test: testTransfromFromstring2-Error:" << endl;
417
    trans->clearParameters();
418

    
419
    XdmNode *input = processor->parseXmlFromString(
420
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
421

    
422
    XsltExecutable * executable = trans->compileFromString(
423
            "<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>");
424

    
425
    if(executable== nullptr) {
426
        sresult->failure++;
427
        sresult->failureList.push_back("testTransformFromstring2Err");
428
        cerr << "testTransformFromstring2Err NULL found" << endl;
429
        if (trans->exceptionOccurred()) {
430
            cerr <<"testTransformFromstring2Err error: "<< trans->getErrorMessage() << endl;
431
        }
432
        return;
433
    }
434

    
435
    executable->setInitialMatchSelection((XdmValue *) input);
436
    const char *output = executable->applyTemplatesReturningString();
437
    if (output== nullptr) {
438
        printf("result is null ====== FAIL ======  \n\nCheck For errors:\n");
439
        sresult->failureList.push_back("testTransformFromstring2Err");
440
        if (executable->exceptionOccurred()) {
441
            cout << "Error message=" << executable->getErrorMessage() << endl;
442
        }
443
    } else {
444
        printf("%s", output);
445
        printf("result is OK \n");
446
    }
447
    fflush(stdout);
448
    delete output;
449

    
450
    trans->exceptionClear();
451
    delete input;
452
    delete executable;
453

    
454
}
455

    
456
void testTrackingOfValueReference(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
457
    trans->clearParameters();
458

    
459
    cout << endl << "Test: TrackingOfValueReference:" << endl;
460
    ostringstream test;
461
    ostringstream valStr;
462
    ostringstream name;
463
    for (int i = 0; i < 10; i++) {
464
        test << "v" << i;
465
        valStr << "<out><person>text1</person><person>text2</person><person>text3</person><value>" << test.str()
466
               << "</value></out>";
467
        name << "value" << i;
468

    
469
        XdmValue *values = (XdmValue *) processor->parseXmlFromString(valStr.str().c_str());
470
        //cout<<"Name:"<<name.str()<<", Value:"<<values->getHead()->getStringValue()<<endl;
471
        trans->setParameter(name.str().c_str(), values);
472
        test.str("");
473
        valStr.str("");
474
        name.str("");
475
    }
476

    
477
    std::map<std::string, XdmValue *> parMap = trans->getParameters();
478
    if (parMap.size() > 0) {
479
        //cout << "Parameter size: " << parMap.size() << endl;
480
        //cout << "Parameter size: " << parMap.size()<< endl;//", Value:"<<trans->getParameters()["value0"]->getHead()->getStringValue()<<endl;
481
        ostringstream name1;
482
        for (int i = 0; i < 10; i++) {
483
            name1 << "param:value" << i;
484
            cout << " i:" << i << " Map size:" << parMap.size() << ", ";
485
            XdmValue *valuei = parMap[name1.str()];
486
            if (valuei != NULL) {
487
                cout << name1.str();
488
                if (valuei->itemAt(0) != NULL)
489
                    cout << "= " << valuei->itemAt(0)->getStringValue();
490
                cout << endl;
491
            } else {
492
                sresult->failure++;
493
                std::cerr << "trackingValueReference ====== FAIL ======" << std::endl;
494
                sresult->failureList.push_back("testTrackingOfValueReference");
495
                return;
496
            }
497
            name1.str("");
498
        }
499
    }
500
    sresult->success++;
501
}
502

    
503
/*Test case should be error.*/
504
void testTrackingOfValueReferenceError(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
505
    trans->clearParameters();
506

    
507
    cout << endl << "Test: TrackingOfValueReference-Error:" << endl;
508
    cout << "Parameter Map size: " << (trans->getParameters().size()) << endl;
509
    ostringstream test;
510
    ostringstream valStr;
511
    ostringstream name;
512
    for (int i = 0; i < 10; i++) {
513
        test << "v" << i;
514
        valStr << "<out><person>text1</person><person>text2</person><person>text3</person><value>" << test.str()
515
               << "<value></out>";
516
        name << "value" << i;
517

    
518
        XdmValue *values = (XdmValue *) processor->parseXmlFromString(valStr.str().c_str());
519
        trans->setParameter(name.str().c_str(), values);
520
        test.str("");
521
        valStr.str("");
522
        name.str("");
523
    }
524
    std::map<std::string, XdmValue *> parMap = trans->getParameters();
525
    cout << "Parameter Map size: " << parMap.size() << endl;
526

    
527
    ostringstream name1;
528
    bool errorFound = false;
529
    for (int i = 0; i < 10; i++) {
530
        name1 << "param:value" << i;
531
        cout << " i:" << i << " Map size:" << parMap.size() << ", ";
532
        try {
533
            XdmValue *valuei = parMap.at(name1.str());
534
            if (valuei != NULL) {
535
                cout << name1.str();
536
                if (valuei->itemAt(0) != NULL)
537
                    cout << "= " << valuei->itemAt(0)->getStringValue();
538
                cout << endl;
539
            }
540
        } catch (const std::out_of_range &oor) {
541
            cout << "Out of range exception occurred. Exception no. " << endl;
542
            if (!errorFound) {
543
                sresult->success++;
544
                errorFound = true;
545

    
546
                return;
547
            }
548
        }
549
        name1.str("");
550
    }
551
    sresult->failure++;
552
    sresult->failureList.push_back("testTrackingOfValueReferenceError");
553

    
554
}
555

    
556
void testValidation(Xslt30Processor *trans, sResultCount *sresult) {
557
    trans->clearParameters();
558

    
559

    
560

    
561
    XsltExecutable * executable = trans->compileFromString(
562
            "<?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>");
563

    
564
    const char *rootValue = executable->callTemplateReturningString("main");
565

    
566

    
567
    if (rootValue== nullptr) {
568
        std::cout << "NULL found" << std::endl;
569
        sresult->failure++;
570
        sresult->failureList.push_back("testValidation");
571
        return;
572

    
573
    } else {
574
        std::cout << "Result=" << rootValue << endl;
575
        sresult->success++;
576
    }
577
    delete executable;
578
}
579

    
580

    
581
void testXdmNodeOutput(Xslt30Processor *trans, sResultCount *sresult) {
582
    trans->clearParameters();
583

    
584

    
585
    std::cout << "testXdmNodeOutput" << std::endl;
586
    XsltExecutable * executable = trans->compileFromString(
587
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template name='go'><a/></xsl:template></xsl:stylesheet>");
588
    XdmValue *rootValue = executable->callTemplateReturningValue("go");
589
    if (rootValue== nullptr) {
590
        cout << "Result is null ====== FAIL ====== " << endl;
591
        sresult->failure++;
592
        sresult->failureList.push_back("testXdmNodeOutput-0.0");
593
        return;
594
    }
595
    XdmItem *rootItem = rootValue->getHead();
596
    if (rootItem== nullptr) {
597

    
598
        cout << "Result is null ====== FAIL ====== " << endl;
599
        sresult->failure++;
600
        sresult->failureList.push_back("testXdmNodeOutput-0");
601
        return;
602
    }
603
    XdmNode *root = (XdmNode *) rootItem;
604
    if (root->getNodeKind() == DOCUMENT) {
605
        cout << "Result is a Document" << endl;
606
    } else {
607
        cout << "Node is of kind:" << root->getNodeKind() << endl;
608
    }
609
    const char *result = executable->callTemplateReturningString("go");
610
    if (string(result).find(string("<a/>")) != std::string::npos) {
611
        sresult->success++;
612
    } else {
613
        //TODO - this test case prints the XML declaration. Check if this correct
614
        sresult->failure++;
615
        cout << "testXdmNodeOutputAndString ======= FAIL========" << endl;
616
        sresult->failureList.push_back("testXdmNodeOutput");
617
    }
618
    delete executable;
619
    delete result;
620

    
621
}
622

    
623
void exampleSimple1(Xslt30Processor *proc, sResultCount *sresult) {
624
    cout << "ExampleSimple1 taken from PHP:" << endl;
625

    
626
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
627
    if(executable== nullptr) {
628
        sresult->failure++;
629
        sresult->failureList.push_back("exampleSimple1");
630
        cerr << "exampleSimple1 NULL found" << endl;
631
        if (proc->exceptionOccurred()) {
632
            cerr <<"exampleSimple1 error: "<< proc->getErrorMessage() << endl;
633
        }
634
        return;
635
    }
636
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
637
    const char *result = executable->applyTemplatesReturningString();
638
    if (result != NULL) {
639
        cout << result << endl;
640
        sresult->success++;
641
        delete result;
642
    } else {
643
        cout << "Result is null ====== FAIL ====== " << endl;
644
        sresult->failure++;
645
    }
646
    proc->clearParameters();
647
    delete executable;
648

    
649
}
650

    
651
void exampleSimple1Err(Xslt30Processor *proc, sResultCount *sresult) {
652
    cout << "ExampleSimple1Err taken from PHP:" << endl;
653

    
654
    XsltExecutable * executable = proc->compileFromFile("err.xsl");
655
    if(executable == nullptr || proc->exceptionOccurred()) {
656
        if( proc->exceptionOccurred()) {
657
            cout << "Error Message= "<< proc->getErrorMessage() << endl;
658
        }
659
        cout << "Result expected as null " << endl;
660
        sresult->success++;
661
        return;
662
    } else {
663
        sresult->failure++;
664
        sresult->failureList.push_back("exampleSimple1Err");
665

    
666
        delete executable;
667
    }
668
    proc->clearParameters();
669

    
670
}
671

    
672

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

    
677
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
678
    if(executable== nullptr) {
679
        sresult->failure++;
680
        sresult->failureList.push_back("exampleSimple2");
681
        cerr << "exampleSimple2 NULL found" << endl;
682
        if (proc->exceptionOccurred()) {
683
            cerr <<"exampleSimple2 error: "<< proc->getErrorMessage() << endl;
684
        }
685
        return;
686
    }
687

    
688
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
689
    const char *filename = "output1.xml";
690
    executable->setOutputFile(filename);
691
    executable->applyTemplatesReturningFile("output1.xml");
692

    
693
    if (CppTestUtils::exists("output1.xml")) {
694
        cout << "The file $filename exists" << endl;
695
        remove("output1.xml");
696
        sresult->success++;
697
    } else {
698
        cout << "The file " << filename << " does not exist" << endl;
699
        if (executable->exceptionOccurred()) {
700
            cout << proc->getErrorMessage() << endl;
701
        }
702
        sresult->failure++;
703
        sresult->failureList.push_back("exampleSimple2");
704
    }
705
    proc->clearParameters();
706
    executable->clearParameters();
707
    delete executable;
708

    
709
}
710

    
711
void exampleSimple3(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
712
    cout << "<b>exampleSimple3:</b><br/>" << endl;
713
    proc->clearParameters();
714

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

    
717
   if(executable == nullptr) {
718
        cout << "executable is NULL" <<endl;
719
        if(proc->exceptionOccurred()) {
720
                cout << proc->getErrorMessage() <<endl;
721
                
722
        }
723

    
724
        return;
725

    
726
   }
727

    
728
    XdmNode *xdmNode = saxonProc->parseXmlFromString("<doc><b>text value of out</b></doc>");
729
    if (xdmNode== nullptr) {
730
        cout << "Error: xdmNode is null'" << endl;
731
        sresult->failure++;
732
        sresult->failureList.push_back("exampleSimple3");
733
        return;
734
    }
735
    executable->setInitialMatchSelection((XdmNode *) xdmNode);
736
    cout << "end of exampleSimple3" << endl;
737
    executable->clearParameters();
738

    
739
    sresult->success++;
740
    delete executable;
741

    
742
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
743

    
744
}
745

    
746
void exampleSimple3a(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
747
    cout << "<b>exampleSimple3a:</b><br/>" << endl;
748
    proc->clearParameters();
749

    
750
    XsltExecutable * executable = proc->compileFromFile(nullptr);
751

    
752

    
753
    if (executable == nullptr) {
754

    
755

    
756
        cout << "end of exampleSimple3a" << endl;
757
        proc->clearParameters();
758

    
759
        sresult->success++;
760
        return;
761
    }
762

    
763
    cout << "Error: executable is not nullptr'" << endl;
764
    sresult->failure++;
765
    sresult->failureList.push_back("exampleSimple3a");
766
    delete executable;
767
    return;
768

    
769

    
770
}
771

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

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

    
777
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
778

    
779
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
780
    if (xdmvalue != NULL) {
781

    
782
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
783

    
784
    } else {
785
        cout << "Xdmvalue is null - ====== FAIL =====" << endl;
786
        sresult->failure++;
787
        sresult->failureList.push_back("exampleParam-1");
788
    }
789
    const char *result = executable->applyTemplatesReturningString();
790
    if (result != NULL) {
791
        cout << "Output:" << result << endl;
792
        sresult->success++;
793
    } else {
794
        cout << "Result is NULL<br/>  ======= fail =====" << endl;
795
        sresult->failure++;
796
        sresult->failureList.push_back("exampleParam-2");
797
    }
798

    
799
    //proc->clearParameters();
800
    //unset($result);
801
    //echo 'again with a no parameter value<br/>';
802

    
803
    executable->setProperty("!indent", "yes");
804
    const char *result2 = executable->applyTemplatesReturningString();
805

    
806

    
807
    if (result2 != NULL) {
808
        cout << result2 << endl;
809
        sresult->success++;
810
    }
811

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

    
817
    const char *result3 = executable->applyTemplatesReturningString();
818
    if (result3 != NULL) {
819
        cout << "Output =" << result3 << endl;
820
        sresult->success++;
821
    } else {
822
        cout << "Error in result ===== FAIL =======" << endl;
823
        sresult->failure++;
824
        sresult->failureList.push_back("exampleParam");
825
    }
826
    proc->clearParameters();
827

    
828

    
829
}
830

    
831
// test parameter and properties maps where we update key, value pair.
832
void exampleParam2(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
833
    cout << "\nExampleParam:</b><br/>" << endl;
834
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
835
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
836

    
837

    
838
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
839
    XdmAtomicValue *xdmvalue2i = saxonProc->makeStringValue("Hello from me");
840
    if (xdmvalue != NULL) {
841

    
842
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
843
        executable->setParameter("a-param", (XdmValue *) xdmvalue2i);
844

    
845
    } else {
846
        cout << "Xdmvalue is null - ====== FAIL =====" << endl;
847
        sresult->failure++;
848
        sresult->failureList.push_back("exampleParam-1");
849
    }
850
    const char *result = executable->applyTemplatesReturningString();
851
    if (result != NULL) {
852
        string sresulti = string(result);
853
        if (sresulti.compare("Hello from me") == 0) {
854
            cout << "Output:" << result << endl;
855
            sresult->success++;
856
        } else {
857
            cout << "Result is " << result << " <br/> ======= fail ===== " << endl;
858
            sresult->failure++;
859
            sresult->failureList.push_back("exampleParam-2");
860
        }
861
    } else {
862
        cout<<"Result is NULL<br/>  ======= fail ====="<<endl;
863
        sresult->failure++;
864
        sresult->failureList.push_back("exampleParam-2");
865
    }
866

    
867
//proc->clearParameters();
868
//unset($result);
869
//echo 'again with a no parameter value<br/>';
870

    
871
    executable->setProperty("!indent", "no");
872
    executable->setProperty("!indent", "yes");
873
    const char *result2 = executable->applyTemplatesReturningString();
874

    
875
    executable->clearProperties();
876

    
877
    if(result2 != NULL) {
878
        cout<<result2<<endl;
879
        sresult->success++;
880

    
881
    }
882

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

    
888
    const char *result3 = executable->applyTemplatesReturningString();
889
    if(result3 != NULL) {
890
        cout<<"Output ="<<result3<<endl;
891

    
892
        sresult->success++;
893
    } else {
894
        cout<<"Error in result ===== FAIL ======="<<endl;
895
        sresult->failure++;
896
        sresult->failureList.push_back("exampleParam");
897
    }
898

    
899
}
900

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

    
905
    proc->setJustInTimeCompilation(true);
906

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

    
921

    
922
}
923

    
924

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

    
929

    
930
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
931
    if (result != NULL) {
932
        cout << "XdmNode returned" << endl;
933
    } else {
934
        printf("result is null \nCheck For errors:");
935
        sresult->failure++;
936
        sresult->failureList.push_back("xmarkTest2");
937
        if (proc->exceptionOccurred() > 0) {
938
            cout << proc->getErrorMessage() << endl;
939
        }
940
    }
941
    proc->clearParameters();
942

    
943

    
944
}
945

    
946
/* XMarkbench mark test q12.xsl with just-in-time=true*/
947
void exampleSimple_xmark(Xslt30Processor *proc, sResultCount *sresult) {
948
    cout << "XMarkbench mark test q12.xsl:" << endl;
949

    
950
    proc->setJustInTimeCompilation(true);
951

    
952
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
953
    if (result != NULL) {
954
        cout << "XdmNode returned" << endl;
955
        delete result;
956
    } else {
957
        printf("result is null \nCheck For errors:");
958
        if (proc->exceptionOccurred() > 0) {
959
            cout << proc->getErrorMessage() << endl;
960
        }
961
    }
962
    proc->clearParameters();
963

    
964
}
965

    
966

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

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

    
975

    
976
    trans->compileFromFileAndSave("test.xsl", "test1.sef");
977
    const char *output = trans->transformFileToString("cat.xml", "test1.sef");
978
    if (output== nullptr) {
979
        printf("result is null \n");
980
        const char *message = trans->getErrorMessage();
981
        if (message != NULL) {
982
            cout << "Error message =" << message << endl;
983
        }
984
        sresult->failure++;
985
        sresult->failureList.push_back("testPackage1");
986

    
987
    } else {
988
        printf("%s", output);
989
        printf("result is OK \n");
990
        sresult->success++;
991
    }
992
    fflush(stdout);
993
    delete output;
994
}
995

    
996

    
997
/*
998
* Test saving and loading a Xslt package
999
*/
1000
void testPackage2(Xslt30Processor *trans, sResultCount *sresult) {
1001

    
1002
    cout << endl << "Test: Saving and loading Packages2 - Error:" << endl;
1003
    trans->clearParameters();
1004

    
1005

    
1006
    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>";
1007

    
1008
    trans->compileFromStringAndSave(stylesheet, "test2.sef");
1009
    const char *output = trans->transformFileToString("cat.xml", "test2.sef");
1010
    if (output== nullptr) {
1011
        printf("result is null as expected \n");
1012
        const char *message = trans->getErrorMessage();
1013
        if (message != NULL) {
1014
            cout << "Error message =" << message << endl;
1015
        }
1016
        sresult->success++;
1017
    } else {
1018
        printf("%s", output);
1019
        printf("result is OK \n");
1020
        sresult->failure++;
1021
        sresult->failureList.push_back("testPackage2");
1022
    }
1023
    fflush(stdout);
1024
    delete output;
1025
}
1026

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

    
1029
    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>";
1030
    cout << endl << "Test: testCallFunction:" << endl;
1031
    XsltExecutable * executable = trans->compileFromString(source);
1032
    XdmValue **valueArray = new XdmValue *[2];
1033

    
1034
    valueArray[0] = (XdmValue *) (proc->makeIntegerValue(2));
1035
    valueArray[1] = (XdmValue *) (proc->makeIntegerValue(3));
1036
    XdmValue *v = executable->callFunctionReturningValue("{http://localhost/}add", valueArray, 2);
1037

    
1038
    if (v != NULL && (v->getHead())->isAtomic() && ((XdmAtomicValue *) (v->getHead()))->getLongValue() == 5) {
1039
        sresult->success++;
1040
    } else {
1041
        if (v != NULL && !(v->getHead())->isAtomic()) {
1042
            cout << "Value in callFunction is not atomic - but expected as atomic value" << endl;
1043
        }
1044
        cout << "testCallFunction ======= FAIL ======" << endl;
1045
        const char *message = executable->getErrorMessage();
1046
        if (message != NULL) {
1047
            cout << "Error message =" << message << endl;
1048
        }
1049
        sresult->failure++;
1050
        sresult->failureList.push_back("testCallFunction");
1051
    }
1052
    delete valueArray[0];
1053
    delete valueArray[1];
1054
    delete [] valueArray;
1055
}
1056

    
1057
void testInitialTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1058

    
1059

    
1060
    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>";
1061
    cout << endl << "Test:testInitialTemplate" << endl;
1062
    XsltExecutable * executable = trans->compileFromString(source);
1063
    if(executable == nullptr) {
1064
        if(trans->exceptionOccurred()) {
1065
            cout << "Error: "<< trans->getErrorMessage() << endl;
1066
        }
1067
        return;
1068
    }
1069
    XdmNode *node = proc->parseXmlFromString("<e/>");
1070

    
1071
    executable->setResultAsRawValue(false);
1072
    std::map<std::string, XdmValue *> parameterValues;
1073
    parameterValues["a"] = proc->makeIntegerValue(12);
1074
    parameterValues["b"] = proc->makeIntegerValue(5);
1075
    executable->setInitialTemplateParameters(parameterValues, false);
1076
    executable->setInitialMatchSelection(node);
1077
    XdmValue *result = executable->applyTemplatesReturningValue();
1078
    if (result != NULL) {
1079
        sresult->success++;
1080
        cout << "Result=" << result->getHead()->getStringValue() << endl;
1081
    } else {
1082
        sresult->failure++;
1083
    }
1084

    
1085
    executable->clearParameters();
1086
    delete result;
1087
    delete executable;
1088

    
1089
}
1090

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

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

    
1097
    if(executable == nullptr) {
1098
        sresult->failure++;
1099
        sresult->failureList.push_back("testResolveUri");
1100
        if(trans->exceptionOccurred()) {
1101
            cout << "Error: "<< trans->getErrorMessage() << endl;
1102
        }
1103
        return;
1104
    }
1105
    XdmValue *value = executable->callTemplateReturningValue("go");
1106

    
1107

    
1108
    if (value== nullptr) {
1109

    
1110
        sresult->failure++;
1111
        sresult->failureList.push_back("testResolveUri");
1112
    } else {
1113

    
1114
        const char *svalue = value->itemAt(0)->getStringValue();
1115
        cout << "testResolveUri = " << svalue << endl;
1116
        sresult->success++;
1117
    }
1118

    
1119

    
1120
}
1121

    
1122
void testContextNotRoot(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1123
    cout << endl << "Test: testContextNotRoot" << endl;
1124

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

    
1127
    XsltExecutable * executable = trans->compileFromString(
1128
            "<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>");
1129

    
1130
    if(executable == nullptr) {
1131
        sresult->failure++;
1132
        sresult->failureList.push_back("testContextNotRoot");
1133
        if(trans->exceptionOccurred()) {
1134
            cout << "Error: "<< trans->getErrorMessage() << endl;
1135
        }
1136
        return;
1137
    }
1138

    
1139
    executable->setGlobalContextItem(node);
1140
    if (node->getChildCount() > 0) {
1141
        XdmNode *eNode = node->getChildren()[0]->getChildren()[0];
1142
        cout << "Node content = " << eNode->toString() << endl;
1143
        executable->setInitialMatchSelection(eNode);
1144
        const char *result = executable->applyTemplatesReturningString();
1145

    
1146
        if (result== nullptr) {
1147

    
1148
            cout << "testCallFunction ======= FAIL ======" << endl;
1149
            const char *message = executable->getErrorMessage();
1150
            if (message != NULL) {
1151
                cout << "Error message =" << message << endl;
1152
            }
1153
            sresult->failure++;
1154
            sresult->failureList.push_back("testContextNotRoot");
1155
        } else {
1156

    
1157
            cout << "testContextNotRoot = " << result << endl;
1158
            sresult->success++;
1159
        }
1160
    }
1161
}
1162

    
1163

    
1164
void testContextNotRootNamedTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1165
    cout << endl << "Test: testContextNotRootNamedTemplate" << endl;
1166

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

    
1169
    XsltExecutable * executable = trans->compileFromString(
1170
            "<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>");
1171

    
1172
    if(executable == nullptr) {
1173
        sresult->failure++;
1174
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1175
        if(trans->exceptionOccurred()) {
1176
            cout << "Error: "<< trans->getErrorMessage() << endl;
1177
        }
1178
        return;
1179
    }
1180

    
1181
    executable->setGlobalContextItem(node);
1182
    const char *result = executable->callTemplateReturningString("main");
1183

    
1184
    if (result== nullptr) {
1185

    
1186
        cout << "testCallFunction ======= FAIL ======" << endl;
1187
        const char *message = executable->getErrorMessage();
1188
        if (message != NULL) {
1189
            cout << "Error message =" << message << endl;
1190
        }
1191
        sresult->failure++;
1192
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1193
    } else {
1194

    
1195
        cout << "testContextNotRoot = " << result << endl;
1196
        sresult->success++;
1197
    }
1198

    
1199
}
1200

    
1201

    
1202
void testContextNotRootNamedTemplateValue(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1203
    cout << endl << "Test: testContextNotRootNamedTemplateValue" << endl;
1204

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

    
1207
    XsltExecutable * executable = trans->compileFromString(
1208
            "<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>");
1209

    
1210
    if(executable == nullptr) {
1211
        sresult->failure++;
1212
        sresult->failureList.push_back("testContentNotRootNamedTemplateValue");
1213
        if(trans->exceptionOccurred()) {
1214
            cout << "Error: "<< trans->getErrorMessage() << endl;
1215
        }
1216
        return;
1217
    }
1218

    
1219
    executable->setGlobalContextItem(node);
1220
    XdmValue *result = executable->callTemplateReturningValue("main");
1221

    
1222
    if (result== nullptr) {
1223

    
1224
        cout << "testCallFunction ======= FAIL ======" << endl;
1225
        const char *message = executable->getErrorMessage();
1226
        if (message != NULL) {
1227
            cout << "Error message =" << message << endl;
1228
        }
1229
        sresult->failure++;
1230
        sresult->failureList.push_back("testContextNotRootNamedTemplateValue");
1231
    } else {
1232

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

    
1237
}
1238

    
1239
void testCallSystemFunction(SaxonProcessor *proc, sResultCount *sresult) {
1240

    
1241
    XdmFunctionItem *fi = XdmFunctionItem::getSystemFunction(proc, "{http://www.w3.org/2005/xpath-functions}parse-json",
1242
                                                            1);
1243
    if (fi== nullptr) {
1244
        sresult->failure++;
1245
        sresult->failureList.push_back("testCallSystemFunction");
1246
        return;
1247
    }
1248

    
1249
    XdmValue ** xdmValue = new XdmValue*[1];
1250
    xdmValue[0] = new XdmValue();
1251
    xdmValue[0]->addXdmItem(proc->makeStringValue("[1,2,3]"));
1252
    XdmValue *result = fi->call(xdmValue, 1);
1253

    
1254
    std::cerr << "Result = " << result->toString() << endl;
1255
    if(result->size() == 3) {
1256

    
1257
        cout << "testCallSystemFunction = " << result->getHead()->getStringValue() << endl;
1258
        sresult->success++;
1259

    
1260
    } else {
1261
        sresult->failure++;
1262
        sresult->failureList.push_back("testCallSystemFunction");
1263

    
1264
    }
1265

    
1266
}
1267

    
1268

    
1269
void testPipeline(SaxonProcessor *proc, sResultCount *sresult) {
1270
    cout << endl << "Test: testPipeline" << endl;
1271

    
1272
    Xslt30Processor * trans = proc->newXslt30Processor();
1273
    XsltExecutable * stage1 = trans->compileFromString(
1274
            "<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>");
1275

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

    
1285
    XdmNode *inn = proc->parseXmlFromString("<z/>");
1286

    
1287
    XsltExecutable *stage2 = 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(stage2 == nullptr) {
1291
        sresult->failure++;
1292
        sresult->failureList.push_back("testPipeline");
1293
        if(trans->exceptionOccurred()) {
1294
            cout << "stage 2 Error: "<< trans->getErrorMessage() << endl;
1295
        }
1296
        return;
1297
    }
1298
    XsltExecutable *stage3 = trans->compileFromString(
1299
            "<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>");
1300

    
1301
    if(stage3 == nullptr) {
1302
        sresult->failure++;
1303
        sresult->failureList.push_back("testPipeline");
1304
        if(trans->exceptionOccurred()) {
1305
            cout << "stage 3 Error: "<< trans->getErrorMessage() << endl;
1306
        }
1307
        return;
1308
    }
1309
    XsltExecutable *stage4 = trans->compileFromString(
1310
            "<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>");
1311

    
1312
    if(stage3 == nullptr) {
1313
        sresult->failure++;
1314
        sresult->failureList.push_back("testPipeline");
1315
        if(trans->exceptionOccurred()) {
1316
            cout << "stage 4 Error: "<< trans->getErrorMessage() << endl;
1317
        }
1318
        return;
1319
    }
1320

    
1321
    XsltExecutable *stage5 = trans->compileFromString(
1322
            "<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>");
1323

    
1324
    if(stage5 == nullptr) {
1325
        sresult->failure++;
1326
        sresult->failureList.push_back("testPipeline");
1327
        if(trans->exceptionOccurred()) {
1328
            cout << "stage 5 Error: "<< trans->getErrorMessage() << endl;
1329
        }
1330
        return;
1331
    }
1332

    
1333
    stage1->setProperty("!indent", "no");
1334
    stage1->setInitialMatchSelection(inn);
1335

    
1336
    XdmValue *d1 = stage1->applyTemplatesReturningValue();
1337
    if (stage1->exceptionOccurred()) {
1338
        sresult->failure++;
1339
        sresult->failureList.push_back("testPipeline");
1340
        cout << "Exception message=" << (stage1->getErrorMessage()) << endl;
1341
        return;
1342
    }
1343

    
1344
    if (d1 == nullptr) {
1345
        cout << "ERROR1 :"<< stage1->getErrorMessage() << endl;;
1346
        sresult->failure++;
1347
        sresult->failureList.push_back("testPipeline-0");
1348
        return;
1349
    }
1350
    XdmItem *d11 = d1->getHead();
1351
    if (d11== nullptr) {
1352

    
1353
        cout << "d11 is NULL\n" << endl;
1354
    }
1355
    const char *data = d1->getHead()->toString();
1356

    
1357
    if (data != NULL) {
1358
        cout << "d1 result=" << data << endl;
1359
    } else {
1360
        sresult->failure++;
1361
        sresult->failureList.push_back("testPipeline-1");
1362

    
1363
        return;
1364
    }
1365
    stage2->setProperty("!indent", "no");
1366
    stage2->setInitialMatchSelection(d1->getHead());
1367
    XdmValue *d2 = stage2->applyTemplatesReturningValue();
1368
    if (d2== nullptr) {
1369
        cout << "ERROR-11\n" << endl;
1370
        if (stage2->exceptionOccurred()) {
1371
            const char *message = stage2->getErrorMessage();
1372
            cout << "exception=" << message << endl;
1373
        }
1374
        sresult->failure++;
1375
        sresult->failureList.push_back("testPipeline-2");
1376
        return;
1377
    }
1378
    stage3->setProperty("!indent", "no");
1379
    stage3->setInitialMatchSelection(d2);
1380
    XdmValue * d3 = stage3->applyTemplatesReturningValue();
1381
    if(d3 == nullptr){
1382
        sresult->failure++;
1383
        sresult->failureList.push_back("testPipeline-3");
1384
        if (stage3->exceptionOccurred()) {
1385
            const char *message = stage3->getErrorMessage();
1386
            cout << "exception=" << message << endl;
1387
        }
1388
      }
1389
    stage4->setProperty("!indent", "no");
1390
    stage4->setInitialMatchSelection(d3);
1391
    XdmValue * d4 = stage4->applyTemplatesReturningValue();
1392
    if(d3== nullptr){
1393
        sresult->failure++;
1394
        sresult->failureList.push_back("testPipeline-1");
1395
        if (stage4->exceptionOccurred()) {
1396
            const char *message = stage4->getErrorMessage();
1397
            cout << "exception=" << message << endl;
1398
        }
1399
        return;
1400
      }
1401
    stage5->setProperty("!indent", "no");
1402
    stage5->setInitialMatchSelection(d4);
1403
    const char * sw = stage5->applyTemplatesReturningString();
1404
    if(sw==NULL){
1405
        sresult->failure++;
1406
        sresult->failureList.push_back("testPipeline-1");
1407
        if (stage5->exceptionOccurred()) {
1408
            const char *message = stage5->getErrorMessage();
1409
            cout << "exception=" << message << endl;
1410
        }
1411
        return;
1412
      }
1413
    cout<<sw<<endl;
1414
    cout << "testPipeline = " << sw << endl;
1415
    sresult->success++;
1416

    
1417

    
1418
}
1419

    
1420

    
1421
void testCatalog(const char * cwd, SaxonProcessor * proc, sResultCount *sresult) {
1422

    
1423

    
1424
    cout << endl << "Test: testCatalog" << endl;
1425
    bool trace = false;
1426
    proc->setcwd(cwd);
1427
    proc->setCatalog("../php/catalog-test/catalog.xml", trace);
1428

    
1429
    Xslt30Processor * trans = proc->newXslt30Processor();
1430

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

    
1433
    if(executable == nullptr) {
1434

    
1435
        if (trans->exceptionOccurred()) {
1436

    
1437
            if(trans->getErrorMessage() != nullptr) {
1438
                const char *message = trans->getErrorMessage();
1439
                cout << "exception=" << message << endl;
1440
            } else {
1441
                const char *message = proc->getErrorMessage();
1442
                if(message != nullptr) {
1443
                    cout << "exception=" << message << endl;
1444
                }
1445
            }
1446
            }
1447
        return;
1448
    }
1449

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

    
1454
    if(result != NULL) {
1455
        std::cerr << "testCatalog result= " << result << std::endl;
1456
    }
1457

    
1458
}
1459

    
1460

    
1461
/*
1462
 *
1463
 *void *RunThread(void *args) {
1464

1465
    struct arg_struct *argsi = (struct arg_struct *)args;
1466
    int threadid = argsi->id;
1467
    Xslt30Processor * trans = argsi->trans;
1468
    long tid;
1469
    tid = (long)threadid;
1470

1471
    trans->attachThread();
1472

1473
   trans->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
1474

1475
    const char *result = trans->applyTemplatesReturningString();
1476
    cout<<" Result from THREAD ID: "<< tid << ", " << result<<endl;
1477
    delete result;
1478
    //pthread_exit(NULL);
1479
    trans->detachThread();
1480
}
1481

1482
void testThreads (SaxonProcessor * processor) {
1483
    pthread_t threads[NUM_THREADS];
1484
    int rc;
1485
    int i;
1486

1487
    Xslt30Processor *  trans = processor->newXslt30Processor();
1488

1489
    trans->compileFromFile("../php/xsl/foo.xsl");
1490
    struct arg_struct args;
1491
    args.trans = trans;
1492

1493
    for( i = 0; i < NUM_THREADS; i++ ) {
1494
        cout << "main() : creating thread, " << i << endl;
1495
        args.id = i;
1496
        rc = pthread_create(&threads[i], NULL, RunThread, (void *)&args);
1497

1498
        if (rc) {
1499
            cout << "Error:unable to create thread," << rc << endl;
1500
            exit(-1);
1501
        }
1502
        (void) pthread_join(threads[i], NULL);
1503
    }
1504
  //  pthread_exit(NULL);
1505
}
1506

1507
 *
1508
 */
1509

    
1510

    
1511
int main(int argc, char* argv[]) {
1512

    
1513

    
1514
    const char * cwd = nullptr;
1515
    if(argc > 0) {
1516
        cwd = argv[1];
1517
    }
1518

    
1519
    SaxonProcessor *processor = new SaxonProcessor(true);
1520
    processor->setConfigurationProperty("http://saxon.sf.net/feature/licenseFileLocation", "/usr/local/lib/saxon-license.lic");
1521
    cout << "Test: Xslt30Processor with Saxon version=" << processor->version() << endl << endl;
1522
    
1523
    char buff[FILENAME_MAX]; //create string buffer to hold path
1524
    GetCurrentDir( buff, FILENAME_MAX );
1525
    cout<<"CWD = "<< buff<<endl;
1526
    if(cwd != nullptr) {
1527
            processor->setcwd(cwd);//"/Users/ond1/work/development/git/saxon-dev/saxondev/src/main/c/samples/cppTests"); //set to the current working directory
1528
    }
1529
    if (processor->isSchemaAwareProcessor()) {
1530

    
1531
        std::cerr << "Processor is Schema Aware" << std::endl;
1532
    } else {
1533
        std::cerr << "Processor is not Schema Aware" << std::endl;
1534
    }
1535

    
1536
    sResultCount *sresult = new sResultCount();
1537
    Xslt30Processor *trans = processor->newXslt30Processor();
1538

    
1539
    if( trans == nullptr) {
1540
        cout<< "Error creating Xslt30Processor"<<endl;
1541
        if(processor->exceptionOccurred()) {
1542
            cout<< "Error message: " <<processor->getErrorMessage()<<endl;
1543

    
1544
        }
1545
        return -1;
1546
    }
1547
    //testValidation(trans,sresult);
1548

    
1549
    testInitialTemplate(processor, trans, sresult);
1550

    
1551
    exampleSimple1Err(trans, sresult);
1552

    
1553
    exampleSimple1(trans, sresult);
1554

    
1555
    exampleSimple_xmark(trans, sresult);
1556

    
1557
    exampleSimple2(trans, sresult);
1558

    
1559
    exampleSimple3(processor, trans, sresult);
1560

    
1561
    exampleSimple3a(processor, trans, sresult);
1562

    
1563
    testApplyTemplatesString1(trans, sresult);
1564

    
1565
    testApplyTemplatesString2(processor, trans, sresult);
1566

    
1567
    testApplyTemplates2a(processor, trans, sresult);
1568

    
1569
    testTransformToString4(processor, trans, sresult);
1570

    
1571

    
1572
    testCatalog(cwd, processor,  sresult);
1573

    
1574

    
1575
    testTransformToString2b(processor, trans, sresult);
1576

    
1577
    testTransformToString3(processor, trans, sresult);
1578
        
1579
    testTransformFromstring(processor, trans, sresult);
1580

    
1581
    //processor->release();
1582
    delete processor;
1583
    processor->release();
1584
    return 0;
1585

    
1586
    testTransformFromstring2Err(processor, trans, sresult);
1587

    
1588
    testTrackingOfValueReference(processor, trans, sresult);
1589

    
1590
    testTrackingOfValueReferenceError(processor, trans, sresult);
1591

    
1592
    testPackage1(trans, sresult);
1593

    
1594
    testPackage2(trans, sresult);
1595

    
1596
    testXdmNodeOutput(trans, sresult);
1597

    
1598
    exampleParam(processor, trans, sresult);
1599

    
1600
    xmarkTest1(trans, sresult);
1601

    
1602
    xmarkTest2(trans, sresult);
1603
    testCallFunction(processor, trans, sresult);
1604
    testResolveUri(processor, trans, sresult);
1605
    testContextNotRoot(processor, trans, sresult);
1606
    testContextNotRootNamedTemplate(processor, trans, sresult);
1607
    testContextNotRootNamedTemplateValue(processor, trans, sresult);
1608
    testPipeline(processor, sresult);
1609

    
1610
    //Available in PE and EE
1611
    //testTransformToStringExtensionFunc(processor, trans);
1612

    
1613
    delete trans;
1614
    delete processor;
1615
    //processor->release();
1616

    
1617

    
1618
    SaxonProcessor *processor2 = new SaxonProcessor(true);
1619

    
1620
    Xslt30Processor *trans2 = processor2->newXslt30Processor();
1621
    testApplyTemplatesString1(trans2, sresult);
1622
    delete trans2;
1623
    processor2->release();
1624

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

    
1628
    std::list<std::string>::iterator it;
1629
    std::cout << "Failed tests:" << std::endl;
1630
// Make iterate point to begining and incerement it one by one till it reaches the end of list.
1631
    for (it = sresult->failureList.begin(); it != sresult->failureList.end(); it++) {
1632
        //Print the contents
1633
        std::cout << it->c_str() << std::endl;
1634

    
1635
    }
1636

    
1637

    
1638
    return 0;
1639
}
(27-27/28)