Project

Profile

Help

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

he / src / main / c / Saxon.C.API / Xslt30Processor.cpp @ 4ee4355d

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
    if(exception != nullptr) {
191
        delete exception;
192
        SaxonProcessor::sxn_environ->env->ExceptionClear();
193
    }
194
    if(message == nullptr) {
195
        exception = proc->checkAndCreateException(cppClass);
196
    } else {
197
        exception = new SaxonApiException(message);
198
    }
199

    
200
}
201

    
202

    
203

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

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

    
229
    }
230

    
231

    
232

    
233
}
234

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

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

    
260
    }
261
}
262

    
263

    
264

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

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

    
276
        } else {
277

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

    
288
        createException();
289

    
290

    
291
     }
292
}
293

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

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

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

    
334
}
335

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

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

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

    
378
}
379

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

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

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

    
425
}
426

    
427

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

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

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

    
452
            return nullptr;
453
        }
454

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

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

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

    
478
        return executable;
479

    
480
        }
481

    
482
}
483

    
484

    
485

    
486

    
487
XdmValue * Xslt30Processor::transformFileToValue(const char* sourcefile,
488
                const char* stylesheetfile) {
489

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

    
495
                return nullptr;
496
        }
497

    
498
        if(sourcefile == nullptr && stylesheetfile == nullptr){
499
        
500
                return nullptr;
501
        }
502

    
503

    
504
        static jmethodID mtvID = nullptr;
505

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

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

    
552

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

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

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

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

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

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

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

    
590
                        createException();
591

    
592
                     }
593
        }
594
        return nullptr;
595

    
596
}
597

    
598

    
599
void Xslt30Processor::transformFileToFile(const char* source,
600
                const char* stylesheet, const char* outputfile) {
601

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

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

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

    
645

    
646
const char * Xslt30Processor::transformFileToString(const char* source,
647
                const char* stylesheet) {
648

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

    
659
        static jmethodID mtsID =  nullptr;
660

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

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

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

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

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

    
712

    
713

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

    
(37-37/55)