Project

Profile

Help

Download (73.4 KB) Statistics
| Branch: | Revision:

he / src / main / c / samples / cppTests / testXSLT30.cpp @ 0c4104cf

1 55b80284 O'Neil Delpratt
2 72bf04c6 Norman Walsh
#include <sstream>
3
#include <stdio.h>
4 55b80284 O'Neil Delpratt
5 72bf04c6 Norman Walsh
#include "../../Saxon.C.API/SaxonProcessor.h"
6
#include "../../Saxon.C.API/XdmValue.h"
7
#include "../../Saxon.C.API/XdmItem.h"
8
#include "../../Saxon.C.API/XdmNode.h"
9 501beb26 O'Neil Delpratt
#include "../../Saxon.C.API/XdmFunctionItem.h"
10 55b80284 O'Neil Delpratt
#include "../../Saxon.C.API/XdmMap.h"
11
#include "../../Saxon.C.API/XdmArray.h"
12 e05bbe34 O'Neil Delpratt
#include "cppExtensionFunction.h"
13
#include "CppTestUtils.h"
14 72bf04c6 Norman Walsh
#include <string>
15 306962bc O'Neil Delpratt
#include <thread>
16 72bf04c6 Norman Walsh
17
18
19 55b80284 O'Neil Delpratt
20 72bf04c6 Norman Walsh
using namespace std;
21 55b80284 O'Neil Delpratt
22
23
#ifdef MEM_DEBUG
24
#define new new(__FILE__, __LINE__)
25
#endif
26
27
28
29
30
31 72bf04c6 Norman Walsh
char fname[] = "_nativeCall";
32
char funcParameters[] = "(Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;)Ljava/lang/Object;";
33
34
JNINativeMethod cppMethods[] =
35 501beb26 O'Neil Delpratt
        {
36
                {
37
                        fname,
38
                        funcParameters,
39 e05bbe34 O'Neil Delpratt
                        (void *) &cppExtensionFunction::cppNativeCall
40 501beb26 O'Neil Delpratt
                }
41
        };
42 72bf04c6 Norman Walsh
43
44
/*
45
* Test transform to String. Source and stylesheet supplied as arguments
46
*/
47 501beb26 O'Neil Delpratt
void testApplyTemplatesString1(Xslt30Processor *trans, sResultCount *sresult) {
48
49 4ee4355d O'Neil Delpratt
    cout << "Test: testApplyTemplatesString1:" << endl;
50 9458d238 O'Neil Delpratt
//    trans->setupXslMessage(false);
51
52 6b6a9706 O'Neil Delpratt
    XsltExecutable *executable = trans->compileFromFile("test.xsl");
53 9458d238 O'Neil Delpratt
54
    if(executable== nullptr) {
55
        sresult->failure++;
56
        sresult->failureList.push_back("testApplyTemplatesString1");
57
        cerr << "testApplyTemplatesString1 NULL found" << endl;
58
        if (trans->exceptionOccurred()) {
59
            cerr <<"testApplyTemplatesString1 error: "<< trans->getErrorMessage() << endl;
60
        }
61
        return;
62
    }
63 6b6a9706 O'Neil Delpratt
    executable->setInitialMatchSelectionAsFile("cat.xml");
64 501beb26 O'Neil Delpratt
65
    const char *output = executable->applyTemplatesReturningString();
66 9458d238 O'Neil Delpratt
    if (output == nullptr) {
67 501beb26 O'Neil Delpratt
        printf("result is null ====== FAIL ====== \n");
68
        sresult->failure++;
69
        fflush(stdout);
70
        sresult->failureList.push_back("testApplyTemplatesString1-0");
71
    } else if (string(output).find(string("<out>text2</out>")) != std::string::npos) {
72 9458d238 O'Neil Delpratt
        printf("%s", output);
73 501beb26 O'Neil Delpratt
        printf("result is OK \n");
74
        sresult->success++;
75 a69dd173 O'Neil Delpratt
        delete output;
76 72bf04c6 Norman Walsh
    } else {
77 501beb26 O'Neil Delpratt
        printf("result is null ====== FAIL ====== \n");
78
        sresult->failure++;
79
        sresult->failureList.push_back("testApplyTemplatesString1-1");
80 a69dd173 O'Neil Delpratt
            std::cout<<"output="<<output<<std::endl;
81
        delete output;
82 501beb26 O'Neil Delpratt
    }
83
    fflush(stdout);
84 9458d238 O'Neil Delpratt
    delete executable;
85 72bf04c6 Norman Walsh
86
}
87
88
/*
89
* Test transform to String. Source and stylesheet supplied as arguments
90
*/
91 501beb26 O'Neil Delpratt
void testTransformToStringExtensionFunc(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
92
93
    cout << endl << "Test: TransformToStringExtensionFunc:" << endl;
94
    bool nativeFound = processor->registerNativeMethods(SaxonProcessor::sxn_environ->env,
95
                                                        "com/saxonica/functions/extfn/cpp/NativeCall",
96
                                                        cppMethods, sizeof(cppMethods) / sizeof(cppMethods[0]));
97
98
    XsltExecutable * executable = trans->compileFromFile("testExtension.xsl");
99 9458d238 O'Neil Delpratt
100
    if(executable== nullptr) {
101
        sresult->failure++;
102
        sresult->failureList.push_back("TransformToStringExtensionFunc");
103
        cerr << "TransformToStringExtensionFunc NULL found" << endl;
104
        if (trans->exceptionOccurred()) {
105
            cerr <<"TransformToStringExtensionFunc error: "<< trans->getErrorMessage() << endl;
106
        }
107
        return;
108
    }
109
110 501beb26 O'Neil Delpratt
    executable->setProperty("extc", "home/ond1/work/svn/latest9.9-saxonc/samples/cppTests/cppExtensionFunction");
111
112
    if (nativeFound) {
113
        const char *output = executable->transformFileToString("cat.xml");
114
115 9458d238 O'Neil Delpratt
        if (output == nullptr) {
116 501beb26 O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->ExceptionDescribe();
117
            printf("result is null ====== FAIL ======  \n");
118 4ee4355d O'Neil Delpratt
            sresult->failure++;
119 501beb26 O'Neil Delpratt
            sresult->failureList.push_back("testTransformToStringExnteionFunc");
120
        } else {
121 a69dd173 O'Neil Delpratt
            sresult->success++;
122 501beb26 O'Neil Delpratt
            printf("result is OK \n");
123
        }
124
        fflush(stdout);
125
        delete output;
126
    } else {
127
        printf("native Class not foun ====== FAIL ====== ");
128 4ee4355d O'Neil Delpratt
        sresult->failure++;
129 501beb26 O'Neil Delpratt
        sresult->failureList.push_back("testTransformToStringExtensionFunc");
130 72bf04c6 Norman Walsh
    }
131 9458d238 O'Neil Delpratt
    delete executable;
132 72bf04c6 Norman Walsh
}
133
134
135
/*
136
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
137
*/
138 501beb26 O'Neil Delpratt
void testApplyTemplatesString2(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
139 72bf04c6 Norman Walsh
140 4ee4355d O'Neil Delpratt
    cout << "Test: testApplyTemplatesString2:" << endl;
141
142 19d839c4 O'Neil Delpratt
143 501beb26 O'Neil Delpratt
    XdmNode *input = processor->parseXmlFromFile("cat.xml");
144 72bf04c6 Norman Walsh
145 9458d238 O'Neil Delpratt
    if (input== nullptr) {
146 501beb26 O'Neil Delpratt
        cout << "Source document is null." << endl;
147 4ee4355d O'Neil Delpratt
        if(processor->exceptionOccurred()) {
148
            cerr<<processor->getErrorMessage()<<endl;
149
        }
150 a69dd173 O'Neil Delpratt
        sresult->failure++;
151
        sresult->failureList.push_back("testApplyTemplatesString2");
152
        return;
153 72bf04c6 Norman Walsh
154
    }
155 79d12c83 O'Neil Delpratt
    XsltExecutable * executable = trans->compileFromFile("test.xsl");
156 a69dd173 O'Neil Delpratt
    if(executable == nullptr) {
157 9458d238 O'Neil Delpratt
        sresult->failure++;
158
        sresult->failureList.push_back("testApplyTemplatesString2");
159
        if (trans->exceptionOccurred()) {
160
            cerr <<"testApplyTemplatesString2 error: "<< trans->getErrorMessage() << endl;
161
        }
162 a69dd173 O'Neil Delpratt
        delete input;
163 9458d238 O'Neil Delpratt
        return;
164
    }
165 a69dd173 O'Neil Delpratt
166 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelection((XdmValue *) input);
167 4ee4355d O'Neil Delpratt
168 501beb26 O'Neil Delpratt
    const char *output = executable->applyTemplatesReturningString();
169 9458d238 O'Neil Delpratt
    if (output== nullptr) {
170 501beb26 O'Neil Delpratt
        printf("result is null ====== FAIL ======  \n");
171
        sresult->failureList.push_back("testApplyTemplatesString2");
172 4ee4355d O'Neil Delpratt
        sresult->failure++;
173 501beb26 O'Neil Delpratt
    } else {
174 a69dd173 O'Neil Delpratt
        sresult->success++;
175 501beb26 O'Neil Delpratt
        printf("%s", output);
176
        printf("result is OK \n");
177 a69dd173 O'Neil Delpratt
        delete output;
178 72bf04c6 Norman Walsh
    }
179 501beb26 O'Neil Delpratt
    fflush(stdout);
180 4ee4355d O'Neil Delpratt
    delete input;
181 501beb26 O'Neil Delpratt
    delete executable;
182 72bf04c6 Norman Walsh
}
183
184
/*
185
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
186
Should be error. Stylesheet file does not exist
187
*/
188 4ee4355d O'Neil Delpratt
void testApplyTemplates2a_Error(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
189
190
    cout << "Test: TransformToString2a_Error:" << endl;
191 72bf04c6 Norman Walsh
192 501beb26 O'Neil Delpratt
    XdmNode *input = processor->parseXmlFromFile("cat.xml");
193
194 a69dd173 O'Neil Delpratt
    if (input == nullptr) {
195 501beb26 O'Neil Delpratt
        cout << "Source document is null. ====== FAIL ======" << endl;
196 4ee4355d O'Neil Delpratt
        if(processor->exceptionOccurred()) {
197
            cerr<<processor->getErrorMessage()<<endl;
198
        }
199 501beb26 O'Neil Delpratt
        sresult->failure++;
200 4ee4355d O'Neil Delpratt
        sresult->failureList.push_back("testApplyTemplates2a_Error");
201
        trans->exceptionClear();
202 501beb26 O'Neil Delpratt
        return;
203
    }
204
    XsltExecutable * executable = trans->compileFromFile("test-error.xsl");
205 9458d238 O'Neil Delpratt
206
    if(executable== nullptr) {
207 4ee4355d O'Neil Delpratt
        printf("Expected result is null \n");
208 a69dd173 O'Neil Delpratt
        sresult->success++;
209
210 9458d238 O'Neil Delpratt
        if (trans->exceptionOccurred()) {
211 4ee4355d O'Neil Delpratt
            cerr <<"testApplyTemplates2a_Error error: "<< trans->getErrorMessage() << endl;
212 9458d238 O'Neil Delpratt
        }
213 a69dd173 O'Neil Delpratt
        delete input;
214 4ee4355d O'Neil Delpratt
        trans->exceptionClear();
215 9458d238 O'Neil Delpratt
        return;
216
    }
217
218 a69dd173 O'Neil Delpratt
    sresult->failure++;
219 4ee4355d O'Neil Delpratt
    sresult->failureList.push_back("testApplyTemplates2a_Error");
220 501beb26 O'Neil Delpratt
    fflush(stdout);
221 4ee4355d O'Neil Delpratt
222 9458d238 O'Neil Delpratt
    delete executable;
223 501beb26 O'Neil Delpratt
224 72bf04c6 Norman Walsh
}
225
226
/*
227
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
228
Should be error. Source file does not exist
229
*/
230 501beb26 O'Neil Delpratt
void testTransformToString2b(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
231 72bf04c6 Norman Walsh
232 501beb26 O'Neil Delpratt
    cout << endl << "Test: TransformToString2b:" << endl;
233 72bf04c6 Norman Walsh
234 a69dd173 O'Neil Delpratt
    const char * result  = trans->transformFileToString("cat-error.xml", "test-error.xsl");
235 72bf04c6 Norman Walsh
236 a69dd173 O'Neil Delpratt
    if(result== nullptr) {
237 4ee4355d O'Neil Delpratt
        if(trans->exceptionOccurred()) {
238
            cerr<<"Expected failure = "<<trans->getErrorMessage()<<endl;
239
        }
240 a69dd173 O'Neil Delpratt
        sresult->success++;
241 4ee4355d O'Neil Delpratt
        trans->exceptionClear();
242 501beb26 O'Neil Delpratt
        return;
243 a69dd173 O'Neil Delpratt
    }else {
244
        delete result;
245 9458d238 O'Neil Delpratt
        sresult->failure++;
246
        sresult->failureList.push_back("testTransformToString2b");
247
        cerr << "testTransformToString2b NULL found" << endl;
248
        if (trans->exceptionOccurred()) {
249 4ee4355d O'Neil Delpratt
            cerr <<"Error: "<< trans->getErrorMessage() << endl;
250 9458d238 O'Neil Delpratt
        }
251 a69dd173 O'Neil Delpratt
        trans->exceptionClear();
252 9458d238 O'Neil Delpratt
        return;
253
    }
254
255 72bf04c6 Norman Walsh
256
}
257
258
259
/*
260
* Test transform to String. stylesheet supplied as argument. Source supplied as xml string
261
and integer parmater created and supplied
262
*/
263 501beb26 O'Neil Delpratt
void testTransformToString3(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
264
265 55b80284 O'Neil Delpratt
    cout << endl << "Test: testTransformToString3" << endl;
266 72bf04c6 Norman Walsh
267 501beb26 O'Neil Delpratt
    XdmNode *inputi = processor->parseXmlFromString(
268
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
269 72bf04c6 Norman Walsh
270 9458d238 O'Neil Delpratt
    if (inputi== nullptr) {
271 4ee4355d O'Neil Delpratt
        cout << "Source document inputi is null." << endl;
272
        if (trans->exceptionOccurred()) {
273
            cerr <<"testTransformToString3 error: "<< trans->getErrorMessage() << endl;
274
        }
275
        sresult->failure++;
276 501beb26 O'Neil Delpratt
        sresult->failureList.push_back("testTransformToString3");
277 146cfacb O'Neil Delpratt
278 501beb26 O'Neil Delpratt
        return;
279 72bf04c6 Norman Walsh
    }
280
281 146cfacb O'Neil Delpratt
    XdmAtomicValue *value1 = processor->makeIntegerValue(10);
282
283 4ee4355d O'Neil Delpratt
    if (value1== nullptr) {
284
        cout << "value1 is null." << endl;
285
        if (trans->exceptionOccurred()) {
286
            cerr <<"testTransformToString3 error: "<< trans->getErrorMessage() << endl;
287
        }
288
        sresult->failure++;
289
        sresult->failureList.push_back("testTransformToString3");
290
        delete inputi;
291
        return;
292
    }
293 146cfacb O'Neil Delpratt
294 501beb26 O'Neil Delpratt
    XsltExecutable * executable = trans->compileFromFile("test.xsl");
295 9458d238 O'Neil Delpratt
296
    if(executable== nullptr) {
297
        sresult->failure++;
298
        sresult->failureList.push_back("testTransformToString3");
299
        cerr << "testTransformToString3 NULL found" << endl;
300
        if (trans->exceptionOccurred()) {
301
            cerr <<"testTransformToString3 error: "<< trans->getErrorMessage() << endl;
302
        }
303 a69dd173 O'Neil Delpratt
        delete value1;
304 9458d238 O'Neil Delpratt
        return;
305
    }
306
307 146cfacb O'Neil Delpratt
308 4ee4355d O'Neil Delpratt
    executable->setParameter("numParam", (XdmValue *) value1);
309 146cfacb O'Neil Delpratt
310 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelection((XdmNode *) inputi);
311
    const char *output = executable->applyTemplatesReturningString();
312 a69dd173 O'Neil Delpratt
    if (output == nullptr) {
313 501beb26 O'Neil Delpratt
        printf("result is null ====== FAIL ====== \n");
314
        sresult->failureList.push_back("testTransformToString3");
315
    } else {
316
        printf("%s", output);
317
        printf("result is OK \n");
318 a69dd173 O'Neil Delpratt
        delete output;
319 72bf04c6 Norman Walsh
    }
320 501beb26 O'Neil Delpratt
    fflush(stdout);
321 a69dd173 O'Neil Delpratt
322 9458d238 O'Neil Delpratt
    delete value1;
323 4ee4355d O'Neil Delpratt
    delete inputi;
324 55b80284 O'Neil Delpratt
    inputi = nullptr;
325 9458d238 O'Neil Delpratt
    delete executable;
326 72bf04c6 Norman Walsh
327
}
328
329
/*
330
* Test transform to String. stylesheet supplied as argument. Source supplied as xml string
331
and integer parmater created and supplied
332
*/
333 501beb26 O'Neil Delpratt
void testTransformToString4(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
334
335 4ee4355d O'Neil Delpratt
    cout <<  "Test: testTransformToString4:" << endl;
336 501beb26 O'Neil Delpratt
337
    XdmNode *input = processor->parseXmlFromString(
338
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
339
340 a69dd173 O'Neil Delpratt
    if (input== nullptr) {
341
        cout << "Source document is null. ====== FAIL ====== " << endl;
342 4ee4355d O'Neil Delpratt
        sresult->failure++;
343 a69dd173 O'Neil Delpratt
        sresult->failureList.push_back("testTransformToString4");
344
    }
345
346
347 d791e94d O'Neil Delpratt
    XdmValue *values = new XdmValue();
348 4ee4355d O'Neil Delpratt
    XdmAtomicValue * a1 = processor->makeIntegerValue(10);
349
    XdmAtomicValue * a2 = processor->makeIntegerValue(5);
350
    XdmAtomicValue * a3 = processor->makeIntegerValue(6);
351
    XdmAtomicValue * a4 = processor->makeIntegerValue(7);
352
    values->addXdmItem(a1);
353
    values->addXdmItem(a2);
354
    values->addXdmItem(a3);
355
    values->addXdmItem(a4);
356 501beb26 O'Neil Delpratt
357 a69dd173 O'Neil Delpratt
358 4ee4355d O'Neil Delpratt
    XdmNode *sheet = processor->parseXmlFromFile("test2.xsl");
359 501beb26 O'Neil Delpratt
360
    XsltExecutable *executable = trans->compileFromXdmNode(sheet);
361 9458d238 O'Neil Delpratt
362
    if(executable== nullptr) {
363
        sresult->failure++;
364
        sresult->failureList.push_back("testTransformToString4");
365
        cerr << "testTransformToString4 NULL found" << endl;
366
        if (trans->exceptionOccurred()) {
367
            cerr <<"testTransformToString4 error: "<< trans->getErrorMessage() << endl;
368
        }
369 a69dd173 O'Neil Delpratt
        delete values;
370 9458d238 O'Neil Delpratt
        return;
371
    }
372
373
    executable->setParameter("values", (XdmValue *) values);
374 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelection((XdmNode *) input);
375 a69dd173 O'Neil Delpratt
    const char *output = executable->applyTemplatesReturningString();
376 9458d238 O'Neil Delpratt
    if (output== nullptr) {
377 501beb26 O'Neil Delpratt
        printf("result is null \n");
378 4ee4355d O'Neil Delpratt
        sresult->failure++;
379 501beb26 O'Neil Delpratt
        sresult->failureList.push_back("testTransformToString4");
380
    } else {
381
        printf("%s", output);
382
        printf("result is OK \n");
383 a69dd173 O'Neil Delpratt
        delete output;
384 72bf04c6 Norman Walsh
    }
385 501beb26 O'Neil Delpratt
    fflush(stdout);
386 a69dd173 O'Neil Delpratt
    delete sheet;
387 4ee4355d O'Neil Delpratt
    delete executable;
388 9458d238 O'Neil Delpratt
    delete input;
389
    delete values;
390 72bf04c6 Norman Walsh
391
}
392
393 501beb26 O'Neil Delpratt
void testTransformFromstring(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
394 4ee4355d O'Neil Delpratt
    cout << endl << "Test: testTransfromFromstring: "<< endl;
395
396 a69dd173 O'Neil Delpratt
    if(processor == nullptr) {
397
        cout<<" processor is null"<<endl;
398
        return;
399 72bf04c6 Norman Walsh
400 a69dd173 O'Neil Delpratt
    }
401 501beb26 O'Neil Delpratt
    XdmNode *input = processor->parseXmlFromString(
402
            "<out><person>text1</person><person>text2</person><person>text3</person></out>");
403 72bf04c6 Norman Walsh
404 146cfacb O'Neil Delpratt
    if(input == nullptr) {
405
        sresult->failure++;
406
        sresult->failureList.push_back("testTransformFromstring");
407
        cerr << "testTransformFromstring NULL found" << endl;
408
        if (trans->exceptionOccurred()) {
409
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
410
        }
411
        return;
412
    }
413
414 501beb26 O'Neil Delpratt
    XsltExecutable * executable = trans->compileFromString(
415
            "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>       <xsl:param name='values' select='(2,3,4)' /><xsl:output method='xml' indent='yes' /><xsl:template match='*'><output><xsl:for-each select='$values' ><out><xsl:value-of select='. * 3'/></out></xsl:for-each></output></xsl:template></xsl:stylesheet>");
416
417 146cfacb O'Neil Delpratt
    if(executable== nullptr) {
418 9458d238 O'Neil Delpratt
        sresult->failure++;
419
        sresult->failureList.push_back("testTransformFromstring");
420
        cerr << "testTransformFromstring NULL found" << endl;
421
        if (trans->exceptionOccurred()) {
422
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
423
        }
424 146cfacb O'Neil Delpratt
        delete input;
425 9458d238 O'Neil Delpratt
        return;
426
    }
427
428 501beb26 O'Neil Delpratt
    const char *output = executable->transformToString((XdmNode *) input);
429 9458d238 O'Neil Delpratt
    if (output== nullptr) {
430 501beb26 O'Neil Delpratt
        printf("result is null ====== FAIL ====== \n");
431 4ee4355d O'Neil Delpratt
        sresult->failure++;
432 501beb26 O'Neil Delpratt
        sresult->failureList.push_back("testTransformFromString");
433 a69dd173 O'Neil Delpratt
        if (trans->exceptionOccurred()) {
434
            cerr <<"testTransformFromstring error: "<< trans->getErrorMessage() << endl;
435
        }
436 501beb26 O'Neil Delpratt
    } else {
437
        printf("%s", output);
438
        printf("result is OK \n");
439 4ee4355d O'Neil Delpratt
        sresult->success++;
440 a69dd173 O'Neil Delpratt
        delete output;
441 72bf04c6 Norman Walsh
    }
442 501beb26 O'Neil Delpratt
    fflush(stdout);
443 9458d238 O'Neil Delpratt
    delete input;
444
    delete executable;
445 72bf04c6 Norman Walsh
446
447
}
448
449
//Test case has error in the stylesheet
450 501beb26 O'Neil Delpratt
void testTransformFromstring2Err(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
451
    cout << endl << "Test: testTransfromFromstring2-Error:" << endl;
452
453
    XsltExecutable * executable = trans->compileFromString(
454
            "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='2.0'>       <xsl:param name='values' select='(2,3,4)' /><xsl:output method='xml' indent='yes' /><xsl:template match='*'><output><xsl:for-each select='$values' ><out><xsl:value-of select='. * 3'/></out><xsl:for-each></output></xsl:template><xsl:stylesheet>");
455 9458d238 O'Neil Delpratt
456
    if(executable== nullptr) {
457 4ee4355d O'Neil Delpratt
        sresult->success++;
458
459 9458d238 O'Neil Delpratt
        if (trans->exceptionOccurred()) {
460 4ee4355d O'Neil Delpratt
            cerr <<"Error expected: "<< trans->getErrorMessage() << endl;
461 9458d238 O'Neil Delpratt
        }
462 306962bc O'Neil Delpratt
        trans->exceptionClear();
463 9458d238 O'Neil Delpratt
        return;
464
    }
465
466 72bf04c6 Norman Walsh
467 4ee4355d O'Neil Delpratt
    sresult->failure++;
468
    sresult->failureList.push_back("testTransfromFromstring2-Error");
469 9458d238 O'Neil Delpratt
    delete executable;
470 72bf04c6 Norman Walsh
471
}
472
473 501beb26 O'Neil Delpratt
void testTrackingOfValueReference(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
474
475
    cout << endl << "Test: TrackingOfValueReference:" << endl;
476
    ostringstream test;
477
    ostringstream valStr;
478
    ostringstream name;
479
    for (int i = 0; i < 10; i++) {
480
        test << "v" << i;
481
        valStr << "<out><person>text1</person><person>text2</person><person>text3</person><value>" << test.str()
482
               << "</value></out>";
483
        name << "value" << i;
484
485
        XdmValue *values = (XdmValue *) processor->parseXmlFromString(valStr.str().c_str());
486
        //cout<<"Name:"<<name.str()<<", Value:"<<values->getHead()->getStringValue()<<endl;
487
        trans->setParameter(name.str().c_str(), values);
488
        test.str("");
489
        valStr.str("");
490
        name.str("");
491 4ee4355d O'Neil Delpratt
492
        if(values == nullptr) {
493
            cerr<<"TrackingOfValueReference failed to create XdmNode object" <<endl;
494
            sresult->failure++;
495
            sresult->failureList.push_back("TrackingOfValueReference");
496
497
        }
498 501beb26 O'Neil Delpratt
    }
499
500
    std::map<std::string, XdmValue *> parMap = trans->getParameters();
501
    if (parMap.size() > 0) {
502 fc47eaa6 O'Neil Delpratt
        //cout << "Parameter size: " << parMap.size() << endl;
503
        //cout << "Parameter size: " << parMap.size()<< endl;//", Value:"<<trans->getParameters()["value0"]->getHead()->getStringValue()<<endl;
504 501beb26 O'Neil Delpratt
        ostringstream name1;
505
        for (int i = 0; i < 10; i++) {
506 4ee4355d O'Neil Delpratt
            name1 << "sparam:value" << i;
507 501beb26 O'Neil Delpratt
            cout << " i:" << i << " Map size:" << parMap.size() << ", ";
508
            XdmValue *valuei = parMap[name1.str()];
509 4ee4355d O'Neil Delpratt
            if (valuei != nullptr) {
510 501beb26 O'Neil Delpratt
                cout << name1.str();
511 4ee4355d O'Neil Delpratt
                if (valuei->itemAt(0) != nullptr)
512 501beb26 O'Neil Delpratt
                    cout << "= " << valuei->itemAt(0)->getStringValue();
513
                cout << endl;
514 4ee4355d O'Neil Delpratt
515 501beb26 O'Neil Delpratt
            } else {
516
                sresult->failure++;
517
                std::cerr << "trackingValueReference ====== FAIL ======" << std::endl;
518
                sresult->failureList.push_back("testTrackingOfValueReference");
519 d0e6f8be O'Neil Delpratt
                return;
520 501beb26 O'Neil Delpratt
            }
521
            name1.str("");
522
        }
523
    }
524 4ee4355d O'Neil Delpratt
    if(parMap.size() > 0 ) {
525
        cerr<<"Deleting map"<<endl;
526
        trans->clearParameters(true);
527
    }
528 501beb26 O'Neil Delpratt
    sresult->success++;
529 72bf04c6 Norman Walsh
}
530
531
/*Test case should be error.*/
532 501beb26 O'Neil Delpratt
void testTrackingOfValueReferenceError(SaxonProcessor *processor, Xslt30Processor *trans, sResultCount *sresult) {
533 146cfacb O'Neil Delpratt
    trans->clearParameters();
534 501beb26 O'Neil Delpratt
535
    cout << endl << "Test: TrackingOfValueReference-Error:" << endl;
536
    cout << "Parameter Map size: " << (trans->getParameters().size()) << endl;
537
    ostringstream test;
538
    ostringstream valStr;
539
    ostringstream name;
540 4ee4355d O'Neil Delpratt
    for (int i = 0; i < 2; i++) {
541 501beb26 O'Neil Delpratt
        test << "v" << i;
542
        valStr << "<out><person>text1</person><person>text2</person><person>text3</person><value>" << test.str()
543
               << "<value></out>";
544
        name << "value" << i;
545
546
        XdmValue *values = (XdmValue *) processor->parseXmlFromString(valStr.str().c_str());
547 4ee4355d O'Neil Delpratt
        if(values == nullptr) {
548
            cerr<<"values NULL ====== FAIL ======="<<endl;
549 306962bc O'Neil Delpratt
            processor->exceptionClear();
550 4ee4355d O'Neil Delpratt
        } else {
551
            trans->setParameter(name.str().c_str(), values);
552
        }
553 501beb26 O'Neil Delpratt
        test.str("");
554
        valStr.str("");
555
        name.str("");
556
    }
557
    std::map<std::string, XdmValue *> parMap = trans->getParameters();
558
    cout << "Parameter Map size: " << parMap.size() << endl;
559
560
    ostringstream name1;
561
    bool errorFound = false;
562 4ee4355d O'Neil Delpratt
    for (int i = 0; i < 2; i++) {
563
        name1 << "sparam:value" << i;
564 501beb26 O'Neil Delpratt
        cout << " i:" << i << " Map size:" << parMap.size() << ", ";
565
        try {
566 306962bc O'Neil Delpratt
567 501beb26 O'Neil Delpratt
            XdmValue *valuei = parMap.at(name1.str());
568 339c45ee Dave Delpratt
            if (valuei != nullptr) {
569 501beb26 O'Neil Delpratt
                cout << name1.str();
570 339c45ee Dave Delpratt
                if (valuei->itemAt(0) != nullptr)
571 501beb26 O'Neil Delpratt
                    cout << "= " << valuei->itemAt(0)->getStringValue();
572
                cout << endl;
573
            }
574
        } catch (const std::out_of_range &oor) {
575 306962bc O'Neil Delpratt
            cout << "Out of range exception occurred. Exception " << endl;
576 501beb26 O'Neil Delpratt
            if (!errorFound) {
577
                sresult->success++;
578
                errorFound = true;
579
580
                return;
581
            }
582
        }
583
        name1.str("");
584
    }
585
    sresult->failure++;
586
    sresult->failureList.push_back("testTrackingOfValueReferenceError");
587
588
}
589
590
void testValidation(Xslt30Processor *trans, sResultCount *sresult) {
591 146cfacb O'Neil Delpratt
    trans->clearParameters();
592 501beb26 O'Neil Delpratt
593
594
595
    XsltExecutable * executable = trans->compileFromString(
596
            "<?xml version='1.0'?><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'                 xmlns:xs='http://www.w3.org/2001/XMLSchema' version='3.0' exclude-result-prefixes='#all'>     <xsl:import-schema><xs:schema><xs:element name='x' type='xs:int'/></xs:schema></xsl:import-schema> <xsl:template name='main'>          <xsl:result-document validation='strict'> <x>3</x>   </xsl:result-document>    </xsl:template>    </xsl:stylesheet>");
597
598 339c45ee Dave Delpratt
599
    if(executable== nullptr) {
600
        sresult->failure++;
601
        sresult->failureList.push_back("testValidation");
602
        if (trans->exceptionOccurred()) {
603
            cerr <<"Error message: "<< trans->getErrorMessage() << endl;
604
        }
605
        trans->exceptionClear();
606
        return;
607
    }
608
609
610
611 501beb26 O'Neil Delpratt
    const char *rootValue = executable->callTemplateReturningString("main");
612
613
614 9458d238 O'Neil Delpratt
    if (rootValue== nullptr) {
615 501beb26 O'Neil Delpratt
        std::cout << "NULL found" << std::endl;
616
        sresult->failure++;
617
        sresult->failureList.push_back("testValidation");
618
        return;
619
620
    } else {
621
        std::cout << "Result=" << rootValue << endl;
622
        sresult->success++;
623
    }
624 9458d238 O'Neil Delpratt
    delete executable;
625 72bf04c6 Norman Walsh
}
626
627
628 501beb26 O'Neil Delpratt
void testXdmNodeOutput(Xslt30Processor *trans, sResultCount *sresult) {
629 72bf04c6 Norman Walsh
630 501beb26 O'Neil Delpratt
    std::cout << "testXdmNodeOutput" << std::endl;
631
    XsltExecutable * executable = trans->compileFromString(
632
            "<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template name='go'><a/></xsl:template></xsl:stylesheet>");
633 339c45ee Dave Delpratt
634
635
    if(executable== nullptr) {
636
        sresult->failure++;
637
        sresult->failureList.push_back("testXdmNodeOutput");
638
        if (trans->exceptionOccurred()) {
639
            cerr <<"Error message: "<< trans->getErrorMessage() << endl;
640
        }
641
        trans->exceptionClear();
642
        return;
643
    }
644
645 501beb26 O'Neil Delpratt
    XdmValue *rootValue = executable->callTemplateReturningValue("go");
646 9458d238 O'Neil Delpratt
    if (rootValue== nullptr) {
647 5d25f10d O'Neil Delpratt
        if (executable->exceptionOccurred()) {
648
            SaxonApiException *exception = executable->getException();
649
            cerr << "Error: " << exception->getMessage() << endl;
650
            delete exception;
651
        }
652 501beb26 O'Neil Delpratt
        sresult->failure++;
653
        sresult->failureList.push_back("testXdmNodeOutput-0.0");
654
        return;
655
    }
656
    XdmItem *rootItem = rootValue->getHead();
657 9458d238 O'Neil Delpratt
    if (rootItem== nullptr) {
658 72bf04c6 Norman Walsh
659 501beb26 O'Neil Delpratt
        cout << "Result is null ====== FAIL ====== " << endl;
660
        sresult->failure++;
661
        sresult->failureList.push_back("testXdmNodeOutput-0");
662 4ee4355d O'Neil Delpratt
        delete rootValue;
663
        delete executable;
664 501beb26 O'Neil Delpratt
        return;
665
    }
666
    XdmNode *root = (XdmNode *) rootItem;
667
    if (root->getNodeKind() == DOCUMENT) {
668
        cout << "Result is a Document" << endl;
669
    } else {
670
        cout << "Node is of kind:" << root->getNodeKind() << endl;
671
    }
672
    const char *result = executable->callTemplateReturningString("go");
673
    if (string(result).find(string("<a/>")) != std::string::npos) {
674
        sresult->success++;
675 4ee4355d O'Neil Delpratt
        delete result;
676 501beb26 O'Neil Delpratt
    } else {
677
        //TODO - this test case prints the XML declaration. Check if this correct
678
        sresult->failure++;
679
        cout << "testXdmNodeOutputAndString ======= FAIL========" << endl;
680 9458d238 O'Neil Delpratt
        sresult->failureList.push_back("testXdmNodeOutput");
681 501beb26 O'Neil Delpratt
    }
682 4ee4355d O'Neil Delpratt
    delete rootValue;
683 9458d238 O'Neil Delpratt
    delete executable;
684 72bf04c6 Norman Walsh
685
}
686
687 501beb26 O'Neil Delpratt
void exampleSimple1(Xslt30Processor *proc, sResultCount *sresult) {
688
    cout << "ExampleSimple1 taken from PHP:" << endl;
689
690
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
691 9458d238 O'Neil Delpratt
    if(executable== nullptr) {
692
        sresult->failure++;
693
        sresult->failureList.push_back("exampleSimple1");
694
        cerr << "exampleSimple1 NULL found" << endl;
695
        if (proc->exceptionOccurred()) {
696
            cerr <<"exampleSimple1 error: "<< proc->getErrorMessage() << endl;
697
        }
698
        return;
699
    }
700 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
701
    const char *result = executable->applyTemplatesReturningString();
702
    if (result != NULL) {
703
        cout << result << endl;
704
        sresult->success++;
705 a69dd173 O'Neil Delpratt
        delete result;
706 501beb26 O'Neil Delpratt
    } else {
707
        cout << "Result is null ====== FAIL ====== " << endl;
708
        sresult->failure++;
709
    }
710 146cfacb O'Neil Delpratt
    proc->clearParameters();
711 9458d238 O'Neil Delpratt
    delete executable;
712 72bf04c6 Norman Walsh
}
713
714 501beb26 O'Neil Delpratt
void exampleSimple1Err(Xslt30Processor *proc, sResultCount *sresult) {
715 e05bbe34 O'Neil Delpratt
    cout << "ExampleSimple1Err taken from PHP:" << endl;
716 72bf04c6 Norman Walsh
717 501beb26 O'Neil Delpratt
    XsltExecutable * executable = proc->compileFromFile("err.xsl");
718 a69dd173 O'Neil Delpratt
    if(executable == nullptr || proc->exceptionOccurred()) {
719
        if( proc->exceptionOccurred()) {
720
            cout << "Error Message= "<< proc->getErrorMessage() << endl;
721
        }
722 e05bbe34 O'Neil Delpratt
        cout << "Result expected as null " << endl;
723 4ee4355d O'Neil Delpratt
        proc->exceptionClear();
724 e05bbe34 O'Neil Delpratt
        sresult->success++;
725 a69dd173 O'Neil Delpratt
        return;
726 e05bbe34 O'Neil Delpratt
    } else {
727 501beb26 O'Neil Delpratt
        sresult->failure++;
728
        sresult->failureList.push_back("exampleSimple1Err");
729 e05bbe34 O'Neil Delpratt
730 a69dd173 O'Neil Delpratt
        delete executable;
731 501beb26 O'Neil Delpratt
    }
732 a69dd173 O'Neil Delpratt
733 501beb26 O'Neil Delpratt
}
734
735 72bf04c6 Norman Walsh
736 501beb26 O'Neil Delpratt
void exampleSimple2(Xslt30Processor *proc, sResultCount *sresult) {
737
    cout << "<b>exampleSimple2:</b><br/>" << endl;
738
739
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
740 9458d238 O'Neil Delpratt
    if(executable== nullptr) {
741 fc47eaa6 O'Neil Delpratt
        sresult->failure++;
742
        sresult->failureList.push_back("exampleSimple2");
743
        cerr << "exampleSimple2 NULL found" << endl;
744 9458d238 O'Neil Delpratt
        if (proc->exceptionOccurred()) {
745 fc47eaa6 O'Neil Delpratt
            cerr <<"exampleSimple2 error: "<< proc->getErrorMessage() << endl;
746
        }
747
        return;
748
    }
749 a69dd173 O'Neil Delpratt
750 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
751
    const char *filename = "output1.xml";
752
    executable->setOutputFile(filename);
753
    executable->applyTemplatesReturningFile("output1.xml");
754
755 e05bbe34 O'Neil Delpratt
    if (CppTestUtils::exists("output1.xml")) {
756 501beb26 O'Neil Delpratt
        cout << "The file $filename exists" << endl;
757
        remove("output1.xml");
758
        sresult->success++;
759
    } else {
760
        cout << "The file " << filename << " does not exist" << endl;
761
        if (executable->exceptionOccurred()) {
762
            cout << proc->getErrorMessage() << endl;
763
        }
764
        sresult->failure++;
765
        sresult->failureList.push_back("exampleSimple2");
766
    }
767 4ee4355d O'Neil Delpratt
768 9458d238 O'Neil Delpratt
    delete executable;
769 72bf04c6 Norman Walsh
770
}
771
772 501beb26 O'Neil Delpratt
void exampleSimple3(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
773
    cout << "<b>exampleSimple3:</b><br/>" << endl;
774 146cfacb O'Neil Delpratt
    proc->clearParameters();
775 501beb26 O'Neil Delpratt
776 4ee4355d O'Neil Delpratt
    XdmNode *xdmNode = saxonProc->parseXmlFromString("<doc><b>text value of out</b></doc>");
777
    if (xdmNode== nullptr) {
778
        cout << "Error: xdmNode is null'" << endl;
779
        if(saxonProc->exceptionOccurred()) {
780
            cout<<"Error message="<<saxonProc->getErrorMessage()<<endl;
781
        }
782
        sresult->failure++;
783
        sresult->failureList.push_back("exampleSimple3");
784
        return;
785
    }
786
787 501beb26 O'Neil Delpratt
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
788
789 146cfacb O'Neil Delpratt
   if(executable == nullptr) {
790 e9b06a56 O'Neil Delpratt
        cout << "executable is NULL" <<endl;
791
        if(proc->exceptionOccurred()) {
792
                cout << proc->getErrorMessage() <<endl;
793
                
794
        }
795 4ee4355d O'Neil Delpratt
        sresult->failure++;
796
        sresult->failureList.push_back("exampleSimple3");
797 e9b06a56 O'Neil Delpratt
        return;
798
   }
799
800 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelection((XdmNode *) xdmNode);
801
    sresult->success++;
802 4ee4355d O'Neil Delpratt
    delete xdmNode;
803 a69dd173 O'Neil Delpratt
    delete executable;
804 501beb26 O'Neil Delpratt
}
805
806 4ee4355d O'Neil Delpratt
void exampleSimple3aError(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
807
    cout << "<b>exampleSimple3aError:</b><br/>" << endl;
808 501beb26 O'Neil Delpratt
809
    XsltExecutable * executable = proc->compileFromFile(nullptr);
810
811
812
    if (executable == nullptr) {
813
814
815 4ee4355d O'Neil Delpratt
        cout << "Expected failure of test exampleSimple3aError:" << endl;
816
        if(proc->exceptionOccurred()) {
817
            cout<<proc->getErrorMessage()<<endl;
818
        }
819 501beb26 O'Neil Delpratt
820
        sresult->success++;
821 4ee4355d O'Neil Delpratt
        proc->exceptionClear();
822 501beb26 O'Neil Delpratt
        return;
823
    }
824
825
    cout << "Error: executable is not nullptr'" << endl;
826
    sresult->failure++;
827 4ee4355d O'Neil Delpratt
    sresult->failureList.push_back("exampleSimple3aError");
828 a69dd173 O'Neil Delpratt
    delete executable;
829 501beb26 O'Neil Delpratt
    return;
830
831
832
}
833
834
void exampleParam(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
835 4ee4355d O'Neil Delpratt
    cout << "Test: ExampleParam" << endl;
836 501beb26 O'Neil Delpratt
837
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
838
839 4ee4355d O'Neil Delpratt
840
    if(executable == nullptr) {
841
        cout << "executable is NULL" <<endl;
842
        if(proc->exceptionOccurred()) {
843 339c45ee Dave Delpratt
            cout << "Error : " << proc->getErrorMessage() <<endl;
844 4ee4355d O'Neil Delpratt
845
        }
846
        sresult->failure++;
847
        sresult->failureList.push_back("exampleParam");
848 339c45ee Dave Delpratt
        proc->exceptionClear();
849 4ee4355d O'Neil Delpratt
        return;
850
851
    }
852
853 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
854
855
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
856 4ee4355d O'Neil Delpratt
    if (xdmvalue != nullptr) {
857 501beb26 O'Neil Delpratt
858
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
859
860
    } else {
861 4ee4355d O'Neil Delpratt
        cout << "Xdmvalue is NULL - ====== FAIL =====" << endl;
862 501beb26 O'Neil Delpratt
        sresult->failure++;
863 4ee4355d O'Neil Delpratt
        sresult->failureList.push_back("exampleParam");
864
        if(executable->exceptionOccurred()) {
865
            SaxonApiException * exception = executable->getException();
866
            cerr<<"Error: " << exception->getMessage() <<endl;
867
            delete exception;
868
        }
869
        delete executable;
870
        return;
871 501beb26 O'Neil Delpratt
    }
872
    const char *result = executable->applyTemplatesReturningString();
873 4ee4355d O'Neil Delpratt
    if (result != nullptr) {
874 501beb26 O'Neil Delpratt
        cout << "Output:" << result << endl;
875
        sresult->success++;
876 4ee4355d O'Neil Delpratt
        delete result;
877 501beb26 O'Neil Delpratt
    } else {
878
        cout << "Result is NULL<br/>  ======= fail =====" << endl;
879
        sresult->failure++;
880 4ee4355d O'Neil Delpratt
        sresult->failureList.push_back("exampleParam");
881
        if(executable->exceptionOccurred()) {
882
            SaxonApiException * exception = executable->getException();
883
            cerr<<"Error: " << exception->getMessage() <<endl;
884
            delete exception;
885
        }
886
        delete executable;
887
        return;
888 501beb26 O'Neil Delpratt
    }
889
890
    //proc->clearParameters();
891
    //unset($result);
892
    //echo 'again with a no parameter value<br/>';
893
894
    executable->setProperty("!indent", "yes");
895
    const char *result2 = executable->applyTemplatesReturningString();
896
897
898 4ee4355d O'Neil Delpratt
    if (result2 != nullptr) {
899
        cout << "Result2 output= " << result2 << endl;
900 501beb26 O'Neil Delpratt
        sresult->success++;
901 4ee4355d O'Neil Delpratt
        delete result2;
902
    } else {
903
        cout << "Result2 is NULL<br/>  ======= fail =====" << endl;
904
        sresult->failure++;
905
        sresult->failureList.push_back("exampleParam");
906
        if(executable->exceptionOccurred()) {
907
            SaxonApiException * exception = executable->getException();
908
            cerr<<"Error: " << exception->getMessage() <<endl;
909
            delete exception;
910
        }
911
        delete xdmvalue;
912
        delete executable;
913
        return;
914 501beb26 O'Neil Delpratt
    }
915
916
    //  unset($result);
917
    // echo 'again with no parameter and no properties value set. This should fail as no contextItem set<br/>';
918 4ee4355d O'Neil Delpratt
    //delete xdmvalue;
919
    //executable->clearParameters();
920
921 501beb26 O'Neil Delpratt
    XdmAtomicValue *xdmValue2 = saxonProc->makeStringValue("goodbye to you");
922 4ee4355d O'Neil Delpratt
923
    if (xdmValue2 == nullptr) {
924
        cout << "Xdmvalue is NULL - ====== FAIL =====" << endl;
925
        sresult->failure++;
926
        sresult->failureList.push_back("exampleParam");
927
        if(executable->exceptionOccurred()) {
928
            SaxonApiException * exception = executable->getException();
929
            cerr<<"Error: " << exception->getMessage() <<endl;
930
            delete exception;
931
        }
932
        delete executable;
933
        delete xdmvalue;
934
        return;
935
    }
936
937 501beb26 O'Neil Delpratt
    executable->setParameter("a-param", (XdmValue *) xdmValue2);
938 4ee4355d O'Neil Delpratt
    delete xdmvalue;
939
940 501beb26 O'Neil Delpratt
941
    const char *result3 = executable->applyTemplatesReturningString();
942 4ee4355d O'Neil Delpratt
    if (result3 != nullptr) {
943 501beb26 O'Neil Delpratt
        cout << "Output =" << result3 << endl;
944 4ee4355d O'Neil Delpratt
945 501beb26 O'Neil Delpratt
        sresult->success++;
946 4ee4355d O'Neil Delpratt
        delete result3;
947
948 501beb26 O'Neil Delpratt
    } else {
949
        cout << "Error in result ===== FAIL =======" << endl;
950
        sresult->failure++;
951
        sresult->failureList.push_back("exampleParam");
952 4ee4355d O'Neil Delpratt
        if(executable->exceptionOccurred()) {
953
            SaxonApiException * exception = executable->getException();
954
            cerr<<"Error: " << exception->getMessage() <<endl;
955
            delete exception;
956
        }
957
958 501beb26 O'Neil Delpratt
    }
959 4ee4355d O'Neil Delpratt
    delete xdmValue2;
960
    delete executable;
961
962 501beb26 O'Neil Delpratt
963
964 72bf04c6 Norman Walsh
}
965
966
// test parameter and properties maps where we update key, value pair.
967 501beb26 O'Neil Delpratt
void exampleParam2(SaxonProcessor *saxonProc, Xslt30Processor *proc, sResultCount *sresult) {
968
    cout << "\nExampleParam:</b><br/>" << endl;
969
    XsltExecutable * executable = proc->compileFromFile("../php/xsl/foo.xsl");
970
    executable->setInitialMatchSelectionAsFile("../php/xml/foo.xml");
971
972
973
    XdmAtomicValue *xdmvalue = saxonProc->makeStringValue("Hello to you");
974
    XdmAtomicValue *xdmvalue2i = saxonProc->makeStringValue("Hello from me");
975
    if (xdmvalue != NULL) {
976
977
        executable->setParameter("a-param", (XdmValue *) xdmvalue);
978
        executable->setParameter("a-param", (XdmValue *) xdmvalue2i);
979
980
    } else {
981
        cout << "Xdmvalue is null - ====== FAIL =====" << endl;
982
        sresult->failure++;
983
        sresult->failureList.push_back("exampleParam-1");
984
    }
985
    const char *result = executable->applyTemplatesReturningString();
986
    if (result != NULL) {
987
        string sresulti = string(result);
988
        if (sresulti.compare("Hello from me") == 0) {
989
            cout << "Output:" << result << endl;
990
            sresult->success++;
991
        } else {
992
            cout << "Result is " << result << " <br/> ======= fail ===== " << endl;
993
            sresult->failure++;
994
            sresult->failureList.push_back("exampleParam-2");
995
        }
996
    } else {
997
        cout<<"Result is NULL<br/>  ======= fail ====="<<endl;
998
        sresult->failure++;
999
        sresult->failureList.push_back("exampleParam-2");
1000
    }
1001
1002
//proc->clearParameters();
1003
//unset($result);
1004
//echo 'again with a no parameter value<br/>';
1005
1006
    executable->setProperty("!indent", "no");
1007
    executable->setProperty("!indent", "yes");
1008
    const char *result2 = executable->applyTemplatesReturningString();
1009
1010
    executable->clearProperties();
1011
1012 339c45ee Dave Delpratt
    if(result2 != nullptr) {
1013 501beb26 O'Neil Delpratt
        cout<<result2<<endl;
1014
        sresult->success++;
1015
1016
    }
1017
1018
    //  unset($result);
1019
    // echo 'again with no parameter and no properties value set. This should fail as no contextItem set<br/>';
1020
    XdmAtomicValue *xdmValue2 = saxonProc->makeStringValue("goodbye to you");
1021
    executable->setParameter("a-param", (XdmValue*)xdmValue2);
1022
1023
    const char *result3 = executable->applyTemplatesReturningString();
1024 339c45ee Dave Delpratt
    if(result3 != nullptr) {
1025 501beb26 O'Neil Delpratt
        cout<<"Output ="<<result3<<endl;
1026
1027
        sresult->success++;
1028
    } else {
1029
        cout<<"Error in result ===== FAIL ======="<<endl;
1030
        sresult->failure++;
1031
        sresult->failureList.push_back("exampleParam");
1032
    }
1033
1034
}
1035
1036
/* XMarkbench mark test q12.xsl with just-in-time=true*/
1037
void xmarkTest1(Xslt30Processor *proc, sResultCount *sresult) {
1038 4ee4355d O'Neil Delpratt
    cout << "Test: xmarkTest1 - XMarkbench mark test q12.xsl (JIT=true):" << endl;
1039 501beb26 O'Neil Delpratt
1040
    proc->setJustInTimeCompilation(true);
1041
1042
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1043 339c45ee Dave Delpratt
    if (result != nullptr && !proc->exceptionOccurred()) {
1044 501beb26 O'Neil Delpratt
        cout << "XdmNode returned" << endl;
1045
        sresult->success++;
1046 4ee4355d O'Neil Delpratt
        delete result;
1047 501beb26 O'Neil Delpratt
    } else {
1048
        printf("result is null \nCheck For errors:");
1049
        sresult->failure++;
1050
        sresult->failureList.push_back("xmarkTest1");
1051 4ee4355d O'Neil Delpratt
        if (proc->exceptionOccurred()) {
1052 306962bc O'Neil Delpratt
            const char * message = proc->getErrorMessage();
1053
            if(message != nullptr) {
1054
                cerr << proc->getErrorMessage() << endl;
1055 339c45ee Dave Delpratt
                proc->exceptionClear();
1056 306962bc O'Neil Delpratt
            } else {
1057
                cerr << "Message is nullptr" << endl;
1058
                SaxonProcessor::sxn_environ->env->ExceptionDescribe();
1059 339c45ee Dave Delpratt
                SaxonProcessor::sxn_environ->env->ExceptionClear();
1060 306962bc O'Neil Delpratt
            }
1061 501beb26 O'Neil Delpratt
        }
1062
    }
1063 339c45ee Dave Delpratt
    proc->setJustInTimeCompilation(false);
1064 72bf04c6 Norman Walsh
1065
}
1066
1067 501beb26 O'Neil Delpratt
1068 72bf04c6 Norman Walsh
/* XMarkbench mark test q12.xsl with just-in-time=true*/
1069 501beb26 O'Neil Delpratt
void xmarkTest2(Xslt30Processor *proc, sResultCount *sresult) {
1070 4ee4355d O'Neil Delpratt
    cout << "Test: xmarkTest2 - XMarkbench mark test q12.xsl (JIT=true):" << endl;
1071 501beb26 O'Neil Delpratt
1072 339c45ee Dave Delpratt
    proc->setJustInTimeCompilation(true);
1073 501beb26 O'Neil Delpratt
1074
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1075 339c45ee Dave Delpratt
    if (result != nullptr && !proc->exceptionOccurred()) {
1076 501beb26 O'Neil Delpratt
        cout << "XdmNode returned" << endl;
1077 4ee4355d O'Neil Delpratt
        sresult->success++;
1078
        delete result;
1079
1080 501beb26 O'Neil Delpratt
    } else {
1081
        printf("result is null \nCheck For errors:");
1082
        sresult->failure++;
1083
        sresult->failureList.push_back("xmarkTest2");
1084
        if (proc->exceptionOccurred() > 0) {
1085 306962bc O'Neil Delpratt
            SaxonApiException * exception = proc->getException();
1086
            if(exception != nullptr) {
1087
                const char *message = exception->getMessage();
1088
                if (message != nullptr) {
1089
                    cerr << message << endl;
1090 339c45ee Dave Delpratt
                    proc->exceptionClear();
1091 306962bc O'Neil Delpratt
                }
1092
                delete exception;
1093
            } else {
1094
                cerr << "Exception object is nullptr" << endl;
1095
                SaxonProcessor::sxn_environ->env->ExceptionDescribe();
1096 339c45ee Dave Delpratt
                SaxonProcessor::sxn_environ->env->ExceptionClear();
1097 306962bc O'Neil Delpratt
            }
1098 501beb26 O'Neil Delpratt
        }
1099
    }
1100
1101 339c45ee Dave Delpratt
    proc->setJustInTimeCompilation(false);
1102 501beb26 O'Neil Delpratt
1103
}
1104 72bf04c6 Norman Walsh
1105
/* XMarkbench mark test q12.xsl with just-in-time=true*/
1106 501beb26 O'Neil Delpratt
void exampleSimple_xmark(Xslt30Processor *proc, sResultCount *sresult) {
1107 4ee4355d O'Neil Delpratt
    cout << "exampleSimple_xmark test - test q12.xsl:" << endl;
1108 72bf04c6 Norman Walsh
1109 501beb26 O'Neil Delpratt
    proc->setJustInTimeCompilation(true);
1110 72bf04c6 Norman Walsh
1111 501beb26 O'Neil Delpratt
    XdmValue *result = proc->transformFileToValue("xmark100k.xml", "q12.xsl");
1112 339c45ee Dave Delpratt
1113
    if (result != nullptr && !proc->exceptionOccurred()) {
1114 501beb26 O'Neil Delpratt
        cout << "XdmNode returned" << endl;
1115 4ee4355d O'Neil Delpratt
        sresult->success++;
1116 a69dd173 O'Neil Delpratt
        delete result;
1117 501beb26 O'Neil Delpratt
    } else {
1118 4ee4355d O'Neil Delpratt
        printf("Result is null \nCheck For errors:");
1119 501beb26 O'Neil Delpratt
        if (proc->exceptionOccurred() > 0) {
1120
            cout << proc->getErrorMessage() << endl;
1121
        }
1122 4ee4355d O'Neil Delpratt
        sresult->failure++;
1123
        sresult->failureList.push_back("exampleSimple_xmark");
1124
        proc->exceptionClear();
1125 501beb26 O'Neil Delpratt
    }
1126 146cfacb O'Neil Delpratt
    proc->clearParameters();
1127 339c45ee Dave Delpratt
    proc->setJustInTimeCompilation(false);
1128 72bf04c6 Norman Walsh
1129 501beb26 O'Neil Delpratt
}
1130 72bf04c6 Norman Walsh
1131
/*
1132
* Test saving nd loading a Xslt package
1133
*/
1134 501beb26 O'Neil Delpratt
void testPackage1(Xslt30Processor *trans, sResultCount *sresult) {
1135
1136 4ee4355d O'Neil Delpratt
    cout << endl << "Test: testPackage1 - Saving and loading Packages:" << endl;
1137 146cfacb O'Neil Delpratt
    trans->clearParameters();
1138 72bf04c6 Norman Walsh
1139
1140
    trans->compileFromFileAndSave("test.xsl", "test1.sef");
1141 501beb26 O'Neil Delpratt
    const char *output = trans->transformFileToString("cat.xml", "test1.sef");
1142 4ee4355d O'Neil Delpratt
1143 9458d238 O'Neil Delpratt
    if (output== nullptr) {
1144 501beb26 O'Neil Delpratt
        printf("result is null \n");
1145
        const char *message = trans->getErrorMessage();
1146
        if (message != NULL) {
1147
            cout << "Error message =" << message << endl;
1148
        }
1149
        sresult->failure++;
1150
        sresult->failureList.push_back("testPackage1");
1151
1152
    } else {
1153
        printf("%s", output);
1154
        printf("result is OK \n");
1155
        sresult->success++;
1156 72bf04c6 Norman Walsh
    }
1157 501beb26 O'Neil Delpratt
    fflush(stdout);
1158 72bf04c6 Norman Walsh
    delete output;
1159
}
1160
1161
1162
/*
1163
* Test saving and loading a Xslt package
1164
*/
1165 4ee4355d O'Neil Delpratt
void testPackage1a(Xslt30Processor *trans, sResultCount *sresult) {
1166 501beb26 O'Neil Delpratt
1167 4ee4355d O'Neil Delpratt
    cout << endl << "Test: testPackage1a" << endl;
1168 146cfacb O'Neil Delpratt
    trans->clearParameters();
1169 72bf04c6 Norman Walsh
1170 4ee4355d O'Neil Delpratt
    trans->compileFromFileAndSave("test.xsl", "test1a.sef");
1171 72bf04c6 Norman Walsh
1172 f299305b O'Neil Delpratt
    if(trans->exceptionOccurred()) {
1173
        const char *message = trans->getErrorMessage();
1174 339c45ee Dave Delpratt
        if (message != nullptr) {
1175 f299305b O'Neil Delpratt
            cout << "Error message =" << message << endl;
1176
        }
1177
        sresult->failure++;
1178
        sresult->failureList.push_back("testPackage1a");
1179
        trans->exceptionClear();
1180
        return;
1181
    }
1182
1183
1184
1185 4ee4355d O'Neil Delpratt
    XsltExecutable * executable = trans->compileFromFile("test1.sef");
1186 339c45ee Dave Delpratt
1187
    if(executable == nullptr) {
1188
        if(trans->exceptionOccurred()) {
1189
          const char *message = trans->getErrorMessage();
1190
          if (message != nullptr) {
1191
            cout << "Error message =" << message << endl;
1192
          }
1193
          sresult->failure++;
1194
          sresult->failureList.push_back("testPackage1a");
1195
          trans->exceptionClear();
1196
            }
1197
        return;   
1198
1199
1200
    }
1201
1202 4ee4355d O'Neil Delpratt
    executable->setInitialMatchSelectionAsFile("cat.xml");
1203
    const char * output = executable->applyTemplatesReturningString();
1204 9458d238 O'Neil Delpratt
    if (output== nullptr) {
1205 4ee4355d O'Neil Delpratt
        printf("result is null \n");
1206 501beb26 O'Neil Delpratt
        const char *message = trans->getErrorMessage();
1207
        if (message != NULL) {
1208
            cout << "Error message =" << message << endl;
1209
        }
1210 4ee4355d O'Neil Delpratt
        sresult->failure++;
1211
        sresult->failureList.push_back("testPackage1a");
1212 55b80284 O'Neil Delpratt
        trans->exceptionClear();
1213 4ee4355d O'Neil Delpratt
1214 501beb26 O'Neil Delpratt
    } else {
1215
        printf("%s", output);
1216
        printf("result is OK \n");
1217 4ee4355d O'Neil Delpratt
        sresult->success++;
1218 55b80284 O'Neil Delpratt
        delete output;
1219 72bf04c6 Norman Walsh
    }
1220 501beb26 O'Neil Delpratt
    fflush(stdout);
1221 55b80284 O'Neil Delpratt
    delete executable;
1222 72bf04c6 Norman Walsh
}
1223
1224 4ee4355d O'Neil Delpratt
1225
/*
1226
* Test saving and loading a Xslt package
1227
*/
1228
void testPackage2_Error(Xslt30Processor *trans, sResultCount *sresult) {
1229
1230
    cout << endl << "Test: testPackage2_Error:" << endl;
1231
1232
    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>";
1233
1234
    trans->compileFromStringAndSave(stylesheet, "test2.sef");
1235
1236 339c45ee Dave Delpratt
    if(trans->exceptionOccurred() || trans->getException() != nullptr) {
1237 4ee4355d O'Neil Delpratt
        const char *message = trans->getErrorMessage();
1238 339c45ee Dave Delpratt
        if (message != nullptr) {
1239 4ee4355d O'Neil Delpratt
            cout << "Error message =" << message << endl;
1240
        }
1241
        sresult->success++;
1242
        trans->exceptionClear();
1243
        return;
1244
1245
    }
1246 339c45ee Dave Delpratt
    trans->exceptionClear();
1247 4ee4355d O'Neil Delpratt
    sresult->failure++;
1248
    sresult->failureList.push_back("testPackage2_Error");
1249
1250
}
1251
1252 501beb26 O'Neil Delpratt
void testCallFunction(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1253
1254
    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>";
1255
    cout << endl << "Test: testCallFunction:" << endl;
1256
    XsltExecutable * executable = trans->compileFromString(source);
1257 4ee4355d O'Neil Delpratt
1258
    if(executable == nullptr) {
1259
        if(trans->exceptionOccurred()) {
1260 306962bc O'Neil Delpratt
            SaxonApiException * exception = trans->getException();
1261
            if(exception != nullptr) {
1262
                cerr << "Error: " << exception->getMessage() << endl;
1263
                delete exception;
1264
            }
1265 4ee4355d O'Neil Delpratt
1266
        }
1267
        sresult->failure++;
1268
        sresult->failureList.push_back("testCallFunction");
1269 306962bc O'Neil Delpratt
        trans->exceptionClear();
1270 4ee4355d O'Neil Delpratt
        return;
1271
1272
1273
    }
1274
1275 501beb26 O'Neil Delpratt
    XdmValue **valueArray = new XdmValue *[2];
1276
1277
    valueArray[0] = (XdmValue *) (proc->makeIntegerValue(2));
1278
    valueArray[1] = (XdmValue *) (proc->makeIntegerValue(3));
1279
    XdmValue *v = executable->callFunctionReturningValue("{http://localhost/}add", valueArray, 2);
1280
1281
    if (v != NULL && (v->getHead())->isAtomic() && ((XdmAtomicValue *) (v->getHead()))->getLongValue() == 5) {
1282
        sresult->success++;
1283 55b80284 O'Neil Delpratt
        delete v;
1284 501beb26 O'Neil Delpratt
    } else {
1285
        if (v != NULL && !(v->getHead())->isAtomic()) {
1286
            cout << "Value in callFunction is not atomic - but expected as atomic value" << endl;
1287
        }
1288
        cout << "testCallFunction ======= FAIL ======" << endl;
1289 4ee4355d O'Neil Delpratt
        if(executable->exceptionOccurred()) {
1290
            SaxonApiException * exception = executable->getException();
1291 306962bc O'Neil Delpratt
            if(exception != nullptr) {
1292
                cerr << "Error: " << exception->getMessage() << endl;
1293
                delete exception;
1294
            }
1295 501beb26 O'Neil Delpratt
        }
1296
        sresult->failure++;
1297
        sresult->failureList.push_back("testCallFunction");
1298 55b80284 O'Neil Delpratt
        executable->exceptionClear();
1299
        delete v;
1300 501beb26 O'Neil Delpratt
    }
1301
    delete valueArray[0];
1302
    delete valueArray[1];
1303 901b6eca O'Neil Delpratt
    delete [] valueArray;
1304 55b80284 O'Neil Delpratt
    delete executable;
1305 501beb26 O'Neil Delpratt
}
1306
1307
void testInitialTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1308
1309
1310
    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>";
1311
    cout << endl << "Test:testInitialTemplate" << endl;
1312
    XsltExecutable * executable = trans->compileFromString(source);
1313 901b6eca O'Neil Delpratt
    if(executable == nullptr) {
1314
        if(trans->exceptionOccurred()) {
1315
            cout << "Error: "<< trans->getErrorMessage() << endl;
1316
        }
1317
        return;
1318
    }
1319 55b80284 O'Neil Delpratt
    XdmNode * node = proc->parseXmlFromString("<e/>");
1320 501beb26 O'Neil Delpratt
1321
    executable->setResultAsRawValue(false);
1322
    std::map<std::string, XdmValue *> parameterValues;
1323 4ee4355d O'Neil Delpratt
1324
    XdmAtomicValue * a1 = proc->makeIntegerValue(12);
1325
    XdmAtomicValue * a2 = proc->makeIntegerValue(5);;
1326
    parameterValues["a"] = a1;
1327
    parameterValues["b"] = a2;
1328 501beb26 O'Neil Delpratt
    executable->setInitialTemplateParameters(parameterValues, false);
1329
    executable->setInitialMatchSelection(node);
1330
    XdmValue *result = executable->applyTemplatesReturningValue();
1331
    if (result != NULL) {
1332
        sresult->success++;
1333
        cout << "Result=" << result->getHead()->getStringValue() << endl;
1334 4ee4355d O'Neil Delpratt
        delete result;
1335 501beb26 O'Neil Delpratt
    } else {
1336
        sresult->failure++;
1337 72bf04c6 Norman Walsh
    }
1338
1339 a69dd173 O'Neil Delpratt
    delete executable;
1340 4ee4355d O'Neil Delpratt
    delete a1;
1341
    delete a2;
1342 55b80284 O'Neil Delpratt
    delete node;
1343
    parameterValues.clear();
1344 72bf04c6 Norman Walsh
}
1345
1346 501beb26 O'Neil Delpratt
void testResolveUri(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1347
    cout << endl << "Test: testResolveUri:" << endl;
1348
1349
    XsltExecutable * executable = trans->compileFromString(
1350
            "<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>");
1351
1352 901b6eca O'Neil Delpratt
    if(executable == nullptr) {
1353
        sresult->failure++;
1354
        sresult->failureList.push_back("testResolveUri");
1355
        if(trans->exceptionOccurred()) {
1356 306962bc O'Neil Delpratt
            const char * message = trans->getErrorMessage();
1357
            if(message != nullptr) {
1358
                cout << "Error: " << trans->getErrorMessage() << endl;
1359
            }
1360 901b6eca O'Neil Delpratt
        }
1361 306962bc O'Neil Delpratt
        trans->exceptionClear();
1362 901b6eca O'Neil Delpratt
        return;
1363
    }
1364 501beb26 O'Neil Delpratt
    XdmValue *value = executable->callTemplateReturningValue("go");
1365
1366
1367 9458d238 O'Neil Delpratt
    if (value== nullptr) {
1368 5d25f10d O'Neil Delpratt
        if (executable->exceptionOccurred()) {
1369
            SaxonApiException *exception = executable->getException();
1370
            cerr << "Error: " << exception->getMessage() << endl;
1371
            delete exception;
1372
        }
1373 501beb26 O'Neil Delpratt
        sresult->failure++;
1374
        sresult->failureList.push_back("testResolveUri");
1375
    } else {
1376
1377
        const char *svalue = value->itemAt(0)->getStringValue();
1378
        cout << "testResolveUri = " << svalue << endl;
1379
        sresult->success++;
1380 306962bc O'Neil Delpratt
        delete value;
1381 501beb26 O'Neil Delpratt
    }
1382
1383 55b80284 O'Neil Delpratt
    delete executable;
1384 72bf04c6 Norman Walsh
}
1385
1386 501beb26 O'Neil Delpratt
void testContextNotRoot(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1387
    cout << endl << "Test: testContextNotRoot" << endl;
1388 72bf04c6 Norman Walsh
1389 501beb26 O'Neil Delpratt
    XdmNode *node = proc->parseXmlFromString("<doc><e>text</e></doc>");
1390 72bf04c6 Norman Walsh
1391 306962bc O'Neil Delpratt
    XsltExecutable *executable = trans->compileFromString(
1392 501beb26 O'Neil Delpratt
            "<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>");
1393 901b6eca O'Neil Delpratt
1394 306962bc O'Neil Delpratt
    if (executable == nullptr) {
1395 901b6eca O'Neil Delpratt
        sresult->failure++;
1396
        sresult->failureList.push_back("testContextNotRoot");
1397 306962bc O'Neil Delpratt
        if (trans->exceptionOccurred()) {
1398
            cout << "Error: " << trans->getErrorMessage() << endl;
1399 901b6eca O'Neil Delpratt
        }
1400
        return;
1401
    }
1402
1403 501beb26 O'Neil Delpratt
    executable->setGlobalContextItem(node);
1404
    if (node->getChildCount() > 0) {
1405 306962bc O'Neil Delpratt
        XdmNode **eNodeL1 = node->getChildren();
1406
        if (eNodeL1 != nullptr) {
1407
            XdmNode ** eNodeChildren = eNodeL1[0]->getChildren();
1408
            if (eNodeChildren != nullptr) {
1409
                XdmNode *eNode = eNodeChildren[0];
1410
                cout << "Node content = " << eNode->toString() << endl;
1411
                executable->setInitialMatchSelection(eNode);
1412
                const char *result = executable->applyTemplatesReturningString();
1413 72bf04c6 Norman Walsh
1414 306962bc O'Neil Delpratt
                if (result == nullptr) {
1415 72bf04c6 Norman Walsh
1416 55b80284 O'Neil Delpratt
                    cout << "testContextNotRoot ======= FAIL ======" << endl;
1417 306962bc O'Neil Delpratt
                    if (executable->exceptionOccurred()) {
1418
                        SaxonApiException *exception = executable->getException();
1419
                        cerr << "Error: " << exception->getMessage() << endl;
1420
                        delete exception;
1421
                    }
1422
                    sresult->failure++;
1423
                    sresult->failureList.push_back("testContextNotRoot");
1424 72bf04c6 Norman Walsh
1425 306962bc O'Neil Delpratt
1426
                } else {
1427
1428
                    cout << "testContextNotRoot = " << result << endl;
1429
                    sresult->success++;
1430
                    delete result;
1431
1432
                }
1433
1434
            }
1435 501beb26 O'Neil Delpratt
        }
1436 306962bc O'Neil Delpratt
    } else {
1437
        sresult->failure++;
1438
        sresult->failureList.push_back("testContextNotRoot");
1439 501beb26 O'Neil Delpratt
    }
1440 306962bc O'Neil Delpratt
1441
    delete executable;
1442
    delete node;
1443 501beb26 O'Neil Delpratt
}
1444 72bf04c6 Norman Walsh
1445 501beb26 O'Neil Delpratt
1446
void testContextNotRootNamedTemplate(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1447
    cout << endl << "Test: testContextNotRootNamedTemplate" << endl;
1448
1449
    XdmNode *node = proc->parseXmlFromString("<doc><e>text</e></doc>");
1450
1451 55b80284 O'Neil Delpratt
    if(node == nullptr) {
1452
        if(proc->exceptionOccurred()) {
1453
            const char * message = proc->getErrorMessage();
1454
            if(message != nullptr) {
1455
                cerr << "Error: " << message << endl;
1456
            }
1457
            proc->exceptionClear();
1458
        }
1459
        sresult->failure++;
1460
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1461
        return;
1462
    }
1463
1464 501beb26 O'Neil Delpratt
    XsltExecutable * executable = trans->compileFromString(
1465
            "<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>");
1466 901b6eca O'Neil Delpratt
1467
    if(executable == nullptr) {
1468
        sresult->failure++;
1469
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1470
        if(trans->exceptionOccurred()) {
1471
            cout << "Error: "<< trans->getErrorMessage() << endl;
1472
        }
1473 55b80284 O'Neil Delpratt
        delete node;
1474 901b6eca O'Neil Delpratt
        return;
1475
    }
1476
1477 501beb26 O'Neil Delpratt
    executable->setGlobalContextItem(node);
1478
    const char *result = executable->callTemplateReturningString("main");
1479
1480 9458d238 O'Neil Delpratt
    if (result== nullptr) {
1481 501beb26 O'Neil Delpratt
1482 55b80284 O'Neil Delpratt
        cout << "testContextNotRootNameTemplate ======= FAIL ======" << endl;
1483 4ee4355d O'Neil Delpratt
        if(executable->exceptionOccurred()) {
1484
            SaxonApiException * exception = executable->getException();
1485
            cerr<<"Error: " << exception->getMessage() <<endl;
1486
            delete exception;
1487 501beb26 O'Neil Delpratt
        }
1488
        sresult->failure++;
1489
        sresult->failureList.push_back("testContextNotRootNamedTemplate");
1490
    } else {
1491
1492
        cout << "testContextNotRoot = " << result << endl;
1493
        sresult->success++;
1494 55b80284 O'Neil Delpratt
        delete result;
1495 501beb26 O'Neil Delpratt
    }
1496 55b80284 O'Neil Delpratt
    delete node;
1497
    delete executable;
1498 72bf04c6 Norman Walsh
1499
}
1500
1501 501beb26 O'Neil Delpratt
1502
void testContextNotRootNamedTemplateValue(SaxonProcessor *proc, Xslt30Processor *trans, sResultCount *sresult) {
1503
    cout << endl << "Test: testContextNotRootNamedTemplateValue" << endl;
1504
1505
    XdmNode *node = proc->parseXmlFromString("<doc><e>text</e></doc>");
1506
1507 55b80284 O'Neil Delpratt
    if(node == nullptr) {
1508
        if(proc->exceptionOccurred()) {
1509
            const char * message = proc->getErrorMessage();
1510
            if(message != nullptr) {
1511
                cerr << "Error: " << message << endl;
1512
            }
1513
            proc->exceptionClear();
1514
        }
1515
        sresult->failure++;
1516
        sresult->failureList.push_back("testContextNotRootNamedTemplateValue");
1517
        return;
1518
    }
1519
1520 501beb26 O'Neil Delpratt
    XsltExecutable * executable = trans->compileFromString(
1521
            "<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>");
1522 901b6eca O'Neil Delpratt
1523
    if(executable == nullptr) {
1524
        sresult->failure++;
1525
        sresult->failureList.push_back("testContentNotRootNamedTemplateValue");
1526
        if(trans->exceptionOccurred()) {
1527
            cout << "Error: "<< trans->getErrorMessage() << endl;
1528
        }
1529 55b80284 O'Neil Delpratt
        delete node;
1530 901b6eca O'Neil Delpratt
        return;
1531
    }
1532
1533 501beb26 O'Neil Delpratt
    executable->setGlobalContextItem(node);
1534
    XdmValue *result = executable->callTemplateReturningValue("main");
1535
1536 55b80284 O'Neil Delpratt
    if (result == nullptr) {
1537 501beb26 O'Neil Delpratt
1538
        cout << "testCallFunction ======= FAIL ======" << endl;
1539 4ee4355d O'Neil Delpratt
        if(executable->exceptionOccurred()) {
1540
            SaxonApiException * exception = executable->getException();
1541
            cerr<<"Error: " << exception->getMessage() <<endl;
1542
            delete exception;
1543 501beb26 O'Neil Delpratt
        }
1544
        sresult->failure++;
1545
        sresult->failureList.push_back("testContextNotRootNamedTemplateValue");
1546
    } else {
1547
1548
        cout << "testContextNotRoot = " << result->getHead()->getStringValue() << endl;
1549
        sresult->success++;
1550 55b80284 O'Neil Delpratt
        delete result;
1551
        result = nullptr;
1552 72bf04c6 Norman Walsh
    }
1553
1554 55b80284 O'Neil Delpratt
    delete node;
1555
    delete executable;
1556
1557
1558 501beb26 O'Neil Delpratt
}
1559 72bf04c6 Norman Walsh
1560 501beb26 O'Neil Delpratt
void testCallSystemFunction(SaxonProcessor *proc, sResultCount *sresult) {
1561 72bf04c6 Norman Walsh
1562 501beb26 O'Neil Delpratt
    XdmFunctionItem *fi = XdmFunctionItem::getSystemFunction(proc, "{http://www.w3.org/2005/xpath-functions}parse-json",
1563
                                                            1);
1564 9458d238 O'Neil Delpratt
    if (fi== nullptr) {
1565 501beb26 O'Neil Delpratt
        sresult->failure++;
1566
        sresult->failureList.push_back("testCallSystemFunction");
1567
        return;
1568
    }
1569
1570
    XdmValue ** xdmValue = new XdmValue*[1];
1571
    xdmValue[0] = new XdmValue();
1572
    xdmValue[0]->addXdmItem(proc->makeStringValue("[1,2,3]"));
1573
    XdmValue *result = fi->call(xdmValue, 1);
1574
1575
    std::cerr << "Result = " << result->toString() << endl;
1576
    if(result->size() == 3) {
1577
1578
        cout << "testCallSystemFunction = " << result->getHead()->getStringValue() << endl;
1579
        sresult->success++;
1580
1581
    } else {
1582
        sresult->failure++;
1583
        sresult->failureList.push_back("testCallSystemFunction");
1584 72bf04c6 Norman Walsh
1585
    }
1586
1587 501beb26 O'Neil Delpratt
}
1588
1589
1590
void testPipeline(SaxonProcessor *proc, sResultCount *sresult) {
1591
    cout << endl << "Test: testPipeline" << endl;
1592 72bf04c6 Norman Walsh
1593 501beb26 O'Neil Delpratt
    Xslt30Processor * trans = proc->newXslt30Processor();
1594 55b80284 O'Neil Delpratt
    if(trans == nullptr) {
1595
        cout << "Error: Xslt30Processor is null - maybe unclean state of JNI" << endl;
1596
        sresult->failure++;
1597
        sresult->failureList.push_back("testPipeline");
1598
        return;
1599
    }
1600 501beb26 O'Neil Delpratt
    XsltExecutable * stage1 = trans->compileFromString(
1601
            "<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>");
1602 901b6eca O'Neil Delpratt
1603
    if(stage1 == nullptr) {
1604
        sresult->failure++;
1605
        sresult->failureList.push_back("testPipeline");
1606
        if(trans->exceptionOccurred()) {
1607 55b80284 O'Neil Delpratt
            const char * message = trans->getErrorMessage();
1608
            cout << "stage 1 Error: " << message << endl;
1609
1610 901b6eca O'Neil Delpratt
        }
1611 55b80284 O'Neil Delpratt
        cout << "Stage 1 Error - exit method " << endl;
1612
        delete trans;
1613 901b6eca O'Neil Delpratt
        return;
1614
    }
1615
1616 501beb26 O'Neil Delpratt
    XdmNode *inn = proc->parseXmlFromString("<z/>");
1617 72bf04c6 Norman Walsh
1618 501beb26 O'Neil Delpratt
    XsltExecutable *stage2 = trans->compileFromString(
1619
            "<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>");
1620
1621 901b6eca O'Neil Delpratt
    if(stage2 == nullptr) {
1622
        sresult->failure++;
1623
        sresult->failureList.push_back("testPipeline");
1624
        if(trans->exceptionOccurred()) {
1625 55b80284 O'Neil Delpratt
            const char * message = trans->getErrorMessage();
1626
            cout << "stage 2 Error: " << message << endl;
1627
1628 901b6eca O'Neil Delpratt
        }
1629 55b80284 O'Neil Delpratt
        cout << "Stage 2 Error - exit method " << endl;
1630
        delete stage1;
1631
        delete trans;
1632
        delete inn;
1633 901b6eca O'Neil Delpratt
        return;
1634
    }
1635 55b80284 O'Neil Delpratt
1636
1637 501beb26 O'Neil Delpratt
    XsltExecutable *stage3 = trans->compileFromString(
1638
            "<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>");
1639
1640 901b6eca O'Neil Delpratt
    if(stage3 == nullptr) {
1641
        sresult->failure++;
1642
        sresult->failureList.push_back("testPipeline");
1643
        if(trans->exceptionOccurred()) {
1644 55b80284 O'Neil Delpratt
            const char * message = trans->getErrorMessage();
1645
            cout << "stage 3 Error: " << message << endl;
1646
1647
        } else {
1648
            cout << "Stage 3 Error: testPipeline failed with exception" << endl;
1649 901b6eca O'Neil Delpratt
        }
1650 55b80284 O'Neil Delpratt
        cout << "Stage 3 Error - exit method " << endl;
1651
        delete stage1;
1652
        delete stage2;
1653
        delete trans;
1654
        delete inn;
1655 901b6eca O'Neil Delpratt
        return;
1656
    }
1657 55b80284 O'Neil Delpratt
1658 501beb26 O'Neil Delpratt
    XsltExecutable *stage4 = trans->compileFromString(
1659
            "<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>");
1660
1661 55b80284 O'Neil Delpratt
    if(stage4 == nullptr) {
1662 901b6eca O'Neil Delpratt
        sresult->failure++;
1663
        sresult->failureList.push_back("testPipeline");
1664
        if(trans->exceptionOccurred()) {
1665 55b80284 O'Neil Delpratt
            const char * message = trans->getErrorMessage();
1666
            cout << "stage 4 Error: " << message << endl;
1667
1668
        } else {
1669
            cout << "Stage 4 Error: testPipeline failed with exception" << endl;
1670
        }
1671
        cout << "Stage 4 Error - exit method " << endl;
1672
        delete stage1;
1673
        delete stage2;
1674
        delete stage3;
1675
        delete trans;
1676
        delete inn;
1677 901b6eca O'Neil Delpratt
        return;
1678
    }
1679
1680 501beb26 O'Neil Delpratt
    XsltExecutable *stage5 = trans->compileFromString(
1681
            "<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>");
1682
1683 901b6eca O'Neil Delpratt
    if(stage5 == nullptr) {
1684 501beb26 O'Neil Delpratt
        sresult->failure++;
1685
        sresult->failureList.push_back("testPipeline");
1686 901b6eca O'Neil Delpratt
        if(trans->exceptionOccurred()) {
1687 55b80284 O'Neil Delpratt
            const char * message = trans->getErrorMessage();
1688
            cout << "stage 5 Error: " << message << endl;
1689
1690
        } else {
1691
            cout << "Stage 5 Error: testPipeline failed with exception" << endl;
1692
        }
1693
        cout << "Stage 5     Error - exit method " << endl;
1694
        delete stage1;
1695
        delete stage2;
1696
        delete stage3;
1697
        delete stage4;
1698
        delete trans;
1699
        delete inn;
1700 501beb26 O'Neil Delpratt
        return;
1701 72bf04c6 Norman Walsh
    }
1702
1703 501beb26 O'Neil Delpratt
    stage1->setProperty("!indent", "no");
1704
    stage1->setInitialMatchSelection(inn);
1705 72bf04c6 Norman Walsh
1706 501beb26 O'Neil Delpratt
    XdmValue *d1 = stage1->applyTemplatesReturningValue();
1707 55b80284 O'Neil Delpratt
    if(d1 == nullptr) {
1708
        if (stage1->exceptionOccurred()) {
1709
            sresult->failure++;
1710
            sresult->failureList.push_back("testPipeline");
1711
            if (stage1->exceptionOccurred()) {
1712
                SaxonApiException *exception = stage1->getException();
1713
                if (exception != nullptr) {
1714
                    cerr << "Error: " << exception->getMessage() << endl;
1715
                    delete exception;
1716
                    exception = nullptr;
1717
                }
1718
            }
1719
            cout << "Stage d1 Error - exit method " << endl;
1720
            delete stage1;
1721
            delete stage2;
1722
            delete stage3;
1723
            delete stage4;
1724
            delete stage5;
1725
            delete trans;
1726
            delete inn;
1727
            return;
1728 4ee4355d O'Neil Delpratt
        }
1729 501beb26 O'Neil Delpratt
    }
1730
1731
    XdmItem *d11 = d1->getHead();
1732 9458d238 O'Neil Delpratt
    if (d11== nullptr) {
1733 72bf04c6 Norman Walsh
1734 501beb26 O'Neil Delpratt
        cout << "d11 is NULL\n" << endl;
1735 55b80284 O'Neil Delpratt
        sresult->failure++;
1736
        sresult->failureList.push_back("testPipeline-1");
1737
        delete stage1;
1738
        delete stage2;
1739
        delete stage3;
1740
        delete stage4;
1741
        delete stage5;
1742
        delete d1;
1743
        delete inn;
1744
        delete trans;
1745
        return;
1746 501beb26 O'Neil Delpratt
    }
1747 55b80284 O'Neil Delpratt
    const char *data = d11->toString();
1748 501beb26 O'Neil Delpratt
1749
    if (data != NULL) {
1750
        cout << "d1 result=" << data << endl;
1751
    } else {
1752 72bf04c6 Norman Walsh
1753 55b80284 O'Neil Delpratt
        cout << "d1 result Error - toString is NULL" << endl;
1754
        delete stage1;
1755
        delete stage2;
1756
        delete stage3;
1757
        delete stage4;
1758
        delete stage5;
1759
        delete d1;
1760
        delete inn;
1761
        delete trans;
1762 501beb26 O'Neil Delpratt
        return;
1763
    }
1764
    stage2->setProperty("!indent", "no");
1765 55b80284 O'Neil Delpratt
    stage2->setInitialMatchSelection(d11);
1766 501beb26 O'Neil Delpratt
    XdmValue *d2 = stage2->applyTemplatesReturningValue();
1767 9458d238 O'Neil Delpratt
    if (d2== nullptr) {
1768 501beb26 O'Neil Delpratt
        cout << "ERROR-11\n" << endl;
1769
        sresult->failure++;
1770
        sresult->failureList.push_back("testPipeline-2");
1771 55b80284 O'Neil Delpratt
        if (stage2->exceptionOccurred()) {
1772
            SaxonApiException *exception = stage2->getException();
1773
            if (exception != nullptr) {
1774
                cerr << "Error: " << exception->getMessage() << endl;
1775
                delete exception;
1776
                exception = nullptr;
1777
            }
1778
        }
1779
        cout << "Stage d2 Error - exit method " << endl;
1780
        delete d1;
1781
        delete inn;
1782
        delete stage1;
1783
        delete stage2;
1784
        delete stage3;
1785
        delete stage4;
1786
        delete stage5;
1787
        delete trans;
1788 501beb26 O'Neil Delpratt
        return;
1789
    }
1790
    stage3->setProperty("!indent", "no");
1791
    stage3->setInitialMatchSelection(d2);
1792
    XdmValue * d3 = stage3->applyTemplatesReturningValue();
1793
    if(d3 == nullptr){
1794
        sresult->failure++;
1795
        sresult->failureList.push_back("testPipeline-3");
1796 55b80284 O'Neil Delpratt
        if (stage3->exceptionOccurred()) {
1797
            SaxonApiException *exception = stage3->getException();
1798
            if (exception != nullptr) {
1799
                cerr << "Error: " << exception->getMessage() << endl;
1800
                delete exception;
1801
                exception = nullptr;
1802
            }
1803 901b6eca O'Neil Delpratt
        }
1804 55b80284 O'Neil Delpratt
        cout << "Stage d3 Error - exit method " << endl;
1805
        delete d1;
1806
        delete d2;
1807
        delete inn;
1808
1809
        delete stage1;
1810
        delete stage2;
1811
        delete stage3;
1812
        delete stage4;
1813
        delete stage5;
1814
        delete trans;
1815
        return;
1816 501beb26 O'Neil Delpratt
      }
1817
    stage4->setProperty("!indent", "no");
1818
    stage4->setInitialMatchSelection(d3);
1819
    XdmValue * d4 = stage4->applyTemplatesReturningValue();
1820 55b80284 O'Neil Delpratt
    if(d4== nullptr){
1821 501beb26 O'Neil Delpratt
        sresult->failure++;
1822 55b80284 O'Neil Delpratt
        sresult->failureList.push_back("testPipeline-4");
1823
        if (stage4->exceptionOccurred()) {
1824
            SaxonApiException *exception = stage4->getException();
1825
            if (exception != nullptr) {
1826
                cerr << "Error: " << exception->getMessage() << endl;
1827
                delete exception;
1828
                exception = nullptr;
1829
            }
1830 901b6eca O'Neil Delpratt
        }
1831 55b80284 O'Neil Delpratt
        cout << "Stage d4 Error - exit method " << endl;
1832
        delete d3;
1833
        delete d2;
1834
        delete d1;
1835
        delete inn;
1836
        delete stage1;
1837
        delete stage2;
1838
        delete stage3;
1839
        delete stage4;
1840
        delete stage5;
1841
        delete trans;
1842 501beb26 O'Neil Delpratt
        return;
1843
      }
1844
    stage5->setProperty("!indent", "no");
1845
    stage5->setInitialMatchSelection(d4);
1846
    const char * sw = stage5->applyTemplatesReturningString();
1847 55b80284 O'Neil Delpratt
    if(sw == nullptr){
1848 501beb26 O'Neil Delpratt
        sresult->failure++;
1849 55b80284 O'Neil Delpratt
        sresult->failureList.push_back("testPipeline-5");
1850
        if (stage5->exceptionOccurred()) {
1851
            SaxonApiException *exception = stage5->getException();
1852
            if (exception != nullptr) {
1853
                cerr << "Error: " << exception->getMessage() << endl;
1854
                delete exception;
1855
            }
1856 901b6eca O'Neil Delpratt
        }
1857 55b80284 O'Neil Delpratt
        cout << "Stage sw Error - exit method " << endl;
1858
        delete stage1;
1859
        delete stage2;
1860
        delete stage3;
1861
        delete stage4;
1862
        delete stage5;
1863
        delete trans;
1864
        delete d4;
1865
        delete d3;
1866
        delete d2;
1867
        delete d1;
1868
        delete inn;
1869 501beb26 O'Neil Delpratt
        return;
1870
      }
1871
    cout<<sw<<endl;
1872
    cout << "testPipeline = " << sw << endl;
1873
    sresult->success++;
1874 55b80284 O'Neil Delpratt
    delete stage1;
1875
    stage1 = nullptr;
1876
1877
    delete stage2;
1878
    stage2 = nullptr;
1879
1880
1881
    delete stage3;
1882
    stage3 = nullptr;
1883
1884
    delete stage4;
1885
    stage4 = nullptr;
1886
1887
    delete stage5;
1888
    stage5 = nullptr;
1889
1890
    delete trans;
1891
    trans = nullptr;
1892
1893
    delete sw;
1894
    sw = nullptr;
1895 501beb26 O'Neil Delpratt
1896 55b80284 O'Neil Delpratt
    delete d4;
1897
    d4 = nullptr;
1898
1899
    delete d3;
1900
    d3 = nullptr;
1901
1902
    delete d2;
1903
    d2 = nullptr;
1904
1905
    delete d1;
1906
    d1 = nullptr;
1907
1908
    delete inn;
1909
    inn = nullptr;
1910 501beb26 O'Neil Delpratt
1911
}
1912
1913 72bf04c6 Norman Walsh
1914 9458d238 O'Neil Delpratt
void testCatalog(const char * cwd, SaxonProcessor * proc, sResultCount *sresult) {
1915 72bf04c6 Norman Walsh
1916 00082f6f O'Neil Delpratt
1917 d0e6f8be O'Neil Delpratt
    cout << endl << "Test: testCatalog" << endl;
1918 9458d238 O'Neil Delpratt
    bool trace = false;
1919
    proc->setcwd(cwd);
1920 501beb26 O'Neil Delpratt
    proc->setCatalog("../php/catalog-test/catalog.xml", trace);
1921
1922 9458d238 O'Neil Delpratt
    Xslt30Processor * trans = proc->newXslt30Processor();
1923
1924 501beb26 O'Neil Delpratt
    XsltExecutable  * executable = trans->compileFromFile("../php/catalog-test/test1.xsl");
1925 901b6eca O'Neil Delpratt
1926
    if(executable == nullptr) {
1927 00082f6f O'Neil Delpratt
1928 901b6eca O'Neil Delpratt
        if (trans->exceptionOccurred()) {
1929 00082f6f O'Neil Delpratt
1930
            if(trans->getErrorMessage() != nullptr) {
1931
                const char *message = trans->getErrorMessage();
1932 4ee4355d O'Neil Delpratt
                cerr << "exception=" << message << endl;
1933 00082f6f O'Neil Delpratt
            } else {
1934
                const char *message = proc->getErrorMessage();
1935
                if(message != nullptr) {
1936 4ee4355d O'Neil Delpratt
                    cerr << "exception-proc=" << message << endl;
1937 00082f6f O'Neil Delpratt
                }
1938
            }
1939 4ee4355d O'Neil Delpratt
        }
1940
        sresult->failure++;
1941
        sresult->failureList.push_back("testCatalog");
1942
        trans->exceptionClear();
1943 55b80284 O'Neil Delpratt
        delete trans;
1944
        trans= nullptr;
1945 d0e6f8be O'Neil Delpratt
        return;
1946 901b6eca O'Neil Delpratt
    }
1947 a69dd173 O'Neil Delpratt
1948 501beb26 O'Neil Delpratt
    executable->setInitialMatchSelectionAsFile("../php/catalog-test/example.xml");
1949 4ee4355d O'Neil Delpratt
1950 501beb26 O'Neil Delpratt
    const char *result = executable->applyTemplatesReturningString();
1951
1952
    if(result != NULL) {
1953
        std::cerr << "testCatalog result= " << result << std::endl;
1954 55b80284 O'Neil Delpratt
        delete result;
1955
        sresult->success++; // TODO - check the results more carefully
1956
    } else {
1957
        sresult->failure++;
1958
        sresult->failureList.push_back("testCatalog");
1959 501beb26 O'Neil Delpratt
    }
1960 55b80284 O'Neil Delpratt
1961 4ee4355d O'Neil Delpratt
1962
    delete executable;
1963 55b80284 O'Neil Delpratt
    executable = nullptr;
1964
    delete trans;
1965
    trans = nullptr;
1966 501beb26 O'Neil Delpratt
1967
}
1968 72bf04c6 Norman Walsh
1969 306962bc O'Neil Delpratt
static int NUM_THREADS = 10;
1970 72bf04c6 Norman Walsh
1971 306962bc O'Neil Delpratt
void RunThread(XsltExecutable * executable, int tid, const std::string filename) {
1972 55b80284 O'Neil Delpratt
    JavaVMAttachArgs att_arg;
1973
    att_arg.version = JNI_VERSION_1_2;
1974
    att_arg.name = NULL;
1975
    att_arg.group = NULL;
1976
1977
   // SaxonProcessor::sxn_environ->jvm->AttachCurrentThread((void**)&SaxonProcessor::sxn_environ->env, &att_arg);
1978
    cerr<<endl<<"RunThread cp0,  THEAD ID="<<tid<<endl;
1979
    fflush(stderr);
1980
    fflush(stdout);
1981 72bf04c6 Norman Walsh
1982 55b80284 O'Neil Delpratt
    if(executable != nullptr) {
1983
       executable->setInitialMatchSelectionAsFile(filename.c_str());
1984 501beb26 O'Neil Delpratt
1985 55b80284 O'Neil Delpratt
        cerr << "RunThread cp1" << endl;
1986 306962bc O'Neil Delpratt
1987 55b80284 O'Neil Delpratt
       const char *result = nullptr;
1988
        result = executable->applyTemplatesReturningString();
1989
        if (result != nullptr) {
1990
            cout << " Result from THREAD ID: " << tid << ", " << result << endl;
1991
            delete result;
1992
        } else {
1993
            cerr << " ===== Failed in THREAD ID: " << tid << endl;
1994
           /* if(executable->exceptionOccurred()) {
1995
                SaxonApiException *exception = executable->getException();
1996

1997
                if (exception != nullptr) {
1998
                    const char *message = exception->getMessage();
1999
                    if (message != nullptr) {
2000
                        cerr << "Error = " << message << endl;
2001
                    }
2002
                    delete exception;
2003 306962bc O'Neil Delpratt
                }
2004 55b80284 O'Neil Delpratt
            } else {
2005
            cerr << "No exception found - result is nullptr" << endl;
2006
            }*/
2007 306962bc O'Neil Delpratt
2008 55b80284 O'Neil Delpratt
        }
2009
    delete executable;
2010
    } else {
2011
        cerr << "XsltExecutable is nullptr" << endl;
2012 306962bc O'Neil Delpratt
    }
2013 72bf04c6 Norman Walsh
2014 55b80284 O'Neil Delpratt
   // (   SaxonProcessor::sxn_environ->jvm)->DetachCurrentThread();
2015 72bf04c6 Norman Walsh
2016 306962bc O'Neil Delpratt
}
2017 72bf04c6 Norman Walsh
2018 306962bc O'Neil Delpratt
void testThreads (SaxonProcessor * processor, Xslt30Processor * trans, sResultCount *sresult) {
2019 501beb26 O'Neil Delpratt
2020 306962bc O'Neil Delpratt
    std::vector<std::string> s = {
2021
            "../data/xmark100k.xml",
2022
            "../data/xmark1.xml",
2023
            "../data/xmark4.xml",
2024
            "../data/xmark10.xml"
2025
    };
2026 501beb26 O'Neil Delpratt
2027 306962bc O'Neil Delpratt
    std::vector<std::thread> threads;
2028 501beb26 O'Neil Delpratt
2029 306962bc O'Neil Delpratt
    for (int i = 0; i < s.size(); i++) {
2030 55b80284 O'Neil Delpratt
2031 306962bc O'Neil Delpratt
        XsltExecutable * executable = trans->compileFromFile("q12.xsl");
2032
            threads.push_back(std::thread(RunThread, executable, i, s[i]));
2033 55b80284 O'Neil Delpratt
2034 501beb26 O'Neil Delpratt
    }
2035 306962bc O'Neil Delpratt
    cerr<<"testThreads cp0"<<endl;
2036
    for (auto &th : threads) {
2037
        th.join();
2038
        cerr<<"testThreads cp1 - loop"<<endl;
2039
    }
2040 501beb26 O'Neil Delpratt
}
2041
2042
2043
2044 e9b06a56 O'Neil Delpratt
int main(int argc, char* argv[]) {
2045
2046
2047
    const char * cwd = nullptr;
2048
    if(argc > 0) {
2049 4bed9808 O'Neil Delpratt
        cwd = argv[1];
2050 e9b06a56 O'Neil Delpratt
    }
2051 501beb26 O'Neil Delpratt
2052
    SaxonProcessor *processor = new SaxonProcessor(true);
2053 fc47eaa6 O'Neil Delpratt
    processor->setConfigurationProperty("http://saxon.sf.net/feature/licenseFileLocation", "/usr/local/lib/saxon-license.lic");
2054 501beb26 O'Neil Delpratt
    cout << "Test: Xslt30Processor with Saxon version=" << processor->version() << endl << endl;
2055 6b6a9706 O'Neil Delpratt
    
2056 e05bbe34 O'Neil Delpratt
    char buff[FILENAME_MAX]; //create string buffer to hold path
2057
    GetCurrentDir( buff, FILENAME_MAX );
2058 9458d238 O'Neil Delpratt
    cout<<"CWD = "<< buff<<endl;
2059 e9b06a56 O'Neil Delpratt
    if(cwd != nullptr) {
2060
            processor->setcwd(cwd);//"/Users/ond1/work/development/git/saxon-dev/saxondev/src/main/c/samples/cppTests"); //set to the current working directory
2061 306962bc O'Neil Delpratt
    } else {
2062
        processor->setcwd(buff);
2063 e9b06a56 O'Neil Delpratt
    }
2064 501beb26 O'Neil Delpratt
    if (processor->isSchemaAwareProcessor()) {
2065
2066
        std::cerr << "Processor is Schema Aware" << std::endl;
2067
    } else {
2068
        std::cerr << "Processor is not Schema Aware" << std::endl;
2069
    }
2070 e05bbe34 O'Neil Delpratt
2071 501beb26 O'Neil Delpratt
    sResultCount *sresult = new sResultCount();
2072
    Xslt30Processor *trans = processor->newXslt30Processor();
2073 8d5b64f3 O'Neil Delpratt
2074
    if( trans == nullptr) {
2075 dfaaba21 O'Neil Delpratt
        cout<< "Error creating Xslt30Processor"<<endl;
2076 8d5b64f3 O'Neil Delpratt
        if(processor->exceptionOccurred()) {
2077 dfaaba21 O'Neil Delpratt
            cout<< "Error message: " <<processor->getErrorMessage()<<endl;
2078 8d5b64f3 O'Neil Delpratt
2079
        }
2080 dfaaba21 O'Neil Delpratt
        return -1;
2081 8d5b64f3 O'Neil Delpratt
    }
2082 501beb26 O'Neil Delpratt
    //testValidation(trans,sresult);
2083 a69dd173 O'Neil Delpratt
2084 501beb26 O'Neil Delpratt
    testInitialTemplate(processor, trans, sresult);
2085 a69dd173 O'Neil Delpratt
2086 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2087 4ee4355d O'Neil Delpratt
2088 501beb26 O'Neil Delpratt
    exampleSimple1Err(trans, sresult);
2089 a69dd173 O'Neil Delpratt
2090 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2091
2092 72bf04c6 Norman Walsh
    exampleSimple1(trans, sresult);
2093 e05bbe34 O'Neil Delpratt
2094 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2095
2096 72bf04c6 Norman Walsh
    exampleSimple_xmark(trans, sresult);
2097 a69dd173 O'Neil Delpratt
2098 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2099
2100 72bf04c6 Norman Walsh
    exampleSimple2(trans, sresult);
2101 a69dd173 O'Neil Delpratt
2102 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2103
2104 72bf04c6 Norman Walsh
    exampleSimple3(processor, trans, sresult);
2105 a69dd173 O'Neil Delpratt
2106 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2107
2108 4ee4355d O'Neil Delpratt
    exampleSimple3aError(processor, trans, sresult);
2109 a69dd173 O'Neil Delpratt
2110 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2111
2112 72bf04c6 Norman Walsh
    testApplyTemplatesString1(trans, sresult);
2113 a69dd173 O'Neil Delpratt
2114 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2115
2116 72bf04c6 Norman Walsh
    testApplyTemplatesString2(processor, trans, sresult);
2117
2118 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2119
2120
2121 4ee4355d O'Neil Delpratt
    testApplyTemplates2a_Error(processor, trans, sresult);
2122 72bf04c6 Norman Walsh
2123 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2124
2125 72bf04c6 Norman Walsh
    testTransformToString4(processor, trans, sresult);
2126 00082f6f O'Neil Delpratt
2127 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2128
2129 9458d238 O'Neil Delpratt
    testCatalog(cwd, processor,  sresult);
2130 146cfacb O'Neil Delpratt
2131 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2132
2133 a69dd173 O'Neil Delpratt
    testTransformToString2b(processor, trans, sresult);
2134 72bf04c6 Norman Walsh
2135 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2136
2137 72bf04c6 Norman Walsh
    testTransformToString3(processor, trans, sresult);
2138 306962bc O'Neil Delpratt
2139
    cout<<endl<<"============================================================="<<endl<<endl;
2140 72bf04c6 Norman Walsh
        
2141
    testTransformFromstring(processor, trans, sresult);
2142
2143 55b80284 O'Neil Delpratt
2144 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2145
2146 a69dd173 O'Neil Delpratt
    testTransformFromstring2Err(processor, trans, sresult);
2147 72bf04c6 Norman Walsh
2148 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2149
2150 501beb26 O'Neil Delpratt
    testTrackingOfValueReference(processor, trans, sresult);
2151 72bf04c6 Norman Walsh
2152 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2153
2154
    testTrackingOfValueReferenceError(processor, trans, sresult);
2155
2156
    cout<<endl<<"============================================================="<<endl<<endl;
2157 72bf04c6 Norman Walsh
2158
    testPackage1(trans, sresult);
2159
2160 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2161
2162 4ee4355d O'Neil Delpratt
    testPackage1a(trans, sresult);
2163
2164 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2165
2166 4ee4355d O'Neil Delpratt
    testPackage2_Error(trans, sresult);
2167 72bf04c6 Norman Walsh
2168 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2169
2170 72bf04c6 Norman Walsh
    testXdmNodeOutput(trans, sresult);
2171
2172 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2173 72bf04c6 Norman Walsh
2174 306962bc O'Neil Delpratt
    exampleParam(processor, trans, sresult);
2175 4ee4355d O'Neil Delpratt
2176 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2177 4ee4355d O'Neil Delpratt
2178 72bf04c6 Norman Walsh
    xmarkTest1(trans, sresult);
2179
2180 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2181 4ee4355d O'Neil Delpratt
2182 306962bc O'Neil Delpratt
    xmarkTest2(trans, sresult);
2183 4ee4355d O'Neil Delpratt
2184 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2185 4ee4355d O'Neil Delpratt
2186 501beb26 O'Neil Delpratt
    testCallFunction(processor, trans, sresult);
2187 4ee4355d O'Neil Delpratt
2188 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2189 4ee4355d O'Neil Delpratt
2190 501beb26 O'Neil Delpratt
    testResolveUri(processor, trans, sresult);
2191 306962bc O'Neil Delpratt
2192
    cout<<endl<<"============================================================="<<endl<<endl;
2193
2194 72bf04c6 Norman Walsh
    testContextNotRoot(processor, trans, sresult);
2195 306962bc O'Neil Delpratt
2196
    cout<<endl<<"============================================================="<<endl<<endl;
2197
2198 501beb26 O'Neil Delpratt
    testContextNotRootNamedTemplate(processor, trans, sresult);
2199 306962bc O'Neil Delpratt
2200
    cout<<endl<<"============================================================="<<endl<<endl;
2201
2202 501beb26 O'Neil Delpratt
    testContextNotRootNamedTemplateValue(processor, trans, sresult);
2203 306962bc O'Neil Delpratt
2204
    cout<<endl<<"============================================================="<<endl<<endl;
2205
2206 55b80284 O'Neil Delpratt
   testPipeline(processor, sresult);
2207 72bf04c6 Norman Walsh
2208 306962bc O'Neil Delpratt
2209
    cout<<endl<<"============================================================="<<endl<<endl;
2210
2211 55b80284 O'Neil Delpratt
    fflush(stdout);
2212
2213 306962bc O'Neil Delpratt
2214 501beb26 O'Neil Delpratt
    //Available in PE and EE
2215
    //testTransformToStringExtensionFunc(processor, trans);
2216 72bf04c6 Norman Walsh
2217 55b80284 O'Neil Delpratt
2218
2219 a69dd173 O'Neil Delpratt
    //processor->release();
2220 55b80284 O'Neil Delpratt
    //return 0;
2221 72bf04c6 Norman Walsh
2222 55b80284 O'Neil Delpratt
    fflush(stdout);
2223 72bf04c6 Norman Walsh
2224
2225 55b80284 O'Neil Delpratt
    //set to the current working directory
2226 70e3c2b3 O'Neil Delpratt
    if(cwd != nullptr) {
2227 55b80284 O'Neil Delpratt
        processor->setcwd(cwd);
2228 70e3c2b3 O'Neil Delpratt
    } else {
2229
        processor->setcwd(buff);
2230
    }
2231 55b80284 O'Neil Delpratt
    Xslt30Processor *trans2 = processor->newXslt30Processor();
2232
    cerr<<" after trans2 cp0"<<endl;
2233 501beb26 O'Neil Delpratt
    testApplyTemplatesString1(trans2, sresult);
2234 306962bc O'Neil Delpratt
    cout<<endl<<"============================================================="<<endl<<endl;
2235
2236
2237
2238 55b80284 O'Neil Delpratt
    //testThreads (processor, trans2, sresult);
2239 306962bc O'Neil Delpratt
2240
2241 55b80284 O'Neil Delpratt
    delete trans;
2242 501beb26 O'Neil Delpratt
    delete trans2;
2243 55b80284 O'Neil Delpratt
    delete processor;
2244 01d6fdb6 O'Neil Delpratt
    processor->release();
2245 55b80284 O'Neil Delpratt
2246 72bf04c6 Norman Walsh
2247 306962bc O'Neil Delpratt
    cout<<endl<<"======================== Test Results ========================"<<endl<<endl;
2248
2249 501beb26 O'Neil Delpratt
    std::cout << "\nTest Results - Number of tests= " << (sresult->success + sresult->failure) << ", Successes = "
2250
              << sresult->success << ",  Failures= " << sresult->failure << std::endl;
2251 72bf04c6 Norman Walsh
2252 501beb26 O'Neil Delpratt
    std::list<std::string>::iterator it;
2253
    std::cout << "Failed tests:" << std::endl;
2254 306962bc O'Neil Delpratt
    // Make iterate point to beginning and increment it one by one until it reaches the end of list.
2255 501beb26 O'Neil Delpratt
    for (it = sresult->failureList.begin(); it != sresult->failureList.end(); it++) {
2256
        //Print the contents
2257
        std::cout << it->c_str() << std::endl;
2258 72bf04c6 Norman Walsh
2259 501beb26 O'Neil Delpratt
    }
2260 72bf04c6 Norman Walsh
2261 55b80284 O'Neil Delpratt
    delete sresult;
2262
2263
#ifdef MEM_DEBUG
2264
    SaxonProcessor::getInfo();
2265
#endif
2266 72bf04c6 Norman Walsh
2267
    return 0;
2268
}