Project

Profile

Help

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

he / src / main / c / Saxon.C.API / Xslt30Processor.cpp @ 5d25f10d

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
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(tempcppXT);
46

    
47
    } else {
48
            createException("Error: Failed to create the Xslt30Processor internal object");
49

    
50
        }
51

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

    
58
        if(cppXT == nullptr) {
59
        createException();
60
        }
61

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

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

    
76

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

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

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

    
97
        jitCompilation = other.jitCompilation;
98

    
99
}
100

    
101

    
102

    
103

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

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

    
113

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

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

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

    
152
void Xslt30Processor::setJustInTimeCompilation(bool jit){
153
    jitCompilation = jit;
154
}
155

    
156

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

    
168
            }
169
        }
170

    
171
        }
172
        parameters.clear();
173
        
174
}
175

    
176

    
177

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

    
183

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

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

    
199
SaxonApiException* Xslt30Processor::getException() {
200
    return exception;
201
}
202

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

    
214
}
215

    
216

    
217

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

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

    
243
    }
244

    
245

    
246

    
247
}
248

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

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

    
274
    }
275
}
276

    
277

    
278

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

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

    
290
        } else {
291

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

    
302
        createException();
303

    
304

    
305
     }
306
}
307

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

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

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

    
348
}
349

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

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

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

    
392
}
393

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

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

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

    
439
}
440

    
441

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

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

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

    
466
            return nullptr;
467
        }
468

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

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

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

    
492
        return executable;
493

    
494
        }
495

    
496
}
497

    
498

    
499

    
500

    
501
XdmValue * Xslt30Processor::transformFileToValue(const char* sourcefile,
502
                const char* stylesheetfile) {
503

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

    
509
                return nullptr;
510
        }
511

    
512
        if(sourcefile == nullptr && stylesheetfile == nullptr){
513
        
514
                return nullptr;
515
        }
516

    
517

    
518
        static jmethodID mtvID = nullptr;
519

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

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

    
566

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

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

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

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

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

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

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

    
604
                        createException();
605

    
606
                     }
607
        }
608
        return nullptr;
609

    
610
}
611

    
612

    
613
void Xslt30Processor::transformFileToFile(const char* source,
614
                const char* stylesheet, const char* outputfile) {
615

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

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

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

    
659

    
660
const char * Xslt30Processor::transformFileToString(const char* source,
661
                const char* stylesheet) {
662

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

    
673
        static jmethodID mtsID =  nullptr;
674

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

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

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

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

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

    
726

    
727

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

    
(38-38/56)