Project

Profile

Help

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

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

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
    if (exception != nullptr) {
116
        std::cerr<< "saxonProc exception= "<< exception <<std::endl;
117
        delete exception;
118
        exception = nullptr;
119
    }
120

    
121
}
122

    
123

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

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

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

    
135
    exceptionClear();
136

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

    
143
}
144

    
145

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

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

    
153
#ifdef DEBUG
154
        env->ExceptionDescribe();
155
#endif
156
        jclass exccls(env->GetObjectClass(exc));
157
        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
        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
            }
179

    
180
            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

    
243
}
244

    
245

    
246
SaxonProcessor::SaxonProcessor(bool l) {
247

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

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

    
257

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

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

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

    
275
    }
276

    
277

    
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

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

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

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

    
299
    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

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

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

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

    
318
    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
    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
#ifdef DEBUG
343

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

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

    
361

    
362
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

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

    
376
    }
377

    
378
}
379

    
380
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
            createException();
411
        }
412
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray1);
413
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray2);
414

    
415
    }
416
}
417

    
418

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

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

    
425

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

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

    
434

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

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

    
441
            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
#endif
447
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, values[i]->getUnderlyingValue());
448
    }
449
    return valueArray;
450

    
451
}
452

    
453

    
454
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

    
464
    int size = parameters.size() + properties.size();
465
#ifdef DEBUG
466
    std::cerr<<"Properties size: "<<properties.size()<<std::endl;
467
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
468
#endif
469
    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

    
479
#ifdef DEBUG
480
            std::cerr<<"map 1"<<std::endl;
481
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
482
#endif
483
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
484
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
485
                                                                            (iter->first).c_str()));
486
#ifdef DEBUG
487
            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
#endif
505

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

    
509
        }
510

    
511
        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

    
521
        return comboArrays;
522

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

    
528
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

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

    
543
        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

    
551
#ifdef DEBUG
552
            std::cerr<<"map 1"<<std::endl;
553
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
554
#endif
555
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
556
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
557
                                                                            (iter->first).c_str()));
558
#ifdef DEBUG
559
            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
#endif
577

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

    
581
        }
582

    
583

    
584
        return comboArrays;
585

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

    
591

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

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

    
617

    
618

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

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

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

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

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

    
637
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
}
645

    
646

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

    
650
        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

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

    
663
        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
        SaxonProcessor::sxn_environ->env->ReleaseStringUTFChars(jstr, tempVersionStr);
670

    
671

    
672
    }
673

    
674
    return versionStr;
675
}
676

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

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

    
687

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

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

    
699
        return;
700
    }
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
        return;
707
    }
708

    
709

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

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

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

    
728
}
729

    
730

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

    
733
    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
    if (!mID) {
736
        std::cerr << "\nError: Saxonc." << "parseXmlString()" << " not found" << std::endl;
737
        return nullptr;
738
    }
739
//TODO SchemaValidator
740

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

    
746
    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
        XdmNode *value = nullptr;
753
        value = new XdmNode(xdmNodei);
754
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(xdmNodei);
755
        return value;
756
    } else if (exceptionOccurred()) {
757
        createException();
758
    }
759

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

    
764
    return nullptr;
765
}
766

    
767
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

    
781
        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

    
801

    
802
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
    if (!mID) {
807
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFile()" << " not found" << std::endl;
808
        return nullptr;
809
    }
810
//TODO SchemaValidator
811
    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
        createException();
818

    
819
    } else {
820

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

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

    
829
    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
    if (!mID) {
832
        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
        createException();
842
    } else {
843
        XdmNode *value = new XdmNode(xdmNodei);
844
        return value;
845
    }
846
    return nullptr;
847
}
848

    
849

    
850
/**
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
    }
861
}
862

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

    
867

    
868
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

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

    
884

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

    
887
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
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
901
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj2);
902
    return value;
903
}
904

    
905
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
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
919
    return value;
920
}
921

    
922
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
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
935
    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
    }
944
    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
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
947
    return value;
948
}
949

    
950
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
    }
956
    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
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
959
    return value;
960
}
961

    
962
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
    }
968
    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
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
971
    return value;
972
}
973

    
974
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
    }
980
    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
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
983
    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
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(val);
990
    return value;
991
}
992

    
993
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
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
997
    return value;
998
}
999

    
1000
const char *SaxonProcessor::getStringValue(XdmItem *item) {
1001
    const char *result = stringValue(SaxonProcessor::sxn_environ, item->getUnderlyingValue());
1002
#ifdef DEBUG
1003
    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
#endif
1009

    
1010
    return result;
1011

    
1012
}
1013

    
1014
#if CVERSION_API_NO >= 123
1015

    
1016
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

    
1032

    
1033
    jshortArray sArray = nullptr;
1034

    
1035
    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

    
1042
    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

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

    
1057

    
1058
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
        createException();
1092
    } 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
        createException();
1134
    } 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
        createException();
1176
    } 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
        }
1206
        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

    
1211
    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

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

    
1226

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

    
1233
    int size = dataMap.size();
1234

    
1235
    if (size > 0) {
1236

    
1237
        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

    
1245

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

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

    
1252
        }
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

    
1262

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

    
1272
    return nullptr;
1273
}
1274

    
1275
#endif
1276

    
1277

    
(13-13/56)