Project

Profile

Help

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

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

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
619
void SaxonProcessor::setResourcesDirectory(const char *dir){}
620
621 ead48a5d O'Neil Delpratt
XsltProcessor *SaxonProcessor::newXsltProcessor() {
622 72bf04c6 Norman Walsh
    return (new XsltProcessor(this, cwd));
623
}
624
625 ead48a5d O'Neil Delpratt
Xslt30Processor *SaxonProcessor::newXslt30Processor() {
626 72bf04c6 Norman Walsh
    return (new Xslt30Processor(this, cwd));
627
}
628
629 ead48a5d O'Neil Delpratt
XQueryProcessor *SaxonProcessor::newXQueryProcessor() {
630
    return (new XQueryProcessor(this, cwd));
631 72bf04c6 Norman Walsh
}
632
633 ead48a5d O'Neil Delpratt
XPathProcessor *SaxonProcessor::newXPathProcessor() {
634 72bf04c6 Norman Walsh
    return (new XPathProcessor(this, cwd));
635
}
636
637 ead48a5d O'Neil Delpratt
SchemaValidator *SaxonProcessor::newSchemaValidator() {
638
    if (licensei) {
639
        return (new SchemaValidator(this, cwd));
640
    } else {
641
        std::cerr << "\nError: Processor is not licensed for schema processing!" << std::endl;
642
        return nullptr;
643
    }
644 72bf04c6 Norman Walsh
}
645
646
647 ead48a5d O'Neil Delpratt
const char *SaxonProcessor::version() {
648
    if (versionStr == nullptr) {
649 72bf04c6 Norman Walsh
650 ead48a5d O'Neil Delpratt
        static jmethodID MID_version = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
651
                                                                                                       "getProductVersion",
652
                                                                                                       "(Lnet/sf/saxon/s9api/Processor;)Ljava/lang/String;");
653
        if (!MID_version) {
654
            std::cerr << "\nError: MyClassInDll " << "SaxonCAPI.getProductVersion()" << " not found" << std::endl;
655
            return nullptr;
656
        }
657 72bf04c6 Norman Walsh
658 01d6fdb6 O'Neil Delpratt
        if(proc == nullptr) {
659
            createException("The Java SaxonProcessor object (i.e. cppXQ) is NULL - Possible exception thrown");
660
            return nullptr;
661
        }
662
663 ead48a5d O'Neil Delpratt
        jstring jstr = (jstring) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, MID_version,
664
                                                                                           proc));
665
        const char *tempVersionStr = SaxonProcessor::sxn_environ->env->GetStringUTFChars(jstr, nullptr);
666
        int verLen = strlen(tempVersionStr) + 22 + strlen(CVERSION) + 1;
667
        versionStr = new char[verLen];
668
        snprintf(versionStr, verLen, "Saxon/C %s %s %s", CVERSION, "running with", tempVersionStr);
669 55b80284 O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->ReleaseStringUTFChars(jstr, tempVersionStr);
670
671 72bf04c6 Norman Walsh
672
    }
673
674
    return versionStr;
675
}
676
677 ead48a5d O'Neil Delpratt
void SaxonProcessor::setcwd(const char *dir) {
678 6b6a9706 O'Neil Delpratt
    if(dir != nullptr) {
679
        cwd = std::string(dir);
680
    }
681 72bf04c6 Norman Walsh
}
682
683 ead48a5d O'Neil Delpratt
const char *SaxonProcessor::getcwd() {
684
    return cwd.c_str();
685 72bf04c6 Norman Walsh
}
686
687
688 ead48a5d O'Neil Delpratt
void SaxonProcessor::setCatalog(const char *catalogFile, bool isTracing) {
689 9458d238 O'Neil Delpratt
    jclass xmlResolverClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/trans/XmlCatalogResolver");
690 ead48a5d O'Neil Delpratt
    static jmethodID catalogMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(xmlResolverClass, "setCatalog",
691
                                                                                      "(Ljava/lang/String;Lnet/sf/saxon/Configuration;Z)V");
692 72bf04c6 Norman Walsh
693 ead48a5d O'Neil Delpratt
    if (!catalogMID) {
694
        std::cerr << "\nError: Saxonc." << "setCatalog()" << " not found" << std::endl;
695
        return;
696
    }
697
    if (catalogFile == nullptr) {
698 72bf04c6 Norman Walsh
699
        return;
700 ead48a5d O'Neil Delpratt
    }
701
    static jmethodID configMID = SaxonProcessor::sxn_environ->env->GetMethodID(procClass, "getUnderlyingConfiguration",
702
                                                                               "()Lnet/sf/saxon/Configuration;");
703
704
    if (!configMID) {
705
        std::cerr << "\nError: Saxonc." << "getUnderlyingConfiguration()" << " not found" << std::endl;
706 72bf04c6 Norman Walsh
        return;
707 ead48a5d O'Neil Delpratt
    }
708
709 72bf04c6 Norman Walsh
710 ead48a5d O'Neil Delpratt
    if (!proc) {
711 306962bc O'Neil Delpratt
        createException("Processor is null in SaxonProcessor.setCatalog");
712 ead48a5d O'Neil Delpratt
        return;
713
    }
714 72bf04c6 Norman Walsh
715 ead48a5d O'Neil Delpratt
    jobject configObj = SaxonProcessor::sxn_environ->env->CallObjectMethod(proc, configMID);
716 72bf04c6 Norman Walsh
717 ead48a5d O'Neil Delpratt
    if (!configObj) {
718 9458d238 O'Neil Delpratt
        std::cout << "proc is null in SaxonProcessor setcatalog - config obj" << std::endl;
719 ead48a5d O'Neil Delpratt
        return;
720
    }
721
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(xmlResolverClass, catalogMID,
722
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(catalogFile),
723
                                                           configObj, (jboolean) isTracing);
724 72bf04c6 Norman Walsh
#ifdef DEBUG
725 ead48a5d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
726 72bf04c6 Norman Walsh
#endif
727 00082f6f O'Neil Delpratt
728 72bf04c6 Norman Walsh
}
729
730
731 ead48a5d O'Neil Delpratt
XdmNode *SaxonProcessor::parseXmlFromString(const char *source) {
732 72bf04c6 Norman Walsh
733 ead48a5d O'Neil Delpratt
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlString",
734
                                                                                    "(Ljava/lang/String;Lnet/sf/saxon/s9api/Processor;Lnet/sf/saxon/s9api/SchemaValidator;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
735 72bf04c6 Norman Walsh
    if (!mID) {
736 ead48a5d O'Neil Delpratt
        std::cerr << "\nError: Saxonc." << "parseXmlString()" << " not found" << std::endl;
737
        return nullptr;
738 72bf04c6 Norman Walsh
    }
739
//TODO SchemaValidator
740
741 306962bc O'Neil Delpratt
    if(source == nullptr) {
742
        createException("Source string is NULL");
743
        return nullptr;
744
    }
745
746 ead48a5d O'Neil Delpratt
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID,
747
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
748
                                                                                        cwd.c_str()), proc, nullptr,
749
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
750
                                                                                        source));
751
    if (xdmNodei) {
752 55b80284 O'Neil Delpratt
        XdmNode *value = nullptr;
753
        value = new XdmNode(xdmNodei);
754
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(xdmNodei);
755 ead48a5d O'Neil Delpratt
        return value;
756
    } else if (exceptionOccurred()) {
757 306962bc O'Neil Delpratt
        createException();
758 ead48a5d O'Neil Delpratt
    }
759
760 72bf04c6 Norman Walsh
#ifdef DEBUG
761 ead48a5d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
762 72bf04c6 Norman Walsh
#endif
763
764 ead48a5d O'Neil Delpratt
    return nullptr;
765 72bf04c6 Norman Walsh
}
766
767 ead48a5d O'Neil Delpratt
int SaxonProcessor::getNodeKind(jobject obj) {
768
    jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmNode;");
769
    static jmethodID nodeKindMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
770
                                                                                             "getNodeKind",
771
                                                                                             "()Lnet/sf/saxon/s9api/XdmNodeKind;");
772
    if (!nodeKindMID) {
773
        std::cerr << "Error: MyClassInDll." << "getNodeKind" << " not found\n"
774
                  << std::endl;
775
        return 0;
776
    }
777
778
    jobject nodeKindObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(obj, nodeKindMID));
779
    if (!nodeKindObj) {
780 72bf04c6 Norman Walsh
781 ead48a5d O'Neil Delpratt
        return 0;
782
    }
783
    jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/option/cpp/XdmUtils;");
784
785
    jmethodID mID2 = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,
786
                                                                                     "convertNodeKindType",
787
                                                                                     "(Lnet/sf/saxon/s9api/XdmNodeKind;)I");
788
789
    if (!mID2) {
790
        std::cerr << "Error: MyClassInDll." << "convertNodeKindType" << " not found\n"
791
                  << std::endl;
792
        return 0;
793
    }
794
    if (!nodeKindObj) {
795
        return 0;
796
    }
797
    int nodeKind = (int) (SaxonProcessor::sxn_environ->env->CallStaticIntMethod(xdmUtilsClass, mID2, nodeKindObj));
798
    return nodeKind;
799
}
800 72bf04c6 Norman Walsh
801
802 ead48a5d O'Neil Delpratt
XdmNode *SaxonProcessor::parseXmlFromFile(const char *source) {
803
804
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlFile",
805
                                                                                    "(Lnet/sf/saxon/s9api/Processor;Ljava/lang/String;Lnet/sf/saxon/s9api/SchemaValidator;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
806 72bf04c6 Norman Walsh
    if (!mID) {
807 ead48a5d O'Neil Delpratt
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFile()" << " not found" << std::endl;
808
        return nullptr;
809 72bf04c6 Norman Walsh
    }
810
//TODO SchemaValidator
811 ead48a5d O'Neil Delpratt
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID, proc,
812
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
813
                                                                                        cwd.c_str()), nullptr,
814
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
815
                                                                                        source));
816
    if (exceptionOccurred()) {
817 306962bc O'Neil Delpratt
        createException();
818 ead48a5d O'Neil Delpratt
819
    } else {
820
821
        XdmNode *value = new XdmNode(xdmNodei);
822
        return value;
823
    }
824
    return nullptr;
825 72bf04c6 Norman Walsh
}
826
827 ead48a5d O'Neil Delpratt
XdmNode *SaxonProcessor::parseXmlFromUri(const char *source) {
828 72bf04c6 Norman Walsh
829 ead48a5d O'Neil Delpratt
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlFile",
830
                                                                                    "(Lnet/sf/saxon/s9api/Processor;Ljava/lang/String;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
831 72bf04c6 Norman Walsh
    if (!mID) {
832 ead48a5d O'Neil Delpratt
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFromUri()" << " not found" << std::endl;
833
        return nullptr;
834
    }
835
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID, proc,
836
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
837
                                                                                        ""),
838
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
839
                                                                                        source));
840
    if (exceptionOccurred()) {
841 306962bc O'Neil Delpratt
        createException();
842 ead48a5d O'Neil Delpratt
    } else {
843
        XdmNode *value = new XdmNode(xdmNodei);
844
        return value;
845
    }
846
    return nullptr;
847 72bf04c6 Norman Walsh
}
848
849
850 ead48a5d O'Neil Delpratt
/**
851
   * Set a configuration property.
852
   *
853
   * @param name of the property
854
   * @param value of the property
855
   */
856
void SaxonProcessor::setConfigurationProperty(const char *name, const char *value) {
857
    if (name != nullptr) {
858
        configProperties.insert(
859
                std::pair<std::string, std::string>(std::string(name), std::string((value == nullptr ? "" : value))));
860 72bf04c6 Norman Walsh
    }
861 ead48a5d O'Neil Delpratt
}
862 72bf04c6 Norman Walsh
863 ead48a5d O'Neil Delpratt
void SaxonProcessor::clearConfigurationProperties() {
864
    configProperties.clear();
865
}
866 72bf04c6 Norman Walsh
867
868 ead48a5d O'Neil Delpratt
void SaxonProcessor::release() {
869
    if (SaxonProcessor::jvmCreatedCPP != 0) {
870
        SaxonProcessor::jvmCreatedCPP = 0;
871
        //std::cerr<<"SaxonProc: JVM finalized calling !"<<std::endl;
872
        finalizeJavaRT(SaxonProcessor::sxn_environ->jvm);
873 72bf04c6 Norman Walsh
874 55b80284 O'Neil Delpratt
        delete SaxonProcessor::sxn_environ;
875 ead48a5d O'Neil Delpratt
        /*clearParameters();
876
        clearProperties();*/
877
    } else {
878 4ee4355d O'Neil Delpratt
#ifdef DEBUG
879 ead48a5d O'Neil Delpratt
        std::cerr << "SaxonProc: JVM finalize not called!" << std::endl;
880 4ee4355d O'Neil Delpratt
#endif
881 ead48a5d O'Neil Delpratt
    }
882 72bf04c6 Norman Walsh
}
883
884
885 ead48a5d O'Neil Delpratt
/* ========= Factory method for Xdm ======== */
886 72bf04c6 Norman Walsh
887 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeStringValue(const char *str) {
888
    jobject obj = getJavaStringValue(SaxonProcessor::sxn_environ, str);
889
    static jmethodID mssID_atomic = nullptr;
890
    if (mssID_atomic == nullptr) {
891
        mssID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
892
                                                                                  "(Ljava/lang/String;)V"));
893
    }
894
    if (!mssID_atomic) {
895
        std::cerr << "XdmAtomic constructor (String)" << std::endl;
896
        return nullptr;
897
    }
898
    jobject obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mssID_atomic, obj));
899
    XdmAtomicValue *value = new XdmAtomicValue(obj2, "xs:string");
900 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
901
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj2);
902 ead48a5d O'Neil Delpratt
    return value;
903
}
904 72bf04c6 Norman Walsh
905 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeStringValue(std::string str) {
906
    jobject obj = getJavaStringValue(SaxonProcessor::sxn_environ, str.c_str());
907
    static jmethodID msID_atomic = nullptr;
908
    if (msID_atomic == nullptr) {
909
        msID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
910
                                                                                 "(Ljava/lang/String;)V"));
911
    }
912
    if (!msID_atomic) {
913
        std::cerr << "XdmAtomic constructor (String)" << std::endl;
914
        return nullptr;
915
    }
916
    jobject obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, msID_atomic, obj));
917
    XdmAtomicValue *value = new XdmAtomicValue(obj2, "xs:string");
918 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
919 ead48a5d O'Neil Delpratt
    return value;
920
}
921 72bf04c6 Norman Walsh
922 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeIntegerValue(int i) {
923
    //jobject obj = integerValue(*SaxonProcessor::sxn_environ, i);
924
    static jmethodID miiID_atomic = nullptr;
925
    if (miiID_atomic == nullptr) {
926
        miiID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
927
    }
928
    if (!miiID_atomic) {
929
        std::cerr << "XdmAtomic constructor (J)" << std::endl;
930
        return nullptr;
931
    }
932
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, miiID_atomic, (jlong) i));
933
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}integer");
934 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
935 ead48a5d O'Neil Delpratt
    return value;
936
}
937
938
XdmAtomicValue *SaxonProcessor::makeDoubleValue(double d) {
939
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
940
    static jmethodID mdID_atomic = nullptr;
941
    if (mdID_atomic == nullptr) {
942
        mdID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(D)V"));
943 72bf04c6 Norman Walsh
    }
944 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mdID_atomic, (jdouble) d));
945
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}double");
946 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
947 ead48a5d O'Neil Delpratt
    return value;
948
}
949 72bf04c6 Norman Walsh
950 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeFloatValue(float d) {
951
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
952
    static jmethodID mfID_atomic = nullptr;
953
    if (mfID_atomic == nullptr) {
954
        mfID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(F)V"));
955 72bf04c6 Norman Walsh
    }
956 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mfID_atomic, (jfloat) d));
957
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}float");
958 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
959 ead48a5d O'Neil Delpratt
    return value;
960
}
961 72bf04c6 Norman Walsh
962 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeLongValue(long l) {
963
    //jobject obj = longValue(*SaxonProcessor::sxn_environ, l);
964
    static jmethodID mlID_atomic = nullptr;
965
    if (mlID_atomic == nullptr) {
966
        mlID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
967 72bf04c6 Norman Walsh
    }
968 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mlID_atomic, (jlong) l));
969
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}long");
970 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
971 ead48a5d O'Neil Delpratt
    return value;
972
}
973 72bf04c6 Norman Walsh
974 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeBooleanValue(bool b) {
975
    //jobject obj = booleanValue(*SaxonProcessor::sxn_environ, b);
976
    static jmethodID mID_atomic = nullptr;
977
    if (mID_atomic == nullptr) {
978
        mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(Z)V"));
979 72bf04c6 Norman Walsh
    }
980 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, (jboolean) b));
981
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}boolean");
982 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
983 ead48a5d O'Neil Delpratt
    return value;
984
}
985
986
XdmAtomicValue *SaxonProcessor::makeQNameValue(const char *str) {
987
    jobject val = xdmValueAsObj(SaxonProcessor::sxn_environ, "QName", str);
988
    XdmAtomicValue *value = new XdmAtomicValue(val, "QName");
989 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(val);
990 ead48a5d O'Neil Delpratt
    return value;
991
}
992 72bf04c6 Norman Walsh
993 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeAtomicValue(const char *typei, const char *strValue) {
994
    jobject obj = xdmValueAsObj(SaxonProcessor::sxn_environ, typei, strValue);
995
    XdmAtomicValue *value = new XdmAtomicValue(obj, typei);
996 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
997 ead48a5d O'Neil Delpratt
    return value;
998
}
999
1000
const char *SaxonProcessor::getStringValue(XdmItem *item) {
1001
    const char *result = stringValue(SaxonProcessor::sxn_environ, item->getUnderlyingValue());
1002 72bf04c6 Norman Walsh
#ifdef DEBUG
1003 ead48a5d O'Neil Delpratt
    if(result == nullptr) {
1004
        std::cout<<"getStringValue of XdmItem is nullptr"<<std::endl;
1005
    } else {
1006
        std::cout<<"getStringValue of XdmItem is OK"<<std::endl;
1007
    }
1008 72bf04c6 Norman Walsh
#endif
1009
1010 ead48a5d O'Neil Delpratt
    return result;
1011
1012
}
1013 72bf04c6 Norman Walsh
1014
#if CVERSION_API_NO >= 123
1015
1016 ead48a5d O'Neil Delpratt
XdmArray *SaxonProcessor::makeArray(short *input, int length) {
1017
    if (input == nullptr) {
1018
        std::cerr << "Error found when converting string to XdmArray" << std::endl;
1019
        return nullptr;
1020
    }
1021
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1022
    static jmethodID mmssID = nullptr;
1023
    if (mmssID == nullptr) {
1024
        mmssID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1025
                                                                                 "([S)Lnet/sf/saxon/s9api/XdmArray;");
1026
    }
1027
    if (!mmssID) {
1028
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([S)" << " not found" << std::endl;
1029
        return nullptr;
1030
    }
1031 72bf04c6 Norman Walsh
1032
1033 ead48a5d O'Neil Delpratt
    jshortArray sArray = nullptr;
1034 72bf04c6 Norman Walsh
1035 ead48a5d O'Neil Delpratt
    sArray = SaxonProcessor::sxn_environ->env->NewShortArray((jint) length);
1036
    jshort fill[length];
1037
    for (int i = 0; i < length; i++) {
1038
        fill[i] = input[i];
1039
    }
1040
    SaxonProcessor::sxn_environ->env->SetShortArrayRegion(sArray, 0, length, fill);
1041 72bf04c6 Norman Walsh
1042 ead48a5d O'Neil Delpratt
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmssID, sArray);
1043
    if (!xdmArrayi) {
1044
        std::cerr << "Error found when converting string to XdmArray";
1045
        return nullptr;
1046
    }
1047 72bf04c6 Norman Walsh
1048 ead48a5d O'Neil Delpratt
    if (exceptionOccurred()) {
1049 306962bc O'Neil Delpratt
        createException();
1050 ead48a5d O'Neil Delpratt
    } else {
1051
        XdmArray *value = new XdmArray(xdmArrayi);
1052
        return value;
1053
    }
1054
    return nullptr;
1055
}
1056 72bf04c6 Norman Walsh
1057
1058 ead48a5d O'Neil Delpratt
XdmArray *SaxonProcessor::makeArray(int *input, int length) {
1059
    if (input == nullptr) {
1060
        std::cerr << "Error found when converting string to XdmArray";
1061
        return nullptr;
1062
    }
1063
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1064
    static jmethodID mmiiID = nullptr;
1065
    if (mmiiID == nullptr) {
1066
        mmiiID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1067
                                                                                 "([I)Lnet/sf/saxon/s9api/XdmArray;");
1068
    }
1069
    if (!mmiiID) {
1070
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([I)" << " not found" << std::endl;
1071
        return nullptr;
1072
    }
1073
1074
1075
    jintArray iArray = nullptr;
1076
1077
    iArray = SaxonProcessor::sxn_environ->env->NewIntArray((jint) length);
1078
    jint fill[length];
1079
    for (int i = 0; i < length; i++) {
1080
        fill[i] = input[i];
1081
    }
1082
    SaxonProcessor::sxn_environ->env->SetIntArrayRegion(iArray, 0, length, fill);
1083
1084
1085
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiiID, iArray);
1086
    if (!xdmArrayi) {
1087
        std::cerr << "Error found when converting string to XdmArray";
1088
        return nullptr;
1089
    }
1090
    if (exceptionOccurred()) {
1091 306962bc O'Neil Delpratt
        createException();
1092 ead48a5d O'Neil Delpratt
    } else {
1093
        XdmArray *value = new XdmArray(xdmArrayi);
1094
        return value;
1095
    }
1096
    return nullptr;
1097
1098
}
1099
1100
XdmArray *SaxonProcessor::makeArray(long *input, int length) {
1101
    if (input == nullptr) {
1102
        std::cerr << "Error found when converting string to XdmArray";
1103
        return nullptr;
1104
    }
1105
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1106
    static jmethodID mmiID = nullptr;
1107
    if (mmiID == nullptr) {
1108
        mmiID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1109
                                                                                "([J)Lnet/sf/saxon/s9api/XdmArray;");
1110
    }
1111
    if (!mmiID) {
1112
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([J)" << " not found" << std::endl;
1113
        return nullptr;
1114
    }
1115
1116
1117
    jlongArray lArray = nullptr;
1118
1119
    lArray = SaxonProcessor::sxn_environ->env->NewLongArray((jint) length);
1120
    jlong fill[length];
1121
    for (int i = 0; i < length; i++) {
1122
        fill[i] = input[i];
1123
    }
1124
    SaxonProcessor::sxn_environ->env->SetLongArrayRegion(lArray, 0, length, fill);
1125
1126
1127
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiID, lArray);
1128
    if (!xdmArrayi) {
1129
        std::cerr << "Error found when converting string to XdmArray";
1130
        return nullptr;
1131
    }
1132
    if (exceptionOccurred()) {
1133 306962bc O'Neil Delpratt
        createException();
1134 ead48a5d O'Neil Delpratt
    } else {
1135
        XdmArray *value = new XdmArray(xdmArrayi);
1136
        return value;
1137
    }
1138
    return nullptr;
1139
}
1140
1141
1142
XdmArray *SaxonProcessor::makeArray(bool *input, int length) {
1143
    if (input == nullptr) {
1144
        std::cerr << "Error found when converting string to XdmArray";
1145
        return nullptr;
1146
    }
1147
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1148
    static jmethodID mmbID = nullptr;
1149
    if (mmbID == nullptr) {
1150
        mmbID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1151
                                                                                "([Z)Lnet/sf/saxon/s9api/XdmArray;");
1152
    }
1153
    if (!mmbID) {
1154
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([Z)" << " not found" << std::endl;
1155
        return nullptr;
1156
    }
1157
1158
1159
    jbooleanArray bArray = nullptr;
1160
1161
    bArray = SaxonProcessor::sxn_environ->env->NewBooleanArray((jint) length);
1162
    jboolean fill[length];
1163
    for (int i = 0; i < length; i++) {
1164
        fill[i] = input[i];
1165
    }
1166
    SaxonProcessor::sxn_environ->env->SetBooleanArrayRegion(bArray, 0, length, fill);
1167
1168
1169
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmbID, bArray);
1170
    if (!xdmArrayi) {
1171
        std::cerr << "Error found when converting string to XdmArray";
1172
        return nullptr;
1173
    }
1174
    if (exceptionOccurred()) {
1175 306962bc O'Neil Delpratt
        createException();
1176 ead48a5d O'Neil Delpratt
    } else {
1177
        XdmArray *value = new XdmArray(xdmArrayi);
1178
        return value;
1179
    }
1180
    return nullptr;
1181
1182
1183
}
1184
1185
1186
XdmArray *SaxonProcessor::makeArray(const char **input, int length) {
1187
    if (input == nullptr || length <= 0) {
1188
        std::cerr << "Error found when converting string to XdmArray";
1189
        return nullptr;
1190
    }
1191
    jobject obj = nullptr;
1192
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1193
    jmethodID mmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1194
                                                                                     "([Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmArray;");
1195
1196
    jmethodID mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
1197
                                                                                      "(Ljava/lang/String;)V"));
1198
    jobjectArray valueArray = nullptr;
1199
    jobject obj2 = nullptr;
1200
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length, xdmAtomicClass, 0);
1201
    for (int i = 0; i < length; i++) {
1202
        if (input[i] == nullptr) {
1203
            std::cerr << "Error found when converting string to XdmArray";
1204
            return nullptr;
1205 72bf04c6 Norman Walsh
        }
1206 ead48a5d O'Neil Delpratt
        obj = getJavaStringValue(SaxonProcessor::sxn_environ, input[i]);
1207
        obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, obj));
1208
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, obj2);
1209
    }
1210 72bf04c6 Norman Walsh
1211 ead48a5d O'Neil Delpratt
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmID, valueArray);
1212
    if (!xdmArrayi) {
1213
        std::cerr << "Error found when converting string to XdmArray";
1214
        return nullptr;
1215
    }
1216 72bf04c6 Norman Walsh
1217 ead48a5d O'Neil Delpratt
    if (exceptionOccurred()) {
1218
        checkAndCreateException(saxonCAPIClass);
1219
    } else {
1220
        XdmArray *value = new XdmArray(xdmArrayi);
1221
        return value;
1222
    }
1223
    return nullptr;
1224
}
1225 72bf04c6 Norman Walsh
1226
1227 901b6eca O'Neil Delpratt
XdmMap *SaxonProcessor::makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap) {
1228 ead48a5d O'Neil Delpratt
    jobjectArray keyArray = nullptr;
1229
    jobjectArray valueArray = nullptr;
1230
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
1231
                                      "java/lang/Object");
1232 72bf04c6 Norman Walsh
1233 ead48a5d O'Neil Delpratt
    int size = dataMap.size();
1234 72bf04c6 Norman Walsh
1235 ead48a5d O'Neil Delpratt
    if (size > 0) {
1236 72bf04c6 Norman Walsh
1237 ead48a5d O'Neil Delpratt
        keyArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1238
                                                                    objectClass, 0);
1239
        valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1240
                                                                      objectClass, 0);
1241
        int i = 0;
1242
        for (std::map<XdmAtomicValue *, XdmValue *>::iterator iter =
1243
                dataMap.begin(); iter != dataMap.end(); ++iter, i++) {
1244 72bf04c6 Norman Walsh
1245
1246 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(keyArray, i,
1247
                                                                    (iter->first)->getUnderlyingValue());
1248 72bf04c6 Norman Walsh
1249 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i,
1250
                                                                    (iter->second)->getUnderlyingValue());
1251 72bf04c6 Norman Walsh
1252 ead48a5d O'Neil Delpratt
        }
1253
        jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
1254
        jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass, "makeXdmMap",
1255
                                                                                         "([Lnet/sf/saxon/s9api/XdmAtomicValue;[Lnet/sf/saxon/s9api/XdmValue;)Lnet/sf/saxon/s9api/XdmMap;");
1256
        if (!xmID) {
1257
            std::cerr << "Error: SaxonDll." << "makeXdmMap"
1258
                      << " not found\n" << std::endl;
1259
            return nullptr;
1260
        }
1261 72bf04c6 Norman Walsh
1262
1263 ead48a5d O'Neil Delpratt
        jobject results = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
1264
                                                                                              keyArray, valueArray));
1265 901b6eca O'Neil Delpratt
        if (exceptionOccurred() || !results) {
1266
            checkAndCreateException(saxonCAPIClass);
1267
        } else {
1268
            return new XdmMap(results);
1269
        }
1270 ead48a5d O'Neil Delpratt
    }
1271 72bf04c6 Norman Walsh
1272 901b6eca O'Neil Delpratt
    return nullptr;
1273 ead48a5d O'Neil Delpratt
}
1274 72bf04c6 Norman Walsh
1275
#endif
1276