Project

Profile

Help

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

he / src / main / c / Saxon.C.API / Xslt30Processor.cpp @ 306962bc

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
Xslt30Processor::Xslt30Processor() {
18

    
19
        SaxonProcessor *p = new SaxonProcessor(false);
20
        Xslt30Processor(p, "");
21

    
22
}
23

    
24
Xslt30Processor::Xslt30Processor(SaxonProcessor * p, std::string curr) {
25

    
26
        proc = p;
27
    tunnel = false;
28
    jitCompilation = false;
29
    exception = nullptr;
30

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

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

    
40
#ifdef DEBUG
41
        jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
42
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
43
    
44
#endif
45

    
46
        if(cppXT == nullptr) {
47
        createException();
48
        }
49

    
50
        if(!(proc->cwd.empty()) && curr.empty()){
51
                cwdXT = proc->cwd;
52
        } else if(!curr.empty()){
53
                cwdXT = curr;
54
        }
55
}
56

    
57
    Xslt30Processor::~Xslt30Processor(){
58
            clearParameters();
59
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(cppXT);
60
            cwdXT.erase();
61
    }
62

    
63

    
64
Xslt30Processor::Xslt30Processor(const Xslt30Processor &other) {
65
        proc = other.proc;
66
        cppClass = other.cppClass;
67
        cppXT = other.cppXT;
68
    cwdXT = other.cwdXT;
69
        tunnel = other.tunnel;
70

    
71
        std::map<std::string, XdmValue*>::const_iterator paramIter = other.parameters.begin();
72
    while(paramIter != other.parameters.end())
73
    {
74

    
75
       XdmValue * valuei = paramIter->second;
76
       if(valuei == nullptr) {
77
                     //std::cerr<<"Error in Xslt30Processor copy constructor"<<std::endl;
78
       } else {
79
            parameters[paramIter->first] = new XdmValue(*(valuei));
80
       }
81
       paramIter++;
82
    }
83

    
84
        jitCompilation = other.jitCompilation;
85

    
86
}
87

    
88

    
89

    
90

    
91
bool Xslt30Processor::exceptionOccurred() {
92
        return proc->exceptionOccurred() || exception != nullptr;
93
}
94

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

    
100

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

    
126
XdmValue* Xslt30Processor::getParameter(const char* name) {
127
        std::map<std::string, XdmValue*>::iterator it;
128
        it = parameters.find("sparam:"+std::string(name));
129
        if (it != parameters.end())
130
          return it->second;
131
            return nullptr;
132
}
133

    
134
bool Xslt30Processor::removeParameter(const char* name) {
135
        return (bool)(parameters.erase("param:"+std::string(name)));
136
}
137

    
138
void Xslt30Processor::setJustInTimeCompilation(bool jit){
139
    jitCompilation = jit;
140
}
141

    
142

    
143
void Xslt30Processor::clearParameters(bool delValues) {
144
        if(delValues){
145
        for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
146
            XdmValue * value = itr->second;
147
            if(value != nullptr) {
148
#ifdef DEBUG
149
                std::cout<<"clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
150
#endif
151
                    delete value;
152

    
153
            }
154
        }
155

    
156
        }
157
        parameters.clear();
158
        
159
}
160

    
161

    
162

    
163
std::map<std::string,XdmValue*>& Xslt30Processor::getParameters(){
164
        std::map<std::string,XdmValue*>& ptr = parameters;
165
        return ptr;
166
}
167

    
168

    
169
void Xslt30Processor::exceptionClear(){
170
 if(exception != nullptr) {
171
         delete exception;
172
         exception = nullptr;
173
        SaxonProcessor::sxn_environ->env->ExceptionClear();
174
 }
175
  
176
 }
177

    
178
   void Xslt30Processor::setcwd(const char* dir){
179
    if (dir!= nullptr) {
180
        cwdXT = std::string(dir);
181
    }
182
   }
183

    
184
SaxonApiException* Xslt30Processor::getException() {
185
    return exception;
186
}
187

    
188
void Xslt30Processor::createException(const char * message) {
189

    
190

    
191
    if(exception != nullptr) {
192
        delete exception;
193
        SaxonProcessor::sxn_environ->env->ExceptionClear();
194
    }
195

    
196
    if(message == nullptr) {
197
        exception = proc->checkAndCreateException(cppClass);
198
    } else {
199
        exception = new SaxonApiException(message);
200
    }
201

    
202
}
203

    
204

    
205

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

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

    
231
    }
232

    
233

    
234

    
235
}
236

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

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

    
262
    }
263
}
264

    
265

    
266

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

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

    
278
        } else {
279

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

    
290
        createException();
291

    
292

    
293
     }
294
}
295

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

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

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

    
336
}
337

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

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

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

    
380
}
381

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

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

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

    
427
}
428

    
429

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

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

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

    
454
            return nullptr;
455
        }
456

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

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

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

    
480
        return executable;
481

    
482
        }
483

    
484
}
485

    
486

    
487

    
488

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

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

    
497
                return nullptr;
498
        }
499

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

    
505

    
506
        static jmethodID mtvID = nullptr;
507

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

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

    
554

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

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

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

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

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

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

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

    
592
                        createException();
593

    
594
                     }
595
        }
596
        return nullptr;
597

    
598
}
599

    
600

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

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

    
616
        if(mtfID == nullptr) {
617
                mtfID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
618
                                        "transformToFile",
619
                                        "(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");
620
        }
621
        if (!mtfID) {
622
        std::cerr<<"Error: "<<getDllname()<<".transformToFile function not found"<<std::endl;
623
        SaxonProcessor::sxn_environ->env->ExceptionClear();
624
        } else {
625
        if(cppXT == nullptr) {
626
#if defined(DEBUG)
627
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
628
#endif
629
            return;
630
        }
631
            JParameters comboArrays;
632
        comboArrays = SaxonProcessor::createParameterJArray2(parameters);
633

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

    
647

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

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

    
661
        static jmethodID mtsID =  nullptr;
662

    
663
        if(mtsID == nullptr) {
664
                        mtsID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
665
                                        "transformToString",
666
                                        "(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");
667
        }
668
        if (!mtsID) {
669
        std::cerr<<"Error: "<<getDllname()<<".transformFileToString not found"<<std::endl;
670
        SaxonProcessor::sxn_environ->env->ExceptionClear();
671
        } else {
672
        if(cppXT == nullptr) {
673
#if defined(DEBUG)
674
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
675
#endif
676
            return nullptr;
677
        }
678
    JParameters comboArrays;
679
    comboArrays = SaxonProcessor::createParameterJArray2(parameters);
680

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

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

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

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

    
714

    
715

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

    
(37-37/55)