Project

Profile

Help

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

he / latest9.6 / hec / samples / cppTests / testXSLT.cpp @ 579a56da

1
#include <sstream>
2

    
3
#include "../../Saxon.C.API/SaxonProcessor.h"
4
#include "../../Saxon.C.API/XdmValue.h"
5
#include "../../Saxon.C.API/XdmItem.h"
6
#include "../../Saxon.C.API/XdmNode.h"
7
#include <string>
8

    
9
// TODO: write test case for checking parameters which are null
10

    
11

    
12
using namespace std;
13

    
14

    
15
/*
16
* Test transform to String. Source and stylesheet supplied as arguments
17
*/
18
void testTransformToString1(SaxonProcessor * processor, XsltProcessor * trans){
19
        
20
  cout<<endl<<"Test: TransformToString1:"<<endl;
21

    
22
    const char * output = trans->transformFileToString("cat.xml", "test.xsl");
23
   if(output == NULL) {
24
      printf("result is null \n");
25

    
26
    }else {
27
      printf("%s", output);
28
      printf("result is OK \n");
29
    }
30
      fflush(stdout);
31
        delete output;
32

    
33
}
34

    
35

    
36
/*
37
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
38
*/
39
void testTransformToString2(SaxonProcessor * processor, XsltProcessor * trans){
40

    
41
  cout<<endl<<"Test: TransformToString2:"<<endl;
42
  trans->clearParameters(true);
43
  trans->clearProperties();
44
    XdmNode * input = processor->parseXmlFromFile("cat.xml");
45

    
46
   if(input == NULL) {
47
        cout<<"Source document is null."<<endl;
48

    
49
    }
50

    
51
     trans->setSourceFromXdmValue((XdmItem*)input);
52
    const char * output = trans->transformFileToString(NULL, "test.xsl");
53
   if(output == NULL) {
54
      printf("result is null \n");
55

    
56
    }else {
57
      printf("%s", output);
58
      printf("result is OK \n");
59
    }
60
      fflush(stdout);
61
    delete output;
62
        
63
        
64

    
65
}
66

    
67
/*
68
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
69
Should be error. Stylesheet file does not exist
70
*/
71
void testTransformToString2a(SaxonProcessor * processor, XsltProcessor * trans){
72

    
73
  cout<<endl<<"Test: TransformToString2a:"<<endl;
74
  trans->clearParameters(true);
75
  trans->clearProperties();
76
    XdmNode * input = processor->parseXmlFromFile("cat.xml");
77

    
78
   if(input == NULL) {
79
        cout<<"Source document is null."<<endl;
80

    
81
    }
82

    
83
     trans->setSourceFromXdmValue((XdmItem*)input);
84
    const char * output = trans->transformFileToString(NULL, "test-error.xsl");
85
   if(output == NULL) {
86
      printf("result is null \n");
87

    
88
    }else {
89
      printf("%s", output);
90
      printf("result is OK \n");
91
    }
92
      fflush(stdout);
93
    delete output;
94
        
95
        
96

    
97
}
98

    
99
/*
100
* Test transform to String. stylesheet supplied as argument. Source supplied as XdmNode
101
Should be error. Source file does not exist
102
*/
103
void testTransformToString2b(SaxonProcessor * processor, XsltProcessor * trans){
104

    
105
  cout<<endl<<"Test: TransformToString2b:"<<endl;
106
  trans->clearParameters(true);
107
  trans->clearProperties();
108
    XdmNode * input = processor->parseXmlFromFile("cat-error.xml");
109

    
110
   if(input == NULL) {
111
        cout<<"Source document is null."<<endl;
112

    
113
    }
114

    
115
     trans->setSourceFromXdmValue((XdmItem*)input);
116
    const char * output = trans->transformFileToString(NULL, "test-error.xsl");
117
   if(output == NULL) {
118
      printf("result is null \nCheck For errors:");
119
      if(trans->exceptionCount()>0) {
120
        cout<<trans->getErrorMessage(0)<<endl;
121
      }        
122
    }else {
123
      printf("%s", output);
124
      printf("result is OK \n");
125
    }
126
      fflush(stdout);
127
    delete output;
128
        
129
    trans->exceptionClear();
130

    
131
}
132

    
133

    
134
/*
135
* Test transform to String. stylesheet supplied as argument. Source supplied as xml string
136
and integer parmater created and supplied
137
*/
138
void testTransformToString3(SaxonProcessor * processor, XsltProcessor * trans){
139

    
140
  cout<<endl<<"Test: TransformToString3:"<<endl;
141
  trans->clearParameters(true);
142
  trans->clearProperties();
143
    XdmNode * inputi = processor->parseXmlFromString("<out><person>text1</person><person>text2</person><person>text3</person></out>");
144

    
145
   XdmAtomicValue * value1 = processor->makeIntegerValue(10);
146

    
147
   trans->setParameter("numParam",(XdmValue *)value1);
148

    
149
   if(inputi == NULL) {
150
        cout<<"Source document inputi is null."<<endl;
151

    
152
    }
153

    
154
    trans->setSourceFromXdmValue((XdmItem*)inputi);
155
    const char * output = trans->transformFileToString(NULL, "test.xsl");
156
   if(output == NULL) {
157
      printf("result is null \n");
158

    
159
    }else {
160
      printf("%s", output);
161
      printf("result is OK \n");
162
    }
163
      fflush(stdout);
164
   delete output;
165

    
166
}
167

    
168
/*
169
* Test transform to String. stylesheet supplied as argument. Source supplied as xml string
170
and integer parmater created and supplied
171
*/
172
void testTransformToString4(SaxonProcessor * processor, XsltProcessor * trans){
173

    
174
  cout<<endl<<"Test: TransformToString4:"<<endl;
175
  trans->clearParameters(true);
176
  trans->clearProperties();
177
    XdmNode * input = processor->parseXmlFromString("<out><person>text1</person><person>text2</person><person>text3</person></out>");
178

    
179
   XdmValue * values = new XdmValue(processor);
180
   values->addXdmItem((XdmItem*)processor->makeIntegerValue(10));
181
  values->addXdmItem((XdmItem*)processor->makeIntegerValue(5));
182
  values->addXdmItem((XdmItem*)processor->makeIntegerValue(6));
183
  values->addXdmItem((XdmItem*)processor->makeIntegerValue(7));
184
   
185
   trans->setParameter("values",(XdmValue *)values);
186

    
187
   if(input == NULL) {
188
        cout<<"Source document is null."<<endl;
189

    
190
    }
191

    
192
    trans->setSourceFromXdmValue((XdmItem*)input);
193
    const char * output = trans->transformFileToString(NULL, "test2.xsl");
194
   if(output == NULL) {
195
      printf("result is null \n");
196

    
197
    }else {
198
      printf("%s", output);
199
      printf("result is OK \n");
200
    }
201
      fflush(stdout);
202
    delete output;
203

    
204
}
205

    
206
void testTransfromFromstring(SaxonProcessor * processor, XsltProcessor * trans){
207
cout<<endl<<"Test: testTransfromFromstring:"<<endl;
208
  trans->clearParameters(true);
209
  trans->clearProperties();
210
    XdmNode * input = processor->parseXmlFromString("<out><person>text1</person><person>text2</person><person>text3</person></out>");
211

    
212
   trans->compileFromString("<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>");
213
trans->setSourceFromXdmValue((XdmItem*)input);
214
 const char * output = trans->transformToString();
215
   if(output == NULL) {
216
      printf("result is null \n");
217

    
218
    }else {
219
      printf("%s", output);
220
      printf("result is OK \n");
221
    }
222
      fflush(stdout);
223
    delete output;
224

    
225

    
226
}
227

    
228
//Test case has error in the stylesheet
229
void testTransfromFromstring2Err(SaxonProcessor * processor, XsltProcessor * trans){
230
cout<<endl<<"Test: testTransfromFromstring2-Error:"<<endl;
231
  trans->clearParameters(true);
232
  trans->clearProperties();
233
    XdmNode * input = processor->parseXmlFromString("<out><person>text1</person><person>text2</person><person>text3</person></out>");
234

    
235
   trans->compileFromString("<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>");
236
trans->setSourceFromXdmValue((XdmItem*)input);
237
 const char * output = trans->transformToString();
238
   if(output == NULL) {
239
      printf("result is null \nCheck For errors:\n");
240
      if(trans->exceptionCount()>0) {
241
        cout<<"Error count="<<trans->exceptionCount()<<", "<<trans->getErrorMessage(0)<<endl;
242
      }        
243
    }else {
244
      printf("%s", output);
245
      printf("result is OK \n");
246
    }
247
      fflush(stdout);
248
    delete output;
249

    
250
   trans->exceptionClear();
251

    
252

    
253
}
254

    
255
void testTrackingOfValueReference(SaxonProcessor * processor, XsltProcessor * trans){
256
  trans->clearParameters(true);
257
  trans->clearProperties();
258
 cout<<endl<<"Test: TrackingOfValueReference:"<<endl;
259
ostringstream test;
260
ostringstream valStr;
261
ostringstream name;
262
        for(int i=0; i<10; i++){
263
                test<<"v"<<i;
264
                valStr<<"<out><person>text1</person><person>text2</person><person>text3</person><value>"<<test.str()<<"</value></out>";
265
                name<<"value"<<i;
266
                
267
                XdmValue * values = (XdmValue*)processor->parseXmlFromString(valStr.str().c_str());
268
                //cout<<"Name:"<<name.str()<<", Value:"<<values->getHead()->getStringValue(processor)<<endl;
269
                trans->setParameter(name.str().c_str(), values);
270
                test.str("");
271
                valStr.str("");
272
                name.str("");
273
        }
274
        
275
        std::map<std::string,XdmValue*> parMap = trans->getParameters();
276
        if(parMap.size()>0) {
277
cout<<"Parameter size: "<<parMap.size()<<endl;
278
        cout<<"Parameter size: "<<parMap.size()<<endl;//", Value:"<<trans->getParameters()["value0"]->getHead()->getStringValue(processor)<<endl;
279
ostringstream name1;
280
        for(int i =0; i < 10;i++){
281
                name1<<"param:value"<<i;
282
                cout<<" i:"<<i<<" Map size:"<<parMap.size()<<", ";
283
                XdmValue * valuei = parMap[name1.str()];
284
                if(valuei != NULL ){
285
                        cout<<name1.str();
286
                        if(valuei->itemAt(0) != NULL)
287
                                cout<<"= "<<valuei->itemAt(0)->getStringValue(processor);
288
                        cout<<endl;
289
                }
290
                name1.str("");
291
        }
292
        }
293
}
294

    
295
/*Test case should be error.*/
296
void testTrackingOfValueReferenceError(SaxonProcessor * processor, XsltProcessor * trans){
297
  trans->clearParameters(true);
298
  trans->clearProperties();
299
 cout<<endl<<"Test: TrackingOfValueReference-Error:"<<endl;
300
cout<<"Parameter Map size: "<<(trans->getParameters().size())<<endl;
301
ostringstream test;
302
ostringstream valStr;
303
ostringstream name;
304
        for(int i=0; i<10; i++){
305
                test<<"v"<<i;
306
                valStr<<"<out><person>text1</person><person>text2</person><person>text3</person><value>"<<test.str()<<"<value></out>";
307
                name<<"value"<<i;
308
                
309
                XdmValue * values = (XdmValue*)processor->parseXmlFromString(valStr.str().c_str());
310
                trans->setParameter(name.str().c_str(), values);
311
                test.str("");
312
                valStr.str("");
313
                name.str("");
314
        }
315
        std::map<std::string,XdmValue*> parMap = trans->getParameters();
316
cout<<"Parameter Map size: "<<parMap.size()<<endl;
317
        
318
ostringstream name1;
319
        for(int i =0; i < 10;i++){
320
                name1<<"param:value"<<i;
321
                cout<<" i:"<<i<<" Map size:"<<parMap.size()<<", ";
322
                 try {
323
                XdmValue * valuei = parMap.at(name1.str());
324
                if(valuei != NULL ){
325
                        cout<<name1.str();
326
                        if(valuei->itemAt(0) != NULL)
327
                                cout<<"= "<<valuei->itemAt(0)->getStringValue(processor);
328
                        cout<<endl;
329
                }
330
                } catch(const std::out_of_range& oor) {
331
                        cout<< "Out of range exception occurred. Exception no. "<<endl;        
332
                }
333
                name1.str("");
334
        }
335
}
336

    
337

    
338
void testXdmNodeOutput(XsltProcessor * trans){
339
 trans->clearParameters(true);
340
  trans->clearProperties();
341

    
342

    
343
  trans->compileFromString("<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template name='go'><a/></xsl:template></xsl:stylesheet>");
344
            trans->setProperty("it","go");
345
            XdmNode * root = (XdmNode*)(trans->transformToValue());
346
             if(root == NULL) {  
347
                             
348
                        cout<<"Result is null"<<endl;
349
                        return;
350
                } 
351
               if(root->getNodeKind() == DOCUMENT){
352
                   cout<<"Result is a Document"<<endl;
353
                } else {
354
                        cout<<"Node is of kind:"<<root->getNodeKind()<<endl;
355
                }
356

    
357
  trans->clearProperties();
358

    
359
}
360

    
361
void exampleSimple1(XsltProcessor  *proc){
362
                cout<<"ExampleSimple1 taken from PHP:"<<endl;
363
                proc->setSourceFromFile("../php/trax/xml/foo.xml");
364
                proc->compileFromFile("../php/trax/xsl/foo.xsl");
365
                        
366
                const char *result = proc->transformToString();               
367
                if(result != NULL) {               
368
                        cout<<result<<endl;
369
                } else {
370
                        cout<<"Result is null"<<endl;
371
                }
372
                proc->clearParameters(true);
373
                proc->clearProperties();            
374
            }
375

    
376
void exampleSimple1Err(XsltProcessor  *proc){
377
                cout<<"ExampleSimple1 taken from PHP:"<<endl;
378
                proc->setSourceFromFile("cat.xml");
379
                proc->compileFromFile("err.xsl");
380
                        
381
                const char *result = proc->transformToString();               
382
                if(result != NULL) {               
383
                        cout<<result<<endl;
384
                } else {
385
                        cout<<"Result is null"<<endl;
386
                }
387
                proc->clearParameters(true);
388
                proc->clearProperties();            
389
            }
390

    
391
int exists(const char *fname)
392
{
393
    FILE *file;
394
    if (file = fopen(fname, "r"))
395
    {
396
        fclose(file);
397
        return 1;
398
    }
399
    return 0;
400
}
401

    
402

    
403
  void exampleSimple2(XsltProcessor  *proc){
404
                cout<<"<b>exampleSimple2:</b><br/>"<<endl;
405
                proc->setSourceFromFile("../php/trax/xml/foo.xml");
406
                proc->compileFromFile("../php/trax/xsl/foo.xsl");
407
                const char * filename = "output1.xml";
408
                proc->setOutputFile(filename);
409
                proc->transformToFile();
410
                                
411
                if (exists(filename)) {
412
                    cout<<"The file $filename exists"<<endl;;
413
                } else {
414
                    cout<<"The file $filename does not exist"<<endl;
415
                }
416
                       proc->clearParameters(true);
417
                proc->clearProperties();
418
            }
419

    
420
void exampleSimple3(SaxonProcessor * saxonProc, XsltProcessor  *proc){
421
                cout<<"<b>exampleSimple3:</b><br/>"<<endl;
422
                //proc->clearParameters(true);
423
                //proc->clearProperties();
424
                XdmNode * xdmNode = saxonProc->parseXmlFromString("<doc><b>text value of out</b></doc>");
425
                if(xdmNode == NULL) {
426
                        cout<<"xdmNode is null'"<<endl;
427
                        return;        
428
                }            
429
                proc->setSourceFromXdmValue((XdmItem*)xdmNode);
430
                cout<<"end of exampleSimple3"<<endl;
431
                proc->clearParameters(true);
432
                proc->clearProperties();
433
}
434

    
435
void exampleParam(SaxonProcessor * saxonProc, XsltProcessor  *proc){
436
                cout<< "\nExampleParam:</b><br/>"<<endl;
437
                proc->setSourceFromFile("../php/trax/xml/foo.xml");
438
                proc->compileFromFile("../php/trax/xsl/foo.xsl");
439
            
440
                XdmAtomicValue * xdmvalue = saxonProc->makeStringValue("Hello to you");
441
                if(xdmvalue !=NULL){
442
                        
443
                        proc->setParameter("a-param", (XdmValue*)xdmvalue);
444
                } else {
445
                        cout<< "Xdmvalue is null"<<endl;
446
                }
447
                const char * result = proc->transformToString();
448
                if(result != NULL) {                
449
                        cout<<"Output:"<<result<<endl;
450
                } else {
451
                        cout<<"Result is NULL<br/>"<<endl;
452
                }
453
               
454
                //proc->clearParameters();                
455
                //unset($result);
456
                //echo 'again with a no parameter value<br/>';
457
                
458
                proc->setProperty("!indent", "yes"); 
459
                const char *result2 = proc->transformToString();
460
               
461
                proc->clearProperties();
462
                if(result2 != NULL) {                
463
                        cout<<result2<<endl;
464
                }
465
               
466
              //  unset($result);
467
               // echo 'again with no parameter and no properties value set. This should fail as no contextItem set<br/>';
468
                XdmAtomicValue * xdmValue2 = saxonProc->makeStringValue("goodbye to you");
469
                proc->setParameter("a-param", (XdmValue*)xdmValue2);
470
                
471
                const char *result3 = proc->transformToString();   
472
                if(result3 != NULL) {             
473
                        cout<<"Output ="<<result3<<endl;
474
                } else {
475
                        cout<<"Error in result"<<endl;
476
                }
477
                proc->clearParameters();
478
                proc->clearProperties(); 
479
                        
480
            }
481

    
482

    
483

    
484
int main()
485
{
486

    
487
    SaxonProcessor * processor = new SaxonProcessor(true);
488
    cout<<"Test: XsltProcessor with Saxon version="<<processor->version()<<endl<<endl; 
489
    //processor->setcwd("/home");
490
    processor->setConfigurationProperty("http://saxon.sf.net/feature/generateByteCode", "off");
491

    
492
    XsltProcessor * trans = processor->newXsltProcessor();
493
    exampleSimple1Err(trans);
494
   exampleSimple1(trans);
495
    exampleSimple2(trans);
496
    exampleSimple3(processor, trans);
497

    
498
    testTransformToString1(processor, trans);
499

    
500
    testTransformToString2(processor, trans);
501

    
502
    testTransformToString2a(processor, trans);
503

    
504
    testTransformToString2b(processor, trans);
505

    
506
    testTransformToString3(processor, trans);
507
        
508
    testTransformToString4(processor, trans);
509

    
510
    testTransfromFromstring(processor, trans);
511

    
512
    testTransfromFromstring2Err(processor, trans);
513

    
514
    testTrackingOfValueReference(processor, trans);
515

    
516
    testTrackingOfValueReferenceError(processor, trans);
517

    
518
    testXdmNodeOutput(trans);
519

    
520
    exampleParam(processor, trans);
521

    
522
    delete trans;
523
     processor->release();
524
    return 0;
525
}
(13-13/13)