Project

Profile

Help

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

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

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