Project

Profile

Help

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

he / latest10 / hec / Saxon.C.API / XsltExecutable.cpp @ c1cc9bd8

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
    messageListenerClass = lookForClass(SaxonProcessor::sxn_environ->env,
29
                                               "net/sf/saxon/option/cpp/SaxonCMessageListener");
30

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

    
35
#endif
36
    tunnel = false;
37
    selection = nullptr;
38
    selectionV = nullptr;
39
    saxonMessageListenerObj = nullptr;
40
    executableObject = exObject;
41
    cwdXE = curr;
42
}
43

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

    
55
}
56

    
57

    
58
XsltExecutable::XsltExecutable(const XsltExecutable &other) {
59

    
60
    /*
61
     * Look for class.
62
     */
63
    cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
64
                            "net/sf/saxon/option/cpp/Xslt30Processor");
65

    
66
    executableObject = other.executableObject;
67
    selectionV = other.selectionV;
68
    if (selectionV != nullptr) {
69
        setInitialMatchSelection(other.selectionV);
70
    } else {
71
        selection = other.selection;
72
    }
73
    tunnel = other.tunnel;
74

    
75
    std::map<std::string, XdmValue *>::const_iterator paramIter = other.parameters.begin();
76
    while (paramIter != other.parameters.end()) {
77

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

    
87
    std::map<std::string, std::string>::const_iterator propIter = other.properties.begin();
88
    while (propIter != other.properties.end()) {
89
        properties[propIter->first] = propIter->second;
90
        propIter++;
91
    }
92

    
93
}
94

    
95

    
96
bool XsltExecutable::exceptionOccurred() {
97
    return SaxonProcessor::sxn_environ->env->ExceptionCheck();
98
}
99

    
100
const char *XsltExecutable::getErrorCode() {
101
    SaxonApiException *exception = SaxonProcessor::checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass,
102
                                                                        nullptr);
103
    if (exception == nullptr) { return nullptr; }
104
    return exception->getErrorCode();
105
}
106

    
107

    
108
void XsltExecutable::setGlobalContextItem(XdmItem *value) {
109
    if (value != nullptr) {
110
        value->incrementRefCount();
111
        parameters["node"] = value;
112
    }
113
}
114

    
115
void XsltExecutable::setGlobalContextFromFile(const char *ifile) {
116
    if (ifile != nullptr) {
117
        setProperty("s", ifile);
118
    }
119
}
120

    
121
void XsltExecutable::setInitialMatchSelection(XdmValue *_selection) {
122
    if (_selection != nullptr) {
123
        _selection->incrementRefCount();
124
        selectionV = _selection;
125
        selection = _selection->getUnderlyingValue();
126
    }
127
}
128

    
129

    
130
void XsltExecutable::setInitialMatchSelectionAsFile(const char *filename) {
131
    if (filename != nullptr) {
132
        selection = SaxonProcessor::sxn_environ->env->NewStringUTF(filename);
133
    }
134
}
135

    
136
void XsltExecutable::setOutputFile(const char *ofile) {
137
    setProperty("o", ofile);
138
}
139

    
140
void XsltExecutable::setBaseOutputURI(const char *baseURI) {
141
    if (baseURI != nullptr) {
142
        setProperty("baseoutput", baseURI);
143
    }
144
}
145

    
146

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

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

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

    
192
bool XsltExecutable::removeParameter(const char *name) {
193
    return (bool) (parameters.erase("param:" + std::string(name)));
194
}
195

    
196

    
197
void XsltExecutable::setResultAsRawValue(bool option) {
198
    if (option) {
199
        setProperty("outvalue", "yes");
200
    }
201
}
202

    
203
Xslt30Processor * Xslt30Processor::clone() {
204
    Xslt30Processor * proc = new Xslt30Processor(*this);
205
    return proc;
206

    
207
}
208

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

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

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

    
235
void XsltExecutable::clearParameters(bool delValues) {
236
    if (delValues) {
237
        for (std::map<std::string, XdmValue *>::iterator itr = parameters.begin(); itr != parameters.end(); itr++) {
238

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

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

    
254
            XdmValue *value = itr->second;
255
            value->decrementRefCount();
256

    
257
        }
258

    
259
        selection = nullptr;
260
    }
261
    parameters.clear();
262

    
263

    
264
}
265

    
266
void XsltExecutable::clearProperties() {
267
    properties.clear();
268

    
269
}
270

    
271

    
272
std::map<std::string, XdmValue *> &XsltExecutable::getParameters() {
273
    std::map<std::string, XdmValue *> &ptr = parameters;
274
    return ptr;
275
}
276

    
277
std::map<std::string, std::string> &XsltExecutable::getProperties() {
278
    std::map<std::string, std::string> &ptr = properties;
279
    return ptr;
280
}
281

    
282
void XsltExecutable::exceptionClear() {
283
    SaxonProcessor::sxn_environ->env->ExceptionClear();
284
}
285

    
286
void XsltExecutable::setcwd(const char *dir) {
287
    if (dir != nullptr) {
288
        cwdXE = std::string(dir);
289
    }
290
}
291

    
292

    
293
void XsltExecutable::exportStylesheet(const char *filename) {
294

    
295

    
296
    static jmethodID exportmID = nullptr;
297

    
298
    if (!exportmID) {
299
        exportmID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "save",
300
                                                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;)V");
301
    }
302

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

    
313
}
314

    
315
void XsltExecutable::applyTemplatesReturningFile(const char *output_filename) {
316

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

    
324
    static jmethodID atmID = nullptr;
325

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

    
331
    }
332

    
333
    if (!atmID) {
334
        std::cerr << "Error: " << getDllname() << "applyTemplatesAsFile" << " not found\n"
335
                  << std::endl;
336

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

    
352
    }
353
    return;
354

    
355
}
356

    
357
const char *XsltExecutable::applyTemplatesReturningString() {
358

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

    
366

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

    
375
    } else {
376
        JParameters comboArrays;
377
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
378

    
379
        jstring result = nullptr;
380
        jobject obj = (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, atsmID,
381
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
382
                                                                                        cwdXE.c_str()), executableObject,selection,
383
                comboArrays.stringArray, comboArrays.objectArray));
384

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

    
401

    
402
}
403

    
404
XdmValue *XsltExecutable::applyTemplatesReturningValue() {
405

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

    
413

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

    
424
    } else {
425
        JParameters comboArrays;
426
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
427

    
428

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

    
453

    
454
            if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass) == JNI_TRUE) {
455
                xdmItem = new XdmAtomicValue(result);
456

    
457
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
458
                return xdmItem;
459

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

    
479
}
480

    
481

    
482
void XsltExecutable::callFunctionReturningFile(const char *functionName, XdmValue **arguments, int argument_length,
483
                                               const char *outfile) {
484

    
485

    
486
    static jmethodID afmID = nullptr;
487

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

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

    
502
        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
503

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

    
525
    }
526
    return;
527

    
528

    
529
}
530

    
531
const char *
532
XsltExecutable::callFunctionReturningString(const char *functionName, XdmValue **arguments, int argument_length) {
533

    
534

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

    
543
    } else {
544
        JParameters comboArrays;
545
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
546
        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
547

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

    
575

    
576
XdmValue *
577
XsltExecutable::callFunctionReturningValue(const char *functionName, XdmValue **arguments, int argument_length) {
578

    
579

    
580
    static jmethodID cfvmID = nullptr;
581
    if (cfvmID == nullptr) {
582
        cfvmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
583
                                                                                 "callFunctionReturningValue",
584
                                                                                 "(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;");
585
    }
586
    if (!cfvmID) {
587
        std::cerr << "Error: " << getDllname() << "callFunctionReturningValue" << " not found\n"
588
                  << std::endl;
589

    
590
    } else {
591
        JParameters comboArrays;
592
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
593
        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
594

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

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

    
623

    
624
            if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass) == JNI_TRUE) {
625
                xdmItem = new XdmAtomicValue(result);
626
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
627
                return xdmItem;
628
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass) == JNI_TRUE) {
629
                xdmItem = new XdmNode(result);
630
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
631
                return xdmItem;
632

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

    
650
}
651

    
652

    
653
void XsltExecutable::callTemplateReturningFile(const char *templateName, const char *outfile) {
654

    
655

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

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

    
683

    
684
    }
685

    
686
}
687

    
688

    
689
const char *XsltExecutable::callTemplateReturningString(const char *templateName) {
690

    
691

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

    
700
    } else {
701
        JParameters comboArrays;
702
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
703

    
704

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

    
732

    
733
}
734

    
735
XdmValue *XsltExecutable::callTemplateReturningValue(const char *templateName) {
736

    
737

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

    
748
    } else {
749
        JParameters comboArrays;
750
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
751

    
752

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

    
779
            XdmItem *xdmItem = nullptr;
780
            if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass) == JNI_TRUE) {
781
                xdmItem = new XdmAtomicValue(result);
782
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
783
                return xdmItem;
784

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

    
807

    
808
XdmValue *XsltExecutable::transformFileToValue(const char *sourcefile) {
809

    
810
    if (sourcefile == nullptr) {
811

    
812
        return nullptr;
813
    }
814

    
815

    
816
    static jmethodID tfvMID = nullptr;
817

    
818
    if (tfvMID == nullptr) {
819
        tfvMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
820
                                                                                 "transformToValue",
821
                                                                                 "(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;");
822
    }
823
    if (!tfvMID) {
824
        std::cerr << "Error: " << getDllname() << ".transformtoValue" << " not found\n"
825
                  << std::endl;
826

    
827
    } else {
828
        JParameters comboArrays;
829
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
830

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

    
857

    
858
            if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass) == JNI_TRUE) {
859
                xdmItem = new XdmAtomicValue(result);
860
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
861
                return xdmItem;
862

    
863
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass) == JNI_TRUE) {
864
                xdmItem = new XdmNode(result);
865
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
866
                return xdmItem;
867

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

    
887
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
888
            return value;
889
        }
890
    }
891
    return nullptr;
892

    
893
}
894

    
895

    
896
void XsltExecutable::transformFileToFile(const char *source, const char *outputfile) {
897

    
898

    
899
    static jmethodID tffMID = nullptr;
900

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

    
910
    } else {
911
        JParameters comboArrays;
912
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
913

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

    
930
}
931

    
932

    
933
void XsltExecutable::setupXslMessage(bool create, const char * filename) {
934
    if (create) {
935

    
936
        static jmethodID messageID =   (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(messageListenerClass,
937
                                                                                    "<init>",
938
                                                                                    "(Ljava/lang/String;Ljava/lang/String;)V");
939
        if (!messageID) {
940
            std::cerr << "Error: "<<"SaxonCMessageListener" << " in " <<getDllname() << " not found\n"
941
                      << std::endl;
942

    
943
        }
944

    
945
        if (filename == nullptr) {
946
            saxonMessageListenerObj = (jobject)SaxonProcessor::sxn_environ->env->NewObject(messageListenerClass, messageID,
947
                                                                                           SaxonProcessor::sxn_environ->env->NewStringUTF("-:on"));
948
            //setProperty("m", "on");
949
        } else {
950
            saxonMessageListenerObj = (jobject)SaxonProcessor::sxn_environ->env->NewObject(messageListenerClass, messageID,
951
                                                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
952
        }
953
    } else {
954
        setProperty("m", "off");
955
    }
956

    
957

    
958
}
959

    
960
XdmValue * XsltExecutable::getXslMessages(){
961

    
962
    if(saxonMessageListenerObj) {
963
        static jmethodID getmessageID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(messageListenerClass,
964
                                                                                                  "getXslMessages",
965
                                                                                                  "()[Lnet/sf/saxon/s9api/XdmNode;");
966
        if (!getmessageID) {
967
            std::cerr << "Error: " << getDllname() << ".getXslMessages" << " not found\n"
968
                      << std::endl;
969

    
970
        } else {
971
            jobjectArray results = (jobjectArray) (
972
                    SaxonProcessor::sxn_environ->env->CallObjectMethod(saxonMessageListenerObj, getmessageID));
973
            int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
974

    
975
            if (sizex > 0) {
976
                XdmValue *value = new XdmValue();
977

    
978
                for (int p = 0; p < sizex; ++p) {
979
                    jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
980
                    value->addUnderlyingValue(resulti);
981
                }
982
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(results);
983
                return value;
984
            }
985
        }
986
    }
987
    return NULL;
988

    
989

    
990
}
991

    
992

    
993
const char *XsltExecutable::transformFileToString(const char *source) {
994

    
995

    
996
    static jmethodID tftMID = tftMID;
997
    if (tftMID == nullptr) {
998
        tftMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
999
                                                                                 "transformToString",
1000
                                                                                 "(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;");
1001
    }
1002
    if (!tftMID) {
1003
        std::cerr << "Error: " << getDllname() << "transformFileToString" << " not found\n"
1004
                  << std::endl;
1005

    
1006
    } else {
1007
        JParameters comboArrays;
1008
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
1009

    
1010
        jstring result = nullptr;
1011
        jobject obj = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, tftMID,
1012
                                                                               SaxonProcessor::sxn_environ->env->NewStringUTF(
1013
                                                                                       cwdXE.c_str()), nullptr,
1014
                                                                               executableObject,
1015
                                                                               (source != nullptr
1016
                                                                                ? SaxonProcessor::sxn_environ->env->NewStringUTF(
1017
                                                                                               source) : nullptr),
1018
                                                                               comboArrays.stringArray,
1019
                                                                               comboArrays.objectArray);
1020
        if (comboArrays.stringArray != nullptr) {
1021
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
1022
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
1023
        }
1024
        if (obj) {
1025
            result = (jstring) obj;
1026
        }
1027
        if (result) {
1028
            const char *str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
1029
                                                                                  nullptr);
1030
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
1031
            return str;
1032
        }
1033
    }
1034
    return nullptr;
1035
}
1036

    
1037

    
1038
const char *XsltExecutable::transformToString(XdmNode *source) {
1039

    
1040
    if (source != nullptr) {
1041
        source->incrementRefCount();
1042
        parameters["node"] = source;
1043
    }
1044
    return transformFileToString(nullptr);
1045
}
1046

    
1047

    
1048
XdmValue *XsltExecutable::transformToValue(XdmNode *source) {
1049

    
1050
    if (source != nullptr) {
1051
        source->incrementRefCount();
1052
        parameters["node"] = source;
1053
    }
1054
    return transformFileToValue(nullptr);
1055
}
1056

    
1057
void XsltExecutable::transformToFile(XdmNode *source) {
1058

    
1059
    if (source != nullptr) {
1060
        source->incrementRefCount();
1061
        parameters["node"] = source;
1062
    }
1063
    transformFileToFile(nullptr, nullptr);
1064
}
1065

    
1066
const char *XsltExecutable::getErrorMessage() {
1067
    SaxonApiException *exception = SaxonProcessor::checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass,
1068
                                                                        nullptr);
1069
    if (exception == nullptr) { return nullptr; }
1070
    return exception->getMessage();
1071
}
1072

    
1073

    
1074
SaxonApiException *XsltExecutable::getException() {
1075
    return SaxonProcessor::checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass, nullptr);
1076
}
(33-33/49)