Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XsltExecutable.cpp @ 9e416749

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