Project

Profile

Help

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

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

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

    
4
#include "Xslt30Processor.h"
5
#include "XdmValue.h"
6
#include "XdmItem.h"
7
#include "XdmNode.h"
8
#include "XdmAtomicValue.h"
9
#include "XdmFunctionItem.h"
10
#include "XdmMap.h"
11
#include "XdmArray.h"
12
#include "XsltExecutable.h"
13
#ifdef DEBUG
14
#include <typeinfo> //used for testing only
15
#endif
16

    
17
#ifdef MEM_DEBUG
18
#define new new(__FILE__, __LINE__)
19
#endif
20

    
21
Xslt30Processor::Xslt30Processor() {
22

    
23
        SaxonProcessor *p = new SaxonProcessor(false);
24
        Xslt30Processor(p, "");
25

    
26
}
27

    
28
Xslt30Processor::Xslt30Processor(SaxonProcessor * p, std::string curr) {
29

    
30
        proc = p;
31
    tunnel = false;
32
    jitCompilation = false;
33
    exception = nullptr;
34

    
35
        /*
36
         * Look for class.
37
         */
38
        cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
39
                        "net/sf/saxon/option/cpp/Xslt30Processor");
40

    
41
        cppXT = createSaxonProcessor2(SaxonProcessor::sxn_environ->env, cppClass,
42
                        "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
43

    
44
#ifdef DEBUG
45
        jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
46
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
47
    
48
#endif
49

    
50
        if(cppXT == nullptr) {
51
        createException();
52
        }
53

    
54
        if(!(proc->cwd.empty()) && curr.empty()){
55
                cwdXT = proc->cwd;
56
        } else if(!curr.empty()){
57
                cwdXT = curr;
58
        }
59
}
60

    
61
    Xslt30Processor::~Xslt30Processor(){
62
            clearParameters();
63
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(cppXT);
64
            cwdXT.erase();
65
            exceptionClear();
66
    }
67

    
68

    
69
Xslt30Processor::Xslt30Processor(const Xslt30Processor &other) {
70
        proc = other.proc;
71
        cppClass = other.cppClass;
72
        cppXT = other.cppXT;
73
    cwdXT = other.cwdXT;
74
        tunnel = other.tunnel;
75

    
76
        std::map<std::string, XdmValue*>::const_iterator paramIter = other.parameters.begin();
77
    while(paramIter != other.parameters.end())
78
    {
79

    
80
       XdmValue * valuei = paramIter->second;
81
       if(valuei == nullptr) {
82
                     //std::cerr<<"Error in Xslt30Processor copy constructor"<<std::endl;
83
       } else {
84
            parameters[paramIter->first] = new XdmValue(*(valuei));
85
       }
86
       paramIter++;
87
    }
88

    
89
        jitCompilation = other.jitCompilation;
90

    
91
}
92

    
93

    
94

    
95

    
96
bool Xslt30Processor::exceptionOccurred() {
97
        return proc->exceptionOccurred() || exception != nullptr;
98
}
99

    
100
const char * Xslt30Processor::getErrorCode() {
101
 if(exception == nullptr) {return nullptr;}
102
 return exception->getErrorCode();
103
 }
104

    
105

    
106
void Xslt30Processor::setParameter(const char* name, XdmValue * value) {
107
        if(value != nullptr && name != nullptr){
108
                value->incrementRefCount();
109
                int s = parameters.size();
110
                std::string skey = ("sparam:"+std::string(name));
111
                parameters[skey] = value;
112
                if(s == parameters.size()) {
113
            std::map<std::string, XdmValue*>::iterator it;
114
            it = parameters.find(skey);
115
            if (it != parameters.end()) {
116
                XdmValue * valuei = it->second;
117
                if(valuei != nullptr) {
118
                    valuei->decrementRefCount();
119
                    //if (valuei->getRefCount() < 1) {
120
                        delete value;
121
                        value = nullptr;
122
                    //}
123
                    parameters.erase(skey);
124
                    parameters[skey] = value;
125
                    value->incrementRefCount();
126
                }
127
            }
128
                }
129
         }
130
}
131

    
132
XdmValue* Xslt30Processor::getParameter(const char* name) {
133
        std::map<std::string, XdmValue*>::iterator it;
134
        it = parameters.find("sparam:"+std::string(name));
135
        if (it != parameters.end())
136
          return it->second;
137
            return nullptr;
138
}
139

    
140
bool Xslt30Processor::removeParameter(const char* name) {
141
        return (bool)(parameters.erase("param:"+std::string(name)));
142
}
143

    
144
void Xslt30Processor::setJustInTimeCompilation(bool jit){
145
    jitCompilation = jit;
146
}
147

    
148

    
149
void Xslt30Processor::clearParameters(bool delValues) {
150
        if(delValues){
151
        for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
152
            XdmValue * value = itr->second;
153
            if(value != nullptr) {
154
#ifdef DEBUG
155
                std::cout<<"clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
156
#endif
157
                    delete value;
158
                    value = nullptr;
159

    
160
            }
161
        }
162

    
163
        }
164
        parameters.clear();
165
        
166
}
167

    
168

    
169

    
170
std::map<std::string,XdmValue*>& Xslt30Processor::getParameters(){
171
        std::map<std::string,XdmValue*>& ptr = parameters;
172
        return ptr;
173
}
174

    
175

    
176
void Xslt30Processor::exceptionClear(){
177
 if(exception != nullptr) {
178
         delete exception;
179
         exception = nullptr;
180
        SaxonProcessor::sxn_environ->env->ExceptionClear();
181
 }
182
  
183
 }
184

    
185
   void Xslt30Processor::setcwd(const char* dir){
186
    if (dir!= nullptr) {
187
        cwdXT = std::string(dir);
188
    }
189
   }
190

    
191
SaxonApiException* Xslt30Processor::getException() {
192
    return exception;
193
}
194

    
195
void Xslt30Processor::createException(const char * message) {
196
    exceptionClear();
197
    if(message == nullptr) {
198
        exception = proc->checkAndCreateException(cppClass);
199
    } else {
200
        exception = new SaxonApiException(message);
201
    }
202

    
203
}
204

    
205

    
206

    
207
void Xslt30Processor::compileFromXdmNodeAndSave(XdmNode * node, const char* filename) {
208
        static jmethodID cAndSNodemID = nullptr;
209

    
210
        if(cAndSNodemID == nullptr) {
211
                        cAndSNodemID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
212
                                        "compileFromXdmNodeAndSave",
213
                                        "(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;)V");
214
        }
215
        if (!cAndSNodemID) {
216
                std::cerr<<"Error: "<<getDllname()<<"compileFromStringAndSave function not found"<<std::endl;
217
        SaxonProcessor::sxn_environ->env->ExceptionClear();
218
        } else {
219
        if(cppXT == nullptr) {
220
#if defined(DEBUG)
221
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
222
#endif
223
            return;
224
        }
225
                
226
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndSNodemID,
227
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
228
                                                node->getUnderlyingValue(), SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
229
                
230
                createException();
231

    
232
    }
233

    
234

    
235

    
236
}
237

    
238
    void Xslt30Processor::compileFromStringAndSave(const char* stylesheetStr, const char* filename){
239
        static jmethodID cAndSStringmID = nullptr;
240
        if(cAndSStringmID == nullptr) {
241
           cAndSStringmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
242
                                        "compileFromStringAndSave",
243
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
244
        }
245
        if (!cAndSStringmID) {
246
        createException("Error: compileFromStringAndSave function not found\n");
247

    
248
        } else {
249
        if(cppXT == nullptr) {
250
#if defined(DEBUG)
251
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
252
#endif
253
            return;
254
        }
255
                
256
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndSStringmID,
257
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
258
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetStr),
259
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
260
                
261
                createException();
262

    
263
    }
264
}
265

    
266

    
267

    
268
    void Xslt30Processor::compileFromFileAndSave(const char* xslFilename, const char* filename){
269
        static jmethodID cAndFStringmID =  nullptr;
270

    
271
        if (cAndFStringmID == nullptr) {
272
            cAndFStringmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
273
                                        "compileFromFileAndSave",
274
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
275
        }
276
        if (!cAndFStringmID) {
277
        createException("Error: compileFromFileAndSave function not found");
278

    
279
        } else {
280

    
281
        if(cppXT == nullptr) {
282
#if defined(DEBUG)
283
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
284
#endif
285
            return;
286
        }
287
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndFStringmID,
288
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
289
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(xslFilename),SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
290

    
291
        createException();
292

    
293

    
294
     }
295
}
296

    
297
XsltExecutable * Xslt30Processor::compileFromString(const char* stylesheetStr) {
298
        static jmethodID cStringmID = nullptr;
299
        if (cStringmID == nullptr) {
300
                        cStringmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
301
                                        "compileFromString",
302
                                        "(Ljava/lang/String;Ljava/lang/String;Z[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
303
        }
304
                                        
305
        if (cStringmID== nullptr) {
306
        createException(strcat(getDllname(), ".compileFromString funtion not found"));
307
                return nullptr;
308

    
309
        } else {
310
        if(cppXT == nullptr) {
311
#if defined(DEBUG)
312
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
313
#endif
314
            return nullptr;
315
        }
316
                JParameters comboArrays;
317
                comboArrays = SaxonProcessor::createParameterJArray2(parameters);
318
                jobject executableObject = (jobject)(
319
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cStringmID,
320
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
321
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetStr), jitCompilation, comboArrays.stringArray, comboArrays.objectArray));
322
                if (!executableObject) {
323
            createException();
324
                        return nullptr;
325
                }
326
                if (comboArrays.stringArray != nullptr) {
327
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
328
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
329
                }
330

    
331
                jobject obj = SaxonProcessor::sxn_environ->env->NewGlobalRef(executableObject);
332
        XsltExecutable * executable = new XsltExecutable(obj, cwdXT);
333
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(executableObject);
334
                return executable;
335
        }
336

    
337
}
338

    
339
XsltExecutable * Xslt30Processor::compileFromXdmNode(XdmNode * node) {
340
        static jmethodID cNodemID = nullptr;
341
    if(cNodemID == nullptr) {
342
                        cNodemID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,"compileFromXdmNode",
343
                        "(Ljava/lang/String;Ljava/lang/Object;Z[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
344
        }
345
        if (!cNodemID) {
346
                std::cerr << "Error: "<< getDllname() << ".compileFromXdmNode"
347
                                << " not found\n" << std::endl;
348
                return nullptr;
349

    
350
        } else {
351
            if(node == nullptr) {
352
            createException("Error in compileFromXdmNode - node argument is NULL");
353
                return nullptr;
354
            }
355
        if(cppXT == nullptr) {
356
#if defined(DEBUG)
357
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
358
#endif
359
            return nullptr;
360
        }
361
                JParameters comboArrays;
362
                comboArrays = SaxonProcessor::createParameterJArray2(parameters);
363
                jobject executableObject = (jobject)(
364
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cNodemID,
365
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
366
                                                node->getUnderlyingValue(), jitCompilation, comboArrays.stringArray, comboArrays.objectArray));
367
                if (!executableObject) {
368
                        createException();
369
                        return nullptr;
370
                }
371
                if (comboArrays.stringArray != nullptr) {
372
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
373
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
374
                }
375

    
376
        XsltExecutable * executable = new XsltExecutable(SaxonProcessor::sxn_environ->env->NewGlobalRef(executableObject));
377
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(executableObject);
378
                return executable;
379
        }
380

    
381
}
382

    
383
XsltExecutable * Xslt30Processor::compileFromAssociatedFile(const char* source) {
384
        static jmethodID cFilemID = nullptr;
385
    if(cFilemID == nullptr) {
386
            cFilemID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
387
                                        "compileFromAssociatedFile",
388
                                        "(Ljava/lang/String;Ljava/lang/String;Z;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
389
        }
390
        if (!cFilemID) {
391
                std::cerr << "Error: "<<getDllname() << ".compileFromAssociatedFile"
392
                                << " not found\n" << std::endl;
393
        SaxonProcessor::sxn_environ->env->ExceptionClear();
394
                return nullptr;
395

    
396
        } else {
397
                
398
                if(source == nullptr) {
399
            createException("Error in compileFromFile method - The Stylesheet file is NULL");
400
                        return nullptr;
401
                }
402
        if(cppXT == nullptr) {
403
#if defined(DEBUG)
404
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
405
#endif
406
            return nullptr;
407
        }
408
                JParameters comboArrays;
409
                comboArrays = SaxonProcessor::createParameterJArray2(parameters);
410
                jobject executableObject = (jobject)(
411
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cFilemID,
412
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
413
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(source), jitCompilation, comboArrays.stringArray, comboArrays.objectArray));
414
                if (!executableObject) {
415
                        createException();
416
                     return nullptr;
417
                }
418
                if (comboArrays.stringArray != nullptr) {
419
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
420
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
421
                }
422

    
423
        XsltExecutable * executable = new XsltExecutable(SaxonProcessor::sxn_environ->env->NewGlobalRef(executableObject));
424
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(executableObject);
425
                return executable;
426
        }
427

    
428
}
429

    
430

    
431
XsltExecutable * Xslt30Processor::compileFromFile(const char* stylesheet) {
432
        static jmethodID cFilemID = nullptr;
433
        if(cFilemID == nullptr) {
434
            cFilemID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
435
                                        "compileFromFile",
436
                                        "(Ljava/lang/String;Ljava/lang/String;Z[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
437
        }
438

    
439
        if (!cFilemID) {
440
                std::cerr << "Error: "<<getDllname() << ".compileFromFile"
441
                                << " not found\n" << std::endl;
442
        SaxonProcessor::sxn_environ->env->ExceptionClear();
443
                return nullptr;
444

    
445
        } else {
446
        if (stylesheet == nullptr) {
447
            createException("The Stylesheet file is NULL");
448
            return nullptr;
449
        }
450
        if(cppXT == nullptr) {
451
#if defined(DEBUG)
452
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
453
#endif
454

    
455
            return nullptr;
456
        }
457

    
458
        JParameters comboArrays;
459
        comboArrays = SaxonProcessor::createParameterJArray2(parameters);
460
        jobject executableObject = (jobject) (
461
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cFilemID,
462
                                                                   SaxonProcessor::sxn_environ->env->NewStringUTF(
463
                                                                           cwdXT.c_str()),
464
                                                                   SaxonProcessor::sxn_environ->env->NewStringUTF(
465
                                                                           stylesheet), jitCompilation,
466
                                                                   comboArrays.stringArray, comboArrays.objectArray));
467
        if (!executableObject) {
468
            createException();
469
            return nullptr;
470

    
471
        }
472
        if (comboArrays.stringArray != nullptr) {
473
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
474
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
475
        }
476

    
477
        XsltExecutable *executable;
478
        executable = new XsltExecutable(SaxonProcessor::sxn_environ->env->NewGlobalRef(executableObject), cwdXT);
479
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(executableObject);
480

    
481
        return executable;
482

    
483
        }
484

    
485
}
486

    
487

    
488

    
489

    
490
XdmValue * Xslt30Processor::transformFileToValue(const char* sourcefile,
491
                const char* stylesheetfile) {
492

    
493
        if(exceptionOccurred()) {
494
                //Possible error detected in the compile phase. Processor not in a clean state.
495
                //Require clearing exception.
496
                SaxonProcessor::sxn_environ->env->ExceptionDescribe();
497

    
498
                return nullptr;
499
        }
500

    
501
        if(sourcefile == nullptr && stylesheetfile == nullptr){
502
        
503
                return nullptr;
504
        }
505

    
506

    
507
        static jmethodID mtvID = nullptr;
508

    
509
        if(mtvID == nullptr) {
510
                        mtvID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
511
                                        "transformToValue",
512
                                        "(Ljava/lang/String;Lnet/sf/saxon/option/cpp/Xslt30Processor;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
513
        }
514
        if (!mtvID) {
515
        std::cerr<<"Error: "<<getDllname()<<".transformtoValue function not found"<<std::endl;
516
        SaxonProcessor::sxn_environ->env->ExceptionClear();
517
        } else {
518
        if(cppXT == nullptr) {
519
//#if defined(DEBUG)
520
            createException("The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown");
521
//#endif
522
            return nullptr;
523
        }
524
            JParameters comboArrays;
525
                comboArrays = SaxonProcessor::createParameterJArray2(parameters);
526
        jstring jcwd = SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str());
527
        jstring jsourcefile = (sourcefile != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(sourcefile) : nullptr);
528
        jstring jstylesheetfile = (stylesheetfile != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetfile) : nullptr);
529
                jobject result = (jobject)(
530
                                SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, mtvID,jcwd, cppXT, nullptr, jsourcefile,
531
                                                                            jstylesheetfile, comboArrays.stringArray, comboArrays.objectArray));
532
                if (comboArrays.stringArray != nullptr) {
533
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
534
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
535
                }
536
        if (jcwd != nullptr) {
537
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(jcwd);
538
        }
539
        if (jsourcefile != nullptr) {
540
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(jsourcefile);
541
        }
542
        if (jstylesheetfile != nullptr) {
543
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(jstylesheetfile);
544
        }
545
                if (result) {
546

    
547
                        jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
548
                          jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
549
                          jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
550
                jclass mapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
551
                jclass arrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmArray");
552
                            XdmValue * value = nullptr;
553
                          XdmItem * xdmItem = nullptr;
554

    
555

    
556
                                  if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
557
                                          xdmItem = new XdmAtomicValue(result);
558
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
559
                        return xdmItem;
560

    
561
                                  } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
562
                                          xdmItem = new XdmNode(result);
563
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
564
                        return xdmItem;
565

    
566
                                  } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
567
                        xdmItem =  new XdmFunctionItem(result);
568
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
569
                        return xdmItem;
570

    
571
                                  } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, mapClass)           == JNI_TRUE) {
572
                        xdmItem =  new XdmMap(result);
573
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
574
                        return xdmItem;
575

    
576
                    } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, arrayClass)           == JNI_TRUE) {
577
                        xdmItem =  new XdmArray(result);
578
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
579
                        return xdmItem;
580

    
581
                    } else {
582
                                        value = new XdmValue(result, true);
583
                                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
584
                                        return value;
585
                                }
586
                                value = new XdmValue();
587
                      value->addUnderlyingValue(result);
588

    
589
                          SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
590
                          return value;
591
                }else {
592

    
593
                        createException();
594

    
595
                     }
596
        }
597
        return nullptr;
598

    
599
}
600

    
601

    
602
void Xslt30Processor::transformFileToFile(const char* source,
603
                const char* stylesheet, const char* outputfile) {
604

    
605
        if(exceptionOccurred()) {
606
                //Possible error detected in the compile phase. Processor not in a clean state.
607
                //Require clearing exception.
608
                return;        
609
        }
610
        if(stylesheet==nullptr){
611
                std::cerr<< "Error: stylesheet has not been set."<<std::endl;
612
                return;
613
        }
614
        //
615
        static jmethodID mtfID = nullptr;
616

    
617
        if(mtfID == nullptr) {
618
                mtfID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
619
                                        "transformToFile",
620
                                        "(Ljava/lang/String;Lnet/sf/saxon/option/cpp/Xslt30Processor;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
621
        }
622
        if (!mtfID) {
623
        std::cerr<<"Error: "<<getDllname()<<".transformToFile function not found"<<std::endl;
624
        SaxonProcessor::sxn_environ->env->ExceptionClear();
625
        } else {
626
        if(cppXT == nullptr) {
627
#if defined(DEBUG)
628
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
629
#endif
630
            return;
631
        }
632
            JParameters comboArrays;
633
        comboArrays = SaxonProcessor::createParameterJArray2(parameters);
634

    
635
                SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, mtfID,
636
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()), cppXT, nullptr,
637
                                                                (source != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(source) : nullptr),
638
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet),        (outputfile != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(outputfile) :nullptr),
639
                                                                comboArrays.stringArray, comboArrays.objectArray);
640
                if (comboArrays.stringArray!= nullptr) {
641
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
642
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
643
                }
644
                }
645
                createException();
646
        }
647

    
648

    
649
const char * Xslt30Processor::transformFileToString(const char* source,
650
                const char* stylesheet) {
651

    
652
        if(exceptionOccurred()) {
653
                //Possible error detected in the compile phase. Processor not in a clean state.
654
                //Require clearing exception.
655
                return nullptr;
656
        }
657
        if(source == nullptr && stylesheet == nullptr){
658
                std::cerr<< "Error: nullptr file name found in transformFiletoString."<<std::endl;
659
                return nullptr;
660
        }
661

    
662
        static jmethodID mtsID =  nullptr;
663

    
664
        if(mtsID == nullptr) {
665
                        mtsID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
666
                                        "transformToString",
667
                                        "(Ljava/lang/String;Lnet/sf/saxon/option/cpp/Xslt30Processor;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)[B");
668
        }
669
        if (!mtsID) {
670
        std::cerr<<"Error: "<<getDllname()<<".transformFileToString not found"<<std::endl;
671
        SaxonProcessor::sxn_environ->env->ExceptionClear();
672
        } else {
673
        if(cppXT == nullptr) {
674
#if defined(DEBUG)
675
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
676
#endif
677
            return nullptr;
678
        }
679
    JParameters comboArrays;
680
    comboArrays = SaxonProcessor::createParameterJArray2(parameters);
681

    
682
    jbyteArray result = nullptr;
683
        jobject obj = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, mtsID,
684
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()), cppXT, nullptr,
685
                                                (source != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(
686
                                                                                                source) : nullptr),
687
                                                                (stylesheet != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet) : nullptr),
688
                                                                comboArrays.stringArray, comboArrays.objectArray);
689
    if (comboArrays.stringArray!= nullptr) {
690
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
691
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
692
    }
693
        if(obj) {
694
                        result = (jbyteArray)obj;
695
        }
696

    
697
        if (result) {
698
        jboolean isCopy = false;
699
        jbyte* b = SaxonProcessor::sxn_environ->env->GetByteArrayElements(result, &isCopy);
700
        jsize num_bytes = SaxonProcessor::sxn_environ->env->GetArrayLength(result);
701

    
702
        const char *str = new char[num_bytes];
703
        memcpy ((void *) str, b , num_bytes );
704

    
705
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
706
        return str;
707
        } else  {
708
            createException();
709
                           
710
        }
711
        }
712
        return nullptr;
713
}
714

    
715

    
716

    
717
const char * Xslt30Processor::getErrorMessage(){
718
         if(exception == nullptr) {return nullptr;}
719
         return exception->getMessage();
720
 }
721

    
(38-38/56)