Project

Profile

Help

Maintenance. Planio will be undergoing a scheduled maintenance this weekend. Between Saturday, July 24 at 9:00 UTC and Sunday, July 25, 22:00 UTC your account might observe occasional downtimes which may last up to several minutes in some cases.

How to connect?
Download (49.2 KB) Statistics
| Branch: | Revision:

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

1
#ifndef __linux__
2
#ifndef __APPLE__
3
//#include "stdafx.h"
4
#include <Tchar.h>
5
#endif
6
#endif
7

    
8
#include "SaxonProcessor.h"
9
#include "XdmValue.h"
10
#include "XdmItem.h"
11
#include "XdmNode.h"
12
#include "XdmFunctionItem.h"
13
#include "XdmMap.h"
14
#include "XdmArray.h"
15
#include "XdmAtomicValue.h"
16

    
17

    
18
//#define DEBUG
19
#ifdef DEBUG
20
#include <signal.h>
21
#endif
22

    
23
#include <stdio.h>
24

    
25

    
26
#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
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
#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
//jobject cpp;
75
const char *failure;
76
sxnc_environment *SaxonProcessor::sxn_environ = 0;
77
int SaxonProcessor::jvmCreatedCPP = 0;
78

    
79
bool SaxonProcessor::exceptionOccurred() {
80
    return SaxonProcessor::sxn_environ->env->ExceptionCheck() || exception != nullptr;
81
}
82

    
83
const char *SaxonProcessor::checkException() {
84
    const char *message = nullptr;
85
    message = checkForException(sxn_environ);
86
    return message;
87
}
88

    
89

    
90

    
91
SaxonApiException *SaxonProcessor::checkAndCreateException(jclass cppClass) {
92
    if (SaxonProcessor::sxn_environ->env->ExceptionCheck()) {
93
        SaxonApiException *exception = checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass, nullptr);
94
#ifdef DEBUG
95
        SaxonProcessor::sxn_environ->env->ExceptionDescribe();
96
#endif
97
        return exception;
98
    }
99
    return nullptr;
100
}
101

    
102
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
void SaxonProcessor::exceptionClear() {
112
    if(exceptionOccurred()) {
113
        SaxonProcessor::sxn_environ->env->ExceptionClear();
114
    }
115
    std::cerr<< "saxonProc exceptionClear cp0"<<std::endl;
116
    if (exception != nullptr) {
117
        std::cerr<< "saxonProc exception= "<< exception <<std::endl;
118
        delete exception;
119
        exception = nullptr;
120
    }
121

    
122
}
123

    
124

    
125
SaxonProcessor::SaxonProcessor() {
126
    licensei = false;
127
    SaxonProcessor(false);
128
}
129

    
130
const char * SaxonProcessor::getResourcesDirectory() {
131
    return NULL;
132
}
133

    
134
void SaxonProcessor::createException(const char * message) {
135

    
136
    exceptionClear();
137

    
138
    if(message == nullptr) {
139
        exception = checkAndCreateException(saxonCAPIClass);
140
    } else {
141
        exception = new SaxonApiException(message);
142
    }
143

    
144
}
145

    
146

    
147
SaxonApiException * SaxonProcessor::checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject) {
148

    
149
    if (env->ExceptionCheck()) {
150
        std::string result1 = "";
151
        std::string errorCode = "";
152
        jthrowable exc = env->ExceptionOccurred();
153

    
154
#ifdef DEBUG
155
        env->ExceptionDescribe();
156
#endif
157
        jclass exccls(env->GetObjectClass(exc));
158
        jclass clscls(env->FindClass("java/lang/Class"));
159

    
160
        jmethodID getName(env->GetMethodID(clscls, "getName", "()Ljava/lang/String;"));
161
        jstring name(static_cast<jstring>(env->CallObjectMethod(exccls, getName)));
162
        char const *utfName(env->GetStringUTFChars(name, 0));
163
        result1 = (std::string(utfName));
164
        env->ReleaseStringUTFChars(name, utfName);
165

    
166
        jmethodID getMessage(env->GetMethodID(exccls, "getMessage", "()Ljava/lang/String;"));
167
        if (getMessage) {
168

    
169
            jstring message((jstring) (env->CallObjectMethod(exc, getMessage)));
170
            char const *utfMessage = nullptr;
171
            if (!message) {
172
                utfMessage = "";
173
                return nullptr;
174
            } else {
175
                utfMessage = (env->GetStringUTFChars(message, 0));
176
            }
177
            if (utfMessage != nullptr) {
178
                result1 = (result1 + " : ") + utfMessage;
179
            }
180

    
181
            env->ReleaseStringUTFChars(message, utfMessage);
182

    
183
            if (callingObject != nullptr && result1.compare(0, 43, "net.sf.saxon.s9api.SaxonApiException", 43) == 0) {
184

    
185
                jclass saxonApiExceptionClass(env->FindClass("net/sf/saxon/s9api/SaxonApiException"));
186
                static jmethodID lineNumID = nullptr;
187
                if (lineNumID == nullptr) {
188
                    lineNumID = env->GetMethodID(saxonApiExceptionClass, "getLinenumber", "()I");
189
                }
190
                static jmethodID ecID = nullptr;
191
                if (ecID == nullptr) {
192
                    ecID = env->GetMethodID(saxonApiExceptionClass, "getErrorCode", "()Ljnet/sf/saxon/s9api/QName;");
193
                }
194
                static jmethodID esysID = nullptr;
195
                if (esysID == nullptr) {
196
                    esysID = env->GetMethodID(saxonApiExceptionClass, "getSystemId", "()Ljava/lang/String;");
197
                }
198

    
199

    
200
                jobject errCodeQName = (jobject) (env->CallObjectMethod(exc, ecID));
201
                jstring errSystemID = (jstring) (env->CallObjectMethod(exc, esysID));
202
                int linenum = env->CallIntMethod(exc, lineNumID);
203

    
204
                jclass qnameClass(env->FindClass("net/sf/saxon/s9api/QName"));
205
                static jmethodID qnameStrID = nullptr;
206
                if (qnameStrID == nullptr) {
207
                    qnameStrID = env->GetMethodID(qnameClass, "toString", "()Ljava/lang/String;");
208
                }
209

    
210
                jstring qnameStr = (jstring) (env->CallObjectMethod(errCodeQName, qnameStrID));
211

    
212

    
213
                SaxonApiException *saxonExceptions = new SaxonApiException(result1.c_str(),
214
                                                                           (qnameStr ? env->GetStringUTFChars(qnameStr,
215
                                                                                                              0)
216
                                                                                     : nullptr),
217
                                                                           (errSystemID ? env->GetStringUTFChars(
218
                                                                                   errSystemID, 0) : nullptr), linenum);
219

    
220
                if (errCodeQName) {
221
                    env->DeleteLocalRef(errCodeQName);
222
                }
223
                if (errSystemID) {
224
                    env->DeleteLocalRef(errSystemID);
225
                }
226
                if (qnameStr) {
227
                    env->DeleteLocalRef(qnameStr);
228
                }
229

    
230
                if (message) {
231
                    env->DeleteLocalRef(message);
232
                }
233
                env->ExceptionClear();
234
                return saxonExceptions;
235
            }
236
        }
237
        SaxonApiException *saxonExceptions = new SaxonApiException(result1.c_str());
238
        //env->ExceptionDescribe();
239
        env->ExceptionClear();
240
        return saxonExceptions;
241
    }
242
    return nullptr;
243

    
244
}
245

    
246

    
247
SaxonProcessor::SaxonProcessor(bool l) {
248

    
249
    cwd = "";
250
    licensei = l;
251
    versionStr = nullptr;
252
    exception = nullptr;
253

    
254
    if (SaxonProcessor::jvmCreatedCPP == 0) {
255
        SaxonProcessor::jvmCreatedCPP = 1;
256
        SaxonProcessor::sxn_environ = new sxnc_environment;//(sxnc_environment *)malloc(sizeof(sxnc_environment));
257

    
258

    
259
        /*
260
         * First of all, load required component.
261
         * By the time of JET initialization, all components should be loaded.
262
         */
263

    
264
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
265

    
266
        /*
267
         * Initialize JET run-time.
268
         * The handle of loaded component is used to retrieve Invocation API.
269
         */
270
        initDefaultJavaRT(SaxonProcessor::sxn_environ);
271
    } else {
272
#ifdef DEBUG
273
        std::cerr<<"SaxonProc constructor: jvm exists! jvmCreatedCPP="<<jvmCreatedCPP<<std::endl;
274
#endif
275

    
276
    }
277

    
278

    
279
    versionClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/Version");
280
    procClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/Processor");
281
    saxonCAPIClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/SaxonCAPI");
282

    
283
    jobject proci = createSaxonProcessor(SaxonProcessor::sxn_environ->env, procClass, "(Z)V", nullptr, licensei);
284
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(proci);
285
    if (!proc) {
286
        std::cout << "proc is nullptr in SaxonProcessor constructor" << std::endl;
287
    }
288

    
289
    xdmAtomicClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
290
#ifdef DEBUG
291
    jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "setDebugMode", "(Z)V");
292
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(saxonCAPIClass, debugMID, (jboolean)true);
293
#endif
294
}
295

    
296
SaxonProcessor::SaxonProcessor(const char *configFile) {
297
    cwd = "";
298
    versionStr = nullptr;
299

    
300
    if (SaxonProcessor::jvmCreatedCPP == 0) {
301
        SaxonProcessor::jvmCreatedCPP = 1;
302
        //SaxonProcessor::sxn_environ= new sxnc_environment;
303
        SaxonProcessor::sxn_environ = (sxnc_environment *) malloc(sizeof(sxnc_environment));
304

    
305
        /*
306
         * First of all, load required component.
307
         * By the time of JET initialization, all components should be loaded.
308
         */
309

    
310
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
311

    
312
        /*
313
         * Initialize JET run-time.
314
         * The handle of loaded component is used to retrieve Invocation API.
315
         */
316
        initDefaultJavaRT(SaxonProcessor::sxn_environ);
317
    }
318

    
319
    versionClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/Version");
320

    
321
    procClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/Processor");
322
    saxonCAPIClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/SaxonCAPI");
323

    
324
    static jmethodID mIDcreateProc = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
325
                                                                                                     "createSaxonProcessor",
326
                                                                                                     "(Ljava/lang/String;)Lnet/sf/saxon/s9api/Processor;");
327
    if (!mIDcreateProc) {
328
        std::cerr << "Error: SaxonDll." << "getPrimitiveTypeName"
329
                  << " not found\n" << std::endl;
330
        return;
331
    }
332
    jobject proci = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mIDcreateProc,
333
                                                                             SaxonProcessor::sxn_environ->env->NewStringUTF(
334
                                                                                     configFile));
335
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(proci);
336
    if (!proc) {
337
        checkAndCreateException(saxonCAPIClass);
338
        std::cerr << "Error: " << getDllname() << ". processor is nullptr in constructor(configFile)" << std::endl;
339
        return;
340
    }
341

    
342
    licensei = true;
343
#ifdef DEBUG
344

    
345
    std::cerr<<"SaxonProc constructor(configFile)"<<std::endl;
346
#endif
347
    xdmAtomicClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
348
}
349

    
350
SaxonProcessor::~SaxonProcessor() {
351
    clearConfigurationProperties();
352
    if(proc) {
353
            SaxonProcessor::sxn_environ->env->DeleteGlobalRef(proc);
354
    }
355
    if (versionStr != nullptr) {
356
        delete versionStr;
357
        versionStr = nullptr;
358
    }
359
    exceptionClear();
360
}
361

    
362

    
363
bool SaxonProcessor::isSchemaAwareProcessor() {
364
    if (!licensei) {
365
        return false;
366
    } else {
367
        static jmethodID MID_schema = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(procClass,
368
                                                                                                "isSchemaAware", "()Z");
369
        if (!MID_schema) {
370
            std::cerr << "\nError: Saxonc " << "SaxonProcessor.isSchemaAware()" << " not found" << std::endl;
371
            return false;
372
        }
373

    
374
        licensei = (jboolean) (SaxonProcessor::sxn_environ->env->CallBooleanMethod(proc, MID_schema));
375
        return licensei;
376

    
377
    }
378

    
379
}
380

    
381
void SaxonProcessor::applyConfigurationProperties() {
382
    if (configProperties.size() > 0) {
383
        int size = configProperties.size();
384
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
385
        jobjectArray stringArray1 = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size, stringClass, 0);
386
        jobjectArray stringArray2 = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size, stringClass, 0);
387
        static jmethodID mIDappConfig = nullptr;
388
        if (mIDappConfig == nullptr) {
389
            mIDappConfig = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
390
                                                                                           "applyToConfiguration",
391
                                                                                           "(Lnet/sf/saxon/s9api/Processor;[Ljava/lang/String;[Ljava/lang/String;)V");
392
            if (!mIDappConfig) {
393
                std::cerr << "Error: SaxonDll." << "applyToConfiguration"
394
                          << " not found\n" << std::endl;
395
                return;
396
            }
397
        }
398
        int i = 0;
399
        std::map<std::string, std::string>::iterator iter = configProperties.begin();
400
        for (iter = configProperties.begin(); iter != configProperties.end(); ++iter, i++) {
401
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray1, i,
402
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
403
                                                                            (iter->first).c_str()));
404
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray2, i,
405
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
406
                                                                            (iter->second).c_str()));
407
        }
408
        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mIDappConfig, proc, stringArray1,
409
                                                                 stringArray2);
410
        if (exceptionOccurred()) {
411
            createException();
412
        }
413
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray1);
414
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray2);
415

    
416
    }
417
}
418

    
419

    
420
jobjectArray SaxonProcessor::createJArray(XdmValue **values, int length) {
421
    jobjectArray valueArray = nullptr;
422

    
423
    jclass xdmValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
424
                                        "net/sf/saxon/s9api/XdmValue");
425

    
426

    
427
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length,
428
                                                                  xdmValueClass, 0);
429

    
430
    for (int i = 0; i < length; i++) {
431
#ifdef DEBUG
432
        std::string s1 = typeid(values[i]).name();
433
        std::cerr<<"In createJArray\nType of itr:"<<s1<<std::endl;
434

    
435

    
436
        jobject xx = values[i]->getUnderlyingValue();
437

    
438
        if(xx == nullptr) {
439
            std::cerr<<"value failed"<<std::endl;
440
        } else {
441

    
442
            std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
443
        }
444
        if(values[i]->getUnderlyingValue() == nullptr) {
445
            std::cerr<<"value["<<i<<"]->getUnderlyingValue() is nullptr"<<std::endl;
446
        }
447
#endif
448
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, values[i]->getUnderlyingValue());
449
    }
450
    return valueArray;
451

    
452
}
453

    
454

    
455
JParameters SaxonProcessor::createParameterJArray(std::map<std::string, XdmValue *> parameters,
456
                                                  std::map<std::string, std::string> properties) {
457
    JParameters comboArrays;
458
    comboArrays.stringArray = nullptr;
459
    comboArrays.objectArray = nullptr;
460
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
461
                                      "java/lang/Object");
462
    jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
463
                                      "java/lang/String");
464

    
465
    int size = parameters.size() + properties.size();
466
#ifdef DEBUG
467
    std::cerr<<"Properties size: "<<properties.size()<<std::endl;
468
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
469
#endif
470
    if (size > 0) {
471

    
472
        comboArrays.objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
473
                                                                                   objectClass, 0);
474
        comboArrays.stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
475
                                                                                   stringClass, 0);
476
        int i = 0;
477
        for (std::map<std::string, XdmValue *>::iterator iter =
478
                parameters.begin(); iter != parameters.end(); ++iter, i++) {
479

    
480
#ifdef DEBUG
481
            std::cerr<<"map 1"<<std::endl;
482
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
483
#endif
484
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
485
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
486
                                                                            (iter->first).c_str()));
487
#ifdef DEBUG
488
            std::string s1 = typeid(iter->second).name();
489
            std::cerr<<"Type of itr:"<<s1<<std::endl;
490

    
491
            if((iter->second) == nullptr) {std::cerr<<"iter->second is nullptr"<<std::endl;
492
            } else {
493
                std::cerr<<"getting underlying value"<<std::endl;
494
            jobject xx = (iter->second)->getUnderlyingValue();
495

    
496
            if(xx == nullptr) {
497
                std::cerr<<"value failed"<<std::endl;
498
            } else {
499

    
500
                std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
501
            }
502
            if((iter->second)->getUnderlyingValue() == nullptr) {
503
                std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
504
            }}
505
#endif
506

    
507
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
508
                                                                    (iter->second)->getUnderlyingValue());
509

    
510
        }
511

    
512
        for (std::map<std::string, std::string>::iterator iter =
513
                properties.begin(); iter != properties.end(); ++iter, i++) {
514
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
515
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
516
                                                                            (iter->first).c_str()));
517
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
518
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
519
                                                                            (iter->second).c_str()));
520
        }
521

    
522
        return comboArrays;
523

    
524
    } else {
525
        return comboArrays;
526
    }
527
}
528

    
529
JParameters SaxonProcessor::createParameterJArray2(std::map<std::string, XdmValue *> parameters) {
530
    JParameters comboArrays;
531
    comboArrays.stringArray = nullptr;
532
    comboArrays.objectArray = nullptr;
533
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
534
                                      "java/lang/Object");
535
    jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
536
                                      "java/lang/String");
537

    
538
    int size = parameters.size();
539
#ifdef DEBUG
540
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
541
#endif
542
    if (size > 0) {
543

    
544
        comboArrays.objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
545
                                                                                   objectClass, 0);
546
        comboArrays.stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
547
                                                                                   stringClass, 0);
548
        int i = 0;
549
        for (std::map<std::string, XdmValue *>::iterator iter =
550
                parameters.begin(); iter != parameters.end(); ++iter, i++) {
551

    
552
#ifdef DEBUG
553
            std::cerr<<"map 1"<<std::endl;
554
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
555
#endif
556
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
557
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
558
                                                                            (iter->first).c_str()));
559
#ifdef DEBUG
560
            std::string s1 = typeid(iter->second).name();
561
            std::cerr<<"Type of itr:"<<s1<<std::endl;
562

    
563
            if((iter->second) == nullptr) {std::cerr<<"iter->second is nullptr"<<std::endl;
564
            } else {
565
                std::cerr<<"getting underlying value"<<std::endl;
566
            jobject xx = (iter->second)->getUnderlyingValue();
567

    
568
            if(xx == nullptr) {
569
                std::cerr<<"value failed"<<std::endl;
570
            } else {
571

    
572
                std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
573
            }
574
            if((iter->second)->getUnderlyingValue() == nullptr) {
575
                std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
576
            }}
577
#endif
578

    
579
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
580
                                                                    (iter->second)->getUnderlyingValue());
581

    
582
        }
583

    
584

    
585
        return comboArrays;
586

    
587
    } else {
588
        return comboArrays;
589
    }
590
}
591

    
592

    
593
SaxonProcessor &SaxonProcessor::operator=(const SaxonProcessor &other) {
594
    versionClass = other.versionClass;
595
    procClass = other.procClass;
596
    saxonCAPIClass = other.saxonCAPIClass;
597
    cwd = other.cwd;
598
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(other.proc);
599
    parameters = other.parameters;
600
    configProperties = other.configProperties;
601
    licensei = other.licensei;
602
    exception = other.exception;
603
    return *this;
604
}
605

    
606
SaxonProcessor::SaxonProcessor(const SaxonProcessor &other) {
607
    versionClass = other.versionClass;
608
    procClass = other.procClass;
609
    saxonCAPIClass = other.saxonCAPIClass;
610
    cwd = other.cwd;
611
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(other.proc);
612
    parameters = other.parameters;
613
    configProperties = other.configProperties;
614
    licensei = other.licensei;
615
    exception = other.exception;
616
}
617

    
618

    
619

    
620
void SaxonProcessor::setResourcesDirectory(const char *dir){}
621

    
622
XsltProcessor *SaxonProcessor::newXsltProcessor() {
623
    return (new XsltProcessor(this, cwd));
624
}
625

    
626
Xslt30Processor *SaxonProcessor::newXslt30Processor() {
627
    return (new Xslt30Processor(this, cwd));
628
}
629

    
630
XQueryProcessor *SaxonProcessor::newXQueryProcessor() {
631
    return (new XQueryProcessor(this, cwd));
632
}
633

    
634
XPathProcessor *SaxonProcessor::newXPathProcessor() {
635
    return (new XPathProcessor(this, cwd));
636
}
637

    
638
SchemaValidator *SaxonProcessor::newSchemaValidator() {
639
    if (licensei) {
640
        return (new SchemaValidator(this, cwd));
641
    } else {
642
        std::cerr << "\nError: Processor is not licensed for schema processing!" << std::endl;
643
        return nullptr;
644
    }
645
}
646

    
647

    
648
const char *SaxonProcessor::version() {
649
    if (versionStr == nullptr) {
650

    
651
        static jmethodID MID_version = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
652
                                                                                                       "getProductVersion",
653
                                                                                                       "(Lnet/sf/saxon/s9api/Processor;)Ljava/lang/String;");
654
        if (!MID_version) {
655
            std::cerr << "\nError: MyClassInDll " << "SaxonCAPI.getProductVersion()" << " not found" << std::endl;
656
            return nullptr;
657
        }
658

    
659
        if(proc == nullptr) {
660
            createException("The Java SaxonProcessor object (i.e. cppXQ) is NULL - Possible exception thrown");
661
            return nullptr;
662
        }
663

    
664
        jstring jstr = (jstring) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, MID_version,
665
                                                                                           proc));
666
        const char *tempVersionStr = SaxonProcessor::sxn_environ->env->GetStringUTFChars(jstr, nullptr);
667
        int verLen = strlen(tempVersionStr) + 22 + strlen(CVERSION) + 1;
668
        versionStr = new char[verLen];
669
        snprintf(versionStr, verLen, "Saxon/C %s %s %s", CVERSION, "running with", tempVersionStr);
670
        SaxonProcessor::sxn_environ->env->ReleaseStringUTFChars(jstr, tempVersionStr);
671

    
672

    
673
    }
674

    
675
    return versionStr;
676
}
677

    
678
void SaxonProcessor::setcwd(const char *dir) {
679
    if(dir != nullptr) {
680
        cwd = std::string(dir);
681
    }
682
}
683

    
684
const char *SaxonProcessor::getcwd() {
685
    return cwd.c_str();
686
}
687

    
688

    
689
void SaxonProcessor::setCatalog(const char *catalogFile, bool isTracing) {
690
    jclass xmlResolverClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/trans/XmlCatalogResolver");
691
    static jmethodID catalogMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(xmlResolverClass, "setCatalog",
692
                                                                                      "(Ljava/lang/String;Lnet/sf/saxon/Configuration;Z)V");
693

    
694
    if (!catalogMID) {
695
        std::cerr << "\nError: Saxonc." << "setCatalog()" << " not found" << std::endl;
696
        return;
697
    }
698
    if (catalogFile == nullptr) {
699

    
700
        return;
701
    }
702
    static jmethodID configMID = SaxonProcessor::sxn_environ->env->GetMethodID(procClass, "getUnderlyingConfiguration",
703
                                                                               "()Lnet/sf/saxon/Configuration;");
704

    
705
    if (!configMID) {
706
        std::cerr << "\nError: Saxonc." << "getUnderlyingConfiguration()" << " not found" << std::endl;
707
        return;
708
    }
709

    
710

    
711
    if (!proc) {
712
        createException("Processor is null in SaxonProcessor.setCatalog");
713
        return;
714
    }
715

    
716
    jobject configObj = SaxonProcessor::sxn_environ->env->CallObjectMethod(proc, configMID);
717

    
718
    if (!configObj) {
719
        std::cout << "proc is null in SaxonProcessor setcatalog - config obj" << std::endl;
720
        return;
721
    }
722
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(xmlResolverClass, catalogMID,
723
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(catalogFile),
724
                                                           configObj, (jboolean) isTracing);
725
#ifdef DEBUG
726
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
727
#endif
728

    
729
}
730

    
731

    
732
XdmNode *SaxonProcessor::parseXmlFromString(const char *source) {
733

    
734
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlString",
735
                                                                                    "(Ljava/lang/String;Lnet/sf/saxon/s9api/Processor;Lnet/sf/saxon/s9api/SchemaValidator;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
736
    if (!mID) {
737
        std::cerr << "\nError: Saxonc." << "parseXmlString()" << " not found" << std::endl;
738
        return nullptr;
739
    }
740
//TODO SchemaValidator
741

    
742
    if(source == nullptr) {
743
        createException("Source string is NULL");
744
        return nullptr;
745
    }
746

    
747
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID,
748
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
749
                                                                                        cwd.c_str()), proc, nullptr,
750
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
751
                                                                                        source));
752
    if (xdmNodei) {
753
        XdmNode *value = nullptr;
754
        value = new XdmNode(xdmNodei);
755
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(xdmNodei);
756
        return value;
757
    } else if (exceptionOccurred()) {
758
        createException();
759
    }
760

    
761
#ifdef DEBUG
762
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
763
#endif
764

    
765
    return nullptr;
766
}
767

    
768
int SaxonProcessor::getNodeKind(jobject obj) {
769
    jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmNode;");
770
    static jmethodID nodeKindMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
771
                                                                                             "getNodeKind",
772
                                                                                             "()Lnet/sf/saxon/s9api/XdmNodeKind;");
773
    if (!nodeKindMID) {
774
        std::cerr << "Error: MyClassInDll." << "getNodeKind" << " not found\n"
775
                  << std::endl;
776
        return 0;
777
    }
778

    
779
    jobject nodeKindObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(obj, nodeKindMID));
780
    if (!nodeKindObj) {
781

    
782
        return 0;
783
    }
784
    jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/option/cpp/XdmUtils;");
785

    
786
    jmethodID mID2 = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,
787
                                                                                     "convertNodeKindType",
788
                                                                                     "(Lnet/sf/saxon/s9api/XdmNodeKind;)I");
789

    
790
    if (!mID2) {
791
        std::cerr << "Error: MyClassInDll." << "convertNodeKindType" << " not found\n"
792
                  << std::endl;
793
        return 0;
794
    }
795
    if (!nodeKindObj) {
796
        return 0;
797
    }
798
    int nodeKind = (int) (SaxonProcessor::sxn_environ->env->CallStaticIntMethod(xdmUtilsClass, mID2, nodeKindObj));
799
    return nodeKind;
800
}
801

    
802

    
803
XdmNode *SaxonProcessor::parseXmlFromFile(const char *source) {
804

    
805
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlFile",
806
                                                                                    "(Lnet/sf/saxon/s9api/Processor;Ljava/lang/String;Lnet/sf/saxon/s9api/SchemaValidator;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
807
    if (!mID) {
808
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFile()" << " not found" << std::endl;
809
        return nullptr;
810
    }
811
//TODO SchemaValidator
812
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID, proc,
813
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
814
                                                                                        cwd.c_str()), nullptr,
815
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
816
                                                                                        source));
817
    if (exceptionOccurred()) {
818
        createException();
819

    
820
    } else {
821

    
822
        XdmNode *value = new XdmNode(xdmNodei);
823
        return value;
824
    }
825
    return nullptr;
826
}
827

    
828
XdmNode *SaxonProcessor::parseXmlFromUri(const char *source) {
829

    
830
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlFile",
831
                                                                                    "(Lnet/sf/saxon/s9api/Processor;Ljava/lang/String;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
832
    if (!mID) {
833
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFromUri()" << " not found" << std::endl;
834
        return nullptr;
835
    }
836
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID, proc,
837
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
838
                                                                                        ""),
839
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
840
                                                                                        source));
841
    if (exceptionOccurred()) {
842
        createException();
843
    } else {
844
        XdmNode *value = new XdmNode(xdmNodei);
845
        return value;
846
    }
847
    return nullptr;
848
}
849

    
850

    
851
/**
852
   * Set a configuration property.
853
   *
854
   * @param name of the property
855
   * @param value of the property
856
   */
857
void SaxonProcessor::setConfigurationProperty(const char *name, const char *value) {
858
    if (name != nullptr) {
859
        configProperties.insert(
860
                std::pair<std::string, std::string>(std::string(name), std::string((value == nullptr ? "" : value))));
861
    }
862
}
863

    
864
void SaxonProcessor::clearConfigurationProperties() {
865
    configProperties.clear();
866
}
867

    
868

    
869
void SaxonProcessor::release() {
870
    if (SaxonProcessor::jvmCreatedCPP != 0) {
871
        SaxonProcessor::jvmCreatedCPP = 0;
872
        //std::cerr<<"SaxonProc: JVM finalized calling !"<<std::endl;
873
        finalizeJavaRT(SaxonProcessor::sxn_environ->jvm);
874

    
875
        delete SaxonProcessor::sxn_environ;
876
        /*clearParameters();
877
        clearProperties();*/
878
    } else {
879
#ifdef DEBUG
880
        std::cerr << "SaxonProc: JVM finalize not called!" << std::endl;
881
#endif
882
    }
883
}
884

    
885

    
886
/* ========= Factory method for Xdm ======== */
887

    
888
XdmAtomicValue *SaxonProcessor::makeStringValue(const char *str) {
889
    jobject obj = getJavaStringValue(SaxonProcessor::sxn_environ, str);
890
    static jmethodID mssID_atomic = nullptr;
891
    if (mssID_atomic == nullptr) {
892
        mssID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
893
                                                                                  "(Ljava/lang/String;)V"));
894
    }
895
    if (!mssID_atomic) {
896
        std::cerr << "XdmAtomic constructor (String)" << std::endl;
897
        return nullptr;
898
    }
899
    jobject obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mssID_atomic, obj));
900
    XdmAtomicValue *value = new XdmAtomicValue(obj2, "xs:string");
901
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
902
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj2);
903
    return value;
904
}
905

    
906
XdmAtomicValue *SaxonProcessor::makeStringValue(std::string str) {
907
    jobject obj = getJavaStringValue(SaxonProcessor::sxn_environ, str.c_str());
908
    static jmethodID msID_atomic = nullptr;
909
    if (msID_atomic == nullptr) {
910
        msID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
911
                                                                                 "(Ljava/lang/String;)V"));
912
    }
913
    if (!msID_atomic) {
914
        std::cerr << "XdmAtomic constructor (String)" << std::endl;
915
        return nullptr;
916
    }
917
    jobject obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, msID_atomic, obj));
918
    XdmAtomicValue *value = new XdmAtomicValue(obj2, "xs:string");
919
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
920
    return value;
921
}
922

    
923
XdmAtomicValue *SaxonProcessor::makeIntegerValue(int i) {
924
    //jobject obj = integerValue(*SaxonProcessor::sxn_environ, i);
925
    static jmethodID miiID_atomic = nullptr;
926
    if (miiID_atomic == nullptr) {
927
        miiID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
928
    }
929
    if (!miiID_atomic) {
930
        std::cerr << "XdmAtomic constructor (J)" << std::endl;
931
        return nullptr;
932
    }
933
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, miiID_atomic, (jlong) i));
934
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}integer");
935
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
936
    return value;
937
}
938

    
939
XdmAtomicValue *SaxonProcessor::makeDoubleValue(double d) {
940
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
941
    static jmethodID mdID_atomic = nullptr;
942
    if (mdID_atomic == nullptr) {
943
        mdID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(D)V"));
944
    }
945
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mdID_atomic, (jdouble) d));
946
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}double");
947
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
948
    return value;
949
}
950

    
951
XdmAtomicValue *SaxonProcessor::makeFloatValue(float d) {
952
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
953
    static jmethodID mfID_atomic = nullptr;
954
    if (mfID_atomic == nullptr) {
955
        mfID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(F)V"));
956
    }
957
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mfID_atomic, (jfloat) d));
958
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}float");
959
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
960
    return value;
961
}
962

    
963
XdmAtomicValue *SaxonProcessor::makeLongValue(long l) {
964
    //jobject obj = longValue(*SaxonProcessor::sxn_environ, l);
965
    static jmethodID mlID_atomic = nullptr;
966
    if (mlID_atomic == nullptr) {
967
        mlID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
968
    }
969
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mlID_atomic, (jlong) l));
970
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}long");
971
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
972
    return value;
973
}
974

    
975
XdmAtomicValue *SaxonProcessor::makeBooleanValue(bool b) {
976
    //jobject obj = booleanValue(*SaxonProcessor::sxn_environ, b);
977
    static jmethodID mID_atomic = nullptr;
978
    if (mID_atomic == nullptr) {
979
        mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(Z)V"));
980
    }
981
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, (jboolean) b));
982
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}boolean");
983
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
984
    return value;
985
}
986

    
987
XdmAtomicValue *SaxonProcessor::makeQNameValue(const char *str) {
988
    jobject val = xdmValueAsObj(SaxonProcessor::sxn_environ, "QName", str);
989
    XdmAtomicValue *value = new XdmAtomicValue(val, "QName");
990
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(val);
991
    return value;
992
}
993

    
994
XdmAtomicValue *SaxonProcessor::makeAtomicValue(const char *typei, const char *strValue) {
995
    jobject obj = xdmValueAsObj(SaxonProcessor::sxn_environ, typei, strValue);
996
    XdmAtomicValue *value = new XdmAtomicValue(obj, typei);
997
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
998
    return value;
999
}
1000

    
1001
const char *SaxonProcessor::getStringValue(XdmItem *item) {
1002
    const char *result = stringValue(SaxonProcessor::sxn_environ, item->getUnderlyingValue());
1003
#ifdef DEBUG
1004
    if(result == nullptr) {
1005
        std::cout<<"getStringValue of XdmItem is nullptr"<<std::endl;
1006
    } else {
1007
        std::cout<<"getStringValue of XdmItem is OK"<<std::endl;
1008
    }
1009
#endif
1010

    
1011
    return result;
1012

    
1013
}
1014

    
1015
#if CVERSION_API_NO >= 123
1016

    
1017
XdmArray *SaxonProcessor::makeArray(short *input, int length) {
1018
    if (input == nullptr) {
1019
        std::cerr << "Error found when converting string to XdmArray" << std::endl;
1020
        return nullptr;
1021
    }
1022
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1023
    static jmethodID mmssID = nullptr;
1024
    if (mmssID == nullptr) {
1025
        mmssID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1026
                                                                                 "([S)Lnet/sf/saxon/s9api/XdmArray;");
1027
    }
1028
    if (!mmssID) {
1029
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([S)" << " not found" << std::endl;
1030
        return nullptr;
1031
    }
1032

    
1033

    
1034
    jshortArray sArray = nullptr;
1035

    
1036
    sArray = SaxonProcessor::sxn_environ->env->NewShortArray((jint) length);
1037
    jshort fill[length];
1038
    for (int i = 0; i < length; i++) {
1039
        fill[i] = input[i];
1040
    }
1041
    SaxonProcessor::sxn_environ->env->SetShortArrayRegion(sArray, 0, length, fill);
1042

    
1043
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmssID, sArray);
1044
    if (!xdmArrayi) {
1045
        std::cerr << "Error found when converting string to XdmArray";
1046
        return nullptr;
1047
    }
1048

    
1049
    if (exceptionOccurred()) {
1050
        createException();
1051
    } else {
1052
        XdmArray *value = new XdmArray(xdmArrayi);
1053
        return value;
1054
    }
1055
    return nullptr;
1056
}
1057

    
1058

    
1059
XdmArray *SaxonProcessor::makeArray(int *input, int length) {
1060
    if (input == nullptr) {
1061
        std::cerr << "Error found when converting string to XdmArray";
1062
        return nullptr;
1063
    }
1064
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1065
    static jmethodID mmiiID = nullptr;
1066
    if (mmiiID == nullptr) {
1067
        mmiiID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1068
                                                                                 "([I)Lnet/sf/saxon/s9api/XdmArray;");
1069
    }
1070
    if (!mmiiID) {
1071
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([I)" << " not found" << std::endl;
1072
        return nullptr;
1073
    }
1074

    
1075

    
1076
    jintArray iArray = nullptr;
1077

    
1078
    iArray = SaxonProcessor::sxn_environ->env->NewIntArray((jint) length);
1079
    jint fill[length];
1080
    for (int i = 0; i < length; i++) {
1081
        fill[i] = input[i];
1082
    }
1083
    SaxonProcessor::sxn_environ->env->SetIntArrayRegion(iArray, 0, length, fill);
1084

    
1085

    
1086
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiiID, iArray);
1087
    if (!xdmArrayi) {
1088
        std::cerr << "Error found when converting string to XdmArray";
1089
        return nullptr;
1090
    }
1091
    if (exceptionOccurred()) {
1092
        createException();
1093
    } else {
1094
        XdmArray *value = new XdmArray(xdmArrayi);
1095
        return value;
1096
    }
1097
    return nullptr;
1098

    
1099
}
1100

    
1101
XdmArray *SaxonProcessor::makeArray(long *input, int length) {
1102
    if (input == nullptr) {
1103
        std::cerr << "Error found when converting string to XdmArray";
1104
        return nullptr;
1105
    }
1106
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1107
    static jmethodID mmiID = nullptr;
1108
    if (mmiID == nullptr) {
1109
        mmiID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1110
                                                                                "([J)Lnet/sf/saxon/s9api/XdmArray;");
1111
    }
1112
    if (!mmiID) {
1113
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([J)" << " not found" << std::endl;
1114
        return nullptr;
1115
    }
1116

    
1117

    
1118
    jlongArray lArray = nullptr;
1119

    
1120
    lArray = SaxonProcessor::sxn_environ->env->NewLongArray((jint) length);
1121
    jlong fill[length];
1122
    for (int i = 0; i < length; i++) {
1123
        fill[i] = input[i];
1124
    }
1125
    SaxonProcessor::sxn_environ->env->SetLongArrayRegion(lArray, 0, length, fill);
1126

    
1127

    
1128
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiID, lArray);
1129
    if (!xdmArrayi) {
1130
        std::cerr << "Error found when converting string to XdmArray";
1131
        return nullptr;
1132
    }
1133
    if (exceptionOccurred()) {
1134
        createException();
1135
    } else {
1136
        XdmArray *value = new XdmArray(xdmArrayi);
1137
        return value;
1138
    }
1139
    return nullptr;
1140
}
1141

    
1142

    
1143
XdmArray *SaxonProcessor::makeArray(bool *input, int length) {
1144
    if (input == nullptr) {
1145
        std::cerr << "Error found when converting string to XdmArray";
1146
        return nullptr;
1147
    }
1148
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1149
    static jmethodID mmbID = nullptr;
1150
    if (mmbID == nullptr) {
1151
        mmbID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1152
                                                                                "([Z)Lnet/sf/saxon/s9api/XdmArray;");
1153
    }
1154
    if (!mmbID) {
1155
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([Z)" << " not found" << std::endl;
1156
        return nullptr;
1157
    }
1158

    
1159

    
1160
    jbooleanArray bArray = nullptr;
1161

    
1162
    bArray = SaxonProcessor::sxn_environ->env->NewBooleanArray((jint) length);
1163
    jboolean fill[length];
1164
    for (int i = 0; i < length; i++) {
1165
        fill[i] = input[i];
1166
    }
1167
    SaxonProcessor::sxn_environ->env->SetBooleanArrayRegion(bArray, 0, length, fill);
1168

    
1169

    
1170
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmbID, bArray);
1171
    if (!xdmArrayi) {
1172
        std::cerr << "Error found when converting string to XdmArray";
1173
        return nullptr;
1174
    }
1175
    if (exceptionOccurred()) {
1176
        createException();
1177
    } else {
1178
        XdmArray *value = new XdmArray(xdmArrayi);
1179
        return value;
1180
    }
1181
    return nullptr;
1182

    
1183

    
1184
}
1185

    
1186

    
1187
XdmArray *SaxonProcessor::makeArray(const char **input, int length) {
1188
    if (input == nullptr || length <= 0) {
1189
        std::cerr << "Error found when converting string to XdmArray";
1190
        return nullptr;
1191
    }
1192
    jobject obj = nullptr;
1193
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1194
    jmethodID mmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1195
                                                                                     "([Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmArray;");
1196

    
1197
    jmethodID mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
1198
                                                                                      "(Ljava/lang/String;)V"));
1199
    jobjectArray valueArray = nullptr;
1200
    jobject obj2 = nullptr;
1201
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length, xdmAtomicClass, 0);
1202
    for (int i = 0; i < length; i++) {
1203
        if (input[i] == nullptr) {
1204
            std::cerr << "Error found when converting string to XdmArray";
1205
            return nullptr;
1206
        }
1207
        obj = getJavaStringValue(SaxonProcessor::sxn_environ, input[i]);
1208
        obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, obj));
1209
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, obj2);
1210
    }
1211

    
1212
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmID, valueArray);
1213
    if (!xdmArrayi) {
1214
        std::cerr << "Error found when converting string to XdmArray";
1215
        return nullptr;
1216
    }
1217

    
1218
    if (exceptionOccurred()) {
1219
        checkAndCreateException(saxonCAPIClass);
1220
    } else {
1221
        XdmArray *value = new XdmArray(xdmArrayi);
1222
        return value;
1223
    }
1224
    return nullptr;
1225
}
1226

    
1227

    
1228
XdmMap *SaxonProcessor::makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap) {
1229
    jobjectArray keyArray = nullptr;
1230
    jobjectArray valueArray = nullptr;
1231
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
1232
                                      "java/lang/Object");
1233

    
1234
    int size = dataMap.size();
1235

    
1236
    if (size > 0) {
1237

    
1238
        keyArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1239
                                                                    objectClass, 0);
1240
        valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1241
                                                                      objectClass, 0);
1242
        int i = 0;
1243
        for (std::map<XdmAtomicValue *, XdmValue *>::iterator iter =
1244
                dataMap.begin(); iter != dataMap.end(); ++iter, i++) {
1245

    
1246

    
1247
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(keyArray, i,
1248
                                                                    (iter->first)->getUnderlyingValue());
1249

    
1250
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i,
1251
                                                                    (iter->second)->getUnderlyingValue());
1252

    
1253
        }
1254
        jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
1255
        jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass, "makeXdmMap",
1256
                                                                                         "([Lnet/sf/saxon/s9api/XdmAtomicValue;[Lnet/sf/saxon/s9api/XdmValue;)Lnet/sf/saxon/s9api/XdmMap;");
1257
        if (!xmID) {
1258
            std::cerr << "Error: SaxonDll." << "makeXdmMap"
1259
                      << " not found\n" << std::endl;
1260
            return nullptr;
1261
        }
1262

    
1263

    
1264
        jobject results = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
1265
                                                                                              keyArray, valueArray));
1266
        if (exceptionOccurred() || !results) {
1267
            checkAndCreateException(saxonCAPIClass);
1268
        } else {
1269
            return new XdmMap(results);
1270
        }
1271
    }
1272

    
1273
    return nullptr;
1274
}
1275

    
1276
#endif
1277

    
1278

    
(13-13/56)