Project

Profile

Help

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

he / src / main / c / Saxon.C.API / Xslt30Processor.cpp @ 7728ae0b

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
        jobject tempcppXT = createSaxonProcessor2(SaxonProcessor::sxn_environ->env, cppClass,
42
                        "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
43
        if(tempcppXT) {
44
        cppXT = SaxonProcessor::sxn_environ->env->NewGlobalRef(tempcppXT);
45
    } else {
46
            createException("Error: Failed to create the Xslt30Processor internal object");
47

    
48
        }
49

    
50
#ifdef DEBUG
51
        jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
52
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
53
    
54
#endif
55

    
56
        if(cppXT == nullptr) {
57
        createException();
58
        }
59

    
60
        if(!(proc->cwd.empty()) && curr.empty()){
61
                cwdXT = proc->cwd;
62
        } else if(!curr.empty()){
63
                cwdXT = curr;
64
        }
65
}
66

    
67
    Xslt30Processor::~Xslt30Processor(){
68
            clearParameters();
69
            SaxonProcessor::sxn_environ->env->DeleteGlobalRef(cppXT);
70
            cwdXT.erase();
71
            exceptionClear();
72
    }
73

    
74

    
75
Xslt30Processor::Xslt30Processor(const Xslt30Processor &other) {
76
        proc = other.proc;
77
        cppClass = other.cppClass;
78
        cppXT = other.cppXT;
79
    cwdXT = other.cwdXT;
80
        tunnel = other.tunnel;
81

    
82
        std::map<std::string, XdmValue*>::const_iterator paramIter = other.parameters.begin();
83
    while(paramIter != other.parameters.end())
84
    {
85

    
86
       XdmValue * valuei = paramIter->second;
87
       if(valuei == nullptr) {
88
                     //std::cerr<<"Error in Xslt30Processor copy constructor"<<std::endl;
89
       } else {
90
            parameters[paramIter->first] = new XdmValue(*(valuei));
91
       }
92
       paramIter++;
93
    }
94

    
95
        jitCompilation = other.jitCompilation;
96

    
97
}
98

    
99

    
100

    
101

    
102
bool Xslt30Processor::exceptionOccurred() {
103
        return proc->exceptionOccurred() || exception != nullptr;
104
}
105

    
106
const char * Xslt30Processor::getErrorCode() {
107
 if(exception == nullptr) {return nullptr;}
108
 return exception->getErrorCode();
109
 }
110

    
111

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

    
138
XdmValue* Xslt30Processor::getParameter(const char* name) {
139
        std::map<std::string, XdmValue*>::iterator it;
140
        it = parameters.find("sparam:"+std::string(name));
141
        if (it != parameters.end())
142
          return it->second;
143
            return nullptr;
144
}
145

    
146
bool Xslt30Processor::removeParameter(const char* name) {
147
        return (bool)(parameters.erase("param:"+std::string(name)));
148
}
149

    
150
void Xslt30Processor::setJustInTimeCompilation(bool jit){
151
    jitCompilation = jit;
152
}
153

    
154

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

    
166
            }
167
        }
168

    
169
        }
170
        parameters.clear();
171
        
172
}
173

    
174

    
175

    
176
std::map<std::string,XdmValue*>& Xslt30Processor::getParameters(){
177
        std::map<std::string,XdmValue*>& ptr = parameters;
178
        return ptr;
179
}
180

    
181

    
182
void Xslt30Processor::exceptionClear(){
183
 if(exception != nullptr) {
184
         delete exception;
185
         exception = nullptr;
186
        SaxonProcessor::sxn_environ->env->ExceptionClear();
187
 }
188
  
189
 }
190

    
191
   void Xslt30Processor::setcwd(const char* dir){
192
    if (dir!= nullptr) {
193
        cwdXT = std::string(dir);
194
    }
195
   }
196

    
197
SaxonApiException* Xslt30Processor::getException() {
198
    return exception;
199
}
200

    
201
void Xslt30Processor::createException(const char * message) {
202
    if(exception != nullptr) {
203
        delete exception;
204
        exception = nullptr;
205
    }
206
    if(message == nullptr) {
207
        exception = proc->checkAndCreateException(cppClass);
208
    } else {
209
        exception = new SaxonApiException(message);
210
    }
211

    
212
}
213

    
214

    
215

    
216
void Xslt30Processor::compileFromXdmNodeAndSave(XdmNode * node, const char* filename) {
217
        static jmethodID cAndSNodemID = nullptr;
218

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

    
241
    }
242

    
243

    
244

    
245
}
246

    
247
    void Xslt30Processor::compileFromStringAndSave(const char* stylesheetStr, const char* filename){
248
        static jmethodID cAndSStringmID = nullptr;
249
        if(cAndSStringmID == nullptr) {
250
           cAndSStringmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
251
                                        "compileFromStringAndSave",
252
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
253
        }
254
        if (!cAndSStringmID) {
255
        createException("Error: compileFromStringAndSave function not found\n");
256

    
257
        } else {
258
        if(cppXT == nullptr) {
259
#if defined(DEBUG)
260
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
261
#endif
262
            return;
263
        }
264
                
265
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndSStringmID,
266
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
267
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetStr),
268
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
269
                
270
                createException();
271

    
272
    }
273
}
274

    
275

    
276

    
277
    void Xslt30Processor::compileFromFileAndSave(const char* xslFilename, const char* filename){
278
        static jmethodID cAndFStringmID =  nullptr;
279

    
280
        if (cAndFStringmID == nullptr) {
281
            cAndFStringmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
282
                                        "compileFromFileAndSave",
283
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
284
        }
285
        if (!cAndFStringmID) {
286
        createException("Error: compileFromFileAndSave function not found");
287

    
288
        } else {
289

    
290
        if(cppXT == nullptr) {
291
#if defined(DEBUG)
292
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
293
#endif
294
            return;
295
        }
296
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndFStringmID,
297
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
298
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(xslFilename),SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
299

    
300
        createException();
301

    
302

    
303
     }
304
}
305

    
306
XsltExecutable * Xslt30Processor::compileFromString(const char* stylesheetStr) {
307
        static jmethodID cStringmID = nullptr;
308
        if (cStringmID == nullptr) {
309
                        cStringmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
310
                                        "compileFromString",
311
                                        "(Ljava/lang/String;Ljava/lang/String;Z[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
312
        }
313
                                        
314
        if (cStringmID== nullptr) {
315
        createException(strcat(getDllname(), ".compileFromString funtion not found"));
316
                return nullptr;
317

    
318
        } else {
319
        if(cppXT == nullptr) {
320
#if defined(DEBUG)
321
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
322
#endif
323
            return nullptr;
324
        }
325
                JParameters comboArrays;
326
                comboArrays = SaxonProcessor::createParameterJArray2(parameters);
327
                jobject executableObject = (jobject)(
328
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cStringmID,
329
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
330
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetStr), jitCompilation, comboArrays.stringArray, comboArrays.objectArray));
331
                if (!executableObject) {
332
            createException();
333
                        return nullptr;
334
                }
335
                if (comboArrays.stringArray != nullptr) {
336
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
337
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
338
                }
339

    
340
                jobject obj = SaxonProcessor::sxn_environ->env->NewGlobalRef(executableObject);
341
        XsltExecutable * executable = new XsltExecutable(obj, cwdXT);
342
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(executableObject);
343
                return executable;
344
        }
345

    
346
}
347

    
348
XsltExecutable * Xslt30Processor::compileFromXdmNode(XdmNode * node) {
349
        static jmethodID cNodemID = nullptr;
350
    if(cNodemID == nullptr) {
351
                        cNodemID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,"compileFromXdmNode",
352
                        "(Ljava/lang/String;Ljava/lang/Object;Z[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
353
        }
354
        if (!cNodemID) {
355
                std::cerr << "Error: "<< getDllname() << ".compileFromXdmNode"
356
                                << " not found\n" << std::endl;
357
                return nullptr;
358

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

    
385
        XsltExecutable * executable = new XsltExecutable(SaxonProcessor::sxn_environ->env->NewGlobalRef(executableObject));
386
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(executableObject);
387
                return executable;
388
        }
389

    
390
}
391

    
392
XsltExecutable * Xslt30Processor::compileFromAssociatedFile(const char* source) {
393
        static jmethodID cFilemID = nullptr;
394
    if(cFilemID == nullptr) {
395
            cFilemID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
396
                                        "compileFromAssociatedFile",
397
                                        "(Ljava/lang/String;Ljava/lang/String;Z;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
398
        }
399
        if (!cFilemID) {
400
                std::cerr << "Error: "<<getDllname() << ".compileFromAssociatedFile"
401
                                << " not found\n" << std::endl;
402
        SaxonProcessor::sxn_environ->env->ExceptionClear();
403
                return nullptr;
404

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

    
432
        XsltExecutable * executable = new XsltExecutable(SaxonProcessor::sxn_environ->env->NewGlobalRef(executableObject));
433
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(executableObject);
434
                return executable;
435
        }
436

    
437
}
438

    
439

    
440
XsltExecutable * Xslt30Processor::compileFromFile(const char* stylesheet) {
441
        static jmethodID cFilemID = nullptr;
442
        if(cFilemID == nullptr) {
443
            cFilemID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
444
                                        "compileFromFile",
445
                                        "(Ljava/lang/String;Ljava/lang/String;Z[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
446
        }
447

    
448
        if (!cFilemID) {
449
                std::cerr << "Error: "<<getDllname() << ".compileFromFile"
450
                                << " not found\n" << std::endl;
451
        SaxonProcessor::sxn_environ->env->ExceptionClear();
452
                return nullptr;
453

    
454
        } else {
455
        if (stylesheet == nullptr) {
456
            createException("The Stylesheet file is NULL");
457
            return nullptr;
458
        }
459
        if(cppXT == nullptr) {
460
#if defined(DEBUG)
461
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
462
#endif
463

    
464
            return nullptr;
465
        }
466

    
467
        JParameters comboArrays;
468
        comboArrays = SaxonProcessor::createParameterJArray2(parameters);
469
        jobject executableObject = (jobject) (
470
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cFilemID,
471
                                                                   SaxonProcessor::sxn_environ->env->NewStringUTF(
472
                                                                           cwdXT.c_str()),
473
                                                                   SaxonProcessor::sxn_environ->env->NewStringUTF(
474
                                                                           stylesheet), jitCompilation,
475
                                                                   comboArrays.stringArray, comboArrays.objectArray));
476
        if (!executableObject) {
477
            createException();
478
            return nullptr;
479

    
480
        }
481
        if (comboArrays.stringArray != nullptr) {
482
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
483
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
484
        }
485

    
486
        XsltExecutable *executable;
487
        executable = new XsltExecutable(SaxonProcessor::sxn_environ->env->NewGlobalRef(executableObject), cwdXT);
488
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(executableObject);
489

    
490
        return executable;
491

    
492
        }
493

    
494
}
495

    
496

    
497

    
498

    
499
XdmValue * Xslt30Processor::transformFileToValue(const char* sourcefile,
500
                const char* stylesheetfile) {
501

    
502
        if(exceptionOccurred()) {
503
                //Possible error detected in the compile phase. Processor not in a clean state.
504
                //Require clearing exception.
505
                SaxonProcessor::sxn_environ->env->ExceptionDescribe();
506

    
507
                return nullptr;
508
        }
509

    
510
        if(sourcefile == nullptr && stylesheetfile == nullptr){
511
        
512
                return nullptr;
513
        }
514

    
515

    
516
        static jmethodID mtvID = nullptr;
517

    
518
        if(mtvID == nullptr) {
519
                        mtvID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
520
                                        "transformToValue",
521
                                        "(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;");
522
        }
523
        if (!mtvID) {
524
        std::cerr<<"Error: "<<getDllname()<<".transformtoValue function not found"<<std::endl;
525
        SaxonProcessor::sxn_environ->env->ExceptionClear();
526
        } else {
527
        if(cppXT == nullptr) {
528
//#if defined(DEBUG)
529
            createException("The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown");
530
//#endif
531
            return nullptr;
532
        }
533
            JParameters comboArrays;
534
                comboArrays = SaxonProcessor::createParameterJArray2(parameters);
535
        jstring jcwd = SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str());
536
        jstring jsourcefile = (sourcefile != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(sourcefile) : nullptr);
537
        jstring jstylesheetfile = (stylesheetfile != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetfile) : nullptr);
538
                jobject result = (jobject)(
539
                                SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, mtvID,jcwd, cppXT, nullptr, jsourcefile,
540
                                                                            jstylesheetfile, comboArrays.stringArray, comboArrays.objectArray));
541
                if (comboArrays.stringArray != nullptr) {
542
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
543
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
544
                }
545
        if (jcwd != nullptr) {
546
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(jcwd);
547
        }
548
        if (jsourcefile != nullptr) {
549
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(jsourcefile);
550
        }
551
        if (jstylesheetfile != nullptr) {
552
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(jstylesheetfile);
553
        }
554
                if (result) {
555

    
556
                        jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
557
                          jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
558
                          jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
559
                jclass mapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
560
                jclass arrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmArray");
561
                            XdmValue * value = nullptr;
562
                          XdmItem * xdmItem = nullptr;
563

    
564

    
565
                                  if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
566
                                          xdmItem = new XdmAtomicValue(result);
567
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
568
                        return xdmItem;
569

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

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

    
580
                                  } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, mapClass)           == JNI_TRUE) {
581
                        xdmItem =  new XdmMap(result);
582
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
583
                        return xdmItem;
584

    
585
                    } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, arrayClass)           == JNI_TRUE) {
586
                        xdmItem =  new XdmArray(result);
587
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
588
                        return xdmItem;
589

    
590
                    } else {
591
                                        value = new XdmValue(result, true);
592
                                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
593
                                        return value;
594
                                }
595
                                value = new XdmValue();
596
                      value->addUnderlyingValue(result);
597

    
598
                          SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
599
                          return value;
600
                }else {
601

    
602
                        createException();
603

    
604
                     }
605
        }
606
        return nullptr;
607

    
608
}
609

    
610

    
611
void Xslt30Processor::transformFileToFile(const char* source,
612
                const char* stylesheet, const char* outputfile) {
613

    
614
        if(exceptionOccurred()) {
615
                //Possible error detected in the compile phase. Processor not in a clean state.
616
                //Require clearing exception.
617
                return;        
618
        }
619
        if(stylesheet==nullptr){
620
                std::cerr<< "Error: stylesheet has not been set."<<std::endl;
621
                return;
622
        }
623
        //
624
        static jmethodID mtfID = nullptr;
625

    
626
        if(mtfID == nullptr) {
627
                mtfID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
628
                                        "transformToFile",
629
                                        "(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");
630
        }
631
        if (!mtfID) {
632
        std::cerr<<"Error: "<<getDllname()<<".transformToFile function not found"<<std::endl;
633
        SaxonProcessor::sxn_environ->env->ExceptionClear();
634
        } else {
635
        if(cppXT == nullptr) {
636
#if defined(DEBUG)
637
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
638
#endif
639
            return;
640
        }
641
            JParameters comboArrays;
642
        comboArrays = SaxonProcessor::createParameterJArray2(parameters);
643

    
644
                SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, mtfID,
645
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()), cppXT, nullptr,
646
                                                                (source != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(source) : nullptr),
647
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet),        (outputfile != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(outputfile) :nullptr),
648
                                                                comboArrays.stringArray, comboArrays.objectArray);
649
                if (comboArrays.stringArray!= nullptr) {
650
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
651
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
652
                }
653
                }
654
                createException();
655
        }
656

    
657

    
658
const char * Xslt30Processor::transformFileToString(const char* source,
659
                const char* stylesheet) {
660

    
661
        if(exceptionOccurred()) {
662
                //Possible error detected in the compile phase. Processor not in a clean state.
663
                //Require clearing exception.
664
                return nullptr;
665
        }
666
        if(source == nullptr && stylesheet == nullptr){
667
                std::cerr<< "Error: nullptr file name found in transformFiletoString."<<std::endl;
668
                return nullptr;
669
        }
670

    
671
        static jmethodID mtsID =  nullptr;
672

    
673
        if(mtsID == nullptr) {
674
                        mtsID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
675
                                        "transformToString",
676
                                        "(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");
677
        }
678
        if (!mtsID) {
679
        std::cerr<<"Error: "<<getDllname()<<".transformFileToString not found"<<std::endl;
680
        SaxonProcessor::sxn_environ->env->ExceptionClear();
681
        } else {
682
        if(cppXT == nullptr) {
683
#if defined(DEBUG)
684
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
685
#endif
686
            return nullptr;
687
        }
688
    JParameters comboArrays;
689
    comboArrays = SaxonProcessor::createParameterJArray2(parameters);
690

    
691
    jbyteArray result = nullptr;
692
        jobject obj = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, mtsID,
693
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()), cppXT, nullptr,
694
                                                (source != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(
695
                                                                                                source) : nullptr),
696
                                                                (stylesheet != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet) : nullptr),
697
                                                                comboArrays.stringArray, comboArrays.objectArray);
698
    if (comboArrays.stringArray!= nullptr) {
699
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
700
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
701
    }
702
        if(obj) {
703
                        result = (jbyteArray)obj;
704
        }
705

    
706
        if (result) {
707
        jboolean isCopy = false;
708
        jbyte* b = SaxonProcessor::sxn_environ->env->GetByteArrayElements(result, &isCopy);
709
        jsize num_bytes = SaxonProcessor::sxn_environ->env->GetArrayLength(result);
710

    
711
        const char *str = new char[num_bytes];
712
        memcpy ((void *) str, b , num_bytes );
713

    
714
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
715
        return str;
716
        } else  {
717
            createException();
718
                           
719
        }
720
        }
721
        return nullptr;
722
}
723

    
724

    
725

    
726
const char * Xslt30Processor::getErrorMessage(){
727
         if(exception == nullptr) {return nullptr;}
728
         return exception->getMessage();
729
 }
730

    
(38-38/56)