Project

Profile

Help

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

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

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() {
35
    const char *message = nullptr;
36
    message = checkForException(sxn_environ);
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
    if(proc) {
281
            SaxonProcessor::sxn_environ->env->DeleteGlobalRef(proc);
282
    }
283
    if (versionStr != nullptr) {
284
        delete versionStr;
285
    }
286
}
287

    
288

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

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

    
303
    }
304

    
305
}
306

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

    
343
    }
344
}
345

    
346

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

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

    
353

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

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

    
362

    
363
        jobject xx = values[i]->getUnderlyingValue();
364

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

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

    
379
}
380

    
381

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

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

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

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

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

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

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

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

    
437
        }
438

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

    
449
        return comboArrays;
450

    
451
    } else {
452
        return comboArrays;
453
    }
454
}
455

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

    
465
    int size = parameters.size();
466
#ifdef DEBUG
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

    
512
        return comboArrays;
513

    
514
    } else {
515
        return comboArrays;
516
    }
517
}
518

    
519

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

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

    
545

    
546

    
547
void SaxonProcessor::setResourcesDirectory(const char *dir){}
548

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

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

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

    
561
XPathProcessor *SaxonProcessor::newXPathProcessor() {
562
    return (new XPathProcessor(this, cwd));
563
}
564

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

    
574

    
575
const char *SaxonProcessor::version() {
576
    if (versionStr == nullptr) {
577

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

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

    
594
    }
595

    
596
    return versionStr;
597
}
598

    
599
void SaxonProcessor::setcwd(const char *dir) {
600
    if(dir != nullptr) {
601
        cwd = std::string(dir);
602
    }
603
}
604

    
605
const char *SaxonProcessor::getcwd() {
606
    return cwd.c_str();
607
}
608

    
609

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

    
615
    if (!catalogMID) {
616
        std::cerr << "\nError: Saxonc." << "setCatalog()" << " not found" << std::endl;
617
        return;
618
    }
619
    if (catalogFile == nullptr) {
620

    
621
        return;
622
    }
623
    static jmethodID configMID = SaxonProcessor::sxn_environ->env->GetMethodID(procClass, "getUnderlyingConfiguration",
624
                                                                               "()Lnet/sf/saxon/Configuration;");
625

    
626
    if (!configMID) {
627
        std::cerr << "\nError: Saxonc." << "getUnderlyingConfiguration()" << " not found" << std::endl;
628
        return;
629
    }
630

    
631

    
632
    if (!proc) {
633
        exception = new SaxonApiException("Processor is null in SaxonProcessor.setCatalog");
634
        return;
635
    }
636

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

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

    
650
}
651

    
652

    
653
XdmNode *SaxonProcessor::parseXmlFromString(const char *source) {
654

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

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

    
676
#ifdef DEBUG
677
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
678
#endif
679

    
680
    return nullptr;
681
}
682

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

    
694
    jobject nodeKindObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(obj, nodeKindMID));
695
    if (!nodeKindObj) {
696

    
697
        return 0;
698
    }
699
    jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/option/cpp/XdmUtils;");
700

    
701
    jmethodID mID2 = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,
702
                                                                                     "convertNodeKindType",
703
                                                                                     "(Lnet/sf/saxon/s9api/XdmNodeKind;)I");
704

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

    
717

    
718
XdmNode *SaxonProcessor::parseXmlFromFile(const char *source) {
719

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

    
736
    } else {
737

    
738
        XdmNode *value = new XdmNode(xdmNodei);
739
        return value;
740
    }
741
    return nullptr;
742
}
743

    
744
XdmNode *SaxonProcessor::parseXmlFromUri(const char *source) {
745

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

    
766

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

    
780
void SaxonProcessor::clearConfigurationProperties() {
781
    configProperties.clear();
782
}
783

    
784

    
785
void SaxonProcessor::release() {
786
    if (SaxonProcessor::jvmCreatedCPP != 0) {
787
        SaxonProcessor::jvmCreatedCPP = 0;
788
        //std::cerr<<"SaxonProc: JVM finalized calling !"<<std::endl;
789
        finalizeJavaRT(SaxonProcessor::sxn_environ->jvm);
790

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

    
801

    
802
/* ========= Factory method for Xdm ======== */
803

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

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

    
839
XdmAtomicValue *SaxonProcessor::makeIntegerValue(int i) {
840
    //jobject obj = integerValue(*SaxonProcessor::sxn_environ, i);
841
    static jmethodID miiID_atomic = nullptr;
842
    if (miiID_atomic == nullptr) {
843
        miiID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
844
    }
845
    if (!miiID_atomic) {
846
        std::cerr << "XdmAtomic constructor (J)" << std::endl;
847
        return nullptr;
848
    }
849
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, miiID_atomic, (jlong) i));
850
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}integer");
851
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
852
    return value;
853
}
854

    
855
XdmAtomicValue *SaxonProcessor::makeDoubleValue(double d) {
856
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
857
    static jmethodID mdID_atomic = nullptr;
858
    if (mdID_atomic == nullptr) {
859
        mdID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(D)V"));
860
    }
861
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mdID_atomic, (jdouble) d));
862
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}double");
863
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
864
    return value;
865
}
866

    
867
XdmAtomicValue *SaxonProcessor::makeFloatValue(float d) {
868
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
869
    static jmethodID mfID_atomic = nullptr;
870
    if (mfID_atomic == nullptr) {
871
        mfID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(F)V"));
872
    }
873
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mfID_atomic, (jfloat) d));
874
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}float");
875
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
876
    return value;
877
}
878

    
879
XdmAtomicValue *SaxonProcessor::makeLongValue(long l) {
880
    //jobject obj = longValue(*SaxonProcessor::sxn_environ, l);
881
    static jmethodID mlID_atomic = nullptr;
882
    if (mlID_atomic == nullptr) {
883
        mlID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
884
    }
885
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mlID_atomic, (jlong) l));
886
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}long");
887
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
888
    return value;
889
}
890

    
891
XdmAtomicValue *SaxonProcessor::makeBooleanValue(bool b) {
892
    //jobject obj = booleanValue(*SaxonProcessor::sxn_environ, b);
893
    static jmethodID mID_atomic = nullptr;
894
    if (mID_atomic == nullptr) {
895
        mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(Z)V"));
896
    }
897
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, (jboolean) b));
898
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}boolean");
899
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
900
    return value;
901
}
902

    
903
XdmAtomicValue *SaxonProcessor::makeQNameValue(const char *str) {
904
    jobject val = xdmValueAsObj(SaxonProcessor::sxn_environ, "QName", str);
905
    XdmAtomicValue *value = new XdmAtomicValue(val, "QName");
906
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(val);
907
    return value;
908
}
909

    
910
XdmAtomicValue *SaxonProcessor::makeAtomicValue(const char *typei, const char *strValue) {
911
    jobject obj = xdmValueAsObj(SaxonProcessor::sxn_environ, typei, strValue);
912
    XdmAtomicValue *value = new XdmAtomicValue(obj, typei);
913
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
914
    return value;
915
}
916

    
917
const char *SaxonProcessor::getStringValue(XdmItem *item) {
918
    const char *result = stringValue(SaxonProcessor::sxn_environ, item->getUnderlyingValue());
919
#ifdef DEBUG
920
    if(result == nullptr) {
921
        std::cout<<"getStringValue of XdmItem is nullptr"<<std::endl;
922
    } else {
923
        std::cout<<"getStringValue of XdmItem is OK"<<std::endl;
924
    }
925
#endif
926

    
927
    return result;
928

    
929
}
930

    
931
#if CVERSION_API_NO >= 123
932

    
933
XdmArray *SaxonProcessor::makeArray(short *input, int length) {
934
    if (input == nullptr) {
935
        std::cerr << "Error found when converting string to XdmArray" << std::endl;
936
        return nullptr;
937
    }
938
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
939
    static jmethodID mmssID = nullptr;
940
    if (mmssID == nullptr) {
941
        mmssID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
942
                                                                                 "([S)Lnet/sf/saxon/s9api/XdmArray;");
943
    }
944
    if (!mmssID) {
945
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([S)" << " not found" << std::endl;
946
        return nullptr;
947
    }
948

    
949

    
950
    jshortArray sArray = nullptr;
951

    
952
    sArray = SaxonProcessor::sxn_environ->env->NewShortArray((jint) length);
953
    jshort fill[length];
954
    for (int i = 0; i < length; i++) {
955
        fill[i] = input[i];
956
    }
957
    SaxonProcessor::sxn_environ->env->SetShortArrayRegion(sArray, 0, length, fill);
958

    
959
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmssID, sArray);
960
    if (!xdmArrayi) {
961
        std::cerr << "Error found when converting string to XdmArray";
962
        return nullptr;
963
    }
964

    
965
    if (exceptionOccurred()) {
966
        exception = checkAndCreateException(saxonCAPIClass);
967
    } else {
968
        XdmArray *value = new XdmArray(xdmArrayi);
969
        return value;
970
    }
971
    return nullptr;
972
}
973

    
974

    
975
XdmArray *SaxonProcessor::makeArray(int *input, int length) {
976
    if (input == nullptr) {
977
        std::cerr << "Error found when converting string to XdmArray";
978
        return nullptr;
979
    }
980
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
981
    static jmethodID mmiiID = nullptr;
982
    if (mmiiID == nullptr) {
983
        mmiiID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
984
                                                                                 "([I)Lnet/sf/saxon/s9api/XdmArray;");
985
    }
986
    if (!mmiiID) {
987
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([I)" << " not found" << std::endl;
988
        return nullptr;
989
    }
990

    
991

    
992
    jintArray iArray = nullptr;
993

    
994
    iArray = SaxonProcessor::sxn_environ->env->NewIntArray((jint) length);
995
    jint fill[length];
996
    for (int i = 0; i < length; i++) {
997
        fill[i] = input[i];
998
    }
999
    SaxonProcessor::sxn_environ->env->SetIntArrayRegion(iArray, 0, length, fill);
1000

    
1001

    
1002
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiiID, iArray);
1003
    if (!xdmArrayi) {
1004
        std::cerr << "Error found when converting string to XdmArray";
1005
        return nullptr;
1006
    }
1007
    if (exceptionOccurred()) {
1008
        exception = checkAndCreateException(saxonCAPIClass);
1009
    } else {
1010
        XdmArray *value = new XdmArray(xdmArrayi);
1011
        return value;
1012
    }
1013
    return nullptr;
1014

    
1015
}
1016

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

    
1033

    
1034
    jlongArray lArray = nullptr;
1035

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

    
1043

    
1044
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiID, lArray);
1045
    if (!xdmArrayi) {
1046
        std::cerr << "Error found when converting string to XdmArray";
1047
        return nullptr;
1048
    }
1049
    if (exceptionOccurred()) {
1050
        exception = checkAndCreateException(saxonCAPIClass);
1051
    } else {
1052
        XdmArray *value = new XdmArray(xdmArrayi);
1053
        return value;
1054
    }
1055
    return nullptr;
1056
}
1057

    
1058

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

    
1075

    
1076
    jbooleanArray bArray = nullptr;
1077

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

    
1085

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

    
1099

    
1100
}
1101

    
1102

    
1103
XdmArray *SaxonProcessor::makeArray(const char **input, int length) {
1104
    if (input == nullptr || length <= 0) {
1105
        std::cerr << "Error found when converting string to XdmArray";
1106
        return nullptr;
1107
    }
1108
    jobject obj = nullptr;
1109
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1110
    jmethodID mmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1111
                                                                                     "([Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmArray;");
1112

    
1113
    jmethodID mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
1114
                                                                                      "(Ljava/lang/String;)V"));
1115
    jobjectArray valueArray = nullptr;
1116
    jobject obj2 = nullptr;
1117
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length, xdmAtomicClass, 0);
1118
    for (int i = 0; i < length; i++) {
1119
        if (input[i] == nullptr) {
1120
            std::cerr << "Error found when converting string to XdmArray";
1121
            return nullptr;
1122
        }
1123
        obj = getJavaStringValue(SaxonProcessor::sxn_environ, input[i]);
1124
        obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, obj));
1125
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, obj2);
1126
    }
1127

    
1128
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmID, valueArray);
1129
    if (!xdmArrayi) {
1130
        std::cerr << "Error found when converting string to XdmArray";
1131
        return nullptr;
1132
    }
1133

    
1134
    if (exceptionOccurred()) {
1135
        checkAndCreateException(saxonCAPIClass);
1136
    } else {
1137
        XdmArray *value = new XdmArray(xdmArrayi);
1138
        return value;
1139
    }
1140
    return nullptr;
1141
}
1142

    
1143

    
1144
XdmMap *SaxonProcessor::makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap) {
1145
    jobjectArray keyArray = nullptr;
1146
    jobjectArray valueArray = nullptr;
1147
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
1148
                                      "java/lang/Object");
1149

    
1150
    int size = dataMap.size();
1151

    
1152
    if (size > 0) {
1153

    
1154
        keyArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1155
                                                                    objectClass, 0);
1156
        valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1157
                                                                      objectClass, 0);
1158
        int i = 0;
1159
        for (std::map<XdmAtomicValue *, XdmValue *>::iterator iter =
1160
                dataMap.begin(); iter != dataMap.end(); ++iter, i++) {
1161

    
1162

    
1163
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(keyArray, i,
1164
                                                                    (iter->first)->getUnderlyingValue());
1165

    
1166
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i,
1167
                                                                    (iter->second)->getUnderlyingValue());
1168

    
1169
        }
1170
        jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
1171
        jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass, "makeXdmMap",
1172
                                                                                         "([Lnet/sf/saxon/s9api/XdmAtomicValue;[Lnet/sf/saxon/s9api/XdmValue;)Lnet/sf/saxon/s9api/XdmMap;");
1173
        if (!xmID) {
1174
            std::cerr << "Error: SaxonDll." << "makeXdmMap"
1175
                      << " not found\n" << std::endl;
1176
            return nullptr;
1177
        }
1178

    
1179

    
1180
        jobject results = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
1181
                                                                                              keyArray, valueArray));
1182
        if (exceptionOccurred() || !results) {
1183
            checkAndCreateException(saxonCAPIClass);
1184
        } else {
1185
            return new XdmMap(results);
1186
        }
1187
    }
1188

    
1189
    return nullptr;
1190
}
1191

    
1192
#endif
1193

    
1194

    
(13-13/55)