Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XsltProcessor.cpp @ 55b80284

1
// XsltProcessor.cpp : Defines the exported functions for the DLL application.
2
//
3

    
4
//#include "xsltProcessor.cc"
5

    
6
#include "XsltProcessor.h"
7
#include "XdmValue.h"
8
#include "XdmItem.h"
9
#include "XdmNode.h"
10
#include "XdmAtomicValue.h"
11
#ifdef DEBUG
12
#include <typeinfo> //used for testing only
13
#endif
14

    
15
#ifdef MEM_DEBUG
16
#define new new(__FILE__, __LINE__)
17
#endif
18

    
19
XsltProcessor::XsltProcessor() {
20

    
21
        SaxonProcessor *p = new SaxonProcessor(false);
22
        XsltProcessor(p, "");
23

    
24
}
25

    
26
XsltProcessor::XsltProcessor(SaxonProcessor * p, std::string curr) {
27

    
28
        proc = p;
29

    
30
        /*
31
         * Look for class.
32
         */
33
        cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
34
                        "net/sf/saxon/option/cpp/XsltProcessor");
35

    
36
        cppXT = createSaxonProcessor2(SaxonProcessor::sxn_environ->env, cppClass,
37
                        "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
38

    
39
#ifdef DEBUG
40
        jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
41
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
42
    
43
#endif
44
        nodeCreated = false;
45
    jitCompilation = false;
46
        exception = nullptr;
47
        if(!(proc->cwd.empty()) && curr.empty()){
48
                cwdXT = proc->cwd;
49
        } else {
50
                cwdXT = curr;
51
        }
52

    
53
}
54

    
55
XsltProcessor::XsltProcessor(const XsltProcessor &other) {
56
        proc = other.proc;
57
        cppClass = other.cppClass;
58
        cppXT = other.cppXT;
59
        stylesheetObject = other.stylesheetObject;
60
    cwdXT = other.cwdXT;
61
        jitCompilation = other.jitCompilation;
62
        std::map<std::string, XdmValue*>::const_iterator paramIter = other.parameters.begin();
63
    while(paramIter != other.parameters.end())
64
    {
65

    
66
       XdmValue * valuei = paramIter->second;
67
       if(valuei == nullptr) {
68
                     //std::cerr<<"Error in XsltProcessor copy constructor"<<std::endl;
69
       } else {
70
            parameters[paramIter->first] = new XdmValue(*(valuei));
71
       }
72
       paramIter++;
73
    }
74

    
75
        std::map<std::string, std::string>::const_iterator propIter = other.properties.begin();
76
        while(propIter != other.properties.end())
77
    {
78
        properties[propIter->first] = propIter->second;
79
        propIter++;
80
    }
81

    
82
}
83

    
84

    
85
XsltProcessor * XsltProcessor::clone() {
86
     XsltProcessor * proc = new XsltProcessor(*this);
87
     return proc;
88
}
89

    
90

    
91
bool XsltProcessor::exceptionOccurred() {
92
        return proc->exceptionOccurred();
93
}
94

    
95
const char * XsltProcessor::getErrorCode() {
96
 if(exception == nullptr) {
97
    return nullptr;
98
 }
99
 return exception->getErrorCode();
100
}
101

    
102
void XsltProcessor::setSourceFromXdmNode(XdmNode * value) {
103
    if(value != nullptr){
104
      value->incrementRefCount();
105
      parameters["node"] = value;
106
    }
107
}
108

    
109
void XsltProcessor::setSourceFromFile(const char * ifile) {
110
        if(ifile != nullptr) {
111
                setProperty("s", ifile);
112
        }
113
}
114

    
115
void XsltProcessor::setOutputFile(const char * ofile) {
116
        setProperty("o", ofile);
117
}
118

    
119
void XsltProcessor::setBaseOutputURI(const char * baseURI) {
120
        if(baseURI != nullptr) {
121
              setProperty("baseoutput", baseURI);
122
        }
123
}
124

    
125
void XsltProcessor::setParameter(const char* name, XdmValue * value) {
126
        if(value != nullptr && name != nullptr){
127
                value->incrementRefCount();
128
                int s = parameters.size();
129
                std::string skey = "param:"+std::string(name);
130
                parameters[skey] = value;
131
                if(s == parameters.size()) {
132
            std::map<std::string, XdmValue*>::iterator it;
133
            it = parameters.find(skey);
134
            if (it != parameters.end()) {
135
                XdmValue * valuei = it->second;
136
                if(valuei != nullptr) {
137
                    valuei->decrementRefCount();
138
                    if (valuei->getRefCount() < 1) {
139
                        delete value;
140
                    }
141
                    parameters.erase(skey);
142
                    parameters[skey] = value;
143
                }
144
            }
145
                }
146
        } 
147
}
148

    
149
XdmValue* XsltProcessor::getParameter(const char* name) {
150
        std::map<std::string, XdmValue*>::iterator it;
151
        it = parameters.find("param:"+std::string(name));
152
        if (it != parameters.end())
153
          return it->second;
154
        return nullptr;
155
}
156

    
157
bool XsltProcessor::removeParameter(const char* name) {
158
        return (bool)(parameters.erase("param:"+std::string(name)));
159
}
160

    
161
void XsltProcessor::setJustInTimeCompilation(bool jit){
162
    jitCompilation = jit;
163
}
164

    
165
void XsltProcessor::setProperty(const char* name, const char* value) {        
166
        if(name != nullptr) {
167
            int s = properties.size();
168
                std::string skey = std::string(name);
169
                properties.insert(std::pair<std::string, std::string>(skey, std::string((value == nullptr ? "" : value))));
170

    
171
                if(s == properties.size()) {
172
            std::map<std::string, std::string>::iterator it;
173
            it = properties.find(skey);
174
            if (it != properties.end()) {
175
                properties.erase(skey);
176
                properties[skey] = std::string((value == nullptr ? "" : value));
177
            }
178
                }
179
        }
180
}
181

    
182
const char* XsltProcessor::getProperty(const char* name) {
183
        std::map<std::string, std::string>::iterator it;
184
        it = properties.find(std::string(name));
185
        if (it != properties.end())
186
          return it->second.c_str();
187
        return nullptr;
188
}
189

    
190
void XsltProcessor::clearParameters(bool delValues) {
191
        if(delValues){
192
                       for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++) {
193

    
194
                XdmValue *value = itr->second;
195
                if (value != nullptr) {
196
                    value->decrementRefCount();
197
#ifdef DEBUG
198
                    std::cout<<"clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
199
#endif
200
                    if (value->getRefCount() < 1) {
201
                        delete value;
202
                    }
203
                }
204
            }
205
                
206
        } else {
207
        for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
208
                
209
                        XdmValue * value = itr->second;
210
                        if(value != nullptr) {
211
                value->decrementRefCount();
212
            }
213
                
214
        }
215

    
216
        }
217
        parameters.clear();
218
}
219

    
220
void XsltProcessor::clearProperties() {
221
        properties.clear();
222
}
223

    
224

    
225

    
226
std::map<std::string,XdmValue*>& XsltProcessor::getParameters(){
227
        std::map<std::string,XdmValue*>& ptr = parameters;
228
        return ptr;
229
}
230

    
231
std::map<std::string,std::string>& XsltProcessor::getProperties(){
232
        std::map<std::string,std::string> &ptr = properties;
233
        return ptr;
234
}
235

    
236
void XsltProcessor::exceptionClear(){
237
 if(exception != nullptr) {
238
         delete exception;
239
         exception = nullptr;
240
        SaxonProcessor::sxn_environ->env->ExceptionClear();
241
 }
242
  
243
 }
244

    
245
   void XsltProcessor::setcwd(const char* dir){
246
    if(dir != nullptr) {
247
        cwdXT = std::string(dir);
248
    }
249
   }
250

    
251
const char* XsltProcessor::checkException() {
252
        /*if(proc->exception == nullptr) {
253
         proc->exception = proc->checkForException(environi, cpp);
254
         }
255
         return proc->exception;*/
256
        return proc->checkException();
257
}
258

    
259

    
260
    void XsltProcessor::compileFromXdmNodeAndSave(XdmNode * node, const char* filename) {
261
        static jmethodID cAndSNodemID =
262
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
263
                                        "compileFromXdmNodeAndSave",
264
                                        "(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;)V");
265
        if (!cAndSNodemID) {
266
                std::cerr << "Error: "<<getDllname() << ".compileFromStringAndSave"
267
                                << " not found\n" << std::endl;
268

    
269
        } else {
270

    
271
                
272
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndSNodemID,
273
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
274
                                                node->getUnderlyingValue(),                                                         SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
275
                
276
                exception = proc->checkAndCreateException(cppClass);
277

    
278
    }
279

    
280

    
281

    
282
}
283

    
284
    void XsltProcessor::compileFromStringAndSave(const char* stylesheetStr, const char* filename){
285
        static jmethodID cAndSStringmID =
286
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
287
                                        "compileFromStringAndSave",
288
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
289
        if (!cAndSStringmID) {
290
                std::cerr << "Error: "<<getDllname() << ".compileFromStringAndSave"
291
                                << " not found\n" << std::endl;
292

    
293
        } else {
294

    
295
                
296
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndSStringmID,
297
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
298
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetStr),                                                         SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
299
                
300
                exception = proc->checkAndCreateException(cppClass);
301

    
302
    }
303
}
304

    
305

    
306

    
307
    void XsltProcessor::compileFromFileAndSave(const char* xslFilename, const char* filename){
308
        static jmethodID cAndFStringmID =
309
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
310
                                        "compileFromFileAndSave",
311
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
312
        if (!cAndFStringmID) {
313
                std::cerr << "Error: "<<getDllname() << ".compileFromFileAndSave"
314
                                << " not found\n" << std::endl;
315

    
316
        } else {
317

    
318
                
319
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndFStringmID,
320
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
321
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(xslFilename),SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
322
                
323
                exception = proc->checkAndCreateException(cppClass);
324

    
325

    
326
     }
327
}
328

    
329
void XsltProcessor::compileFromString(const char* stylesheetStr) {
330
        static jmethodID cStringmID =
331
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
332
                                        "createStylesheetFromString",
333
                                        "(Ljava/lang/String;Ljava/lang/String;)Lnet/sf/saxon/s9api/XsltExecutable;");
334
        if (!cStringmID) {
335
                std::cerr << "Error: "<<getDllname() << "createStylesheetFromString"
336
                                << " not found\n" << std::endl;
337

    
338
        } else {
339
                releaseStylesheet();
340
            if(jitCompilation) {
341
                setProperty("jit:", "yes");
342
            } else {
343
            setProperty("jit:", "no");
344
            }
345
                stylesheetObject = (jobject)(
346
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cStringmID,
347
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
348
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetStr)));
349
                if (!stylesheetObject) {
350
                        exception = proc->checkAndCreateException(cppClass);
351
                }
352
        }
353

    
354
}
355

    
356
void XsltProcessor::compileFromXdmNode(XdmNode * node) {
357
        static jmethodID cNodemID =
358
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
359
                                        "createStylesheetFromFile",
360
                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XdmNode;)Lnet/sf/saxon/s9api/XsltExecutable;");
361
        if (!cNodemID) {
362
                std::cerr << "Error: "<< getDllname() << "createStylesheetFromXdmNode"
363
                                << " not found\n" << std::endl;
364

    
365
        } else {
366
                releaseStylesheet();
367
            if(jitCompilation) {
368
                setProperty("jit:", "yes");
369
            } else {
370
            setProperty("jit:", "no");
371
            }
372
                stylesheetObject = (jobject)(
373
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cNodemID,
374
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
375
                                                node->getUnderlyingValue()));
376
                if (!stylesheetObject) {
377
                        exception = proc->checkAndCreateException(cppClass);
378
                        //cout << "Error in compileFromXdmNode" << endl;
379
                }
380
        }
381

    
382
}
383

    
384
void XsltProcessor::compileFromFile(const char* stylesheet) {
385
        static jmethodID cFilemID =
386
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
387
                                        "createStylesheetFromFile",
388
                                        "(Ljava/lang/String;Ljava/lang/String;)Lnet/sf/saxon/s9api/XsltExecutable;");
389
        if (!cFilemID) {
390
                std::cerr << "Error: "<<getDllname() << "createStylesheetFromFile"
391
                                << " not found\n" << std::endl;
392

    
393
        } else {
394
                releaseStylesheet();
395

    
396
                stylesheetObject = (jobject)(
397
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cFilemID,
398
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
399
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet)));
400
                if (!stylesheetObject) {
401
                        exception = proc->checkAndCreateException(cppClass);
402
                     
403
                }
404
                //SaxonProcessor::sxn_environ->env->NewGlobalRef(stylesheetObject);
405
        }
406

    
407
}
408

    
409
void XsltProcessor::releaseStylesheet() {
410

    
411
        stylesheetObject = nullptr;
412
        
413
}
414

    
415

    
416

    
417
XdmValue * XsltProcessor::transformFileToValue(const char* sourcefile,
418
                const char* stylesheetfile) {
419

    
420
        if(exceptionOccurred()) {
421
                //Possible error detected in the compile phase. Processor not in a clean state.
422
                //Require clearing exception.
423
                return nullptr;
424
        }
425

    
426
        if(sourcefile == nullptr && stylesheetfile == nullptr && !stylesheetObject){
427
        
428
                return nullptr;
429
        }
430

    
431

    
432
        static jmethodID mID =
433
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
434
                                        "transformToNode",
435
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmNode;");
436
        if (!mID) {
437
                std::cerr << "Error: "<< getDllname() << "transformtoNode" << " not found\n"
438
                                << std::endl;
439

    
440
        } else {
441
                jobjectArray stringArray = nullptr;
442
                jobjectArray objectArray = nullptr;
443
                jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
444
                                "java/lang/Object");
445
                jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
446
                                "java/lang/String");
447

    
448
                int size = parameters.size() + properties.size();
449
                if (size > 0) {
450
                        objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
451
                                        objectClass, 0);
452
                        stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
453
                                        stringClass, 0);
454
                        int i = 0;
455
                        for (std::map<std::string, XdmValue*>::iterator iter =
456
                                        parameters.begin(); iter != parameters.end(); ++iter, i++) {
457
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
458
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
459
                                                                (iter->first).c_str()));
460
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
461
                                                (iter->second)->getUnderlyingValue());
462
                        }
463
                        for (std::map<std::string, std::string>::iterator iter =
464
                                        properties.begin(); iter != properties.end(); ++iter, i++) {
465
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
466
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
467
                                                                (iter->first).c_str()));
468
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
469
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
470
                                                                (iter->second).c_str()));
471
                        }
472
                }
473
                jobject result = (jobject)(
474
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, mID,
475
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
476
                                                (sourcefile != nullptr ?
477
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(sourcefile) :
478
                                                                nullptr),
479
                                                (stylesheetfile != nullptr ?
480
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
481
                                                                                stylesheetfile) :
482
                                                                nullptr), stringArray, objectArray));
483
                if (size > 0) {
484
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
485
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
486
                }
487
                if (result) {
488
                        XdmNode * node = new XdmNode(result);
489
                        XdmValue * value = new XdmValue();
490
                        value->addXdmItem(node);
491
                        return value;
492
                }else {
493
        
494
                        exception = proc->checkAndCreateException(cppClass);
495
                           
496
                     }
497
        }
498
        return nullptr;
499

    
500
}
501

    
502

    
503
void XsltProcessor::transformFileToFile(const char* source,
504
                const char* stylesheet, const char* outputfile) {
505

    
506
        if(exceptionOccurred()) {
507
                //Possible error detected in the compile phase. Processor not in a clean state.
508
                //Require clearing exception.
509
                return;        
510
        }
511
        if(source == nullptr && outputfile == nullptr && !stylesheetObject){
512
                
513
                return;
514
        }
515

    
516
        jmethodID mID =
517
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
518
                                        "transformToFile",
519
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
520
        if (!mID) {
521
                std::cerr << "Error: "<<getDllname() << "transformToFile" << " not found\n"
522
                                << std::endl;
523

    
524
        } else {
525
                jobjectArray stringArray = nullptr;
526
                jobjectArray objectArray = nullptr;
527
                jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
528
                                "java/lang/Object");
529
                jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
530
                                "java/lang/String");
531

    
532
                int size = parameters.size() + properties.size();
533
#ifdef DEBUG
534
                std::cerr<<"Properties size: "<<properties.size()<<std::endl;
535
                std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
536
                std::cerr<<"size:"<<size<<std::endl;
537
#endif
538
                if (size > 0) {
539
                        objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
540
                                        objectClass, 0);
541
                        stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
542
                                        stringClass, 0);
543
                
544
                        int i = 0;
545
                        for (std::map<std::string, XdmValue*>::iterator iter =
546
                                        parameters.begin(); iter != parameters.end(); ++iter, i++) {
547

    
548
#ifdef DEBUG
549
                                std::cerr<<"map 1"<<std::endl;
550
                                std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
551
#endif
552
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
553
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
554
                                                                (iter->first).c_str()));
555
#ifdef DEBUG
556
                                std::string s1 = typeid(iter->second).name();
557
                                std::cerr<<"Type of itr:"<<s1<<std::endl;
558
                                jobject xx = (iter->second)->getUnderlyingValue();
559
                                if(xx == nullptr) {
560
                                        std::cerr<<"value failed"<<std::endl;
561
                                } else {
562

    
563
                                        std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
564
                                }
565
                                if((iter->second)->getUnderlyingValue() == nullptr) {
566
                                        std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
567
                                }
568
#endif
569
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
570
                                                (iter->second)->getUnderlyingValue());
571

    
572
                        }
573

    
574
                        for (std::map<std::string, std::string>::iterator iter =
575
                                        properties.begin(); iter != properties.end(); ++iter, i++) {
576
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
577
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
578
                                                                (iter->first).c_str()));
579
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
580
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
581
                                                                (iter->second).c_str()));
582
                        }
583
                }
584
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, mID,
585
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
586
                                                                (source != nullptr ?
587
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
588
                                                                                                source) :
589
                                                                                nullptr),
590
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet),nullptr,
591
                                                                stringArray, objectArray);
592
                if (size > 0) {
593
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
594
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
595
                }
596
                
597
        }
598

    
599
        proc->checkAndCreateException(cppClass);
600
}
601

    
602
void XsltProcessor::setupXslMessage(bool show, const char* filename) {
603
    if(show) {
604
        if(filename == nullptr) {
605
            setProperty("m", "on");
606
        } else {
607
            setProperty("m", filename);
608
        }
609
    } else {
610
        setProperty("m", "off");
611
    }
612
}
613

    
614

    
615
const char * XsltProcessor::transformFileToString(const char* source,
616
                const char* stylesheet) {
617

    
618
        if(exceptionOccurred()) {
619
                //Possible error detected in the compile phase. Processor not in a clean state.
620
                //Require clearing exception.
621
                return nullptr;
622
        }
623
        if(source == nullptr && stylesheet == nullptr && !stylesheetObject){
624
                std::cerr<< "Error: The most recent StylesheetObject failed. Please check exceptions"<<std::endl;
625
                return nullptr;
626
        }
627

    
628
        jmethodID mID =
629
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
630
                                        "transformToString",
631
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
632
        if (!mID) {
633
                std::cerr << "Error: "<<getDllname() << "transformFileToString" << " not found\n"
634
                                << std::endl;
635

    
636
        } else {
637
                jobjectArray stringArray = nullptr;
638
                jobjectArray objectArray = nullptr;
639
                jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
640
                                "java/lang/Object");
641
                jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
642
                                "java/lang/String");
643

    
644
                int size = parameters.size() + properties.size();
645
#ifdef DEBUG
646
                std::cerr<<"Properties size: "<<properties.size()<<std::endl;
647
                std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
648
#endif
649
                if (size > 0) {
650
                        objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
651
                                        objectClass, 0);
652
                        stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
653
                                        stringClass, 0);
654
                        int i = 0;
655
                        for (std::map<std::string, XdmValue*>::iterator iter =
656
                                        parameters.begin(); iter != parameters.end(); ++iter, i++) {
657

    
658
#ifdef DEBUG
659
                                std::cerr<<"map 1"<<std::endl;
660
                                std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
661
#endif
662
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
663
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
664
                                                                (iter->first).c_str()));
665
#ifdef DEBUG
666
                                std::string s1 = typeid(iter->second).name();
667
                                std::cerr<<"Type of itr:"<<s1<<std::endl;
668

    
669

    
670
                                jobject xx = (iter->second)->getUnderlyingValue();
671

    
672
                                if(xx == nullptr) {
673
                                        std::cerr<<"value failed"<<std::endl;
674
                                } else {
675

    
676
                                        std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
677
                                }
678
                                if((iter->second)->getUnderlyingValue() == nullptr) {
679
                                        std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
680
                                }
681
#endif
682

    
683
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
684
                                                (iter->second)->getUnderlyingValue());
685

    
686
                        }
687

    
688
                        for (std::map<std::string, std::string>::iterator iter =
689
                                        properties.begin(); iter != properties.end(); ++iter, i++) {
690
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
691
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
692
                                                                (iter->first).c_str()));
693
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
694
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
695
                                                                (iter->second).c_str()));
696
                        }
697
                }
698

    
699
        jstring result = nullptr;
700
        jobject obj =
701
                                (
702
                                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, mID,
703
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
704
                                                                (source != nullptr ?
705
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
706
                                                                                                source) :
707
                                                                                nullptr),
708
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet),
709
                                                                stringArray, objectArray));
710
                if(obj) {
711
                        result = (jstring)obj;
712
                }                
713
                if (size > 0) {
714
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
715
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
716
                }
717
                if (result) {
718
                        const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
719
                                        nullptr);
720
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
721
                        return str;
722
                } else  {
723
                        exception = proc->checkAndCreateException(cppClass);
724
                           
725
                     }
726
        }
727
        return nullptr;
728
}
729

    
730

    
731
   const char * XsltProcessor::transformToString(){
732
        if(!stylesheetObject){
733
                std::cerr<< "Error: No styleheet found. Please compile stylsheet before calling transformToString or check exceptions"<<std::endl;
734
                return nullptr;
735
        }
736
        return transformFileToString(nullptr, nullptr);
737
   }
738

    
739

    
740
    XdmValue * XsltProcessor::transformToValue(){
741
        if(!stylesheetObject){
742
                std::cerr<< "Error: No styleheet found. Please compile stylsheet before calling transformToValue or check exceptions"<<std::endl;
743
                return nullptr;
744
        }
745
        return transformFileToValue(nullptr, nullptr);
746
   }
747

    
748
    void XsltProcessor::transformToFile(){
749
        if(!stylesheetObject){
750
                std::cerr<< "Error: No styleheet found. Please compile stylsheet before calling transformToFile or check exceptions"<<std::endl;
751
                return;
752
        }
753
        transformFileToFile(nullptr, nullptr, nullptr);
754
   }
755

    
756
const char * XsltProcessor::getErrorMessage(){
757
         if(exception == nullptr) {
758
             return nullptr;
759
         }
760
         return exception->getMessage();
761
 }
762

    
763
SaxonApiException* XsltProcessor::getException() {
764
    return exception;
765
}
766

    
(42-42/56)