Project

Profile

Help

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

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

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
XsltProcessor *SaxonProcessor::newXsltProcessor() {
619
    return (new XsltProcessor(this, cwd));
620
}
621

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

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

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

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

    
643

    
644
const char *SaxonProcessor::version() {
645
    if (versionStr == nullptr) {
646

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

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

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

    
668

    
669
    }
670

    
671
    return versionStr;
672
}
673

    
674
void SaxonProcessor::setcwd(const char *dir) {
675
    if(dir != nullptr) {
676
        cwd = std::string(dir);
677
    }
678
}
679

    
680
const char *SaxonProcessor::getcwd() {
681
    return cwd.c_str();
682
}
683

    
684

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

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

    
696
        return;
697
    }
698
    static jmethodID configMID = SaxonProcessor::sxn_environ->env->GetMethodID(procClass, "getUnderlyingConfiguration",
699
                                                                               "()Lnet/sf/saxon/Configuration;");
700

    
701
    if (!configMID) {
702
        std::cerr << "\nError: Saxonc." << "getUnderlyingConfiguration()" << " not found" << std::endl;
703
        return;
704
    }
705

    
706

    
707
    if (!proc) {
708
        createException("Processor is null in SaxonProcessor.setCatalog");
709
        return;
710
    }
711

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

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

    
725
}
726

    
727

    
728
XdmNode *SaxonProcessor::parseXmlFromString(const char *source) {
729

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

    
738
    if(source == nullptr) {
739
        createException("Source string is NULL");
740
        return nullptr;
741
    }
742

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

    
757
#ifdef DEBUG
758
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
759
#endif
760

    
761
    return nullptr;
762
}
763

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

    
775
    jobject nodeKindObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(obj, nodeKindMID));
776
    if (!nodeKindObj) {
777

    
778
        return 0;
779
    }
780
    jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/option/cpp/XdmUtils;");
781

    
782
    jmethodID mID2 = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,
783
                                                                                     "convertNodeKindType",
784
                                                                                     "(Lnet/sf/saxon/s9api/XdmNodeKind;)I");
785

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

    
798

    
799
XdmNode *SaxonProcessor::parseXmlFromFile(const char *source) {
800

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

    
816
    } else {
817

    
818
        XdmNode *value = new XdmNode(xdmNodei);
819
        return value;
820
    }
821
    return nullptr;
822
}
823

    
824
XdmNode *SaxonProcessor::parseXmlFromUri(const char *source) {
825

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

    
846

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

    
860
void SaxonProcessor::clearConfigurationProperties() {
861
    configProperties.clear();
862
}
863

    
864

    
865
void SaxonProcessor::release() {
866
    if (SaxonProcessor::jvmCreatedCPP != 0) {
867
        SaxonProcessor::jvmCreatedCPP = 0;
868
        //std::cerr<<"SaxonProc: JVM finalized calling !"<<std::endl;
869
        finalizeJavaRT(SaxonProcessor::sxn_environ->jvm);
870

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

    
881

    
882
/* ========= Factory method for Xdm ======== */
883

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

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

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

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

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

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

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

    
983
XdmAtomicValue *SaxonProcessor::makeQNameValue(const char *str) {
984
    jobject val = xdmValueAsObj(SaxonProcessor::sxn_environ, "QName", str);
985
    XdmAtomicValue *value = new XdmAtomicValue(val, "QName");
986
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(val);
987
    return value;
988
}
989

    
990
XdmAtomicValue *SaxonProcessor::makeAtomicValue(const char *typei, const char *strValue) {
991
    jobject obj = xdmValueAsObj(SaxonProcessor::sxn_environ, typei, strValue);
992
    XdmAtomicValue *value = new XdmAtomicValue(obj, typei);
993
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
994
    return value;
995
}
996

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

    
1007
    return result;
1008

    
1009
}
1010

    
1011
#if CVERSION_API_NO >= 123
1012

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

    
1029

    
1030
    jshortArray sArray = nullptr;
1031

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

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

    
1045
    if (exceptionOccurred()) {
1046
        createException();
1047
    } else {
1048
        XdmArray *value = new XdmArray(xdmArrayi);
1049
        return value;
1050
    }
1051
    return nullptr;
1052
}
1053

    
1054

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

    
1071

    
1072
    jintArray iArray = nullptr;
1073

    
1074
    iArray = SaxonProcessor::sxn_environ->env->NewIntArray((jint) length);
1075
    jint fill[length];
1076
    for (int i = 0; i < length; i++) {
1077
        fill[i] = input[i];
1078
    }
1079
    SaxonProcessor::sxn_environ->env->SetIntArrayRegion(iArray, 0, length, fill);
1080

    
1081

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

    
1095
}
1096

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

    
1113

    
1114
    jlongArray lArray = nullptr;
1115

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

    
1123

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

    
1138

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

    
1155

    
1156
    jbooleanArray bArray = nullptr;
1157

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

    
1165

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

    
1179

    
1180
}
1181

    
1182

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

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

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

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

    
1223

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

    
1230
    int size = dataMap.size();
1231

    
1232
    if (size > 0) {
1233

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

    
1242

    
1243
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(keyArray, i,
1244
                                                                    (iter->first)->getUnderlyingValue());
1245

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

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

    
1259

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

    
1269
    return nullptr;
1270
}
1271

    
1272
#endif
1273

    
1274

    
(13-13/51)