Project

Profile

Help

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

he / latest10 / hec / Saxon.C.API / XsltExecutable.cpp @ 6304fb8b

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

    
4
#include "XsltExecutable.h"
5
#include "XdmValue.h"
6
#include "XdmItem.h"
7
#include "XdmNode.h"
8
#include "XdmFunctionItem.h"
9
#include "XdmMap.h"
10
#include "XdmArray.h"
11
#include "XdmAtomicValue.h"
12

    
13
#ifdef DEBUG
14
#include <typeinfo> //used for testing only
15
#endif
16

    
17

    
18
XsltExecutable::XsltExecutable(jobject exObject, std::string curr) {
19

    
20

    
21

    
22
    /*
23
     * Look for class.
24
     */
25
    cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
26
                            "net/sf/saxon/option/cpp/Xslt30Processor");
27

    
28
#ifdef DEBUG
29
    jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
30
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
31

    
32
#endif
33
    tunnel = false;
34
    selection = nullptr;
35
    selectionV = nullptr;
36
    executableObject = exObject;
37
    cwdXE = curr;
38
}
39

    
40
XsltExecutable::~XsltExecutable() {
41
    clearProperties();
42
    clearParameters();
43
    if (selectionV != nullptr) {
44
        selectionV->decrementRefCount();
45
        if (selectionV->getRefCount() == 0) {
46
            delete selectionV;
47
        }
48
    }
49
    SaxonProcessor::sxn_environ->env->DeleteGlobalRef(executableObject);
50

    
51
}
52

    
53

    
54
XsltExecutable::XsltExecutable(const XsltExecutable &other) {
55

    
56
    /*
57
     * Look for class.
58
     */
59
    cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
60
                            "net/sf/saxon/option/cpp/Xslt30Processor");
61

    
62
    executableObject = other.executableObject;
63
    selectionV = other.selectionV;
64
    if (selectionV != nullptr) {
65
        setInitialMatchSelection(other.selectionV);
66
    } else {
67
        selection = other.selection;
68
    }
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
        XdmValue *valuei = paramIter->second;
75
        if (valuei == nullptr) {
76
            std::cerr << "Error in XsltExecutable copy constructor" << std::endl;
77
        } else {
78
            parameters[paramIter->first] = new XdmValue(*(valuei));
79
        }
80
        paramIter++;
81
    }
82

    
83
    std::map<std::string, std::string>::const_iterator propIter = other.properties.begin();
84
    while (propIter != other.properties.end()) {
85
        properties[propIter->first] = propIter->second;
86
        propIter++;
87
    }
88

    
89
}
90

    
91

    
92
bool XsltExecutable::exceptionOccurred() {
93
    return SaxonProcessor::sxn_environ->env->ExceptionCheck();
94
}
95

    
96
const char *XsltExecutable::getErrorCode() {
97
    SaxonApiException *exception = SaxonProcessor::checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass,
98
                                                                        nullptr);
99
    if (exception == nullptr) { return nullptr; }
100
    return exception->getErrorCode();
101
}
102

    
103

    
104
void XsltExecutable::setGlobalContextItem(XdmItem *value) {
105
    if (value != nullptr) {
106
        value->incrementRefCount();
107
        parameters["node"] = value;
108
    }
109
}
110

    
111
void XsltExecutable::setGlobalContextFromFile(const char *ifile) {
112
    if (ifile != nullptr) {
113
        setProperty("s", ifile);
114
    }
115
}
116

    
117
void XsltExecutable::setInitialMatchSelection(XdmValue *_selection) {
118
    if (_selection != nullptr) {
119
        _selection->incrementRefCount();
120
        selectionV = _selection;
121
        selection = _selection->getUnderlyingValue();
122
    }
123
}
124

    
125

    
126
void XsltExecutable::setInitialMatchSelectionAsFile(const char *filename) {
127
    if (filename != nullptr) {
128
        selection = SaxonProcessor::sxn_environ->env->NewStringUTF(filename);
129
    }
130
}
131

    
132
void XsltExecutable::setOutputFile(const char *ofile) {
133
    setProperty("o", ofile);
134
}
135

    
136
void XsltExecutable::setBaseOutputURI(const char *baseURI) {
137
    if (baseURI != nullptr) {
138
        setProperty("baseoutput", baseURI);
139
    }
140
}
141

    
142

    
143
void XsltExecutable::setParameter(const char *name, XdmValue *value) {
144
    if (value != nullptr && name != nullptr) {
145
        value->incrementRefCount();
146
        int s = parameters.size();
147
        std::string skey = "param:" + std::string(name);
148
        parameters[skey] = value;
149
        if (s == parameters.size()) {
150
            std::map<std::string, XdmValue *>::iterator it;
151
            it = parameters.find(skey);
152
            if (it != parameters.end()) {
153
                XdmValue *valuei = it->second;
154
                valuei->decrementRefCount();
155
                if (valuei != nullptr && valuei->getRefCount() < 1) {
156
                    delete value;
157
                }
158
                parameters.erase(skey);
159
                parameters[skey] = value;
160
            }
161
        }
162
    }
163
}
164

    
165
void XsltExecutable::setInitialTemplateParameters(std::map<std::string, XdmValue *> _itparameters, bool _tunnel) {
166
    for (std::map<std::string, XdmValue *>::iterator itr = _itparameters.begin(); itr != _itparameters.end(); itr++) {
167
        parameters["itparam:" + std::string(itr->first)] = itr->second;
168
    }
169
    tunnel = _tunnel;
170
    if (tunnel) {
171
        setProperty("tunnel", "true");
172
    }
173
}
174

    
175
XdmValue *XsltExecutable::getParameter(const char *name) {
176
    std::map<std::string, XdmValue *>::iterator it;
177
    it = parameters.find("param:" + std::string(name));
178
    if (it != parameters.end())
179
        return it->second;
180
    else {
181
        it = parameters.find("sparam:" + std::string(name));
182
        if (it != parameters.end())
183
            return it->second;
184
    }
185
    return nullptr;
186
}
187

    
188
bool XsltExecutable::removeParameter(const char *name) {
189
    return (bool) (parameters.erase("param:" + std::string(name)));
190
}
191

    
192

    
193
void XsltExecutable::setResultAsRawValue(bool option) {
194
    if (option) {
195
        setProperty("outvalue", "yes");
196
    }
197
}
198

    
199
Xslt30Processor * Xslt30Processor::clone() {
200
    Xslt30Processor * proc = new Xslt30Processor(*this);
201
    return proc;
202

    
203
}
204

    
205
void XsltExecutable::setProperty(const char *name, const char *value) {
206
    if (name != nullptr) {
207
        int s = properties.size();
208
        std:
209
        std::string skey = std::string(name);
210
        properties.insert(std::pair<std::string, std::string>(skey, std::string((value == nullptr ? "" : value))));
211

    
212
        if (s == properties.size()) {
213
            std::map<std::string, std::string>::iterator it;
214
            it = properties.find(skey);
215
            if (it != properties.end()) {
216
                properties.erase(skey);
217
                properties[skey] = std::string((value == nullptr ? "" : value));
218
            }
219
        }
220
    }
221
}
222

    
223
const char *XsltExecutable::getProperty(const char *name) {
224
    std::map<std::string, std::string>::iterator it;
225
    it = properties.find(std::string(name));
226
    if (it != properties.end())
227
        return it->second.c_str();
228
    return nullptr;
229
}
230

    
231
void XsltExecutable::clearParameters(bool delValues) {
232
    if (delValues) {
233
        for (std::map<std::string, XdmValue *>::iterator itr = parameters.begin(); itr != parameters.end(); itr++) {
234

    
235
            XdmValue *value = itr->second;
236
            value->decrementRefCount();
237
#ifdef DEBUG
238
            std::cout<<"clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
239
#endif
240
            if (value != nullptr && value->getRefCount() < 1) {
241
                delete value;
242
            }
243
        }
244

    
245
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(selection);
246
        selection = nullptr;
247
    } else {
248
        for (std::map<std::string, XdmValue *>::iterator itr = parameters.begin(); itr != parameters.end(); itr++) {
249

    
250
            XdmValue *value = itr->second;
251
            value->decrementRefCount();
252

    
253
        }
254

    
255
        selection = nullptr;
256
    }
257
    parameters.clear();
258

    
259

    
260
}
261

    
262
void XsltExecutable::clearProperties() {
263
    properties.clear();
264

    
265
}
266

    
267

    
268
std::map<std::string, XdmValue *> &XsltExecutable::getParameters() {
269
    std::map<std::string, XdmValue *> &ptr = parameters;
270
    return ptr;
271
}
272

    
273
std::map<std::string, std::string> &XsltExecutable::getProperties() {
274
    std::map<std::string, std::string> &ptr = properties;
275
    return ptr;
276
}
277

    
278
void XsltExecutable::exceptionClear() {
279
    SaxonProcessor::sxn_environ->env->ExceptionClear();
280
}
281

    
282
void XsltExecutable::setcwd(const char *dir) {
283
    if (dir != nullptr) {
284
        cwdXE = std::string(dir);
285
    }
286
}
287

    
288

    
289
void XsltExecutable::exportStylesheet(const char *filename) {
290

    
291

    
292
    static jmethodID exportmID = nullptr;
293

    
294
    if (!exportmID) {
295
        exportmID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "save",
296
                                                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;)V");
297
    }
298

    
299
    if (filename == nullptr) {
300
        std::cerr << "Error: Error: export file name is nullptr" << std::endl;
301
        return;
302
    }
303
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, exportmID,
304
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(
305
                                                                   cwdXE.c_str()),
306
                                                           executableObject,
307
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
308

    
309
}
310

    
311
void XsltExecutable::applyTemplatesReturningFile(const char *output_filename) {
312

    
313
    if (selection == nullptr) {
314
        std::cerr
315
                << "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."
316
                << std::endl;
317
        return;
318
    }
319

    
320
    static jmethodID atmID = nullptr;
321

    
322
    if (atmID == nullptr) {
323
        (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
324
                                                                        "applyTemplatesReturningFile",
325
                                                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
326

    
327
    }
328

    
329
    if (!atmID) {
330
        std::cerr << "Error: " << getDllname() << "applyTemplatesAsFile" << " not found\n"
331
                  << std::endl;
332

    
333
    } else {
334
        JParameters comboArrays;
335
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
336
        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, atmID,
337
                                                                 SaxonProcessor::sxn_environ->env->NewStringUTF(
338
                                                                         cwdXE.c_str()), executableObject, selection,
339
                                                                 (output_filename != nullptr
340
                                                                  ? SaxonProcessor::sxn_environ->env->NewStringUTF(
341
                                                                                 output_filename) : nullptr),
342
                                                                 comboArrays.stringArray, comboArrays.objectArray);
343
        if (comboArrays.stringArray != nullptr) {
344
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
345
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
346
        }
347

    
348
    }
349
    return;
350

    
351
}
352

    
353
const char *XsltExecutable::applyTemplatesReturningString() {
354

    
355
    if (selection == nullptr) {
356
        std::cerr
357
                << "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."
358
                << std::endl;
359
        return nullptr;
360
    }
361

    
362

    
363
    jmethodID atsmID =
364
            (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
365
                                                                            "applyTemplatesReturningString",
366
                                                                            "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
367
    if (!atsmID) {
368
        std::cerr << "Error: " << getDllname() << "applyTemplatesAsString" << " not found\n"
369
                  << std::endl;
370

    
371
    } else {
372
        JParameters comboArrays;
373
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
374

    
375
        jstring result = nullptr;
376
        jobject obj = (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, atsmID,
377
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
378
                                                                                        cwdXE.c_str()), executableObject,selection,
379
                comboArrays.stringArray, comboArrays.objectArray));
380

    
381
        if (obj) {
382
            result = (jstring) obj;
383
        }
384
        if (comboArrays.stringArray != nullptr) {
385
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
386
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
387
        }
388
        if (result) {
389
            const char *str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
390
                                                                                  nullptr);
391
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
392
            return str;
393
        }
394
    }
395
    return nullptr;
396

    
397

    
398
}
399

    
400
XdmValue *XsltExecutable::applyTemplatesReturningValue() {
401

    
402
    if (selection == nullptr) {
403
        std::cerr
404
                << "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."
405
                << std::endl;
406
        return nullptr;
407
    }
408

    
409

    
410
    static jmethodID atsmID = nullptr;
411
    if (atsmID == nullptr) {
412
        atsmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
413
                                                                                 "applyTemplatesReturningValue",
414
                                                                                 "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
415
    }
416
    if (!atsmID) {
417
        std::cerr << "Error: " << getDllname() << "applyTemplatesAsValue" << " not found\n"
418
                  << std::endl;
419

    
420
    } else {
421
        JParameters comboArrays;
422
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
423

    
424

    
425
        // jstring result = nullptr;
426
        jobject result = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, atsmID,
427
                                                                                             SaxonProcessor::sxn_environ->env->NewStringUTF(
428
                                                                                                     cwdXE.c_str()),
429
                                                                                             executableObject,
430
                                                                                             selection,
431
                                                                                             comboArrays.stringArray,
432
                                                                                             comboArrays.objectArray));
433
        /*if(obj) {
434
            result = (jobject)obj;
435
        }*/
436
        if (comboArrays.stringArray != nullptr) {
437
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
438
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
439
        }
440
        if (result) {
441
            jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
442
                                                   "net/sf/saxon/s9api/XdmAtomicValue");
443
            jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
444
            jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
445
                                                    "net/sf/saxon/s9api/XdmFunctionItem");
446
            XdmValue *value = nullptr;
447
            XdmItem *xdmItem = nullptr;
448

    
449

    
450
            if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass) == JNI_TRUE) {
451
                xdmItem = new XdmAtomicValue(result);
452

    
453
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
454
                return xdmItem;
455

    
456
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass) == JNI_TRUE) {
457
                xdmItem = new XdmNode(result);
458
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
459
                return xdmItem;
460
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass) == JNI_TRUE) {
461
                xdmItem = new XdmFunctionItem(result);
462
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
463
                return xdmItem;
464
            } else {
465
                value = new XdmValue(result, true);
466
                for (int z = 0; z < value->size(); z++) {
467
                }
468
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
469
                return value;
470
            }
471
        }
472
    }
473
    return nullptr;
474

    
475
}
476

    
477

    
478
void XsltExecutable::callFunctionReturningFile(const char *functionName, XdmValue **arguments, int argument_length,
479
                                               const char *outfile) {
480

    
481

    
482
    static jmethodID afmID = nullptr;
483

    
484
    if (afmID == nullptr) {
485
        afmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
486
                                                                                "callFunctionReturningFile",
487
                                                                                "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)V");
488
    }
489

    
490
    if (!afmID) {
491
        std::cerr << "Error: " << getDllname() << "callFunctionReturningFile" << " not found\n"
492
                  << std::endl;
493
        return;
494
    } else {
495
        JParameters comboArrays;
496
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
497

    
498
        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
499

    
500
        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, afmID,
501
                                                                 SaxonProcessor::sxn_environ->env->NewStringUTF(
502
                                                                         cwdXE.c_str()),
503
                                                                 executableObject,
504
                                                                 (functionName != nullptr ?
505
                                                                  SaxonProcessor::sxn_environ->env->NewStringUTF(
506
                                                                          functionName) :
507
                                                                  nullptr), argumentJArray,
508
                                                                 (outfile != nullptr ?
509
                                                                  SaxonProcessor::sxn_environ->env->NewStringUTF(
510
                                                                          outfile) :
511
                                                                  nullptr),
512
                                                                 comboArrays.stringArray, comboArrays.objectArray);
513
        if (comboArrays.stringArray != nullptr) {
514
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
515
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
516
        }
517
        if (argumentJArray != nullptr) {
518
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(argumentJArray);
519
        }
520

    
521
    }
522
    return;
523

    
524

    
525
}
526

    
527
const char *
528
XsltExecutable::callFunctionReturningString(const char *functionName, XdmValue **arguments, int argument_length) {
529

    
530

    
531
    static jmethodID afsmID =
532
            (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
533
                                                                            "callFunctionReturningString",
534
                                                                            "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
535
    if (!afsmID) {
536
        std::cerr << "Error: " << getDllname() << "callFunctionReturningString" << " not found\n"
537
                  << std::endl;
538

    
539
    } else {
540
        JParameters comboArrays;
541
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
542
        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
543

    
544
        jstring result = nullptr;
545
        jobject obj = (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, afsmID,
546
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
547
                                                                                        cwdXE.c_str()), executableObject,
548
                                                                                        (functionName != nullptr
549
                                                                                         ? SaxonProcessor::sxn_environ->env->NewStringUTF(
550
                                                                                                        functionName)
551
                                                                                         : nullptr),
552
                                                                                argumentJArray, comboArrays.stringArray,
553
                                                                                comboArrays.objectArray));
554
        if (obj) {
555
            result = (jstring) obj;
556
        }
557
        if (comboArrays.stringArray != nullptr) {
558
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
559
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
560
        }
561
        if (result) {
562
            const char *str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
563
                                                                                  nullptr);
564
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
565
            return str;
566
        }
567
    }
568
    return nullptr;
569
}
570

    
571

    
572
XdmValue *
573
XsltExecutable::callFunctionReturningValue(const char *functionName, XdmValue **arguments, int argument_length) {
574

    
575

    
576
    static jmethodID cfvmID = nullptr;
577
    if (cfvmID == nullptr) {
578
        cfvmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
579
                                                                                 "callFunctionReturningValue",
580
                                                                                 "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
581
    }
582
    if (!cfvmID) {
583
        std::cerr << "Error: " << getDllname() << "callFunctionReturningValue" << " not found\n"
584
                  << std::endl;
585

    
586
    } else {
587
        JParameters comboArrays;
588
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
589
        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
590

    
591
        jobject result = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, cfvmID,
592
                                                                                             SaxonProcessor::sxn_environ->env->NewStringUTF(
593
                                                                                                     cwdXE.c_str()),
594
                                                                                             executableObject,
595
                                                                                             (functionName != nullptr
596
                                                                                              ? SaxonProcessor::sxn_environ->env->NewStringUTF(
597
                                                                                                             functionName)
598
                                                                                              : nullptr),
599
                                                                                             argumentJArray,
600
                                                                                             comboArrays.stringArray,
601
                                                                                             comboArrays.objectArray));
602

    
603
        if (comboArrays.stringArray != nullptr) {
604
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
605
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
606
        }
607
        if (argumentJArray != nullptr) {
608
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(argumentJArray);
609
        }
610
        if (result) {
611
            jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
612
                                                   "net/sf/saxon/s9api/XdmAtomicValue");
613
            jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
614
            jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
615
                                                    "net/sf/saxon/s9api/XdmFunctionItem");
616
            XdmValue *value = nullptr;
617
            XdmItem *xdmItem = nullptr;
618

    
619

    
620
            if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass) == JNI_TRUE) {
621
                xdmItem = new XdmAtomicValue(result);
622
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
623
                return xdmItem;
624
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass) == JNI_TRUE) {
625
                xdmItem = new XdmNode(result);
626
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
627
                return xdmItem;
628

    
629
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass) == JNI_TRUE) {
630
                xdmItem = new XdmFunctionItem(result);
631
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
632
                return xdmItem;
633
            } else {
634
                value = new XdmValue(result, true);
635
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
636
                return value;
637
            }
638
            value = new XdmValue();
639
            value->addUnderlyingValue(result);
640
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
641
            return value;
642
        }
643
    }
644
    return nullptr;
645

    
646
}
647

    
648

    
649
void XsltExecutable::callTemplateReturningFile(const char *templateName, const char *outfile) {
650

    
651

    
652
    static jmethodID ctmID =
653
            (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
654
                                                                            "callTemplateReturningFile",
655
                                                                            "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
656
    if (!ctmID) {
657
        std::cerr << "Error: " << getDllname() << "callTemplateReturningFile" << " not found\n"
658
                  << std::endl;
659

    
660
    } else {
661
        JParameters comboArrays;
662
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
663
        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, ctmID,
664
                                                                 SaxonProcessor::sxn_environ->env->NewStringUTF(
665
                                                                         cwdXE.c_str()),
666
                                                                 executableObject,
667
                                                                 (templateName != nullptr
668
                                                                  ? SaxonProcessor::sxn_environ->env->NewStringUTF(
669
                                                                                 templateName) : nullptr),
670
                                                                 (outfile != nullptr
671
                                                                  ? SaxonProcessor::sxn_environ->env->NewStringUTF(
672
                                                                                 outfile) : nullptr),
673
                                                                 comboArrays.stringArray, comboArrays.objectArray);
674
        if (comboArrays.stringArray != nullptr) {
675
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
676
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
677
        }
678

    
679

    
680
    }
681

    
682
}
683

    
684

    
685
const char *XsltExecutable::callTemplateReturningString(const char *templateName) {
686

    
687

    
688
    jmethodID ctsmID =
689
            (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
690
                                                                            "callTemplateReturningString",
691
                                                                            "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
692
    if (!ctsmID) {
693
        std::cerr << "Error: " << getDllname() << "callTemplateReturningString" << " not found\n"
694
                  << std::endl;
695

    
696
    } else {
697
        JParameters comboArrays;
698
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
699

    
700

    
701
        jstring result = nullptr;
702
        jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, ctsmID,
703
                                                                                          SaxonProcessor::sxn_environ->env->NewStringUTF(
704
                                                                                                  cwdXE.c_str()),
705
                                                                                          executableObject,
706
                                                                                          (templateName != nullptr
707
                                                                                           ? SaxonProcessor::sxn_environ->env->NewStringUTF(
708
                                                                                                          templateName)
709
                                                                                           : nullptr),
710
                                                                                          comboArrays.stringArray,
711
                                                                                          comboArrays.objectArray));
712
        if (obj) {
713
            result = (jstring) obj;
714
        }
715
        if (comboArrays.stringArray != nullptr) {
716
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
717
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
718
        }
719
        if (result) {
720
            const char *str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
721
                                                                                  nullptr);
722
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
723
            return str;
724
        }
725
    }
726
    return nullptr;
727

    
728

    
729
}
730

    
731
XdmValue *XsltExecutable::callTemplateReturningValue(const char *templateName) {
732

    
733

    
734
    static jmethodID ctsmID = nullptr;
735
    if (ctsmID == nullptr) {
736
        ctsmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
737
                                                                                 "callTemplateReturningValue",
738
                                                                                 "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
739
    }
740
    if (!ctsmID) {
741
        std::cerr << "Error: " << getDllname() << "callTemplateReturningValue" << " not found\n"
742
                  << std::endl;
743

    
744
    } else {
745
        JParameters comboArrays;
746
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
747

    
748

    
749
        jstring result = nullptr;
750
        jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, ctsmID,
751
                                                                                          SaxonProcessor::sxn_environ->env->NewStringUTF(
752
                                                                                                  cwdXE.c_str()),
753
                                                                                          executableObject,
754
                                                                                          (templateName != nullptr
755
                                                                                           ? SaxonProcessor::sxn_environ->env->NewStringUTF(
756
                                                                                                          templateName)
757
                                                                                           : nullptr),
758
                                                                                          comboArrays.stringArray,
759
                                                                                          comboArrays.objectArray));
760
        if (obj) {
761
            result = (jstring) obj;
762
        }
763
        if (comboArrays.stringArray != nullptr) {
764
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
765
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
766
        }
767
        if (result) {
768
            jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
769
                                                   "net/sf/saxon/s9api/XdmAtomicValue");
770
            jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
771
            jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
772
                                                    "net/sf/saxon/s9api/XdmFunctionItem");
773
            XdmValue *value = nullptr;
774

    
775
            XdmItem *xdmItem = nullptr;
776
            if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass) == JNI_TRUE) {
777
                xdmItem = new XdmAtomicValue(result);
778
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
779
                return xdmItem;
780

    
781
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass) == JNI_TRUE) {
782
                xdmItem = new XdmNode(result);
783
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
784
                return xdmItem;
785
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass) == JNI_TRUE) {
786
                xdmItem = new XdmFunctionItem(result);
787
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
788
                return xdmItem;
789
            } else {
790
                value = new XdmValue(result, true);
791
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
792
                return value;
793
            }
794
            value = new XdmValue();
795
            value->addUnderlyingValue(result);
796
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
797
            return value;
798
        }
799
    }
800
    return nullptr;
801
}
802

    
803

    
804
XdmValue *XsltExecutable::transformFileToValue(const char *sourcefile) {
805

    
806
    if (sourcefile == nullptr) {
807

    
808
        return nullptr;
809
    }
810

    
811

    
812
    static jmethodID tfvMID = nullptr;
813

    
814
    if (tfvMID == nullptr) {
815
        tfvMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
816
                                                                                 "transformToValue",
817
                                                                                 "(Ljava/lang/String;Lnet/sf/saxon/option/cpp/Xslt30Processor;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
818
    }
819
    if (!tfvMID) {
820
        std::cerr << "Error: " << getDllname() << ".transformtoValue" << " not found\n"
821
                  << std::endl;
822

    
823
    } else {
824
        JParameters comboArrays;
825
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
826

    
827
        jobject result = (jobject) (
828
                SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, tfvMID,
829
                                                                         SaxonProcessor::sxn_environ->env->NewStringUTF(
830
                                                                                 cwdXE.c_str()), nullptr, executableObject,
831
                                                                         (sourcefile != nullptr
832
                                                                          ? SaxonProcessor::sxn_environ->env->NewStringUTF(
833
                                                                                         sourcefile) : nullptr),
834
                                                                         comboArrays.stringArray,
835
                                                                         comboArrays.objectArray));
836
        if (comboArrays.stringArray != nullptr) {
837
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
838
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
839
        }
840
        if (result) {
841
            jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
842
                                                   "net/sf/saxon/s9api/XdmAtomicValue");
843
            jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
844
            jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
845
                                                    "net/sf/saxon/s9api/XdmFunctionItem");
846
            jclass mapItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
847
                                                    "net/sf/saxon/s9api/XdmMap");
848
            jclass arrayItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
849
                                               "net/sf/saxon/s9api/XdmArray");
850
            XdmValue *value = nullptr;
851
            XdmItem *xdmItem = nullptr;
852

    
853

    
854
            if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass) == JNI_TRUE) {
855
                xdmItem = new XdmAtomicValue(result);
856
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
857
                return xdmItem;
858

    
859
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass) == JNI_TRUE) {
860
                xdmItem = new XdmNode(result);
861
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
862
                return xdmItem;
863

    
864
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass) == JNI_TRUE) {
865
                xdmItem = new XdmFunctionItem(result);
866
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
867
                return xdmItem;
868
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, mapItemClass) == JNI_TRUE) {
869
                xdmItem = new XdmMap(result);
870
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
871
                return xdmItem;
872
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, arrayItemClass) == JNI_TRUE) {
873
                xdmItem = new XdmArray(result);
874
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
875
                return xdmItem;
876
            } else {
877
                value = new XdmValue(result, true);
878
                return value;
879
            }
880
            value = new XdmValue();
881
            value->addUnderlyingValue(result);
882

    
883
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
884
            return value;
885
        }
886
    }
887
    return nullptr;
888

    
889
}
890

    
891

    
892
void XsltExecutable::transformFileToFile(const char *source, const char *outputfile) {
893

    
894

    
895
    static jmethodID tffMID = nullptr;
896

    
897
    if (tffMID == nullptr) {
898
        tffMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
899
                                                                                 "transformToFile",
900
                                                                                 "(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");
901
    }
902
    if (!tffMID) {
903
        std::cerr << "Error: " << getDllname() << "transformToFile" << " not found\n"
904
                  << std::endl;
905

    
906
    } else {
907
        JParameters comboArrays;
908
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
909

    
910
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, tffMID,
911
                                                               SaxonProcessor::sxn_environ->env->NewStringUTF(
912
                                                                       cwdXE.c_str()), nullptr, executableObject,
913
                                                                       (source != nullptr
914
                                                                        ? SaxonProcessor::sxn_environ->env->NewStringUTF(
915
                                                                                       source) : nullptr), nullptr,
916
                                                               (outputfile != nullptr
917
                                                                ? SaxonProcessor::sxn_environ->env->NewStringUTF(
918
                                                                               outputfile) : nullptr),
919
                                                               comboArrays.stringArray, comboArrays.objectArray);
920
        if (comboArrays.stringArray != nullptr) {
921
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
922
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
923
        }
924
    }
925

    
926
}
927

    
928

    
929
void XsltExecutable::setupXslMessage(bool show, const char * filename) {
930
    if (show) {
931
        if (filename == nullptr) {
932
            setProperty("m", "on");
933
        } else {
934
            setProperty("m", filename);
935
        }
936
    } else {
937
        setProperty("m", "off");
938
    }
939
}
940

    
941

    
942
const char *XsltExecutable::transformFileToString(const char *source) {
943

    
944

    
945
    static jmethodID tftMID = tftMID;
946
    if (tftMID == nullptr) {
947
        tftMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
948
                                                                                 "transformToString",
949
                                                                                 "(Ljava/lang/String;Lnet/sf/saxon/option/cpp/Xslt30Processor;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
950
    }
951
    if (!tftMID) {
952
        std::cerr << "Error: " << getDllname() << "transformFileToString" << " not found\n"
953
                  << std::endl;
954

    
955
    } else {
956
        JParameters comboArrays;
957
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
958

    
959
        jstring result = nullptr;
960
        jobject obj = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, tftMID,
961
                                                                               SaxonProcessor::sxn_environ->env->NewStringUTF(
962
                                                                                       cwdXE.c_str()), nullptr,
963
                                                                               executableObject,
964
                                                                               (source != nullptr
965
                                                                                ? SaxonProcessor::sxn_environ->env->NewStringUTF(
966
                                                                                               source) : nullptr),
967
                                                                               comboArrays.stringArray,
968
                                                                               comboArrays.objectArray);
969
        if (comboArrays.stringArray != nullptr) {
970
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
971
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
972
        }
973
        if (obj) {
974
            result = (jstring) obj;
975
        }
976
        if (result) {
977
            const char *str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
978
                                                                                  nullptr);
979
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
980
            return str;
981
        }
982
    }
983
    return nullptr;
984
}
985

    
986

    
987
const char *XsltExecutable::transformToString(XdmNode *source) {
988

    
989
    if (source != nullptr) {
990
        source->incrementRefCount();
991
        parameters["node"] = source;
992
    }
993
    return transformFileToString(nullptr);
994
}
995

    
996

    
997
XdmValue *XsltExecutable::transformToValue(XdmNode *source) {
998

    
999
    if (source != nullptr) {
1000
        source->incrementRefCount();
1001
        parameters["node"] = source;
1002
    }
1003
    return transformFileToValue(nullptr);
1004
}
1005

    
1006
void XsltExecutable::transformToFile(XdmNode *source) {
1007

    
1008
    if (source != nullptr) {
1009
        source->incrementRefCount();
1010
        parameters["node"] = source;
1011
    }
1012
    transformFileToFile(nullptr, nullptr);
1013
}
1014

    
1015
const char *XsltExecutable::getErrorMessage() {
1016
    SaxonApiException *exception = SaxonProcessor::checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass,
1017
                                                                        nullptr);
1018
    if (exception == nullptr) { return nullptr; }
1019
    return exception->getMessage();
1020
}
1021

    
1022

    
1023
SaxonApiException *XsltExecutable::getException() {
1024
    return SaxonProcessor::checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass, nullptr);
1025
}
(33-33/49)