Project

Profile

Help

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

he / src / main / c / Saxon.C.API / SaxonProcessor.cpp @ 6af44631

1 ead48a5d O'Neil Delpratt
#ifndef __linux__
2 72bf04c6 Norman Walsh
#ifndef __APPLE__
3 ead48a5d O'Neil Delpratt
//#include "stdafx.h"
4
#include <Tchar.h>
5 72bf04c6 Norman Walsh
#endif
6
#endif
7
8
#include "SaxonProcessor.h"
9
#include "XdmValue.h"
10
#include "XdmItem.h"
11
#include "XdmNode.h"
12 ead48a5d O'Neil Delpratt
#include "XdmFunctionItem.h"
13
#include "XdmMap.h"
14
#include "XdmArray.h"
15 72bf04c6 Norman Walsh
#include "XdmAtomicValue.h"
16
17 55b80284 O'Neil Delpratt
18 766106fe O'Neil Delpratt
//#define DEBUG
19 72bf04c6 Norman Walsh
#ifdef DEBUG
20
#include <signal.h>
21
#endif
22 ead48a5d O'Neil Delpratt
23 72bf04c6 Norman Walsh
#include <stdio.h>
24
25
26 55b80284 O'Neil Delpratt
#if defined MEM_DEBUG
27
28
void* newImpl(std::size_t sz,char const* file, int line){
29
    static int counter{};
30
    void* ptr= std::malloc(sz);
31
    std::cerr << "Saxon/C Mem test: "<<file << ": " << line << " " <<  ptr << std::endl;
32
    myAlloc.push_back(ptr);
33
    return ptr;
34
}
35
36
void* operator new(std::size_t sz,char const* file, int line){
37
return newImpl(sz,file,line);
38
}
39
40
void* operator new [](std::size_t sz,char const* file, int line){
41
return newImpl(sz,file,line);
42
}
43
44
void operator delete(void* ptr) noexcept{
45
    auto ind= std::distance(myAlloc.begin(),std::find(myAlloc.begin(), myAlloc.end(),ptr));
46
    myAlloc[ind]= nullptr;
47
    std::free(ptr);
48
}
49
50 7728ae0b O'Neil Delpratt
void operator delete(void*, std::size_t) noexcept {
51
    auto ind= std::distance(myAlloc.begin(),std::find(myAlloc.begin(), myAlloc.end(),ptr));
52
    myAlloc[ind]= nullptr;
53
    std::free(ptr);
54
55
}
56
57 55b80284 O'Neil Delpratt
#define new new(__FILE__, __LINE__)
58
59
void SaxonProcessor::getInfo(){
60
61
    std::cout << std::endl;
62
63
    std::cout << "Allocation: " << std::endl;
64
    for (auto i: myAlloc){
65
        if (i != nullptr ) std::cout << " " << i << std::endl;
66
    }
67
68
    std::cout << std::endl;
69
70
}
71
72
#endif
73
74 72bf04c6 Norman Walsh
//jobject cpp;
75 ead48a5d O'Neil Delpratt
const char *failure;
76
sxnc_environment *SaxonProcessor::sxn_environ = 0;
77
int SaxonProcessor::jvmCreatedCPP = 0;
78 72bf04c6 Norman Walsh
79 ead48a5d O'Neil Delpratt
bool SaxonProcessor::exceptionOccurred() {
80 00082f6f O'Neil Delpratt
    return SaxonProcessor::sxn_environ->env->ExceptionCheck() || exception != nullptr;
81 72bf04c6 Norman Walsh
}
82
83 4ee4355d O'Neil Delpratt
const char *SaxonProcessor::checkException() {
84 ead48a5d O'Neil Delpratt
    const char *message = nullptr;
85 4ee4355d O'Neil Delpratt
    message = checkForException(sxn_environ);
86 ead48a5d O'Neil Delpratt
    return message;
87
}
88 72bf04c6 Norman Walsh
89 55b80284 O'Neil Delpratt
90
91 ead48a5d O'Neil Delpratt
SaxonApiException *SaxonProcessor::checkAndCreateException(jclass cppClass) {
92 00082f6f O'Neil Delpratt
    if (SaxonProcessor::sxn_environ->env->ExceptionCheck()) {
93 ead48a5d O'Neil Delpratt
        SaxonApiException *exception = checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass, nullptr);
94 72bf04c6 Norman Walsh
#ifdef DEBUG
95 ead48a5d O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->ExceptionDescribe();
96 72bf04c6 Norman Walsh
#endif
97 ead48a5d O'Neil Delpratt
        return exception;
98
    }
99
    return nullptr;
100 72bf04c6 Norman Walsh
}
101
102 8d5b64f3 O'Neil Delpratt
const char *SaxonProcessor::getErrorMessage() {
103
    if(exception == nullptr) {
104
        exception =  SaxonProcessor::checkForExceptionCPP(SaxonProcessor::sxn_environ->env,
105
                                                                            saxonCAPIClass,nullptr);
106
    }
107
    if (exception == nullptr) { return nullptr; }
108
    return exception->getMessage();
109
}
110
111 ead48a5d O'Neil Delpratt
void SaxonProcessor::exceptionClear() {
112 306962bc O'Neil Delpratt
    if(exceptionOccurred()) {
113
        SaxonProcessor::sxn_environ->env->ExceptionClear();
114
    }
115
    if (exception != nullptr) {
116 55b80284 O'Neil Delpratt
        std::cerr<< "saxonProc exception= "<< exception <<std::endl;
117 306962bc O'Neil Delpratt
        delete exception;
118 55b80284 O'Neil Delpratt
        exception = nullptr;
119 306962bc O'Neil Delpratt
    }
120 00082f6f O'Neil Delpratt
121 ead48a5d O'Neil Delpratt
}
122 72bf04c6 Norman Walsh
123
124
SaxonProcessor::SaxonProcessor() {
125
    licensei = false;
126
    SaxonProcessor(false);
127
}
128
129 766106fe O'Neil Delpratt
const char * SaxonProcessor::getResourcesDirectory() {
130
    return NULL;
131
}
132
133 306962bc O'Neil Delpratt
void SaxonProcessor::createException(const char * message) {
134
135 55b80284 O'Neil Delpratt
    exceptionClear();
136 306962bc O'Neil Delpratt
137
    if(message == nullptr) {
138
        exception = checkAndCreateException(saxonCAPIClass);
139
    } else {
140
        exception = new SaxonApiException(message);
141
    }
142
143
}
144
145 72bf04c6 Norman Walsh
146 306962bc O'Neil Delpratt
SaxonApiException * SaxonProcessor::checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject) {
147 72bf04c6 Norman Walsh
148
    if (env->ExceptionCheck()) {
149 ead48a5d O'Neil Delpratt
        std::string result1 = "";
150
        std::string errorCode = "";
151
        jthrowable exc = env->ExceptionOccurred();
152 72bf04c6 Norman Walsh
153 ead48a5d O'Neil Delpratt
#ifdef DEBUG
154
        env->ExceptionDescribe();
155 72bf04c6 Norman Walsh
#endif
156 ead48a5d O'Neil Delpratt
        jclass exccls(env->GetObjectClass(exc));
157 72bf04c6 Norman Walsh
        jclass clscls(env->FindClass("java/lang/Class"));
158
159
        jmethodID getName(env->GetMethodID(clscls, "getName", "()Ljava/lang/String;"));
160
        jstring name(static_cast<jstring>(env->CallObjectMethod(exccls, getName)));
161 ead48a5d O'Neil Delpratt
        char const *utfName(env->GetStringUTFChars(name, 0));
162
        result1 = (std::string(utfName));
163
        env->ReleaseStringUTFChars(name, utfName);
164
165
        jmethodID getMessage(env->GetMethodID(exccls, "getMessage", "()Ljava/lang/String;"));
166
        if (getMessage) {
167
168
            jstring message((jstring) (env->CallObjectMethod(exc, getMessage)));
169
            char const *utfMessage = nullptr;
170
            if (!message) {
171
                utfMessage = "";
172
                return nullptr;
173
            } else {
174
                utfMessage = (env->GetStringUTFChars(message, 0));
175
            }
176
            if (utfMessage != nullptr) {
177
                result1 = (result1 + " : ") + utfMessage;
178 72bf04c6 Norman Walsh
            }
179
180 ead48a5d O'Neil Delpratt
            env->ReleaseStringUTFChars(message, utfMessage);
181
182
            if (callingObject != nullptr && result1.compare(0, 43, "net.sf.saxon.s9api.SaxonApiException", 43) == 0) {
183
184
                jclass saxonApiExceptionClass(env->FindClass("net/sf/saxon/s9api/SaxonApiException"));
185
                static jmethodID lineNumID = nullptr;
186
                if (lineNumID == nullptr) {
187
                    lineNumID = env->GetMethodID(saxonApiExceptionClass, "getLinenumber", "()I");
188
                }
189
                static jmethodID ecID = nullptr;
190
                if (ecID == nullptr) {
191
                    ecID = env->GetMethodID(saxonApiExceptionClass, "getErrorCode", "()Ljnet/sf/saxon/s9api/QName;");
192
                }
193
                static jmethodID esysID = nullptr;
194
                if (esysID == nullptr) {
195
                    esysID = env->GetMethodID(saxonApiExceptionClass, "getSystemId", "()Ljava/lang/String;");
196
                }
197
198
199
                jobject errCodeQName = (jobject) (env->CallObjectMethod(exc, ecID));
200
                jstring errSystemID = (jstring) (env->CallObjectMethod(exc, esysID));
201
                int linenum = env->CallIntMethod(exc, lineNumID);
202
203
                jclass qnameClass(env->FindClass("net/sf/saxon/s9api/QName"));
204
                static jmethodID qnameStrID = nullptr;
205
                if (qnameStrID == nullptr) {
206
                    qnameStrID = env->GetMethodID(qnameClass, "toString", "()Ljava/lang/String;");
207
                }
208
209
                jstring qnameStr = (jstring) (env->CallObjectMethod(errCodeQName, qnameStrID));
210
211
212
                SaxonApiException *saxonExceptions = new SaxonApiException(result1.c_str(),
213
                                                                           (qnameStr ? env->GetStringUTFChars(qnameStr,
214
                                                                                                              0)
215
                                                                                     : nullptr),
216
                                                                           (errSystemID ? env->GetStringUTFChars(
217
                                                                                   errSystemID, 0) : nullptr), linenum);
218
219
                if (errCodeQName) {
220
                    env->DeleteLocalRef(errCodeQName);
221
                }
222
                if (errSystemID) {
223
                    env->DeleteLocalRef(errSystemID);
224
                }
225
                if (qnameStr) {
226
                    env->DeleteLocalRef(qnameStr);
227
                }
228
229
                if (message) {
230
                    env->DeleteLocalRef(message);
231
                }
232
                env->ExceptionClear();
233
                return saxonExceptions;
234
            }
235
        }
236
        SaxonApiException *saxonExceptions = new SaxonApiException(result1.c_str());
237
        //env->ExceptionDescribe();
238
        env->ExceptionClear();
239
        return saxonExceptions;
240
    }
241
    return nullptr;
242 72bf04c6 Norman Walsh
243
}
244
245
246 ead48a5d O'Neil Delpratt
SaxonProcessor::SaxonProcessor(bool l) {
247 72bf04c6 Norman Walsh
248 ead48a5d O'Neil Delpratt
    cwd = "";
249 72bf04c6 Norman Walsh
    licensei = l;
250 ead48a5d O'Neil Delpratt
    versionStr = nullptr;
251 01d6fdb6 O'Neil Delpratt
    exception = nullptr;
252 ead48a5d O'Neil Delpratt
253
    if (SaxonProcessor::jvmCreatedCPP == 0) {
254
        SaxonProcessor::jvmCreatedCPP = 1;
255
        SaxonProcessor::sxn_environ = new sxnc_environment;//(sxnc_environment *)malloc(sizeof(sxnc_environment));
256 72bf04c6 Norman Walsh
257
258 ead48a5d O'Neil Delpratt
        /*
259
         * First of all, load required component.
260
         * By the time of JET initialization, all components should be loaded.
261
         */
262 72bf04c6 Norman Walsh
263 ead48a5d O'Neil Delpratt
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
264 72bf04c6 Norman Walsh
265 ead48a5d O'Neil Delpratt
        /*
266
         * Initialize JET run-time.
267
         * The handle of loaded component is used to retrieve Invocation API.
268
         */
269
        initDefaultJavaRT(SaxonProcessor::sxn_environ);
270 72bf04c6 Norman Walsh
    } else {
271
#ifdef DEBUG
272 ead48a5d O'Neil Delpratt
        std::cerr<<"SaxonProc constructor: jvm exists! jvmCreatedCPP="<<jvmCreatedCPP<<std::endl;
273 72bf04c6 Norman Walsh
#endif
274
275 ead48a5d O'Neil Delpratt
    }
276
277 72bf04c6 Norman Walsh
278
    versionClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/Version");
279
    procClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/Processor");
280
    saxonCAPIClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/SaxonCAPI");
281 ead48a5d O'Neil Delpratt
282
    jobject proci = createSaxonProcessor(SaxonProcessor::sxn_environ->env, procClass, "(Z)V", nullptr, licensei);
283 61decfd8 Norman Walsh
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(proci);
284 ead48a5d O'Neil Delpratt
    if (!proc) {
285
        std::cout << "proc is nullptr in SaxonProcessor constructor" << std::endl;
286
    }
287 72bf04c6 Norman Walsh
288
    xdmAtomicClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
289
#ifdef DEBUG
290 ead48a5d O'Neil Delpratt
    jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "setDebugMode", "(Z)V");
291
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(saxonCAPIClass, debugMID, (jboolean)true);
292 72bf04c6 Norman Walsh
#endif
293
}
294
295 ead48a5d O'Neil Delpratt
SaxonProcessor::SaxonProcessor(const char *configFile) {
296
    cwd = "";
297
    versionStr = nullptr;
298 72bf04c6 Norman Walsh
299 ead48a5d O'Neil Delpratt
    if (SaxonProcessor::jvmCreatedCPP == 0) {
300
        SaxonProcessor::jvmCreatedCPP = 1;
301
        //SaxonProcessor::sxn_environ= new sxnc_environment;
302
        SaxonProcessor::sxn_environ = (sxnc_environment *) malloc(sizeof(sxnc_environment));
303 72bf04c6 Norman Walsh
304 ead48a5d O'Neil Delpratt
        /*
305
         * First of all, load required component.
306
         * By the time of JET initialization, all components should be loaded.
307
         */
308 72bf04c6 Norman Walsh
309 ead48a5d O'Neil Delpratt
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
310 72bf04c6 Norman Walsh
311 ead48a5d O'Neil Delpratt
        /*
312
         * Initialize JET run-time.
313
         * The handle of loaded component is used to retrieve Invocation API.
314
         */
315
        initDefaultJavaRT(SaxonProcessor::sxn_environ);
316 72bf04c6 Norman Walsh
    }
317 ead48a5d O'Neil Delpratt
318 72bf04c6 Norman Walsh
    versionClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/Version");
319
320
    procClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/Processor");
321
    saxonCAPIClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/SaxonCAPI");
322
323 ead48a5d O'Neil Delpratt
    static jmethodID mIDcreateProc = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
324
                                                                                                     "createSaxonProcessor",
325
                                                                                                     "(Ljava/lang/String;)Lnet/sf/saxon/s9api/Processor;");
326
    if (!mIDcreateProc) {
327
        std::cerr << "Error: SaxonDll." << "getPrimitiveTypeName"
328
                  << " not found\n" << std::endl;
329
        return;
330
    }
331
    jobject proci = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mIDcreateProc,
332
                                                                             SaxonProcessor::sxn_environ->env->NewStringUTF(
333
                                                                                     configFile));
334
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(proci);
335
    if (!proc) {
336
        checkAndCreateException(saxonCAPIClass);
337
        std::cerr << "Error: " << getDllname() << ". processor is nullptr in constructor(configFile)" << std::endl;
338
        return;
339
    }
340
341
    licensei = true;
342 72bf04c6 Norman Walsh
#ifdef DEBUG
343
344 ead48a5d O'Neil Delpratt
    std::cerr<<"SaxonProc constructor(configFile)"<<std::endl;
345 72bf04c6 Norman Walsh
#endif
346
    xdmAtomicClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
347
}
348
349 ead48a5d O'Neil Delpratt
SaxonProcessor::~SaxonProcessor() {
350
    clearConfigurationProperties();
351 85a9912e Dave Delpratt
    if(proc) {
352
            SaxonProcessor::sxn_environ->env->DeleteGlobalRef(proc);
353
    }
354 ead48a5d O'Neil Delpratt
    if (versionStr != nullptr) {
355
        delete versionStr;
356 55b80284 O'Neil Delpratt
        versionStr = nullptr;
357 ead48a5d O'Neil Delpratt
    }
358 306962bc O'Neil Delpratt
    exceptionClear();
359 ead48a5d O'Neil Delpratt
}
360 72bf04c6 Norman Walsh
361
362 ead48a5d O'Neil Delpratt
bool SaxonProcessor::isSchemaAwareProcessor() {
363
    if (!licensei) {
364
        return false;
365
    } else {
366
        static jmethodID MID_schema = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(procClass,
367
                                                                                                "isSchemaAware", "()Z");
368
        if (!MID_schema) {
369
            std::cerr << "\nError: Saxonc " << "SaxonProcessor.isSchemaAware()" << " not found" << std::endl;
370
            return false;
371
        }
372 72bf04c6 Norman Walsh
373 ead48a5d O'Neil Delpratt
        licensei = (jboolean) (SaxonProcessor::sxn_environ->env->CallBooleanMethod(proc, MID_schema));
374
        return licensei;
375 72bf04c6 Norman Walsh
376 ead48a5d O'Neil Delpratt
    }
377 72bf04c6 Norman Walsh
378
}
379
380 ead48a5d O'Neil Delpratt
void SaxonProcessor::applyConfigurationProperties() {
381
    if (configProperties.size() > 0) {
382
        int size = configProperties.size();
383
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
384
        jobjectArray stringArray1 = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size, stringClass, 0);
385
        jobjectArray stringArray2 = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size, stringClass, 0);
386
        static jmethodID mIDappConfig = nullptr;
387
        if (mIDappConfig == nullptr) {
388
            mIDappConfig = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
389
                                                                                           "applyToConfiguration",
390
                                                                                           "(Lnet/sf/saxon/s9api/Processor;[Ljava/lang/String;[Ljava/lang/String;)V");
391
            if (!mIDappConfig) {
392
                std::cerr << "Error: SaxonDll." << "applyToConfiguration"
393
                          << " not found\n" << std::endl;
394
                return;
395
            }
396
        }
397
        int i = 0;
398
        std::map<std::string, std::string>::iterator iter = configProperties.begin();
399
        for (iter = configProperties.begin(); iter != configProperties.end(); ++iter, i++) {
400
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray1, i,
401
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
402
                                                                            (iter->first).c_str()));
403
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray2, i,
404
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
405
                                                                            (iter->second).c_str()));
406
        }
407
        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mIDappConfig, proc, stringArray1,
408
                                                                 stringArray2);
409
        if (exceptionOccurred()) {
410 306962bc O'Neil Delpratt
            createException();
411 ead48a5d O'Neil Delpratt
        }
412
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray1);
413
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray2);
414
415
    }
416 72bf04c6 Norman Walsh
}
417
418
419 ead48a5d O'Neil Delpratt
jobjectArray SaxonProcessor::createJArray(XdmValue **values, int length) {
420
    jobjectArray valueArray = nullptr;
421 72bf04c6 Norman Walsh
422
    jclass xdmValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
423 ead48a5d O'Neil Delpratt
                                        "net/sf/saxon/s9api/XdmValue");
424 72bf04c6 Norman Walsh
425
426
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length,
427 ead48a5d O'Neil Delpratt
                                                                  xdmValueClass, 0);
428 72bf04c6 Norman Walsh
429 ead48a5d O'Neil Delpratt
    for (int i = 0; i < length; i++) {
430 72bf04c6 Norman Walsh
#ifdef DEBUG
431 ead48a5d O'Neil Delpratt
        std::string s1 = typeid(values[i]).name();
432
        std::cerr<<"In createJArray\nType of itr:"<<s1<<std::endl;
433 72bf04c6 Norman Walsh
434
435 ead48a5d O'Neil Delpratt
        jobject xx = values[i]->getUnderlyingValue();
436 72bf04c6 Norman Walsh
437 ead48a5d O'Neil Delpratt
        if(xx == nullptr) {
438
            std::cerr<<"value failed"<<std::endl;
439
        } else {
440 72bf04c6 Norman Walsh
441 ead48a5d O'Neil Delpratt
            std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
442
        }
443
        if(values[i]->getUnderlyingValue() == nullptr) {
444
            std::cerr<<"value["<<i<<"]->getUnderlyingValue() is nullptr"<<std::endl;
445
        }
446 72bf04c6 Norman Walsh
#endif
447 ead48a5d O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, values[i]->getUnderlyingValue());
448 72bf04c6 Norman Walsh
    }
449
    return valueArray;
450
451 ead48a5d O'Neil Delpratt
}
452 72bf04c6 Norman Walsh
453
454 ead48a5d O'Neil Delpratt
JParameters SaxonProcessor::createParameterJArray(std::map<std::string, XdmValue *> parameters,
455
                                                  std::map<std::string, std::string> properties) {
456
    JParameters comboArrays;
457
    comboArrays.stringArray = nullptr;
458
    comboArrays.objectArray = nullptr;
459
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
460
                                      "java/lang/Object");
461
    jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
462
                                      "java/lang/String");
463 72bf04c6 Norman Walsh
464 ead48a5d O'Neil Delpratt
    int size = parameters.size() + properties.size();
465 72bf04c6 Norman Walsh
#ifdef DEBUG
466 ead48a5d O'Neil Delpratt
    std::cerr<<"Properties size: "<<properties.size()<<std::endl;
467
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
468 72bf04c6 Norman Walsh
#endif
469 ead48a5d O'Neil Delpratt
    if (size > 0) {
470
471
        comboArrays.objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
472
                                                                                   objectClass, 0);
473
        comboArrays.stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
474
                                                                                   stringClass, 0);
475
        int i = 0;
476
        for (std::map<std::string, XdmValue *>::iterator iter =
477
                parameters.begin(); iter != parameters.end(); ++iter, i++) {
478 72bf04c6 Norman Walsh
479
#ifdef DEBUG
480 ead48a5d O'Neil Delpratt
            std::cerr<<"map 1"<<std::endl;
481
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
482 72bf04c6 Norman Walsh
#endif
483 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
484
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
485
                                                                            (iter->first).c_str()));
486 72bf04c6 Norman Walsh
#ifdef DEBUG
487 ead48a5d O'Neil Delpratt
            std::string s1 = typeid(iter->second).name();
488
            std::cerr<<"Type of itr:"<<s1<<std::endl;
489
490
            if((iter->second) == nullptr) {std::cerr<<"iter->second is nullptr"<<std::endl;
491
            } else {
492
                std::cerr<<"getting underlying value"<<std::endl;
493
            jobject xx = (iter->second)->getUnderlyingValue();
494
495
            if(xx == nullptr) {
496
                std::cerr<<"value failed"<<std::endl;
497
            } else {
498
499
                std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
500
            }
501
            if((iter->second)->getUnderlyingValue() == nullptr) {
502
                std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
503
            }}
504 72bf04c6 Norman Walsh
#endif
505
506 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
507
                                                                    (iter->second)->getUnderlyingValue());
508 72bf04c6 Norman Walsh
509 ead48a5d O'Neil Delpratt
        }
510 72bf04c6 Norman Walsh
511 ead48a5d O'Neil Delpratt
        for (std::map<std::string, std::string>::iterator iter =
512
                properties.begin(); iter != properties.end(); ++iter, i++) {
513
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
514
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
515
                                                                            (iter->first).c_str()));
516
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
517
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
518
                                                                            (iter->second).c_str()));
519
        }
520 72bf04c6 Norman Walsh
521 ead48a5d O'Neil Delpratt
        return comboArrays;
522 72bf04c6 Norman Walsh
523 ead48a5d O'Neil Delpratt
    } else {
524
        return comboArrays;
525 72bf04c6 Norman Walsh
    }
526 ead48a5d O'Neil Delpratt
}
527 72bf04c6 Norman Walsh
528 ead48a5d O'Neil Delpratt
JParameters SaxonProcessor::createParameterJArray2(std::map<std::string, XdmValue *> parameters) {
529
    JParameters comboArrays;
530
    comboArrays.stringArray = nullptr;
531
    comboArrays.objectArray = nullptr;
532
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
533
                                      "java/lang/Object");
534
    jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
535
                                      "java/lang/String");
536 72bf04c6 Norman Walsh
537 ead48a5d O'Neil Delpratt
    int size = parameters.size();
538 72bf04c6 Norman Walsh
#ifdef DEBUG
539 ead48a5d O'Neil Delpratt
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
540 72bf04c6 Norman Walsh
#endif
541 ead48a5d O'Neil Delpratt
    if (size > 0) {
542 72bf04c6 Norman Walsh
543 ead48a5d O'Neil Delpratt
        comboArrays.objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
544
                                                                                   objectClass, 0);
545
        comboArrays.stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
546
                                                                                   stringClass, 0);
547
        int i = 0;
548
        for (std::map<std::string, XdmValue *>::iterator iter =
549
                parameters.begin(); iter != parameters.end(); ++iter, i++) {
550 72bf04c6 Norman Walsh
551
#ifdef DEBUG
552 ead48a5d O'Neil Delpratt
            std::cerr<<"map 1"<<std::endl;
553
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
554 72bf04c6 Norman Walsh
#endif
555 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
556
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
557
                                                                            (iter->first).c_str()));
558 72bf04c6 Norman Walsh
#ifdef DEBUG
559 ead48a5d O'Neil Delpratt
            std::string s1 = typeid(iter->second).name();
560
            std::cerr<<"Type of itr:"<<s1<<std::endl;
561
562
            if((iter->second) == nullptr) {std::cerr<<"iter->second is nullptr"<<std::endl;
563
            } else {
564
                std::cerr<<"getting underlying value"<<std::endl;
565
            jobject xx = (iter->second)->getUnderlyingValue();
566
567
            if(xx == nullptr) {
568
                std::cerr<<"value failed"<<std::endl;
569
            } else {
570
571
                std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
572
            }
573
            if((iter->second)->getUnderlyingValue() == nullptr) {
574
                std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
575
            }}
576 72bf04c6 Norman Walsh
#endif
577
578 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
579
                                                                    (iter->second)->getUnderlyingValue());
580 72bf04c6 Norman Walsh
581 ead48a5d O'Neil Delpratt
        }
582 72bf04c6 Norman Walsh
583
584 ead48a5d O'Neil Delpratt
        return comboArrays;
585 72bf04c6 Norman Walsh
586 ead48a5d O'Neil Delpratt
    } else {
587
        return comboArrays;
588 72bf04c6 Norman Walsh
    }
589 ead48a5d O'Neil Delpratt
}
590 72bf04c6 Norman Walsh
591
592 ead48a5d O'Neil Delpratt
SaxonProcessor &SaxonProcessor::operator=(const SaxonProcessor &other) {
593
    versionClass = other.versionClass;
594
    procClass = other.procClass;
595
    saxonCAPIClass = other.saxonCAPIClass;
596
    cwd = other.cwd;
597 9e416749 O'Neil Delpratt
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(other.proc);
598 ead48a5d O'Neil Delpratt
    parameters = other.parameters;
599
    configProperties = other.configProperties;
600
    licensei = other.licensei;
601
    exception = other.exception;
602
    return *this;
603 72bf04c6 Norman Walsh
}
604
605
SaxonProcessor::SaxonProcessor(const SaxonProcessor &other) {
606 ead48a5d O'Neil Delpratt
    versionClass = other.versionClass;
607
    procClass = other.procClass;
608
    saxonCAPIClass = other.saxonCAPIClass;
609
    cwd = other.cwd;
610 9e416749 O'Neil Delpratt
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(other.proc);
611 ead48a5d O'Neil Delpratt
    parameters = other.parameters;
612
    configProperties = other.configProperties;
613
    licensei = other.licensei;
614
    exception = other.exception;
615 72bf04c6 Norman Walsh
}
616
617 03fcd3f9 O'Neil Delpratt
618 ead48a5d O'Neil Delpratt
XsltProcessor *SaxonProcessor::newXsltProcessor() {
619 72bf04c6 Norman Walsh
    return (new XsltProcessor(this, cwd));
620
}
621
622 ead48a5d O'Neil Delpratt
Xslt30Processor *SaxonProcessor::newXslt30Processor() {
623 72bf04c6 Norman Walsh
    return (new Xslt30Processor(this, cwd));
624
}
625
626 ead48a5d O'Neil Delpratt
XQueryProcessor *SaxonProcessor::newXQueryProcessor() {
627
    return (new XQueryProcessor(this, cwd));
628 72bf04c6 Norman Walsh
}
629
630 ead48a5d O'Neil Delpratt
XPathProcessor *SaxonProcessor::newXPathProcessor() {
631 72bf04c6 Norman Walsh
    return (new XPathProcessor(this, cwd));
632
}
633
634 ead48a5d O'Neil Delpratt
SchemaValidator *SaxonProcessor::newSchemaValidator() {
635
    if (licensei) {
636
        return (new SchemaValidator(this, cwd));
637
    } else {
638
        std::cerr << "\nError: Processor is not licensed for schema processing!" << std::endl;
639
        return nullptr;
640
    }
641 72bf04c6 Norman Walsh
}
642
643
644 ead48a5d O'Neil Delpratt
const char *SaxonProcessor::version() {
645
    if (versionStr == nullptr) {
646 72bf04c6 Norman Walsh
647 ead48a5d O'Neil Delpratt
        static jmethodID MID_version = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
648
                                                                                                       "getProductVersion",
649
                                                                                                       "(Lnet/sf/saxon/s9api/Processor;)Ljava/lang/String;");
650
        if (!MID_version) {
651
            std::cerr << "\nError: MyClassInDll " << "SaxonCAPI.getProductVersion()" << " not found" << std::endl;
652
            return nullptr;
653
        }
654 72bf04c6 Norman Walsh
655 01d6fdb6 O'Neil Delpratt
        if(proc == nullptr) {
656
            createException("The Java SaxonProcessor object (i.e. cppXQ) is NULL - Possible exception thrown");
657
            return nullptr;
658
        }
659
660 ead48a5d O'Neil Delpratt
        jstring jstr = (jstring) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, MID_version,
661
                                                                                           proc));
662
        const char *tempVersionStr = SaxonProcessor::sxn_environ->env->GetStringUTFChars(jstr, nullptr);
663
        int verLen = strlen(tempVersionStr) + 22 + strlen(CVERSION) + 1;
664
        versionStr = new char[verLen];
665
        snprintf(versionStr, verLen, "Saxon/C %s %s %s", CVERSION, "running with", tempVersionStr);
666 55b80284 O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->ReleaseStringUTFChars(jstr, tempVersionStr);
667
668 72bf04c6 Norman Walsh
669
    }
670
671
    return versionStr;
672
}
673
674 ead48a5d O'Neil Delpratt
void SaxonProcessor::setcwd(const char *dir) {
675 6b6a9706 O'Neil Delpratt
    if(dir != nullptr) {
676
        cwd = std::string(dir);
677
    }
678 72bf04c6 Norman Walsh
}
679
680 ead48a5d O'Neil Delpratt
const char *SaxonProcessor::getcwd() {
681
    return cwd.c_str();
682 72bf04c6 Norman Walsh
}
683
684
685 ead48a5d O'Neil Delpratt
void SaxonProcessor::setCatalog(const char *catalogFile, bool isTracing) {
686 9458d238 O'Neil Delpratt
    jclass xmlResolverClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/trans/XmlCatalogResolver");
687 ead48a5d O'Neil Delpratt
    static jmethodID catalogMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(xmlResolverClass, "setCatalog",
688
                                                                                      "(Ljava/lang/String;Lnet/sf/saxon/Configuration;Z)V");
689 72bf04c6 Norman Walsh
690 ead48a5d O'Neil Delpratt
    if (!catalogMID) {
691
        std::cerr << "\nError: Saxonc." << "setCatalog()" << " not found" << std::endl;
692
        return;
693
    }
694
    if (catalogFile == nullptr) {
695 72bf04c6 Norman Walsh
696
        return;
697 ead48a5d O'Neil Delpratt
    }
698
    static jmethodID configMID = SaxonProcessor::sxn_environ->env->GetMethodID(procClass, "getUnderlyingConfiguration",
699
                                                                               "()Lnet/sf/saxon/Configuration;");
700
701
    if (!configMID) {
702
        std::cerr << "\nError: Saxonc." << "getUnderlyingConfiguration()" << " not found" << std::endl;
703 72bf04c6 Norman Walsh
        return;
704 ead48a5d O'Neil Delpratt
    }
705
706 72bf04c6 Norman Walsh
707 ead48a5d O'Neil Delpratt
    if (!proc) {
708 306962bc O'Neil Delpratt
        createException("Processor is null in SaxonProcessor.setCatalog");
709 ead48a5d O'Neil Delpratt
        return;
710
    }
711 72bf04c6 Norman Walsh
712 ead48a5d O'Neil Delpratt
    jobject configObj = SaxonProcessor::sxn_environ->env->CallObjectMethod(proc, configMID);
713 72bf04c6 Norman Walsh
714 ead48a5d O'Neil Delpratt
    if (!configObj) {
715 9458d238 O'Neil Delpratt
        std::cout << "proc is null in SaxonProcessor setcatalog - config obj" << std::endl;
716 ead48a5d O'Neil Delpratt
        return;
717
    }
718
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(xmlResolverClass, catalogMID,
719
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(catalogFile),
720
                                                           configObj, (jboolean) isTracing);
721 72bf04c6 Norman Walsh
#ifdef DEBUG
722 ead48a5d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
723 72bf04c6 Norman Walsh
#endif
724 00082f6f O'Neil Delpratt
725 72bf04c6 Norman Walsh
}
726
727
728 ead48a5d O'Neil Delpratt
XdmNode *SaxonProcessor::parseXmlFromString(const char *source) {
729 72bf04c6 Norman Walsh
730 ead48a5d O'Neil Delpratt
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlString",
731
                                                                                    "(Ljava/lang/String;Lnet/sf/saxon/s9api/Processor;Lnet/sf/saxon/s9api/SchemaValidator;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
732 72bf04c6 Norman Walsh
    if (!mID) {
733 ead48a5d O'Neil Delpratt
        std::cerr << "\nError: Saxonc." << "parseXmlString()" << " not found" << std::endl;
734
        return nullptr;
735 72bf04c6 Norman Walsh
    }
736
//TODO SchemaValidator
737
738 306962bc O'Neil Delpratt
    if(source == nullptr) {
739
        createException("Source string is NULL");
740
        return nullptr;
741
    }
742
743 ead48a5d O'Neil Delpratt
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID,
744
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
745
                                                                                        cwd.c_str()), proc, nullptr,
746
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
747
                                                                                        source));
748
    if (xdmNodei) {
749 55b80284 O'Neil Delpratt
        XdmNode *value = nullptr;
750
        value = new XdmNode(xdmNodei);
751
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(xdmNodei);
752 ead48a5d O'Neil Delpratt
        return value;
753
    } else if (exceptionOccurred()) {
754 306962bc O'Neil Delpratt
        createException();
755 ead48a5d O'Neil Delpratt
    }
756
757 72bf04c6 Norman Walsh
#ifdef DEBUG
758 ead48a5d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
759 72bf04c6 Norman Walsh
#endif
760
761 ead48a5d O'Neil Delpratt
    return nullptr;
762 72bf04c6 Norman Walsh
}
763
764 ead48a5d O'Neil Delpratt
int SaxonProcessor::getNodeKind(jobject obj) {
765
    jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmNode;");
766
    static jmethodID nodeKindMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
767
                                                                                             "getNodeKind",
768
                                                                                             "()Lnet/sf/saxon/s9api/XdmNodeKind;");
769
    if (!nodeKindMID) {
770
        std::cerr << "Error: MyClassInDll." << "getNodeKind" << " not found\n"
771
                  << std::endl;
772
        return 0;
773
    }
774
775
    jobject nodeKindObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(obj, nodeKindMID));
776
    if (!nodeKindObj) {
777 72bf04c6 Norman Walsh
778 ead48a5d O'Neil Delpratt
        return 0;
779
    }
780
    jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/option/cpp/XdmUtils;");
781
782
    jmethodID mID2 = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,
783
                                                                                     "convertNodeKindType",
784
                                                                                     "(Lnet/sf/saxon/s9api/XdmNodeKind;)I");
785
786
    if (!mID2) {
787
        std::cerr << "Error: MyClassInDll." << "convertNodeKindType" << " not found\n"
788
                  << std::endl;
789
        return 0;
790
    }
791
    if (!nodeKindObj) {
792
        return 0;
793
    }
794
    int nodeKind = (int) (SaxonProcessor::sxn_environ->env->CallStaticIntMethod(xdmUtilsClass, mID2, nodeKindObj));
795
    return nodeKind;
796
}
797 72bf04c6 Norman Walsh
798
799 ead48a5d O'Neil Delpratt
XdmNode *SaxonProcessor::parseXmlFromFile(const char *source) {
800
801
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlFile",
802
                                                                                    "(Lnet/sf/saxon/s9api/Processor;Ljava/lang/String;Lnet/sf/saxon/s9api/SchemaValidator;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
803 72bf04c6 Norman Walsh
    if (!mID) {
804 ead48a5d O'Neil Delpratt
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFile()" << " not found" << std::endl;
805
        return nullptr;
806 72bf04c6 Norman Walsh
    }
807
//TODO SchemaValidator
808 ead48a5d O'Neil Delpratt
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID, proc,
809
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
810
                                                                                        cwd.c_str()), nullptr,
811
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
812
                                                                                        source));
813
    if (exceptionOccurred()) {
814 306962bc O'Neil Delpratt
        createException();
815 ead48a5d O'Neil Delpratt
816
    } else {
817
818
        XdmNode *value = new XdmNode(xdmNodei);
819
        return value;
820
    }
821
    return nullptr;
822 72bf04c6 Norman Walsh
}
823
824 ead48a5d O'Neil Delpratt
XdmNode *SaxonProcessor::parseXmlFromUri(const char *source) {
825 72bf04c6 Norman Walsh
826 ead48a5d O'Neil Delpratt
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlFile",
827
                                                                                    "(Lnet/sf/saxon/s9api/Processor;Ljava/lang/String;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
828 72bf04c6 Norman Walsh
    if (!mID) {
829 ead48a5d O'Neil Delpratt
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFromUri()" << " not found" << std::endl;
830
        return nullptr;
831
    }
832
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID, proc,
833
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
834
                                                                                        ""),
835
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
836
                                                                                        source));
837
    if (exceptionOccurred()) {
838 306962bc O'Neil Delpratt
        createException();
839 ead48a5d O'Neil Delpratt
    } else {
840
        XdmNode *value = new XdmNode(xdmNodei);
841
        return value;
842
    }
843
    return nullptr;
844 72bf04c6 Norman Walsh
}
845
846
847 ead48a5d O'Neil Delpratt
/**
848
   * Set a configuration property.
849
   *
850
   * @param name of the property
851
   * @param value of the property
852
   */
853
void SaxonProcessor::setConfigurationProperty(const char *name, const char *value) {
854
    if (name != nullptr) {
855
        configProperties.insert(
856
                std::pair<std::string, std::string>(std::string(name), std::string((value == nullptr ? "" : value))));
857 72bf04c6 Norman Walsh
    }
858 ead48a5d O'Neil Delpratt
}
859 72bf04c6 Norman Walsh
860 ead48a5d O'Neil Delpratt
void SaxonProcessor::clearConfigurationProperties() {
861
    configProperties.clear();
862
}
863 72bf04c6 Norman Walsh
864
865 ead48a5d O'Neil Delpratt
void SaxonProcessor::release() {
866
    if (SaxonProcessor::jvmCreatedCPP != 0) {
867
        SaxonProcessor::jvmCreatedCPP = 0;
868
        //std::cerr<<"SaxonProc: JVM finalized calling !"<<std::endl;
869
        finalizeJavaRT(SaxonProcessor::sxn_environ->jvm);
870 72bf04c6 Norman Walsh
871 55b80284 O'Neil Delpratt
        delete SaxonProcessor::sxn_environ;
872 ead48a5d O'Neil Delpratt
        /*clearParameters();
873
        clearProperties();*/
874
    } else {
875 4ee4355d O'Neil Delpratt
#ifdef DEBUG
876 ead48a5d O'Neil Delpratt
        std::cerr << "SaxonProc: JVM finalize not called!" << std::endl;
877 4ee4355d O'Neil Delpratt
#endif
878 ead48a5d O'Neil Delpratt
    }
879 72bf04c6 Norman Walsh
}
880
881
882 ead48a5d O'Neil Delpratt
/* ========= Factory method for Xdm ======== */
883 72bf04c6 Norman Walsh
884 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeStringValue(const char *str) {
885
    jobject obj = getJavaStringValue(SaxonProcessor::sxn_environ, str);
886
    static jmethodID mssID_atomic = nullptr;
887
    if (mssID_atomic == nullptr) {
888
        mssID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
889
                                                                                  "(Ljava/lang/String;)V"));
890
    }
891
    if (!mssID_atomic) {
892
        std::cerr << "XdmAtomic constructor (String)" << std::endl;
893
        return nullptr;
894
    }
895
    jobject obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mssID_atomic, obj));
896
    XdmAtomicValue *value = new XdmAtomicValue(obj2, "xs:string");
897 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
898
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj2);
899 ead48a5d O'Neil Delpratt
    return value;
900
}
901 72bf04c6 Norman Walsh
902 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeStringValue(std::string str) {
903
    jobject obj = getJavaStringValue(SaxonProcessor::sxn_environ, str.c_str());
904
    static jmethodID msID_atomic = nullptr;
905
    if (msID_atomic == nullptr) {
906
        msID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
907
                                                                                 "(Ljava/lang/String;)V"));
908
    }
909
    if (!msID_atomic) {
910
        std::cerr << "XdmAtomic constructor (String)" << std::endl;
911
        return nullptr;
912
    }
913
    jobject obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, msID_atomic, obj));
914
    XdmAtomicValue *value = new XdmAtomicValue(obj2, "xs:string");
915 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
916 ead48a5d O'Neil Delpratt
    return value;
917
}
918 72bf04c6 Norman Walsh
919 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeIntegerValue(int i) {
920
    //jobject obj = integerValue(*SaxonProcessor::sxn_environ, i);
921
    static jmethodID miiID_atomic = nullptr;
922
    if (miiID_atomic == nullptr) {
923
        miiID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
924
    }
925
    if (!miiID_atomic) {
926
        std::cerr << "XdmAtomic constructor (J)" << std::endl;
927
        return nullptr;
928
    }
929
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, miiID_atomic, (jlong) i));
930
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}integer");
931 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
932 ead48a5d O'Neil Delpratt
    return value;
933
}
934
935
XdmAtomicValue *SaxonProcessor::makeDoubleValue(double d) {
936
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
937
    static jmethodID mdID_atomic = nullptr;
938
    if (mdID_atomic == nullptr) {
939
        mdID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(D)V"));
940 72bf04c6 Norman Walsh
    }
941 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mdID_atomic, (jdouble) d));
942
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}double");
943 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
944 ead48a5d O'Neil Delpratt
    return value;
945
}
946 72bf04c6 Norman Walsh
947 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeFloatValue(float d) {
948
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
949
    static jmethodID mfID_atomic = nullptr;
950
    if (mfID_atomic == nullptr) {
951
        mfID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(F)V"));
952 72bf04c6 Norman Walsh
    }
953 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mfID_atomic, (jfloat) d));
954
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}float");
955 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
956 ead48a5d O'Neil Delpratt
    return value;
957
}
958 72bf04c6 Norman Walsh
959 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeLongValue(long l) {
960
    //jobject obj = longValue(*SaxonProcessor::sxn_environ, l);
961
    static jmethodID mlID_atomic = nullptr;
962
    if (mlID_atomic == nullptr) {
963
        mlID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
964 72bf04c6 Norman Walsh
    }
965 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mlID_atomic, (jlong) l));
966
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}long");
967 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
968 ead48a5d O'Neil Delpratt
    return value;
969
}
970 72bf04c6 Norman Walsh
971 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeBooleanValue(bool b) {
972
    //jobject obj = booleanValue(*SaxonProcessor::sxn_environ, b);
973
    static jmethodID mID_atomic = nullptr;
974
    if (mID_atomic == nullptr) {
975
        mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(Z)V"));
976 72bf04c6 Norman Walsh
    }
977 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, (jboolean) b));
978
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}boolean");
979 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
980 ead48a5d O'Neil Delpratt
    return value;
981
}
982
983
XdmAtomicValue *SaxonProcessor::makeQNameValue(const char *str) {
984
    jobject val = xdmValueAsObj(SaxonProcessor::sxn_environ, "QName", str);
985
    XdmAtomicValue *value = new XdmAtomicValue(val, "QName");
986 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(val);
987 ead48a5d O'Neil Delpratt
    return value;
988
}
989 72bf04c6 Norman Walsh
990 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeAtomicValue(const char *typei, const char *strValue) {
991
    jobject obj = xdmValueAsObj(SaxonProcessor::sxn_environ, typei, strValue);
992
    XdmAtomicValue *value = new XdmAtomicValue(obj, typei);
993 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
994 ead48a5d O'Neil Delpratt
    return value;
995
}
996
997
const char *SaxonProcessor::getStringValue(XdmItem *item) {
998
    const char *result = stringValue(SaxonProcessor::sxn_environ, item->getUnderlyingValue());
999 72bf04c6 Norman Walsh
#ifdef DEBUG
1000 ead48a5d O'Neil Delpratt
    if(result == nullptr) {
1001
        std::cout<<"getStringValue of XdmItem is nullptr"<<std::endl;
1002
    } else {
1003
        std::cout<<"getStringValue of XdmItem is OK"<<std::endl;
1004
    }
1005 72bf04c6 Norman Walsh
#endif
1006
1007 ead48a5d O'Neil Delpratt
    return result;
1008
1009
}
1010 72bf04c6 Norman Walsh
1011
#if CVERSION_API_NO >= 123
1012
1013 ead48a5d O'Neil Delpratt
XdmArray *SaxonProcessor::makeArray(short *input, int length) {
1014
    if (input == nullptr) {
1015
        std::cerr << "Error found when converting string to XdmArray" << std::endl;
1016
        return nullptr;
1017
    }
1018
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1019
    static jmethodID mmssID = nullptr;
1020
    if (mmssID == nullptr) {
1021
        mmssID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1022
                                                                                 "([S)Lnet/sf/saxon/s9api/XdmArray;");
1023
    }
1024
    if (!mmssID) {
1025
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([S)" << " not found" << std::endl;
1026
        return nullptr;
1027
    }
1028 72bf04c6 Norman Walsh
1029
1030 ead48a5d O'Neil Delpratt
    jshortArray sArray = nullptr;
1031 72bf04c6 Norman Walsh
1032 ead48a5d O'Neil Delpratt
    sArray = SaxonProcessor::sxn_environ->env->NewShortArray((jint) length);
1033
    jshort fill[length];
1034
    for (int i = 0; i < length; i++) {
1035
        fill[i] = input[i];
1036
    }
1037
    SaxonProcessor::sxn_environ->env->SetShortArrayRegion(sArray, 0, length, fill);
1038 72bf04c6 Norman Walsh
1039 ead48a5d O'Neil Delpratt
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmssID, sArray);
1040
    if (!xdmArrayi) {
1041
        std::cerr << "Error found when converting string to XdmArray";
1042
        return nullptr;
1043
    }
1044 72bf04c6 Norman Walsh
1045 ead48a5d O'Neil Delpratt
    if (exceptionOccurred()) {
1046 306962bc O'Neil Delpratt
        createException();
1047 ead48a5d O'Neil Delpratt
    } else {
1048
        XdmArray *value = new XdmArray(xdmArrayi);
1049
        return value;
1050
    }
1051
    return nullptr;
1052
}
1053 72bf04c6 Norman Walsh
1054
1055 ead48a5d O'Neil Delpratt
XdmArray *SaxonProcessor::makeArray(int *input, int length) {
1056
    if (input == nullptr) {
1057
        std::cerr << "Error found when converting string to XdmArray";
1058
        return nullptr;
1059
    }
1060
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1061
    static jmethodID mmiiID = nullptr;
1062
    if (mmiiID == nullptr) {
1063
        mmiiID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1064
                                                                                 "([I)Lnet/sf/saxon/s9api/XdmArray;");
1065
    }
1066
    if (!mmiiID) {
1067
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([I)" << " not found" << std::endl;
1068
        return nullptr;
1069
    }
1070
1071
1072
    jintArray iArray = nullptr;
1073
1074
    iArray = SaxonProcessor::sxn_environ->env->NewIntArray((jint) length);
1075
    jint fill[length];
1076
    for (int i = 0; i < length; i++) {
1077
        fill[i] = input[i];
1078
    }
1079
    SaxonProcessor::sxn_environ->env->SetIntArrayRegion(iArray, 0, length, fill);
1080
1081
1082
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiiID, iArray);
1083
    if (!xdmArrayi) {
1084
        std::cerr << "Error found when converting string to XdmArray";
1085
        return nullptr;
1086
    }
1087
    if (exceptionOccurred()) {
1088 306962bc O'Neil Delpratt
        createException();
1089 ead48a5d O'Neil Delpratt
    } else {
1090
        XdmArray *value = new XdmArray(xdmArrayi);
1091
        return value;
1092
    }
1093
    return nullptr;
1094
1095
}
1096
1097
XdmArray *SaxonProcessor::makeArray(long *input, int length) {
1098
    if (input == nullptr) {
1099
        std::cerr << "Error found when converting string to XdmArray";
1100
        return nullptr;
1101
    }
1102
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1103
    static jmethodID mmiID = nullptr;
1104
    if (mmiID == nullptr) {
1105
        mmiID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1106
                                                                                "([J)Lnet/sf/saxon/s9api/XdmArray;");
1107
    }
1108
    if (!mmiID) {
1109
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([J)" << " not found" << std::endl;
1110
        return nullptr;
1111
    }
1112
1113
1114
    jlongArray lArray = nullptr;
1115
1116
    lArray = SaxonProcessor::sxn_environ->env->NewLongArray((jint) length);
1117
    jlong fill[length];
1118
    for (int i = 0; i < length; i++) {
1119
        fill[i] = input[i];
1120
    }
1121
    SaxonProcessor::sxn_environ->env->SetLongArrayRegion(lArray, 0, length, fill);
1122
1123
1124
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiID, lArray);
1125
    if (!xdmArrayi) {
1126
        std::cerr << "Error found when converting string to XdmArray";
1127
        return nullptr;
1128
    }
1129
    if (exceptionOccurred()) {
1130 306962bc O'Neil Delpratt
        createException();
1131 ead48a5d O'Neil Delpratt
    } else {
1132
        XdmArray *value = new XdmArray(xdmArrayi);
1133
        return value;
1134
    }
1135
    return nullptr;
1136
}
1137
1138
1139
XdmArray *SaxonProcessor::makeArray(bool *input, int length) {
1140
    if (input == nullptr) {
1141
        std::cerr << "Error found when converting string to XdmArray";
1142
        return nullptr;
1143
    }
1144
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1145
    static jmethodID mmbID = nullptr;
1146
    if (mmbID == nullptr) {
1147
        mmbID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1148
                                                                                "([Z)Lnet/sf/saxon/s9api/XdmArray;");
1149
    }
1150
    if (!mmbID) {
1151
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([Z)" << " not found" << std::endl;
1152
        return nullptr;
1153
    }
1154
1155
1156
    jbooleanArray bArray = nullptr;
1157
1158
    bArray = SaxonProcessor::sxn_environ->env->NewBooleanArray((jint) length);
1159
    jboolean fill[length];
1160
    for (int i = 0; i < length; i++) {
1161
        fill[i] = input[i];
1162
    }
1163
    SaxonProcessor::sxn_environ->env->SetBooleanArrayRegion(bArray, 0, length, fill);
1164
1165
1166
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmbID, bArray);
1167
    if (!xdmArrayi) {
1168
        std::cerr << "Error found when converting string to XdmArray";
1169
        return nullptr;
1170
    }
1171
    if (exceptionOccurred()) {
1172 306962bc O'Neil Delpratt
        createException();
1173 ead48a5d O'Neil Delpratt
    } else {
1174
        XdmArray *value = new XdmArray(xdmArrayi);
1175
        return value;
1176
    }
1177
    return nullptr;
1178
1179
1180
}
1181
1182
1183
XdmArray *SaxonProcessor::makeArray(const char **input, int length) {
1184
    if (input == nullptr || length <= 0) {
1185
        std::cerr << "Error found when converting string to XdmArray";
1186
        return nullptr;
1187
    }
1188
    jobject obj = nullptr;
1189
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1190
    jmethodID mmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1191
                                                                                     "([Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmArray;");
1192
1193
    jmethodID mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
1194
                                                                                      "(Ljava/lang/String;)V"));
1195
    jobjectArray valueArray = nullptr;
1196
    jobject obj2 = nullptr;
1197
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length, xdmAtomicClass, 0);
1198
    for (int i = 0; i < length; i++) {
1199
        if (input[i] == nullptr) {
1200
            std::cerr << "Error found when converting string to XdmArray";
1201
            return nullptr;
1202 72bf04c6 Norman Walsh
        }
1203 ead48a5d O'Neil Delpratt
        obj = getJavaStringValue(SaxonProcessor::sxn_environ, input[i]);
1204
        obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, obj));
1205
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, obj2);
1206
    }
1207 72bf04c6 Norman Walsh
1208 ead48a5d O'Neil Delpratt
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmID, valueArray);
1209
    if (!xdmArrayi) {
1210
        std::cerr << "Error found when converting string to XdmArray";
1211
        return nullptr;
1212
    }
1213 72bf04c6 Norman Walsh
1214 ead48a5d O'Neil Delpratt
    if (exceptionOccurred()) {
1215
        checkAndCreateException(saxonCAPIClass);
1216
    } else {
1217
        XdmArray *value = new XdmArray(xdmArrayi);
1218
        return value;
1219
    }
1220
    return nullptr;
1221
}
1222 72bf04c6 Norman Walsh
1223
1224 901b6eca O'Neil Delpratt
XdmMap *SaxonProcessor::makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap) {
1225 ead48a5d O'Neil Delpratt
    jobjectArray keyArray = nullptr;
1226
    jobjectArray valueArray = nullptr;
1227
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
1228
                                      "java/lang/Object");
1229 72bf04c6 Norman Walsh
1230 ead48a5d O'Neil Delpratt
    int size = dataMap.size();
1231 72bf04c6 Norman Walsh
1232 ead48a5d O'Neil Delpratt
    if (size > 0) {
1233 72bf04c6 Norman Walsh
1234 ead48a5d O'Neil Delpratt
        keyArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1235
                                                                    objectClass, 0);
1236
        valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1237
                                                                      objectClass, 0);
1238
        int i = 0;
1239
        for (std::map<XdmAtomicValue *, XdmValue *>::iterator iter =
1240
                dataMap.begin(); iter != dataMap.end(); ++iter, i++) {
1241 72bf04c6 Norman Walsh
1242
1243 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(keyArray, i,
1244
                                                                    (iter->first)->getUnderlyingValue());
1245 72bf04c6 Norman Walsh
1246 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i,
1247
                                                                    (iter->second)->getUnderlyingValue());
1248 72bf04c6 Norman Walsh
1249 ead48a5d O'Neil Delpratt
        }
1250
        jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
1251
        jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass, "makeXdmMap",
1252
                                                                                         "([Lnet/sf/saxon/s9api/XdmAtomicValue;[Lnet/sf/saxon/s9api/XdmValue;)Lnet/sf/saxon/s9api/XdmMap;");
1253
        if (!xmID) {
1254
            std::cerr << "Error: SaxonDll." << "makeXdmMap"
1255
                      << " not found\n" << std::endl;
1256
            return nullptr;
1257
        }
1258 72bf04c6 Norman Walsh
1259
1260 ead48a5d O'Neil Delpratt
        jobject results = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
1261
                                                                                              keyArray, valueArray));
1262 901b6eca O'Neil Delpratt
        if (exceptionOccurred() || !results) {
1263
            checkAndCreateException(saxonCAPIClass);
1264
        } else {
1265
            return new XdmMap(results);
1266
        }
1267 ead48a5d O'Neil Delpratt
    }
1268 72bf04c6 Norman Walsh
1269 901b6eca O'Neil Delpratt
    return nullptr;
1270 ead48a5d O'Neil Delpratt
}
1271 72bf04c6 Norman Walsh
1272
#endif
1273