Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XsltExecutable.cpp @ 55b80284

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