Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XsltExecutable.cpp @ 02e2c377

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