Project

Profile

Help

Download (49.8 KB) Statistics
| Branch: | Revision:

he / src / main / c / Saxon.C.API / XsltExecutable.cpp @ 03fcd3f9

1 72bf04c6 Norman Walsh
// 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 88101661 O'Neil Delpratt
#include "XdmFunctionItem.h"
9
#include "XdmMap.h"
10
#include "XdmArray.h"
11 72bf04c6 Norman Walsh
#include "XdmAtomicValue.h"
12 88101661 O'Neil Delpratt
13 72bf04c6 Norman Walsh
#ifdef DEBUG
14
#include <typeinfo> //used for testing only
15
#endif
16
17
18 88101661 O'Neil Delpratt
XsltExecutable::XsltExecutable(jobject exObject, std::string curr) {
19
20
21 72bf04c6 Norman Walsh
22 88101661 O'Neil Delpratt
    /*
23
     * Look for class.
24
     */
25
    cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
26
                            "net/sf/saxon/option/cpp/Xslt30Processor");
27 72bf04c6 Norman Walsh
28 88101661 O'Neil Delpratt
    messageListenerClass = lookForClass(SaxonProcessor::sxn_environ->env,
29
                                               "net/sf/saxon/option/cpp/SaxonCMessageListener");
30 72bf04c6 Norman Walsh
31
#ifdef DEBUG
32 88101661 O'Neil Delpratt
    jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
33
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
34 72bf04c6 Norman Walsh
35
#endif
36 88101661 O'Neil Delpratt
    tunnel = false;
37
    selection = nullptr;
38
    selectionV = nullptr;
39
    saxonMessageListenerObj = nullptr;
40
    executableObject = exObject;
41 72bf04c6 Norman Walsh
    cwdXE = curr;
42
}
43
44 88101661 O'Neil Delpratt
XsltExecutable::~XsltExecutable() {
45
    clearProperties();
46
    clearParameters();
47
    if (selectionV != nullptr) {
48
        selectionV->decrementRefCount();
49
        if (selectionV->getRefCount() == 0) {
50
            delete selectionV;
51
        }
52 a69dd173 O'Neil Delpratt
    } else {
53
        if (selection) {
54
            SaxonProcessor::sxn_environ->env->DeleteGlobalRef(selection);
55
        }
56 88101661 O'Neil Delpratt
    }
57
    SaxonProcessor::sxn_environ->env->DeleteGlobalRef(executableObject);
58 72bf04c6 Norman Walsh
59 88101661 O'Neil Delpratt
}
60 72bf04c6 Norman Walsh
61
62
XsltExecutable::XsltExecutable(const XsltExecutable &other) {
63
64 88101661 O'Neil Delpratt
    /*
65
     * Look for class.
66
     */
67
    cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
68
                            "net/sf/saxon/option/cpp/Xslt30Processor");
69
70
    executableObject = other.executableObject;
71
    selectionV = other.selectionV;
72
    if (selectionV != nullptr) {
73
        setInitialMatchSelection(other.selectionV);
74
    } else {
75
        selection = other.selection;
76
    }
77
    tunnel = other.tunnel;
78
79
    std::map<std::string, XdmValue *>::const_iterator paramIter = other.parameters.begin();
80
    while (paramIter != other.parameters.end()) {
81
82
        XdmValue *valuei = paramIter->second;
83
        if (valuei == nullptr) {
84 146cfacb O'Neil Delpratt
            std::cerr << "Error in XsltExecutable copy constructor - parameter value is NULL" << std::endl;
85 88101661 O'Neil Delpratt
        } else {
86 72bf04c6 Norman Walsh
            parameters[paramIter->first] = new XdmValue(*(valuei));
87 88101661 O'Neil Delpratt
        }
88
        paramIter++;
89 72bf04c6 Norman Walsh
    }
90
91 88101661 O'Neil Delpratt
    std::map<std::string, std::string>::const_iterator propIter = other.properties.begin();
92
    while (propIter != other.properties.end()) {
93 72bf04c6 Norman Walsh
        properties[propIter->first] = propIter->second;
94
        propIter++;
95
    }
96
97
}
98
99
100
bool XsltExecutable::exceptionOccurred() {
101 88101661 O'Neil Delpratt
    return SaxonProcessor::sxn_environ->env->ExceptionCheck();
102 72bf04c6 Norman Walsh
}
103
104 88101661 O'Neil Delpratt
const char *XsltExecutable::getErrorCode() {
105
    SaxonApiException *exception = SaxonProcessor::checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass,
106
                                                                        nullptr);
107
    if (exception == nullptr) { return nullptr; }
108
    return exception->getErrorCode();
109
}
110 72bf04c6 Norman Walsh
111
112 88101661 O'Neil Delpratt
void XsltExecutable::setGlobalContextItem(XdmItem *value) {
113
    if (value != nullptr) {
114
        value->incrementRefCount();
115
        parameters["node"] = value;
116 72bf04c6 Norman Walsh
    }
117
}
118
119 88101661 O'Neil Delpratt
void XsltExecutable::setGlobalContextFromFile(const char *ifile) {
120
    if (ifile != nullptr) {
121
        setProperty("s", ifile);
122
    }
123 72bf04c6 Norman Walsh
}
124
125 88101661 O'Neil Delpratt
void XsltExecutable::setInitialMatchSelection(XdmValue *_selection) {
126
    if (_selection != nullptr) {
127
        _selection->incrementRefCount();
128
        selectionV = _selection;
129 146cfacb O'Neil Delpratt
        selection = SaxonProcessor::sxn_environ->env->NewGlobalRef(_selection->getUnderlyingValue());
130 72bf04c6 Norman Walsh
    }
131
}
132
133
134 88101661 O'Neil Delpratt
void XsltExecutable::setInitialMatchSelectionAsFile(const char *filename) {
135
    if (filename != nullptr) {
136 a69dd173 O'Neil Delpratt
        selection = SaxonProcessor::sxn_environ->env->NewGlobalRef(SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
137
138 72bf04c6 Norman Walsh
    }
139
}
140
141 88101661 O'Neil Delpratt
void XsltExecutable::setOutputFile(const char *ofile) {
142
    setProperty("o", ofile);
143 72bf04c6 Norman Walsh
}
144
145 88101661 O'Neil Delpratt
void XsltExecutable::setBaseOutputURI(const char *baseURI) {
146
    if (baseURI != nullptr) {
147
        setProperty("baseoutput", baseURI);
148
    }
149 72bf04c6 Norman Walsh
}
150
151
152 88101661 O'Neil Delpratt
void XsltExecutable::setParameter(const char *name, XdmValue *value) {
153
    if (value != nullptr && name != nullptr) {
154
        value->incrementRefCount();
155
        int s = parameters.size();
156
        std::string skey = "param:" + std::string(name);
157
        parameters[skey] = value;
158
        if (s == parameters.size()) {
159
            std::map<std::string, XdmValue *>::iterator it;
160 72bf04c6 Norman Walsh
            it = parameters.find(skey);
161
            if (it != parameters.end()) {
162 88101661 O'Neil Delpratt
                XdmValue *valuei = it->second;
163 146cfacb O'Neil Delpratt
                if(valuei != nullptr) {
164
                    valuei->decrementRefCount();
165
                    if (valuei->getRefCount() < 1) {
166
                        delete value;
167
                    }
168
                    parameters.erase(skey);
169
                    parameters[skey] = value;
170 72bf04c6 Norman Walsh
                }
171
            }
172 88101661 O'Neil Delpratt
        }
173
    }
174
}
175
176
void XsltExecutable::setInitialTemplateParameters(std::map<std::string, XdmValue *> _itparameters, bool _tunnel) {
177
    for (std::map<std::string, XdmValue *>::iterator itr = _itparameters.begin(); itr != _itparameters.end(); itr++) {
178
        parameters["itparam:" + std::string(itr->first)] = itr->second;
179 a69dd173 O'Neil Delpratt
        itr->second->incrementRefCount();
180 88101661 O'Neil Delpratt
    }
181
    tunnel = _tunnel;
182
    if (tunnel) {
183
        setProperty("tunnel", "true");
184
    }
185
}
186
187
XdmValue *XsltExecutable::getParameter(const char *name) {
188
    std::map<std::string, XdmValue *>::iterator it;
189
    it = parameters.find("param:" + std::string(name));
190
    if (it != parameters.end())
191
        return it->second;
192
    else {
193
        it = parameters.find("sparam:" + std::string(name));
194 72bf04c6 Norman Walsh
        if (it != parameters.end())
195 88101661 O'Neil Delpratt
            return it->second;
196
    }
197
    return nullptr;
198 72bf04c6 Norman Walsh
}
199
200 88101661 O'Neil Delpratt
bool XsltExecutable::removeParameter(const char *name) {
201
    return (bool) (parameters.erase("param:" + std::string(name)));
202 72bf04c6 Norman Walsh
}
203
204
205
void XsltExecutable::setResultAsRawValue(bool option) {
206 88101661 O'Neil Delpratt
    if (option) {
207
        setProperty("outvalue", "yes");
208
    }
209
}
210
211 79d12c83 O'Neil Delpratt
XsltExecutable * XsltExecutable::clone() {
212
    XsltExecutable * executable = new XsltExecutable(*this);
213
    return executable;
214 88101661 O'Neil Delpratt
215
}
216
217
void XsltExecutable::setProperty(const char *name, const char *value) {
218
    if (name != nullptr) {
219
        int s = properties.size();
220 b789718d O'Neil Delpratt
221 88101661 O'Neil Delpratt
        std::string skey = std::string(name);
222
        properties.insert(std::pair<std::string, std::string>(skey, std::string((value == nullptr ? "" : value))));
223
224
        if (s == properties.size()) {
225 72bf04c6 Norman Walsh
            std::map<std::string, std::string>::iterator it;
226
            it = properties.find(skey);
227
            if (it != properties.end()) {
228
                properties.erase(skey);
229 88101661 O'Neil Delpratt
                properties[skey] = std::string((value == nullptr ? "" : value));
230 72bf04c6 Norman Walsh
            }
231 88101661 O'Neil Delpratt
        }
232
    }
233 72bf04c6 Norman Walsh
}
234
235 88101661 O'Neil Delpratt
const char *XsltExecutable::getProperty(const char *name) {
236
    std::map<std::string, std::string>::iterator it;
237
    it = properties.find(std::string(name));
238
    if (it != properties.end())
239
        return it->second.c_str();
240
    return nullptr;
241 72bf04c6 Norman Walsh
}
242
243
void XsltExecutable::clearParameters(bool delValues) {
244 88101661 O'Neil Delpratt
    if (delValues) {
245
        for (std::map<std::string, XdmValue *>::iterator itr = parameters.begin(); itr != parameters.end(); itr++) {
246 72bf04c6 Norman Walsh
247 88101661 O'Neil Delpratt
            XdmValue *value = itr->second;
248 146cfacb O'Neil Delpratt
            if(value != nullptr) {
249
                value->decrementRefCount();
250 72bf04c6 Norman Walsh
#ifdef DEBUG
251 146cfacb O'Neil Delpratt
                std::cout<<"clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
252 72bf04c6 Norman Walsh
#endif
253 146cfacb O'Neil Delpratt
                if (value->getRefCount() < 1) {
254
                    delete value;
255
                }
256 88101661 O'Neil Delpratt
            }
257
        }
258 72bf04c6 Norman Walsh
259 03fcd3f9 O'Neil Delpratt
        if(selection) {
260
            SaxonProcessor::sxn_environ->env->DeleteGlobalRef(selection);
261
            selection = nullptr;
262
        }
263 88101661 O'Neil Delpratt
    } else {
264
        for (std::map<std::string, XdmValue *>::iterator itr = parameters.begin(); itr != parameters.end(); itr++) {
265 72bf04c6 Norman Walsh
266 88101661 O'Neil Delpratt
            XdmValue *value = itr->second;
267 146cfacb O'Neil Delpratt
            if(value != nullptr) {
268
                value->decrementRefCount();
269
            }
270 72bf04c6 Norman Walsh
271 88101661 O'Neil Delpratt
        }
272 03fcd3f9 O'Neil Delpratt
        if(selection) {
273
            SaxonProcessor::sxn_environ->env->DeleteGlobalRef(selection);
274
            selection = nullptr;
275
        }
276 88101661 O'Neil Delpratt
    }
277
    parameters.clear();
278 72bf04c6 Norman Walsh
279
280
}
281
282
void XsltExecutable::clearProperties() {
283 88101661 O'Neil Delpratt
    properties.clear();
284 72bf04c6 Norman Walsh
285
}
286
287
288 88101661 O'Neil Delpratt
std::map<std::string, XdmValue *> &XsltExecutable::getParameters() {
289
    std::map<std::string, XdmValue *> &ptr = parameters;
290
    return ptr;
291
}
292
293
std::map<std::string, std::string> &XsltExecutable::getProperties() {
294
    std::map<std::string, std::string> &ptr = properties;
295
    return ptr;
296
}
297 72bf04c6 Norman Walsh
298 88101661 O'Neil Delpratt
void XsltExecutable::exceptionClear() {
299
    SaxonProcessor::sxn_environ->env->ExceptionClear();
300 72bf04c6 Norman Walsh
}
301
302 88101661 O'Neil Delpratt
void XsltExecutable::setcwd(const char *dir) {
303
    if (dir != nullptr) {
304
        cwdXE = std::string(dir);
305
    }
306 72bf04c6 Norman Walsh
}
307
308
309 88101661 O'Neil Delpratt
void XsltExecutable::exportStylesheet(const char *filename) {
310
311
312
    static jmethodID exportmID = nullptr;
313
314
    if (!exportmID) {
315
        exportmID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "save",
316
                                                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;)V");
317
    }
318
319
    if (filename == nullptr) {
320
        std::cerr << "Error: Error: export file name is nullptr" << std::endl;
321
        return;
322
    }
323
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, exportmID,
324
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(
325
                                                                   cwdXE.c_str()),
326
                                                           executableObject,
327
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
328
329
}
330
331
void XsltExecutable::applyTemplatesReturningFile(const char *output_filename) {
332
333
    if (selection == nullptr) {
334
        std::cerr
335
                << "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."
336
                << std::endl;
337
        return;
338
    }
339
340
    static jmethodID atmID = nullptr;
341
342
    if (atmID == nullptr) {
343 901b6eca O'Neil Delpratt
        atmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
344 88101661 O'Neil Delpratt
                                                                        "applyTemplatesReturningFile",
345
                                                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
346
    }
347
348
    if (!atmID) {
349 901b6eca O'Neil Delpratt
        std::cerr << "Error: " << getDllname() << ".applyTemplatesReturningFile" << " not found\n"
350 88101661 O'Neil Delpratt
                  << std::endl;
351
352
    } else {
353
        JParameters comboArrays;
354
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
355
        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, atmID,
356
                                                                 SaxonProcessor::sxn_environ->env->NewStringUTF(
357
                                                                         cwdXE.c_str()), executableObject, selection,
358
                                                                 (output_filename != nullptr
359
                                                                  ? SaxonProcessor::sxn_environ->env->NewStringUTF(
360
                                                                                 output_filename) : nullptr),
361
                                                                 comboArrays.stringArray, comboArrays.objectArray);
362
        if (comboArrays.stringArray != nullptr) {
363
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
364
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
365
        }
366 72bf04c6 Norman Walsh
367
    }
368 88101661 O'Neil Delpratt
    return;
369
370
}
371
372
const char *XsltExecutable::applyTemplatesReturningString() {
373 72bf04c6 Norman Walsh
374 88101661 O'Neil Delpratt
    if (selection == nullptr) {
375
        std::cerr
376
                << "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."
377
                << std::endl;
378
        return nullptr;
379
    }
380
381
382 901b6eca O'Neil Delpratt
    static jmethodID atsmID = nullptr;
383
384
    if (atsmID == nullptr) {
385
        atsmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
386
                                                                             "applyTemplatesReturningString",
387
                                                                             "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)[B");
388
    }
389 88101661 O'Neil Delpratt
    if (!atsmID) {
390
        std::cerr << "Error: " << getDllname() << "applyTemplatesAsString" << " not found\n"
391
                  << std::endl;
392
393
    } else {
394
        JParameters comboArrays;
395
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
396
397 e05bbe34 O'Neil Delpratt
        jbyteArray result = nullptr;
398 88101661 O'Neil Delpratt
        jobject obj = (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, atsmID,
399
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
400
                                                                                        cwdXE.c_str()), executableObject,selection,
401
                comboArrays.stringArray, comboArrays.objectArray));
402
403
        if (obj) {
404 e05bbe34 O'Neil Delpratt
            result = (jbyteArray) obj;
405 88101661 O'Neil Delpratt
        }
406
        if (comboArrays.stringArray != nullptr) {
407
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
408
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
409
        }
410
        if (result) {
411 e05bbe34 O'Neil Delpratt
            jboolean isCopy = false;
412
            jbyte* b = SaxonProcessor::sxn_environ->env->GetByteArrayElements(result, &isCopy);
413 9458d238 O'Neil Delpratt
            jsize num_bytes = SaxonProcessor::sxn_environ->env->GetArrayLength(result);
414
415
            const char *str = new char[num_bytes];
416
            memcpy ((void *) str, b , num_bytes );
417
418 88101661 O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
419
            return str;
420
        }
421
    }
422
    return nullptr;
423
424
425
}
426
427
XdmValue *XsltExecutable::applyTemplatesReturningValue() {
428
429
    if (selection == nullptr) {
430
        std::cerr
431
                << "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."
432
                << std::endl;
433
        return nullptr;
434
    }
435
436
437 901b6eca O'Neil Delpratt
    static jmethodID atsvmID = nullptr;
438
    if (atsvmID == nullptr) {
439
        atsvmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
440 88101661 O'Neil Delpratt
                                                                                 "applyTemplatesReturningValue",
441
                                                                                 "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
442
    }
443 901b6eca O'Neil Delpratt
    if (!atsvmID) {
444 88101661 O'Neil Delpratt
        std::cerr << "Error: " << getDllname() << "applyTemplatesAsValue" << " not found\n"
445
                  << std::endl;
446
447
    } else {
448
        JParameters comboArrays;
449
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
450
451
452
        // jstring result = nullptr;
453 901b6eca O'Neil Delpratt
        jobject result = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, atsvmID,
454 88101661 O'Neil Delpratt
                                                                                             SaxonProcessor::sxn_environ->env->NewStringUTF(
455
                                                                                                     cwdXE.c_str()),
456
                                                                                             executableObject,
457
                                                                                             selection,
458
                                                                                             comboArrays.stringArray,
459
                                                                                             comboArrays.objectArray));
460
        /*if(obj) {
461
            result = (jobject)obj;
462
        }*/
463
        if (comboArrays.stringArray != nullptr) {
464
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
465
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
466
        }
467
        if (result) {
468
            jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
469
                                                   "net/sf/saxon/s9api/XdmAtomicValue");
470
            jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
471
            jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
472
                                                    "net/sf/saxon/s9api/XdmFunctionItem");
473
            XdmValue *value = nullptr;
474
            XdmItem *xdmItem = nullptr;
475
476
477
            if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass) == JNI_TRUE) {
478
                xdmItem = new XdmAtomicValue(result);
479
480
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
481
                return xdmItem;
482
483
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass) == JNI_TRUE) {
484
                xdmItem = new XdmNode(result);
485
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
486
                return xdmItem;
487
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass) == JNI_TRUE) {
488
                xdmItem = new XdmFunctionItem(result);
489
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
490
                return xdmItem;
491
            } else {
492
                value = new XdmValue(result, true);
493
                for (int z = 0; z < value->size(); z++) {
494
                }
495
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
496
                return value;
497
            }
498
        }
499
    }
500
    return nullptr;
501
502
}
503
504
505
void XsltExecutable::callFunctionReturningFile(const char *functionName, XdmValue **arguments, int argument_length,
506
                                               const char *outfile) {
507
508
509
    static jmethodID afmID = nullptr;
510
511
    if (afmID == nullptr) {
512
        afmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
513
                                                                                "callFunctionReturningFile",
514
                                                                                "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)V");
515
    }
516
517
    if (!afmID) {
518
        std::cerr << "Error: " << getDllname() << "callFunctionReturningFile" << " not found\n"
519
                  << std::endl;
520
        return;
521
    } else {
522
        JParameters comboArrays;
523
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
524
525
        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
526
527
        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, afmID,
528
                                                                 SaxonProcessor::sxn_environ->env->NewStringUTF(
529
                                                                         cwdXE.c_str()),
530
                                                                 executableObject,
531
                                                                 (functionName != nullptr ?
532
                                                                  SaxonProcessor::sxn_environ->env->NewStringUTF(
533
                                                                          functionName) :
534
                                                                  nullptr), argumentJArray,
535
                                                                 (outfile != nullptr ?
536
                                                                  SaxonProcessor::sxn_environ->env->NewStringUTF(
537
                                                                          outfile) :
538
                                                                  nullptr),
539
                                                                 comboArrays.stringArray, comboArrays.objectArray);
540
        if (comboArrays.stringArray != nullptr) {
541
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
542
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
543
        }
544
        if (argumentJArray != nullptr) {
545
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(argumentJArray);
546
        }
547
548
    }
549
    return;
550
551
552
}
553
554
const char *
555
XsltExecutable::callFunctionReturningString(const char *functionName, XdmValue **arguments, int argument_length) {
556
557
558 901b6eca O'Neil Delpratt
    static jmethodID afsmID = nullptr;
559
560
    if(afsmID == nullptr) {
561
        (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
562
                                                                        "callFunctionReturningString",
563 9458d238 O'Neil Delpratt
                                                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)[B");
564 901b6eca O'Neil Delpratt
    }
565 88101661 O'Neil Delpratt
    if (!afsmID) {
566
        std::cerr << "Error: " << getDllname() << "callFunctionReturningString" << " not found\n"
567
                  << std::endl;
568
569
    } else {
570
        JParameters comboArrays;
571
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
572
        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
573
574 9458d238 O'Neil Delpratt
        jbyteArray result = nullptr;
575 88101661 O'Neil Delpratt
        jobject obj = (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, afsmID,
576
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
577
                                                                                        cwdXE.c_str()), executableObject,
578
                                                                                        (functionName != nullptr
579
                                                                                         ? SaxonProcessor::sxn_environ->env->NewStringUTF(
580
                                                                                                        functionName)
581
                                                                                         : nullptr),
582
                                                                                argumentJArray, comboArrays.stringArray,
583
                                                                                comboArrays.objectArray));
584
        if (obj) {
585 9458d238 O'Neil Delpratt
            result = (jbyteArray) obj;
586 88101661 O'Neil Delpratt
        }
587
        if (comboArrays.stringArray != nullptr) {
588
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
589
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
590
        }
591
        if (result) {
592 9458d238 O'Neil Delpratt
            jboolean isCopy = false;
593
            jbyte* b = SaxonProcessor::sxn_environ->env->GetByteArrayElements(result, &isCopy);
594
            jsize num_bytes = SaxonProcessor::sxn_environ->env->GetArrayLength(result);
595
596
            const char *str = new char[num_bytes];
597
            memcpy ((void *) str, b , num_bytes );
598
599 88101661 O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
600
            return str;
601
        }
602
    }
603
    return nullptr;
604 72bf04c6 Norman Walsh
}
605
606
607 88101661 O'Neil Delpratt
XdmValue *
608
XsltExecutable::callFunctionReturningValue(const char *functionName, XdmValue **arguments, int argument_length) {
609
610
611
    static jmethodID cfvmID = nullptr;
612
    if (cfvmID == nullptr) {
613
        cfvmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
614
                                                                                 "callFunctionReturningValue",
615
                                                                                 "(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;");
616
    }
617
    if (!cfvmID) {
618
        std::cerr << "Error: " << getDllname() << "callFunctionReturningValue" << " not found\n"
619
                  << std::endl;
620 72bf04c6 Norman Walsh
621 88101661 O'Neil Delpratt
    } else {
622
        JParameters comboArrays;
623
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
624
        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
625
626
        jobject result = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, cfvmID,
627
                                                                                             SaxonProcessor::sxn_environ->env->NewStringUTF(
628
                                                                                                     cwdXE.c_str()),
629
                                                                                             executableObject,
630
                                                                                             (functionName != nullptr
631
                                                                                              ? SaxonProcessor::sxn_environ->env->NewStringUTF(
632
                                                                                                             functionName)
633
                                                                                              : nullptr),
634
                                                                                             argumentJArray,
635
                                                                                             comboArrays.stringArray,
636
                                                                                             comboArrays.objectArray));
637
638
        if (comboArrays.stringArray != nullptr) {
639
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
640
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
641
        }
642
        if (argumentJArray != nullptr) {
643
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(argumentJArray);
644
        }
645
        if (result) {
646
            jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
647
                                                   "net/sf/saxon/s9api/XdmAtomicValue");
648
            jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
649
            jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
650
                                                    "net/sf/saxon/s9api/XdmFunctionItem");
651
            XdmValue *value = nullptr;
652
            XdmItem *xdmItem = nullptr;
653 72bf04c6 Norman Walsh
654
655 88101661 O'Neil Delpratt
            if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass) == JNI_TRUE) {
656
                xdmItem = new XdmAtomicValue(result);
657
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
658
                return xdmItem;
659
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass) == JNI_TRUE) {
660
                xdmItem = new XdmNode(result);
661
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
662
                return xdmItem;
663 72bf04c6 Norman Walsh
664 88101661 O'Neil Delpratt
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass) == JNI_TRUE) {
665
                xdmItem = new XdmFunctionItem(result);
666
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
667
                return xdmItem;
668
            } else {
669
                value = new XdmValue(result, true);
670
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
671
                return value;
672
            }
673
            value = new XdmValue();
674
            value->addUnderlyingValue(result);
675
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
676
            return value;
677 72bf04c6 Norman Walsh
        }
678 88101661 O'Neil Delpratt
    }
679
    return nullptr;
680
681
}
682
683
684
void XsltExecutable::callTemplateReturningFile(const char *templateName, const char *outfile) {
685
686
687 901b6eca O'Neil Delpratt
    static jmethodID ctmID = nullptr;
688
689
    if(ctmID == nullptr) {
690
        ctmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
691
                                                                        "callTemplateReturningFile",
692
                                                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
693
    }
694 88101661 O'Neil Delpratt
    if (!ctmID) {
695
        std::cerr << "Error: " << getDllname() << "callTemplateReturningFile" << " not found\n"
696
                  << std::endl;
697 72bf04c6 Norman Walsh
698 88101661 O'Neil Delpratt
    } else {
699
        JParameters comboArrays;
700
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
701
        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, ctmID,
702
                                                                 SaxonProcessor::sxn_environ->env->NewStringUTF(
703
                                                                         cwdXE.c_str()),
704
                                                                 executableObject,
705
                                                                 (templateName != nullptr
706
                                                                  ? SaxonProcessor::sxn_environ->env->NewStringUTF(
707
                                                                                 templateName) : nullptr),
708
                                                                 (outfile != nullptr
709
                                                                  ? SaxonProcessor::sxn_environ->env->NewStringUTF(
710
                                                                                 outfile) : nullptr),
711
                                                                 comboArrays.stringArray, comboArrays.objectArray);
712
        if (comboArrays.stringArray != nullptr) {
713
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
714
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
715 72bf04c6 Norman Walsh
        }
716 88101661 O'Neil Delpratt
717 72bf04c6 Norman Walsh
718
    }
719
720 88101661 O'Neil Delpratt
}
721
722
723
const char *XsltExecutable::callTemplateReturningString(const char *templateName) {
724
725
726 901b6eca O'Neil Delpratt
    jmethodID ctsmID = nullptr;
727
728
    if(ctsmID == nullptr) {
729
        ctsmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
730
                                                                        "callTemplateReturningString",
731 9458d238 O'Neil Delpratt
                                                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)[B");
732 901b6eca O'Neil Delpratt
    }
733 88101661 O'Neil Delpratt
    if (!ctsmID) {
734
        std::cerr << "Error: " << getDllname() << "callTemplateReturningString" << " not found\n"
735
                  << std::endl;
736
737
    } else {
738
        JParameters comboArrays;
739
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
740 72bf04c6 Norman Walsh
741
742 9458d238 O'Neil Delpratt
        jbyteArray result = nullptr;
743 88101661 O'Neil Delpratt
        jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, ctsmID,
744
                                                                                          SaxonProcessor::sxn_environ->env->NewStringUTF(
745
                                                                                                  cwdXE.c_str()),
746
                                                                                          executableObject,
747
                                                                                          (templateName != nullptr
748
                                                                                           ? SaxonProcessor::sxn_environ->env->NewStringUTF(
749
                                                                                                          templateName)
750
                                                                                           : nullptr),
751
                                                                                          comboArrays.stringArray,
752
                                                                                          comboArrays.objectArray));
753
        if (obj) {
754 9458d238 O'Neil Delpratt
            result = (jbyteArray) obj;
755 88101661 O'Neil Delpratt
        }
756
        if (comboArrays.stringArray != nullptr) {
757
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
758
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
759
        }
760
        if (result) {
761 9458d238 O'Neil Delpratt
            jboolean isCopy = false;
762
            jbyte* b = SaxonProcessor::sxn_environ->env->GetByteArrayElements(result, &isCopy);
763
            jsize num_bytes = SaxonProcessor::sxn_environ->env->GetArrayLength(result);
764
765
            const char *str = new char[num_bytes];
766
            memcpy ((void *) str, b , num_bytes );
767
768 88101661 O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
769
            return str;
770
        }
771
    }
772
    return nullptr;
773 72bf04c6 Norman Walsh
774
775 88101661 O'Neil Delpratt
}
776
777
XdmValue *XsltExecutable::callTemplateReturningValue(const char *templateName) {
778 72bf04c6 Norman Walsh
779
780 901b6eca O'Neil Delpratt
    static jmethodID ctsvmID = nullptr;
781
    if (ctsvmID == nullptr) {
782
        ctsvmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
783 88101661 O'Neil Delpratt
                                                                                 "callTemplateReturningValue",
784
                                                                                 "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
785
    }
786 901b6eca O'Neil Delpratt
    if (!ctsvmID) {
787 88101661 O'Neil Delpratt
        std::cerr << "Error: " << getDllname() << "callTemplateReturningValue" << " not found\n"
788
                  << std::endl;
789 72bf04c6 Norman Walsh
790 88101661 O'Neil Delpratt
    } else {
791 72bf04c6 Norman Walsh
        JParameters comboArrays;
792 88101661 O'Neil Delpratt
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
793
794
795
        jstring result = nullptr;
796 901b6eca O'Neil Delpratt
        jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, ctsvmID,
797 88101661 O'Neil Delpratt
                                                                                          SaxonProcessor::sxn_environ->env->NewStringUTF(
798
                                                                                                  cwdXE.c_str()),
799
                                                                                          executableObject,
800
                                                                                          (templateName != nullptr
801
                                                                                           ? SaxonProcessor::sxn_environ->env->NewStringUTF(
802
                                                                                                          templateName)
803
                                                                                           : nullptr),
804
                                                                                          comboArrays.stringArray,
805
                                                                                          comboArrays.objectArray));
806
        if (obj) {
807
            result = (jstring) obj;
808
        }
809
        if (comboArrays.stringArray != nullptr) {
810
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
811
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
812
        }
813 72bf04c6 Norman Walsh
        if (result) {
814 88101661 O'Neil Delpratt
            jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
815
                                                   "net/sf/saxon/s9api/XdmAtomicValue");
816
            jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
817
            jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
818
                                                    "net/sf/saxon/s9api/XdmFunctionItem");
819
            XdmValue *value = nullptr;
820
821
            XdmItem *xdmItem = nullptr;
822
            if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass) == JNI_TRUE) {
823
                xdmItem = new XdmAtomicValue(result);
824
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
825
                return xdmItem;
826
827
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass) == JNI_TRUE) {
828
                xdmItem = new XdmNode(result);
829
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
830
                return xdmItem;
831
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass) == JNI_TRUE) {
832
                xdmItem = new XdmFunctionItem(result);
833 72bf04c6 Norman Walsh
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
834
                return xdmItem;
835 88101661 O'Neil Delpratt
            } else {
836
                value = new XdmValue(result, true);
837
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
838
                return value;
839
            }
840
            value = new XdmValue();
841 72bf04c6 Norman Walsh
            value->addUnderlyingValue(result);
842 88101661 O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
843
            return value;
844
        }
845
    }
846
    return nullptr;
847
}
848
849 72bf04c6 Norman Walsh
850 88101661 O'Neil Delpratt
XdmValue *XsltExecutable::transformFileToValue(const char *sourcefile) {
851 72bf04c6 Norman Walsh
852 88101661 O'Neil Delpratt
    if (sourcefile == nullptr) {
853
854
        return nullptr;
855 72bf04c6 Norman Walsh
    }
856
857
858 88101661 O'Neil Delpratt
    static jmethodID tfvMID = nullptr;
859 72bf04c6 Norman Walsh
860 88101661 O'Neil Delpratt
    if (tfvMID == nullptr) {
861
        tfvMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
862
                                                                                 "transformToValue",
863
                                                                                 "(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;");
864
    }
865
    if (!tfvMID) {
866
        std::cerr << "Error: " << getDllname() << ".transformtoValue" << " not found\n"
867
                  << std::endl;
868 72bf04c6 Norman Walsh
869 88101661 O'Neil Delpratt
    } else {
870
        JParameters comboArrays;
871
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
872
873
        jobject result = (jobject) (
874
                SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, tfvMID,
875
                                                                         SaxonProcessor::sxn_environ->env->NewStringUTF(
876
                                                                                 cwdXE.c_str()), nullptr, executableObject,
877
                                                                         (sourcefile != nullptr
878
                                                                          ? SaxonProcessor::sxn_environ->env->NewStringUTF(
879
                                                                                         sourcefile) : nullptr),
880
                                                                         comboArrays.stringArray,
881
                                                                         comboArrays.objectArray));
882
        if (comboArrays.stringArray != nullptr) {
883
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
884
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
885
        }
886
        if (result) {
887
            jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
888
                                                   "net/sf/saxon/s9api/XdmAtomicValue");
889
            jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
890
            jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
891
                                                    "net/sf/saxon/s9api/XdmFunctionItem");
892
            jclass mapItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
893
                                                    "net/sf/saxon/s9api/XdmMap");
894
            jclass arrayItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
895
                                               "net/sf/saxon/s9api/XdmArray");
896
            XdmValue *value = nullptr;
897
            XdmItem *xdmItem = nullptr;
898
899
900
            if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass) == JNI_TRUE) {
901
                xdmItem = new XdmAtomicValue(result);
902
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
903
                return xdmItem;
904
905
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass) == JNI_TRUE) {
906
                xdmItem = new XdmNode(result);
907
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
908
                return xdmItem;
909
910
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass) == JNI_TRUE) {
911
                xdmItem = new XdmFunctionItem(result);
912
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
913
                return xdmItem;
914
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, mapItemClass) == JNI_TRUE) {
915
                xdmItem = new XdmMap(result);
916
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
917
                return xdmItem;
918
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, arrayItemClass) == JNI_TRUE) {
919
                xdmItem = new XdmArray(result);
920
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
921
                return xdmItem;
922
            } else {
923
                value = new XdmValue(result, true);
924
                return value;
925
            }
926
            value = new XdmValue();
927
            value->addUnderlyingValue(result);
928
929
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
930
            return value;
931
        }
932
    }
933
    return nullptr;
934
935
}
936
937
938
void XsltExecutable::transformFileToFile(const char *source, const char *outputfile) {
939 72bf04c6 Norman Walsh
940 88101661 O'Neil Delpratt
941
    static jmethodID tffMID = nullptr;
942
943
    if (tffMID == nullptr) {
944
        tffMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
945
                                                                                 "transformToFile",
946
                                                                                 "(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");
947
    }
948
    if (!tffMID) {
949
        std::cerr << "Error: " << getDllname() << "transformToFile" << " not found\n"
950
                  << std::endl;
951
952
    } else {
953 72bf04c6 Norman Walsh
        JParameters comboArrays;
954
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
955
956 88101661 O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, tffMID,
957
                                                               SaxonProcessor::sxn_environ->env->NewStringUTF(
958
                                                                       cwdXE.c_str()), nullptr, executableObject,
959
                                                                       (source != nullptr
960
                                                                        ? SaxonProcessor::sxn_environ->env->NewStringUTF(
961
                                                                                       source) : nullptr), nullptr,
962
                                                               (outputfile != nullptr
963
                                                                ? SaxonProcessor::sxn_environ->env->NewStringUTF(
964
                                                                               outputfile) : nullptr),
965
                                                               comboArrays.stringArray, comboArrays.objectArray);
966
        if (comboArrays.stringArray != nullptr) {
967
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
968
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
969
        }
970
    }
971 72bf04c6 Norman Walsh
972 88101661 O'Neil Delpratt
}
973 72bf04c6 Norman Walsh
974
975 88101661 O'Neil Delpratt
void XsltExecutable::setupXslMessage(bool create, const char * filename) {
976
    if (create) {
977 72bf04c6 Norman Walsh
978 901b6eca O'Neil Delpratt
        static jmethodID messageID =   nullptr;
979
        if(messageID == nullptr) {
980
            messageID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(messageListenerClass,
981
                                                                                  "<init>",
982
                                                                                  "(Ljava/lang/String;Ljava/lang/String;)V");
983
        }
984 88101661 O'Neil Delpratt
        if (!messageID) {
985
            std::cerr << "Error: "<<"SaxonCMessageListener" << " in " <<getDllname() << " not found\n"
986
                      << std::endl;
987 72bf04c6 Norman Walsh
988 88101661 O'Neil Delpratt
        }
989
990
        if (filename == nullptr) {
991
            saxonMessageListenerObj = (jobject)SaxonProcessor::sxn_environ->env->NewObject(messageListenerClass, messageID,
992
                                                                                           SaxonProcessor::sxn_environ->env->NewStringUTF("-:on"));
993
            //setProperty("m", "on");
994 72bf04c6 Norman Walsh
        } else {
995 88101661 O'Neil Delpratt
            saxonMessageListenerObj = (jobject)SaxonProcessor::sxn_environ->env->NewObject(messageListenerClass, messageID,
996
                                                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
997 72bf04c6 Norman Walsh
        }
998
    } else {
999
        setProperty("m", "off");
1000
    }
1001 88101661 O'Neil Delpratt
1002
1003 72bf04c6 Norman Walsh
}
1004
1005 88101661 O'Neil Delpratt
XdmValue * XsltExecutable::getXslMessages(){
1006 72bf04c6 Norman Walsh
1007 88101661 O'Neil Delpratt
    if(saxonMessageListenerObj) {
1008
        static jmethodID getmessageID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(messageListenerClass,
1009
                                                                                                  "getXslMessages",
1010
                                                                                                  "()[Lnet/sf/saxon/s9api/XdmNode;");
1011
        if (!getmessageID) {
1012
            std::cerr << "Error: " << getDllname() << ".getXslMessages" << " not found\n"
1013
                      << std::endl;
1014 72bf04c6 Norman Walsh
1015 88101661 O'Neil Delpratt
        } else {
1016
            jobjectArray results = (jobjectArray) (
1017
                    SaxonProcessor::sxn_environ->env->CallObjectMethod(saxonMessageListenerObj, getmessageID));
1018
            int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
1019 72bf04c6 Norman Walsh
1020 88101661 O'Neil Delpratt
            if (sizex > 0) {
1021
                XdmValue *value = new XdmValue();
1022 72bf04c6 Norman Walsh
1023 88101661 O'Neil Delpratt
                for (int p = 0; p < sizex; ++p) {
1024
                    jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
1025
                    value->addUnderlyingValue(resulti);
1026
                }
1027
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(results);
1028
                return value;
1029
            }
1030
        }
1031 72bf04c6 Norman Walsh
    }
1032 88101661 O'Neil Delpratt
    return NULL;
1033
1034 72bf04c6 Norman Walsh
1035
}
1036
1037
1038 88101661 O'Neil Delpratt
const char *XsltExecutable::transformFileToString(const char *source) {
1039 72bf04c6 Norman Walsh
1040
1041 88101661 O'Neil Delpratt
    static jmethodID tftMID = tftMID;
1042
    if (tftMID == nullptr) {
1043
        tftMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
1044
                                                                                 "transformToString",
1045 a69dd173 O'Neil Delpratt
                                                                                 "(Ljava/lang/String;Lnet/sf/saxon/option/cpp/Xslt30Processor;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)[B");
1046 88101661 O'Neil Delpratt
    }
1047
    if (!tftMID) {
1048 a69dd173 O'Neil Delpratt
        std::cerr << "Error: " << getDllname() << "transformToString" << " not found\n"
1049 88101661 O'Neil Delpratt
                  << std::endl;
1050 72bf04c6 Norman Walsh
1051 88101661 O'Neil Delpratt
    } else {
1052
        JParameters comboArrays;
1053
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
1054 72bf04c6 Norman Walsh
1055 a69dd173 O'Neil Delpratt
        jbyteArray result = nullptr;
1056 88101661 O'Neil Delpratt
        jobject obj = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, tftMID,
1057
                                                                               SaxonProcessor::sxn_environ->env->NewStringUTF(
1058
                                                                                       cwdXE.c_str()), nullptr,
1059
                                                                               executableObject,
1060
                                                                               (source != nullptr
1061
                                                                                ? SaxonProcessor::sxn_environ->env->NewStringUTF(
1062
                                                                                               source) : nullptr),
1063 a69dd173 O'Neil Delpratt
                                                                               nullptr,
1064 88101661 O'Neil Delpratt
                                                                               comboArrays.stringArray,
1065
                                                                               comboArrays.objectArray);
1066
        if (comboArrays.stringArray != nullptr) {
1067
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
1068
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
1069
        }
1070
        if (obj) {
1071 a69dd173 O'Neil Delpratt
            result = (jbyteArray) obj;
1072 88101661 O'Neil Delpratt
        }
1073
        if (result) {
1074 a69dd173 O'Neil Delpratt
1075
            jboolean isCopy = false;
1076
            jbyte* b = SaxonProcessor::sxn_environ->env->GetByteArrayElements(result, &isCopy);
1077
            jsize num_bytes = SaxonProcessor::sxn_environ->env->GetArrayLength(result);
1078
1079
            const char *str = new char[num_bytes];
1080
            memcpy ((void *) str, b , num_bytes );
1081
1082 88101661 O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
1083
            return str;
1084
        }
1085
    }
1086
    return nullptr;
1087
}
1088
1089
1090
const char *XsltExecutable::transformToString(XdmNode *source) {
1091 72bf04c6 Norman Walsh
1092 88101661 O'Neil Delpratt
    if (source != nullptr) {
1093
        source->incrementRefCount();
1094
        parameters["node"] = source;
1095
    }
1096
    return transformFileToString(nullptr);
1097
}
1098 72bf04c6 Norman Walsh
1099
1100 88101661 O'Neil Delpratt
XdmValue *XsltExecutable::transformToValue(XdmNode *source) {
1101
1102
    if (source != nullptr) {
1103
        source->incrementRefCount();
1104
        parameters["node"] = source;
1105
    }
1106
    return transformFileToValue(nullptr);
1107
}
1108
1109
void XsltExecutable::transformToFile(XdmNode *source) {
1110
1111
    if (source != nullptr) {
1112
        source->incrementRefCount();
1113
        parameters["node"] = source;
1114
    }
1115
    transformFileToFile(nullptr, nullptr);
1116
}
1117
1118
const char *XsltExecutable::getErrorMessage() {
1119
    SaxonApiException *exception = SaxonProcessor::checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass,
1120
                                                                        nullptr);
1121
    if (exception == nullptr) { return nullptr; }
1122
    return exception->getMessage();
1123
}
1124
1125
1126
SaxonApiException *XsltExecutable::getException() {
1127
    return SaxonProcessor::checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass, nullptr);
1128
}