Project

Profile

Help

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

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

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
//#define DEBUG
18
#ifdef DEBUG
19
#include <signal.h>
20
#endif
21

    
22
#include <stdio.h>
23

    
24

    
25
//jobject cpp;
26
const char *failure;
27
sxnc_environment *SaxonProcessor::sxn_environ = 0;
28
int SaxonProcessor::jvmCreatedCPP = 0;
29

    
30
bool SaxonProcessor::exceptionOccurred() {
31
    return SaxonProcessor::sxn_environ->env->ExceptionCheck() || exception != nullptr;
32
}
33

    
34
const char *SaxonProcessor::checkException(jobject cpp) {
35
    const char *message = nullptr;
36
    message = checkForException(sxn_environ, cpp);
37
    return message;
38
}
39

    
40
SaxonApiException *SaxonProcessor::checkAndCreateException(jclass cppClass) {
41
    if (SaxonProcessor::sxn_environ->env->ExceptionCheck()) {
42
        SaxonApiException *exception = checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass, nullptr);
43
#ifdef DEBUG
44
        SaxonProcessor::sxn_environ->env->ExceptionDescribe();
45
#endif
46
        return exception;
47
    }
48
    return nullptr;
49
}
50

    
51
const char *SaxonProcessor::getErrorMessage() {
52
    if(exception == nullptr) {
53
        exception =  SaxonProcessor::checkForExceptionCPP(SaxonProcessor::sxn_environ->env,
54
                                                                            saxonCAPIClass,nullptr);
55
    }
56
    if (exception == nullptr) { return nullptr; }
57
    return exception->getMessage();
58
}
59

    
60
void SaxonProcessor::exceptionClear() {
61
    SaxonProcessor::sxn_environ->env->ExceptionClear();
62

    
63
}
64

    
65

    
66
SaxonProcessor::SaxonProcessor() {
67
    licensei = false;
68
    SaxonProcessor(false);
69
}
70

    
71
const char * SaxonProcessor::getResourcesDirectory() {
72
    return NULL;
73
}
74

    
75

    
76
SaxonApiException *SaxonProcessor::checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject) {
77

    
78
    if (env->ExceptionCheck()) {
79
        std::string result1 = "";
80
        std::string errorCode = "";
81
        jthrowable exc = env->ExceptionOccurred();
82

    
83
#ifdef DEBUG
84
        env->ExceptionDescribe();
85
#endif
86
        jclass exccls(env->GetObjectClass(exc));
87
        jclass clscls(env->FindClass("java/lang/Class"));
88

    
89
        jmethodID getName(env->GetMethodID(clscls, "getName", "()Ljava/lang/String;"));
90
        jstring name(static_cast<jstring>(env->CallObjectMethod(exccls, getName)));
91
        char const *utfName(env->GetStringUTFChars(name, 0));
92
        result1 = (std::string(utfName));
93
        env->ReleaseStringUTFChars(name, utfName);
94

    
95
        jmethodID getMessage(env->GetMethodID(exccls, "getMessage", "()Ljava/lang/String;"));
96
        if (getMessage) {
97

    
98
            jstring message((jstring) (env->CallObjectMethod(exc, getMessage)));
99
            char const *utfMessage = nullptr;
100
            if (!message) {
101
                utfMessage = "";
102
                return nullptr;
103
            } else {
104
                utfMessage = (env->GetStringUTFChars(message, 0));
105
            }
106
            if (utfMessage != nullptr) {
107
                result1 = (result1 + " : ") + utfMessage;
108
            }
109

    
110
            env->ReleaseStringUTFChars(message, utfMessage);
111

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

    
114
                jclass saxonApiExceptionClass(env->FindClass("net/sf/saxon/s9api/SaxonApiException"));
115
                static jmethodID lineNumID = nullptr;
116
                if (lineNumID == nullptr) {
117
                    lineNumID = env->GetMethodID(saxonApiExceptionClass, "getLinenumber", "()I");
118
                }
119
                static jmethodID ecID = nullptr;
120
                if (ecID == nullptr) {
121
                    ecID = env->GetMethodID(saxonApiExceptionClass, "getErrorCode", "()Ljnet/sf/saxon/s9api/QName;");
122
                }
123
                static jmethodID esysID = nullptr;
124
                if (esysID == nullptr) {
125
                    esysID = env->GetMethodID(saxonApiExceptionClass, "getSystemId", "()Ljava/lang/String;");
126
                }
127

    
128

    
129
                jobject errCodeQName = (jobject) (env->CallObjectMethod(exc, ecID));
130
                jstring errSystemID = (jstring) (env->CallObjectMethod(exc, esysID));
131
                int linenum = env->CallIntMethod(exc, lineNumID);
132

    
133
                jclass qnameClass(env->FindClass("net/sf/saxon/s9api/QName"));
134
                static jmethodID qnameStrID = nullptr;
135
                if (qnameStrID == nullptr) {
136
                    qnameStrID = env->GetMethodID(qnameClass, "toString", "()Ljava/lang/String;");
137
                }
138

    
139
                jstring qnameStr = (jstring) (env->CallObjectMethod(errCodeQName, qnameStrID));
140

    
141

    
142
                SaxonApiException *saxonExceptions = new SaxonApiException(result1.c_str(),
143
                                                                           (qnameStr ? env->GetStringUTFChars(qnameStr,
144
                                                                                                              0)
145
                                                                                     : nullptr),
146
                                                                           (errSystemID ? env->GetStringUTFChars(
147
                                                                                   errSystemID, 0) : nullptr), linenum);
148

    
149
                if (errCodeQName) {
150
                    env->DeleteLocalRef(errCodeQName);
151
                }
152
                if (errSystemID) {
153
                    env->DeleteLocalRef(errSystemID);
154
                }
155
                if (qnameStr) {
156
                    env->DeleteLocalRef(qnameStr);
157
                }
158

    
159
                if (message) {
160
                    env->DeleteLocalRef(message);
161
                }
162
                env->ExceptionClear();
163
                return saxonExceptions;
164
            }
165
        }
166
        SaxonApiException *saxonExceptions = new SaxonApiException(result1.c_str());
167
        //env->ExceptionDescribe();
168
        env->ExceptionClear();
169
        return saxonExceptions;
170
    }
171
    return nullptr;
172

    
173
}
174

    
175

    
176
SaxonProcessor::SaxonProcessor(bool l) {
177

    
178
    cwd = "";
179
    licensei = l;
180
    versionStr = nullptr;
181

    
182
    if (SaxonProcessor::jvmCreatedCPP == 0) {
183
        SaxonProcessor::jvmCreatedCPP = 1;
184
        SaxonProcessor::sxn_environ = new sxnc_environment;//(sxnc_environment *)malloc(sizeof(sxnc_environment));
185

    
186

    
187
        /*
188
         * First of all, load required component.
189
         * By the time of JET initialization, all components should be loaded.
190
         */
191

    
192
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
193

    
194
        /*
195
         * Initialize JET run-time.
196
         * The handle of loaded component is used to retrieve Invocation API.
197
         */
198
        initDefaultJavaRT(SaxonProcessor::sxn_environ);
199
    } else {
200
#ifdef DEBUG
201
        std::cerr<<"SaxonProc constructor: jvm exists! jvmCreatedCPP="<<jvmCreatedCPP<<std::endl;
202
#endif
203

    
204
    }
205

    
206

    
207
    versionClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/Version");
208
    procClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/Processor");
209
    saxonCAPIClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/SaxonCAPI");
210

    
211
    jobject proci = createSaxonProcessor(SaxonProcessor::sxn_environ->env, procClass, "(Z)V", nullptr, licensei);
212
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(proci);
213
    if (!proc) {
214
        std::cout << "proc is nullptr in SaxonProcessor constructor" << std::endl;
215
    }
216

    
217
    xdmAtomicClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
218
#ifdef DEBUG
219
    jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "setDebugMode", "(Z)V");
220
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(saxonCAPIClass, debugMID, (jboolean)true);
221
#endif
222
}
223

    
224
SaxonProcessor::SaxonProcessor(const char *configFile) {
225
    cwd = "";
226
    versionStr = nullptr;
227

    
228
    if (SaxonProcessor::jvmCreatedCPP == 0) {
229
        SaxonProcessor::jvmCreatedCPP = 1;
230
        //SaxonProcessor::sxn_environ= new sxnc_environment;
231
        SaxonProcessor::sxn_environ = (sxnc_environment *) malloc(sizeof(sxnc_environment));
232

    
233
        /*
234
         * First of all, load required component.
235
         * By the time of JET initialization, all components should be loaded.
236
         */
237

    
238
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
239

    
240
        /*
241
         * Initialize JET run-time.
242
         * The handle of loaded component is used to retrieve Invocation API.
243
         */
244
        initDefaultJavaRT(SaxonProcessor::sxn_environ);
245
    }
246

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

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

    
252
    static jmethodID mIDcreateProc = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
253
                                                                                                     "createSaxonProcessor",
254
                                                                                                     "(Ljava/lang/String;)Lnet/sf/saxon/s9api/Processor;");
255
    if (!mIDcreateProc) {
256
        std::cerr << "Error: SaxonDll." << "getPrimitiveTypeName"
257
                  << " not found\n" << std::endl;
258
        return;
259
    }
260
    jobject proci = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mIDcreateProc,
261
                                                                             SaxonProcessor::sxn_environ->env->NewStringUTF(
262
                                                                                     configFile));
263
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(proci);
264
    if (!proc) {
265
        checkAndCreateException(saxonCAPIClass);
266
        std::cerr << "Error: " << getDllname() << ". processor is nullptr in constructor(configFile)" << std::endl;
267
        return;
268
    }
269

    
270
    licensei = true;
271
#ifdef DEBUG
272

    
273
    std::cerr<<"SaxonProc constructor(configFile)"<<std::endl;
274
#endif
275
    xdmAtomicClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
276
}
277

    
278
SaxonProcessor::~SaxonProcessor() {
279
    clearConfigurationProperties();
280
    SaxonProcessor::sxn_environ->env->DeleteGlobalRef(proc);
281
    if (versionStr != nullptr) {
282
        delete versionStr;
283
    }
284
}
285

    
286

    
287
bool SaxonProcessor::isSchemaAwareProcessor() {
288
    if (!licensei) {
289
        return false;
290
    } else {
291
        static jmethodID MID_schema = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(procClass,
292
                                                                                                "isSchemaAware", "()Z");
293
        if (!MID_schema) {
294
            std::cerr << "\nError: Saxonc " << "SaxonProcessor.isSchemaAware()" << " not found" << std::endl;
295
            return false;
296
        }
297

    
298
        licensei = (jboolean) (SaxonProcessor::sxn_environ->env->CallBooleanMethod(proc, MID_schema));
299
        return licensei;
300

    
301
    }
302

    
303
}
304

    
305
void SaxonProcessor::applyConfigurationProperties() {
306
    if (configProperties.size() > 0) {
307
        int size = configProperties.size();
308
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
309
        jobjectArray stringArray1 = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size, stringClass, 0);
310
        jobjectArray stringArray2 = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size, stringClass, 0);
311
        static jmethodID mIDappConfig = nullptr;
312
        if (mIDappConfig == nullptr) {
313
            mIDappConfig = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
314
                                                                                           "applyToConfiguration",
315
                                                                                           "(Lnet/sf/saxon/s9api/Processor;[Ljava/lang/String;[Ljava/lang/String;)V");
316
            if (!mIDappConfig) {
317
                std::cerr << "Error: SaxonDll." << "applyToConfiguration"
318
                          << " not found\n" << std::endl;
319
                return;
320
            }
321
        }
322
        int i = 0;
323
        std::map<std::string, std::string>::iterator iter = configProperties.begin();
324
        for (iter = configProperties.begin(); iter != configProperties.end(); ++iter, i++) {
325
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray1, i,
326
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
327
                                                                            (iter->first).c_str()));
328
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray2, i,
329
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
330
                                                                            (iter->second).c_str()));
331
        }
332
        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mIDappConfig, proc, stringArray1,
333
                                                                 stringArray2);
334
        if (exceptionOccurred()) {
335
            exception = checkAndCreateException(saxonCAPIClass);
336
            exceptionClear();
337
        }
338
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray1);
339
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray2);
340

    
341
    }
342
}
343

    
344

    
345
jobjectArray SaxonProcessor::createJArray(XdmValue **values, int length) {
346
    jobjectArray valueArray = nullptr;
347

    
348
    jclass xdmValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
349
                                        "net/sf/saxon/s9api/XdmValue");
350

    
351

    
352
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length,
353
                                                                  xdmValueClass, 0);
354

    
355
    for (int i = 0; i < length; i++) {
356
#ifdef DEBUG
357
        std::string s1 = typeid(values[i]).name();
358
        std::cerr<<"In createJArray\nType of itr:"<<s1<<std::endl;
359

    
360

    
361
        jobject xx = values[i]->getUnderlyingValue();
362

    
363
        if(xx == nullptr) {
364
            std::cerr<<"value failed"<<std::endl;
365
        } else {
366

    
367
            std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
368
        }
369
        if(values[i]->getUnderlyingValue() == nullptr) {
370
            std::cerr<<"value["<<i<<"]->getUnderlyingValue() is nullptr"<<std::endl;
371
        }
372
#endif
373
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, values[i]->getUnderlyingValue());
374
    }
375
    return valueArray;
376

    
377
}
378

    
379

    
380
JParameters SaxonProcessor::createParameterJArray(std::map<std::string, XdmValue *> parameters,
381
                                                  std::map<std::string, std::string> properties) {
382
    JParameters comboArrays;
383
    comboArrays.stringArray = nullptr;
384
    comboArrays.objectArray = nullptr;
385
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
386
                                      "java/lang/Object");
387
    jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
388
                                      "java/lang/String");
389

    
390
    int size = parameters.size() + properties.size();
391
#ifdef DEBUG
392
    std::cerr<<"Properties size: "<<properties.size()<<std::endl;
393
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
394
#endif
395
    if (size > 0) {
396

    
397
        comboArrays.objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
398
                                                                                   objectClass, 0);
399
        comboArrays.stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
400
                                                                                   stringClass, 0);
401
        int i = 0;
402
        for (std::map<std::string, XdmValue *>::iterator iter =
403
                parameters.begin(); iter != parameters.end(); ++iter, i++) {
404

    
405
#ifdef DEBUG
406
            std::cerr<<"map 1"<<std::endl;
407
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
408
#endif
409
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
410
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
411
                                                                            (iter->first).c_str()));
412
#ifdef DEBUG
413
            std::string s1 = typeid(iter->second).name();
414
            std::cerr<<"Type of itr:"<<s1<<std::endl;
415

    
416
            if((iter->second) == nullptr) {std::cerr<<"iter->second is nullptr"<<std::endl;
417
            } else {
418
                std::cerr<<"getting underlying value"<<std::endl;
419
            jobject xx = (iter->second)->getUnderlyingValue();
420

    
421
            if(xx == nullptr) {
422
                std::cerr<<"value failed"<<std::endl;
423
            } else {
424

    
425
                std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
426
            }
427
            if((iter->second)->getUnderlyingValue() == nullptr) {
428
                std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
429
            }}
430
#endif
431

    
432
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
433
                                                                    (iter->second)->getUnderlyingValue());
434

    
435
        }
436

    
437
        for (std::map<std::string, std::string>::iterator iter =
438
                properties.begin(); iter != properties.end(); ++iter, i++) {
439
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
440
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
441
                                                                            (iter->first).c_str()));
442
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
443
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
444
                                                                            (iter->second).c_str()));
445
        }
446

    
447
        return comboArrays;
448

    
449
    } else {
450
        return comboArrays;
451
    }
452
}
453

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

    
463
    int size = parameters.size();
464
#ifdef DEBUG
465
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
466
#endif
467
    if (size > 0) {
468

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

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

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

    
493
            if(xx == nullptr) {
494
                std::cerr<<"value failed"<<std::endl;
495
            } else {
496

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

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

    
507
        }
508

    
509

    
510
        return comboArrays;
511

    
512
    } else {
513
        return comboArrays;
514
    }
515
}
516

    
517

    
518
SaxonProcessor &SaxonProcessor::operator=(const SaxonProcessor &other) {
519
    versionClass = other.versionClass;
520
    procClass = other.procClass;
521
    saxonCAPIClass = other.saxonCAPIClass;
522
    cwd = other.cwd;
523
    proc = other.proc;
524
    //SaxonProcessor::sxn_environ= other.environ;
525
    parameters = other.parameters;
526
    configProperties = other.configProperties;
527
    licensei = other.licensei;
528
    exception = other.exception;
529
    return *this;
530
}
531

    
532
SaxonProcessor::SaxonProcessor(const SaxonProcessor &other) {
533
    versionClass = other.versionClass;
534
    procClass = other.procClass;
535
    saxonCAPIClass = other.saxonCAPIClass;
536
    cwd = other.cwd;
537
    proc = other.proc;
538
    //SaxonProcessor::sxn_environ= other.environ;
539
    parameters = other.parameters;
540
    configProperties = other.configProperties;
541
    licensei = other.licensei;
542
    exception = other.exception;
543
}
544

    
545
XsltProcessor *SaxonProcessor::newXsltProcessor() {
546
    return (new XsltProcessor(this, cwd));
547
}
548

    
549
Xslt30Processor *SaxonProcessor::newXslt30Processor() {
550
    return (new Xslt30Processor(this, cwd));
551
}
552

    
553
XQueryProcessor *SaxonProcessor::newXQueryProcessor() {
554
    return (new XQueryProcessor(this, cwd));
555
}
556

    
557
XPathProcessor *SaxonProcessor::newXPathProcessor() {
558
    return (new XPathProcessor(this, cwd));
559
}
560

    
561
SchemaValidator *SaxonProcessor::newSchemaValidator() {
562
    if (licensei) {
563
        return (new SchemaValidator(this, cwd));
564
    } else {
565
        std::cerr << "\nError: Processor is not licensed for schema processing!" << std::endl;
566
        return nullptr;
567
    }
568
}
569

    
570

    
571
const char *SaxonProcessor::version() {
572
    if (versionStr == nullptr) {
573

    
574
        static jmethodID MID_version = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
575
                                                                                                       "getProductVersion",
576
                                                                                                       "(Lnet/sf/saxon/s9api/Processor;)Ljava/lang/String;");
577
        if (!MID_version) {
578
            std::cerr << "\nError: MyClassInDll " << "SaxonCAPI.getProductVersion()" << " not found" << std::endl;
579
            return nullptr;
580
        }
581

    
582
        jstring jstr = (jstring) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, MID_version,
583
                                                                                           proc));
584
        const char *tempVersionStr = SaxonProcessor::sxn_environ->env->GetStringUTFChars(jstr, nullptr);
585
        int verLen = strlen(tempVersionStr) + 22 + strlen(CVERSION) + 1;
586
        versionStr = new char[verLen];
587
        snprintf(versionStr, verLen, "Saxon/C %s %s %s", CVERSION, "running with", tempVersionStr);
588
        delete tempVersionStr;
589

    
590
    }
591

    
592
    return versionStr;
593
}
594

    
595
void SaxonProcessor::setcwd(const char *dir) {
596
    if(dir != nullptr) {
597
        cwd = std::string(dir);
598
    }
599
}
600

    
601
const char *SaxonProcessor::getcwd() {
602
    return cwd.c_str();
603
}
604

    
605

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

    
611
    if (!catalogMID) {
612
        std::cerr << "\nError: Saxonc." << "setCatalog()" << " not found" << std::endl;
613
        return;
614
    }
615
    if (catalogFile == nullptr) {
616

    
617
        return;
618
    }
619
    static jmethodID configMID = SaxonProcessor::sxn_environ->env->GetMethodID(procClass, "getUnderlyingConfiguration",
620
                                                                               "()Lnet/sf/saxon/Configuration;");
621

    
622
    if (!configMID) {
623
        std::cerr << "\nError: Saxonc." << "getUnderlyingConfiguration()" << " not found" << std::endl;
624
        return;
625
    }
626

    
627

    
628
    if (!proc) {
629
        exception = new SaxonApiException("Processor is null in SaxonProcessor.setCatalog");
630
        return;
631
    }
632

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

    
635
    if (!configObj) {
636
        std::cout << "proc is null in SaxonProcessor setcatalog - config obj" << std::endl;
637
        return;
638
    }
639
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(xmlResolverClass, catalogMID,
640
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(catalogFile),
641
                                                           configObj, (jboolean) isTracing);
642
#ifdef DEBUG
643
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
644
#endif
645

    
646
}
647

    
648

    
649
XdmNode *SaxonProcessor::parseXmlFromString(const char *source) {
650

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

    
659
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID,
660
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
661
                                                                                        cwd.c_str()), proc, nullptr,
662
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
663
                                                                                        source));
664
    if (xdmNodei) {
665
        XdmNode *value = new XdmNode(xdmNodei);
666
        return value;
667
    } else if (exceptionOccurred()) {
668
        exception = checkAndCreateException(saxonCAPIClass);
669
        exceptionClear();
670
    }
671

    
672
#ifdef DEBUG
673
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
674
#endif
675

    
676
    return nullptr;
677
}
678

    
679
int SaxonProcessor::getNodeKind(jobject obj) {
680
    jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmNode;");
681
    static jmethodID nodeKindMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
682
                                                                                             "getNodeKind",
683
                                                                                             "()Lnet/sf/saxon/s9api/XdmNodeKind;");
684
    if (!nodeKindMID) {
685
        std::cerr << "Error: MyClassInDll." << "getNodeKind" << " not found\n"
686
                  << std::endl;
687
        return 0;
688
    }
689

    
690
    jobject nodeKindObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(obj, nodeKindMID));
691
    if (!nodeKindObj) {
692

    
693
        return 0;
694
    }
695
    jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/option/cpp/XdmUtils;");
696

    
697
    jmethodID mID2 = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,
698
                                                                                     "convertNodeKindType",
699
                                                                                     "(Lnet/sf/saxon/s9api/XdmNodeKind;)I");
700

    
701
    if (!mID2) {
702
        std::cerr << "Error: MyClassInDll." << "convertNodeKindType" << " not found\n"
703
                  << std::endl;
704
        return 0;
705
    }
706
    if (!nodeKindObj) {
707
        return 0;
708
    }
709
    int nodeKind = (int) (SaxonProcessor::sxn_environ->env->CallStaticIntMethod(xdmUtilsClass, mID2, nodeKindObj));
710
    return nodeKind;
711
}
712

    
713

    
714
XdmNode *SaxonProcessor::parseXmlFromFile(const char *source) {
715

    
716
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlFile",
717
                                                                                    "(Lnet/sf/saxon/s9api/Processor;Ljava/lang/String;Lnet/sf/saxon/s9api/SchemaValidator;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
718
    if (!mID) {
719
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFile()" << " not found" << std::endl;
720
        return nullptr;
721
    }
722
//TODO SchemaValidator
723
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID, proc,
724
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
725
                                                                                        cwd.c_str()), nullptr,
726
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
727
                                                                                        source));
728
    if (exceptionOccurred()) {
729
        exception = checkAndCreateException(saxonCAPIClass);
730
        exceptionClear();
731

    
732
    } else {
733

    
734
        XdmNode *value = new XdmNode(xdmNodei);
735
        return value;
736
    }
737
    return nullptr;
738
}
739

    
740
XdmNode *SaxonProcessor::parseXmlFromUri(const char *source) {
741

    
742
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlFile",
743
                                                                                    "(Lnet/sf/saxon/s9api/Processor;Ljava/lang/String;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
744
    if (!mID) {
745
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFromUri()" << " not found" << std::endl;
746
        return nullptr;
747
    }
748
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID, proc,
749
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
750
                                                                                        ""),
751
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
752
                                                                                        source));
753
    if (exceptionOccurred()) {
754
        exception = checkAndCreateException(saxonCAPIClass);
755
    } else {
756
        XdmNode *value = new XdmNode(xdmNodei);
757
        return value;
758
    }
759
    return nullptr;
760
}
761

    
762

    
763
/**
764
   * Set a configuration property.
765
   *
766
   * @param name of the property
767
   * @param value of the property
768
   */
769
void SaxonProcessor::setConfigurationProperty(const char *name, const char *value) {
770
    if (name != nullptr) {
771
        configProperties.insert(
772
                std::pair<std::string, std::string>(std::string(name), std::string((value == nullptr ? "" : value))));
773
    }
774
}
775

    
776
void SaxonProcessor::clearConfigurationProperties() {
777
    configProperties.clear();
778
}
779

    
780

    
781
void SaxonProcessor::release() {
782
    if (SaxonProcessor::jvmCreatedCPP != 0) {
783
        SaxonProcessor::jvmCreatedCPP = 0;
784
        //std::cerr<<"SaxonProc: JVM finalized calling !"<<std::endl;
785
        finalizeJavaRT(SaxonProcessor::sxn_environ->jvm);
786

    
787
        //delete SaxonProcessor::sxn_environ;
788
        /*clearParameters();
789
        clearProperties();*/
790
    } else {
791
//#ifdef DEBUG
792
        std::cerr << "SaxonProc: JVM finalize not called!" << std::endl;
793
//#endif
794
    }
795
}
796

    
797

    
798
/* ========= Factory method for Xdm ======== */
799

    
800
XdmAtomicValue *SaxonProcessor::makeStringValue(const char *str) {
801
    jobject obj = getJavaStringValue(SaxonProcessor::sxn_environ, str);
802
    static jmethodID mssID_atomic = nullptr;
803
    if (mssID_atomic == nullptr) {
804
        mssID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
805
                                                                                  "(Ljava/lang/String;)V"));
806
    }
807
    if (!mssID_atomic) {
808
        std::cerr << "XdmAtomic constructor (String)" << std::endl;
809
        return nullptr;
810
    }
811
    jobject obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mssID_atomic, obj));
812
    XdmAtomicValue *value = new XdmAtomicValue(obj2, "xs:string");
813
    return value;
814
}
815

    
816
XdmAtomicValue *SaxonProcessor::makeStringValue(std::string str) {
817
    jobject obj = getJavaStringValue(SaxonProcessor::sxn_environ, str.c_str());
818
    static jmethodID msID_atomic = nullptr;
819
    if (msID_atomic == nullptr) {
820
        msID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
821
                                                                                 "(Ljava/lang/String;)V"));
822
    }
823
    if (!msID_atomic) {
824
        std::cerr << "XdmAtomic constructor (String)" << std::endl;
825
        return nullptr;
826
    }
827
    jobject obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, msID_atomic, obj));
828
    XdmAtomicValue *value = new XdmAtomicValue(obj2, "xs:string");
829
    return value;
830
}
831

    
832
XdmAtomicValue *SaxonProcessor::makeIntegerValue(int i) {
833
    //jobject obj = integerValue(*SaxonProcessor::sxn_environ, i);
834
    static jmethodID miiID_atomic = nullptr;
835
    if (miiID_atomic == nullptr) {
836
        miiID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
837
    }
838
    if (!miiID_atomic) {
839
        std::cerr << "XdmAtomic constructor (J)" << std::endl;
840
        return nullptr;
841
    }
842
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, miiID_atomic, (jlong) i));
843
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}integer");
844
    return value;
845
}
846

    
847
XdmAtomicValue *SaxonProcessor::makeDoubleValue(double d) {
848
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
849
    static jmethodID mdID_atomic = nullptr;
850
    if (mdID_atomic == nullptr) {
851
        mdID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(D)V"));
852
    }
853
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mdID_atomic, (jdouble) d));
854
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}double");
855
    return value;
856
}
857

    
858
XdmAtomicValue *SaxonProcessor::makeFloatValue(float d) {
859
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
860
    static jmethodID mfID_atomic = nullptr;
861
    if (mfID_atomic == nullptr) {
862
        mfID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(F)V"));
863
    }
864
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mfID_atomic, (jfloat) d));
865
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}float");
866
    return value;
867
}
868

    
869
XdmAtomicValue *SaxonProcessor::makeLongValue(long l) {
870
    //jobject obj = longValue(*SaxonProcessor::sxn_environ, l);
871
    static jmethodID mlID_atomic = nullptr;
872
    if (mlID_atomic == nullptr) {
873
        mlID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
874
    }
875
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mlID_atomic, (jlong) l));
876
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}long");
877
    return value;
878
}
879

    
880
XdmAtomicValue *SaxonProcessor::makeBooleanValue(bool b) {
881
    //jobject obj = booleanValue(*SaxonProcessor::sxn_environ, b);
882
    static jmethodID mID_atomic = nullptr;
883
    if (mID_atomic == nullptr) {
884
        mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(Z)V"));
885
    }
886
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, (jboolean) b));
887
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}boolean");
888
    return value;
889
}
890

    
891
XdmAtomicValue *SaxonProcessor::makeQNameValue(const char *str) {
892
    jobject val = xdmValueAsObj(SaxonProcessor::sxn_environ, "QName", str);
893
    XdmAtomicValue *value = new XdmAtomicValue(val, "QName");
894
    return value;
895
}
896

    
897
XdmAtomicValue *SaxonProcessor::makeAtomicValue(const char *typei, const char *strValue) {
898
    jobject obj = xdmValueAsObj(SaxonProcessor::sxn_environ, typei, strValue);
899
    XdmAtomicValue *value = new XdmAtomicValue(obj, typei);
900
    return value;
901
}
902

    
903
const char *SaxonProcessor::getStringValue(XdmItem *item) {
904
    const char *result = stringValue(SaxonProcessor::sxn_environ, item->getUnderlyingValue());
905
#ifdef DEBUG
906
    if(result == nullptr) {
907
        std::cout<<"getStringValue of XdmItem is nullptr"<<std::endl;
908
    } else {
909
        std::cout<<"getStringValue of XdmItem is OK"<<std::endl;
910
    }
911
#endif
912

    
913
    return result;
914

    
915
}
916

    
917
#if CVERSION_API_NO >= 123
918

    
919
XdmArray *SaxonProcessor::makeArray(short *input, int length) {
920
    if (input == nullptr) {
921
        std::cerr << "Error found when converting string to XdmArray" << std::endl;
922
        return nullptr;
923
    }
924
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
925
    static jmethodID mmssID = nullptr;
926
    if (mmssID == nullptr) {
927
        mmssID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
928
                                                                                 "([S)Lnet/sf/saxon/s9api/XdmArray;");
929
    }
930
    if (!mmssID) {
931
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([S)" << " not found" << std::endl;
932
        return nullptr;
933
    }
934

    
935

    
936
    jshortArray sArray = nullptr;
937

    
938
    sArray = SaxonProcessor::sxn_environ->env->NewShortArray((jint) length);
939
    jshort fill[length];
940
    for (int i = 0; i < length; i++) {
941
        fill[i] = input[i];
942
    }
943
    SaxonProcessor::sxn_environ->env->SetShortArrayRegion(sArray, 0, length, fill);
944

    
945
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmssID, sArray);
946
    if (!xdmArrayi) {
947
        std::cerr << "Error found when converting string to XdmArray";
948
        return nullptr;
949
    }
950

    
951
    if (exceptionOccurred()) {
952
        exception = checkAndCreateException(saxonCAPIClass);
953
    } else {
954
        XdmArray *value = new XdmArray(xdmArrayi);
955
        return value;
956
    }
957
    return nullptr;
958
}
959

    
960

    
961
XdmArray *SaxonProcessor::makeArray(int *input, int length) {
962
    if (input == nullptr) {
963
        std::cerr << "Error found when converting string to XdmArray";
964
        return nullptr;
965
    }
966
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
967
    static jmethodID mmiiID = nullptr;
968
    if (mmiiID == nullptr) {
969
        mmiiID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
970
                                                                                 "([I)Lnet/sf/saxon/s9api/XdmArray;");
971
    }
972
    if (!mmiiID) {
973
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([I)" << " not found" << std::endl;
974
        return nullptr;
975
    }
976

    
977

    
978
    jintArray iArray = nullptr;
979

    
980
    iArray = SaxonProcessor::sxn_environ->env->NewIntArray((jint) length);
981
    jint fill[length];
982
    for (int i = 0; i < length; i++) {
983
        fill[i] = input[i];
984
    }
985
    SaxonProcessor::sxn_environ->env->SetIntArrayRegion(iArray, 0, length, fill);
986

    
987

    
988
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiiID, iArray);
989
    if (!xdmArrayi) {
990
        std::cerr << "Error found when converting string to XdmArray";
991
        return nullptr;
992
    }
993
    if (exceptionOccurred()) {
994
        exception = checkAndCreateException(saxonCAPIClass);
995
    } else {
996
        XdmArray *value = new XdmArray(xdmArrayi);
997
        return value;
998
    }
999
    return nullptr;
1000

    
1001
}
1002

    
1003
XdmArray *SaxonProcessor::makeArray(long *input, int length) {
1004
    if (input == nullptr) {
1005
        std::cerr << "Error found when converting string to XdmArray";
1006
        return nullptr;
1007
    }
1008
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1009
    static jmethodID mmiID = nullptr;
1010
    if (mmiID == nullptr) {
1011
        mmiID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1012
                                                                                "([J)Lnet/sf/saxon/s9api/XdmArray;");
1013
    }
1014
    if (!mmiID) {
1015
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([J)" << " not found" << std::endl;
1016
        return nullptr;
1017
    }
1018

    
1019

    
1020
    jlongArray lArray = nullptr;
1021

    
1022
    lArray = SaxonProcessor::sxn_environ->env->NewLongArray((jint) length);
1023
    jlong fill[length];
1024
    for (int i = 0; i < length; i++) {
1025
        fill[i] = input[i];
1026
    }
1027
    SaxonProcessor::sxn_environ->env->SetLongArrayRegion(lArray, 0, length, fill);
1028

    
1029

    
1030
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiID, lArray);
1031
    if (!xdmArrayi) {
1032
        std::cerr << "Error found when converting string to XdmArray";
1033
        return nullptr;
1034
    }
1035
    if (exceptionOccurred()) {
1036
        exception = checkAndCreateException(saxonCAPIClass);
1037
    } else {
1038
        XdmArray *value = new XdmArray(xdmArrayi);
1039
        return value;
1040
    }
1041
    return nullptr;
1042
}
1043

    
1044

    
1045
XdmArray *SaxonProcessor::makeArray(bool *input, int length) {
1046
    if (input == nullptr) {
1047
        std::cerr << "Error found when converting string to XdmArray";
1048
        return nullptr;
1049
    }
1050
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1051
    static jmethodID mmbID = nullptr;
1052
    if (mmbID == nullptr) {
1053
        mmbID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1054
                                                                                "([Z)Lnet/sf/saxon/s9api/XdmArray;");
1055
    }
1056
    if (!mmbID) {
1057
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([Z)" << " not found" << std::endl;
1058
        return nullptr;
1059
    }
1060

    
1061

    
1062
    jbooleanArray bArray = nullptr;
1063

    
1064
    bArray = SaxonProcessor::sxn_environ->env->NewBooleanArray((jint) length);
1065
    jboolean fill[length];
1066
    for (int i = 0; i < length; i++) {
1067
        fill[i] = input[i];
1068
    }
1069
    SaxonProcessor::sxn_environ->env->SetBooleanArrayRegion(bArray, 0, length, fill);
1070

    
1071

    
1072
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmbID, bArray);
1073
    if (!xdmArrayi) {
1074
        std::cerr << "Error found when converting string to XdmArray";
1075
        return nullptr;
1076
    }
1077
    if (exceptionOccurred()) {
1078
        exception = checkAndCreateException(saxonCAPIClass);
1079
    } else {
1080
        XdmArray *value = new XdmArray(xdmArrayi);
1081
        return value;
1082
    }
1083
    return nullptr;
1084

    
1085

    
1086
}
1087

    
1088

    
1089
XdmArray *SaxonProcessor::makeArray(const char **input, int length) {
1090
    if (input == nullptr || length <= 0) {
1091
        std::cerr << "Error found when converting string to XdmArray";
1092
        return nullptr;
1093
    }
1094
    jobject obj = nullptr;
1095
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1096
    jmethodID mmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1097
                                                                                     "([Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmArray;");
1098

    
1099
    jmethodID mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
1100
                                                                                      "(Ljava/lang/String;)V"));
1101
    jobjectArray valueArray = nullptr;
1102
    jobject obj2 = nullptr;
1103
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length, xdmAtomicClass, 0);
1104
    for (int i = 0; i < length; i++) {
1105
        if (input[i] == nullptr) {
1106
            std::cerr << "Error found when converting string to XdmArray";
1107
            return nullptr;
1108
        }
1109
        obj = getJavaStringValue(SaxonProcessor::sxn_environ, input[i]);
1110
        obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, obj));
1111
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, obj2);
1112
    }
1113

    
1114
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmID, valueArray);
1115
    if (!xdmArrayi) {
1116
        std::cerr << "Error found when converting string to XdmArray";
1117
        return nullptr;
1118
    }
1119

    
1120
    if (exceptionOccurred()) {
1121
        checkAndCreateException(saxonCAPIClass);
1122
    } else {
1123
        XdmArray *value = new XdmArray(xdmArrayi);
1124
        return value;
1125
    }
1126
    return nullptr;
1127
}
1128

    
1129

    
1130
XdmMap *SaxonProcessor::makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap) {
1131
    jobjectArray keyArray = nullptr;
1132
    jobjectArray valueArray = nullptr;
1133
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
1134
                                      "java/lang/Object");
1135

    
1136
    int size = dataMap.size();
1137

    
1138
    if (size > 0) {
1139

    
1140
        keyArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1141
                                                                    objectClass, 0);
1142
        valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1143
                                                                      objectClass, 0);
1144
        int i = 0;
1145
        for (std::map<XdmAtomicValue *, XdmValue *>::iterator iter =
1146
                dataMap.begin(); iter != dataMap.end(); ++iter, i++) {
1147

    
1148

    
1149
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(keyArray, i,
1150
                                                                    (iter->first)->getUnderlyingValue());
1151

    
1152
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i,
1153
                                                                    (iter->second)->getUnderlyingValue());
1154

    
1155
        }
1156
        jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
1157
        jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass, "makeXdmMap",
1158
                                                                                         "([Lnet/sf/saxon/s9api/XdmAtomicValue;[Lnet/sf/saxon/s9api/XdmValue;)Lnet/sf/saxon/s9api/XdmMap;");
1159
        if (!xmID) {
1160
            std::cerr << "Error: SaxonDll." << "makeXdmMap"
1161
                      << " not found\n" << std::endl;
1162
            return nullptr;
1163
        }
1164

    
1165

    
1166
        jobject results = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
1167
                                                                                              keyArray, valueArray));
1168
        if (exceptionOccurred() || !results) {
1169
            checkAndCreateException(saxonCAPIClass);
1170
        } else {
1171
            return new XdmMap(results);
1172
        }
1173
    }
1174

    
1175
    return nullptr;
1176
}
1177

    
1178
#endif
1179

    
1180

    
(13-13/55)