Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XsltExecutable.cpp @ 5d25f10d

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