Project

Profile

Help

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

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

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
    if(exceptionOccurred()) {
62
        SaxonProcessor::sxn_environ->env->ExceptionClear();
63
    }
64
    if (exception != nullptr) {
65
        delete exception;
66
    }
67

    
68
}
69

    
70

    
71
SaxonProcessor::SaxonProcessor() {
72
    licensei = false;
73
    SaxonProcessor(false);
74
}
75

    
76
const char * SaxonProcessor::getResourcesDirectory() {
77
    return NULL;
78
}
79

    
80
void SaxonProcessor::createException(const char * message) {
81

    
82
    if(exceptionOccurred()) {
83
        delete exception;
84
        SaxonProcessor::sxn_environ->env->ExceptionClear();
85
    }
86

    
87
    if(message == nullptr) {
88
        exception = checkAndCreateException(saxonCAPIClass);
89
    } else {
90
        exception = new SaxonApiException(message);
91
    }
92

    
93
}
94

    
95

    
96
SaxonApiException * SaxonProcessor::checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject) {
97

    
98
    if (env->ExceptionCheck()) {
99
        std::string result1 = "";
100
        std::string errorCode = "";
101
        jthrowable exc = env->ExceptionOccurred();
102

    
103
#ifdef DEBUG
104
        env->ExceptionDescribe();
105
#endif
106
        jclass exccls(env->GetObjectClass(exc));
107
        jclass clscls(env->FindClass("java/lang/Class"));
108

    
109
        jmethodID getName(env->GetMethodID(clscls, "getName", "()Ljava/lang/String;"));
110
        jstring name(static_cast<jstring>(env->CallObjectMethod(exccls, getName)));
111
        char const *utfName(env->GetStringUTFChars(name, 0));
112
        result1 = (std::string(utfName));
113
        env->ReleaseStringUTFChars(name, utfName);
114

    
115
        jmethodID getMessage(env->GetMethodID(exccls, "getMessage", "()Ljava/lang/String;"));
116
        if (getMessage) {
117

    
118
            jstring message((jstring) (env->CallObjectMethod(exc, getMessage)));
119
            char const *utfMessage = nullptr;
120
            if (!message) {
121
                utfMessage = "";
122
                return nullptr;
123
            } else {
124
                utfMessage = (env->GetStringUTFChars(message, 0));
125
            }
126
            if (utfMessage != nullptr) {
127
                result1 = (result1 + " : ") + utfMessage;
128
            }
129

    
130
            env->ReleaseStringUTFChars(message, utfMessage);
131

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

    
134
                jclass saxonApiExceptionClass(env->FindClass("net/sf/saxon/s9api/SaxonApiException"));
135
                static jmethodID lineNumID = nullptr;
136
                if (lineNumID == nullptr) {
137
                    lineNumID = env->GetMethodID(saxonApiExceptionClass, "getLinenumber", "()I");
138
                }
139
                static jmethodID ecID = nullptr;
140
                if (ecID == nullptr) {
141
                    ecID = env->GetMethodID(saxonApiExceptionClass, "getErrorCode", "()Ljnet/sf/saxon/s9api/QName;");
142
                }
143
                static jmethodID esysID = nullptr;
144
                if (esysID == nullptr) {
145
                    esysID = env->GetMethodID(saxonApiExceptionClass, "getSystemId", "()Ljava/lang/String;");
146
                }
147

    
148

    
149
                jobject errCodeQName = (jobject) (env->CallObjectMethod(exc, ecID));
150
                jstring errSystemID = (jstring) (env->CallObjectMethod(exc, esysID));
151
                int linenum = env->CallIntMethod(exc, lineNumID);
152

    
153
                jclass qnameClass(env->FindClass("net/sf/saxon/s9api/QName"));
154
                static jmethodID qnameStrID = nullptr;
155
                if (qnameStrID == nullptr) {
156
                    qnameStrID = env->GetMethodID(qnameClass, "toString", "()Ljava/lang/String;");
157
                }
158

    
159
                jstring qnameStr = (jstring) (env->CallObjectMethod(errCodeQName, qnameStrID));
160

    
161

    
162
                SaxonApiException *saxonExceptions = new SaxonApiException(result1.c_str(),
163
                                                                           (qnameStr ? env->GetStringUTFChars(qnameStr,
164
                                                                                                              0)
165
                                                                                     : nullptr),
166
                                                                           (errSystemID ? env->GetStringUTFChars(
167
                                                                                   errSystemID, 0) : nullptr), linenum);
168

    
169
                if (errCodeQName) {
170
                    env->DeleteLocalRef(errCodeQName);
171
                }
172
                if (errSystemID) {
173
                    env->DeleteLocalRef(errSystemID);
174
                }
175
                if (qnameStr) {
176
                    env->DeleteLocalRef(qnameStr);
177
                }
178

    
179
                if (message) {
180
                    env->DeleteLocalRef(message);
181
                }
182
                env->ExceptionClear();
183
                return saxonExceptions;
184
            }
185
        }
186
        SaxonApiException *saxonExceptions = new SaxonApiException(result1.c_str());
187
        //env->ExceptionDescribe();
188
        env->ExceptionClear();
189
        return saxonExceptions;
190
    }
191
    return nullptr;
192

    
193
}
194

    
195

    
196
SaxonProcessor::SaxonProcessor(bool l) {
197

    
198
    cwd = "";
199
    licensei = l;
200
    versionStr = nullptr;
201

    
202
    if (SaxonProcessor::jvmCreatedCPP == 0) {
203
        SaxonProcessor::jvmCreatedCPP = 1;
204
        SaxonProcessor::sxn_environ = new sxnc_environment;//(sxnc_environment *)malloc(sizeof(sxnc_environment));
205

    
206

    
207
        /*
208
         * First of all, load required component.
209
         * By the time of JET initialization, all components should be loaded.
210
         */
211

    
212
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
213

    
214
        /*
215
         * Initialize JET run-time.
216
         * The handle of loaded component is used to retrieve Invocation API.
217
         */
218
        initDefaultJavaRT(SaxonProcessor::sxn_environ);
219
    } else {
220
#ifdef DEBUG
221
        std::cerr<<"SaxonProc constructor: jvm exists! jvmCreatedCPP="<<jvmCreatedCPP<<std::endl;
222
#endif
223

    
224
    }
225

    
226

    
227
    versionClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/Version");
228
    procClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/Processor");
229
    saxonCAPIClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/SaxonCAPI");
230

    
231
    jobject proci = createSaxonProcessor(SaxonProcessor::sxn_environ->env, procClass, "(Z)V", nullptr, licensei);
232
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(proci);
233
    if (!proc) {
234
        std::cout << "proc is nullptr in SaxonProcessor constructor" << std::endl;
235
    }
236

    
237
    xdmAtomicClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
238
#ifdef DEBUG
239
    jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "setDebugMode", "(Z)V");
240
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(saxonCAPIClass, debugMID, (jboolean)true);
241
#endif
242
}
243

    
244
SaxonProcessor::SaxonProcessor(const char *configFile) {
245
    cwd = "";
246
    versionStr = nullptr;
247

    
248
    if (SaxonProcessor::jvmCreatedCPP == 0) {
249
        SaxonProcessor::jvmCreatedCPP = 1;
250
        //SaxonProcessor::sxn_environ= new sxnc_environment;
251
        SaxonProcessor::sxn_environ = (sxnc_environment *) malloc(sizeof(sxnc_environment));
252

    
253
        /*
254
         * First of all, load required component.
255
         * By the time of JET initialization, all components should be loaded.
256
         */
257

    
258
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
259

    
260
        /*
261
         * Initialize JET run-time.
262
         * The handle of loaded component is used to retrieve Invocation API.
263
         */
264
        initDefaultJavaRT(SaxonProcessor::sxn_environ);
265
    }
266

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

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

    
272
    static jmethodID mIDcreateProc = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
273
                                                                                                     "createSaxonProcessor",
274
                                                                                                     "(Ljava/lang/String;)Lnet/sf/saxon/s9api/Processor;");
275
    if (!mIDcreateProc) {
276
        std::cerr << "Error: SaxonDll." << "getPrimitiveTypeName"
277
                  << " not found\n" << std::endl;
278
        return;
279
    }
280
    jobject proci = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mIDcreateProc,
281
                                                                             SaxonProcessor::sxn_environ->env->NewStringUTF(
282
                                                                                     configFile));
283
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(proci);
284
    if (!proc) {
285
        checkAndCreateException(saxonCAPIClass);
286
        std::cerr << "Error: " << getDllname() << ". processor is nullptr in constructor(configFile)" << std::endl;
287
        return;
288
    }
289

    
290
    licensei = true;
291
#ifdef DEBUG
292

    
293
    std::cerr<<"SaxonProc constructor(configFile)"<<std::endl;
294
#endif
295
    xdmAtomicClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
296
}
297

    
298
SaxonProcessor::~SaxonProcessor() {
299
    clearConfigurationProperties();
300
    if(proc) {
301
            SaxonProcessor::sxn_environ->env->DeleteGlobalRef(proc);
302
    }
303
    if (versionStr != nullptr) {
304
        delete versionStr;
305
    }
306
    exceptionClear();
307
}
308

    
309

    
310
bool SaxonProcessor::isSchemaAwareProcessor() {
311
    if (!licensei) {
312
        return false;
313
    } else {
314
        static jmethodID MID_schema = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(procClass,
315
                                                                                                "isSchemaAware", "()Z");
316
        if (!MID_schema) {
317
            std::cerr << "\nError: Saxonc " << "SaxonProcessor.isSchemaAware()" << " not found" << std::endl;
318
            return false;
319
        }
320

    
321
        licensei = (jboolean) (SaxonProcessor::sxn_environ->env->CallBooleanMethod(proc, MID_schema));
322
        return licensei;
323

    
324
    }
325

    
326
}
327

    
328
void SaxonProcessor::applyConfigurationProperties() {
329
    if (configProperties.size() > 0) {
330
        int size = configProperties.size();
331
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
332
        jobjectArray stringArray1 = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size, stringClass, 0);
333
        jobjectArray stringArray2 = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size, stringClass, 0);
334
        static jmethodID mIDappConfig = nullptr;
335
        if (mIDappConfig == nullptr) {
336
            mIDappConfig = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
337
                                                                                           "applyToConfiguration",
338
                                                                                           "(Lnet/sf/saxon/s9api/Processor;[Ljava/lang/String;[Ljava/lang/String;)V");
339
            if (!mIDappConfig) {
340
                std::cerr << "Error: SaxonDll." << "applyToConfiguration"
341
                          << " not found\n" << std::endl;
342
                return;
343
            }
344
        }
345
        int i = 0;
346
        std::map<std::string, std::string>::iterator iter = configProperties.begin();
347
        for (iter = configProperties.begin(); iter != configProperties.end(); ++iter, i++) {
348
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray1, i,
349
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
350
                                                                            (iter->first).c_str()));
351
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray2, i,
352
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
353
                                                                            (iter->second).c_str()));
354
        }
355
        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mIDappConfig, proc, stringArray1,
356
                                                                 stringArray2);
357
        if (exceptionOccurred()) {
358
            createException();
359
        }
360
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray1);
361
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray2);
362

    
363
    }
364
}
365

    
366

    
367
jobjectArray SaxonProcessor::createJArray(XdmValue **values, int length) {
368
    jobjectArray valueArray = nullptr;
369

    
370
    jclass xdmValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
371
                                        "net/sf/saxon/s9api/XdmValue");
372

    
373

    
374
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length,
375
                                                                  xdmValueClass, 0);
376

    
377
    for (int i = 0; i < length; i++) {
378
#ifdef DEBUG
379
        std::string s1 = typeid(values[i]).name();
380
        std::cerr<<"In createJArray\nType of itr:"<<s1<<std::endl;
381

    
382

    
383
        jobject xx = values[i]->getUnderlyingValue();
384

    
385
        if(xx == nullptr) {
386
            std::cerr<<"value failed"<<std::endl;
387
        } else {
388

    
389
            std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
390
        }
391
        if(values[i]->getUnderlyingValue() == nullptr) {
392
            std::cerr<<"value["<<i<<"]->getUnderlyingValue() is nullptr"<<std::endl;
393
        }
394
#endif
395
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, values[i]->getUnderlyingValue());
396
    }
397
    return valueArray;
398

    
399
}
400

    
401

    
402
JParameters SaxonProcessor::createParameterJArray(std::map<std::string, XdmValue *> parameters,
403
                                                  std::map<std::string, std::string> properties) {
404
    JParameters comboArrays;
405
    comboArrays.stringArray = nullptr;
406
    comboArrays.objectArray = nullptr;
407
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
408
                                      "java/lang/Object");
409
    jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
410
                                      "java/lang/String");
411

    
412
    int size = parameters.size() + properties.size();
413
#ifdef DEBUG
414
    std::cerr<<"Properties size: "<<properties.size()<<std::endl;
415
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
416
#endif
417
    if (size > 0) {
418

    
419
        comboArrays.objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
420
                                                                                   objectClass, 0);
421
        comboArrays.stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
422
                                                                                   stringClass, 0);
423
        int i = 0;
424
        for (std::map<std::string, XdmValue *>::iterator iter =
425
                parameters.begin(); iter != parameters.end(); ++iter, i++) {
426

    
427
#ifdef DEBUG
428
            std::cerr<<"map 1"<<std::endl;
429
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
430
#endif
431
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
432
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
433
                                                                            (iter->first).c_str()));
434
#ifdef DEBUG
435
            std::string s1 = typeid(iter->second).name();
436
            std::cerr<<"Type of itr:"<<s1<<std::endl;
437

    
438
            if((iter->second) == nullptr) {std::cerr<<"iter->second is nullptr"<<std::endl;
439
            } else {
440
                std::cerr<<"getting underlying value"<<std::endl;
441
            jobject xx = (iter->second)->getUnderlyingValue();
442

    
443
            if(xx == nullptr) {
444
                std::cerr<<"value failed"<<std::endl;
445
            } else {
446

    
447
                std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
448
            }
449
            if((iter->second)->getUnderlyingValue() == nullptr) {
450
                std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
451
            }}
452
#endif
453

    
454
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
455
                                                                    (iter->second)->getUnderlyingValue());
456

    
457
        }
458

    
459
        for (std::map<std::string, std::string>::iterator iter =
460
                properties.begin(); iter != properties.end(); ++iter, i++) {
461
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
462
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
463
                                                                            (iter->first).c_str()));
464
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
465
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
466
                                                                            (iter->second).c_str()));
467
        }
468

    
469
        return comboArrays;
470

    
471
    } else {
472
        return comboArrays;
473
    }
474
}
475

    
476
JParameters SaxonProcessor::createParameterJArray2(std::map<std::string, XdmValue *> parameters) {
477
    JParameters comboArrays;
478
    comboArrays.stringArray = nullptr;
479
    comboArrays.objectArray = nullptr;
480
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
481
                                      "java/lang/Object");
482
    jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
483
                                      "java/lang/String");
484

    
485
    int size = parameters.size();
486
#ifdef DEBUG
487
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
488
#endif
489
    if (size > 0) {
490

    
491
        comboArrays.objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
492
                                                                                   objectClass, 0);
493
        comboArrays.stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
494
                                                                                   stringClass, 0);
495
        int i = 0;
496
        for (std::map<std::string, XdmValue *>::iterator iter =
497
                parameters.begin(); iter != parameters.end(); ++iter, i++) {
498

    
499
#ifdef DEBUG
500
            std::cerr<<"map 1"<<std::endl;
501
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
502
#endif
503
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
504
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
505
                                                                            (iter->first).c_str()));
506
#ifdef DEBUG
507
            std::string s1 = typeid(iter->second).name();
508
            std::cerr<<"Type of itr:"<<s1<<std::endl;
509

    
510
            if((iter->second) == nullptr) {std::cerr<<"iter->second is nullptr"<<std::endl;
511
            } else {
512
                std::cerr<<"getting underlying value"<<std::endl;
513
            jobject xx = (iter->second)->getUnderlyingValue();
514

    
515
            if(xx == nullptr) {
516
                std::cerr<<"value failed"<<std::endl;
517
            } else {
518

    
519
                std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
520
            }
521
            if((iter->second)->getUnderlyingValue() == nullptr) {
522
                std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
523
            }}
524
#endif
525

    
526
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
527
                                                                    (iter->second)->getUnderlyingValue());
528

    
529
        }
530

    
531

    
532
        return comboArrays;
533

    
534
    } else {
535
        return comboArrays;
536
    }
537
}
538

    
539

    
540
SaxonProcessor &SaxonProcessor::operator=(const SaxonProcessor &other) {
541
    versionClass = other.versionClass;
542
    procClass = other.procClass;
543
    saxonCAPIClass = other.saxonCAPIClass;
544
    cwd = other.cwd;
545
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(other.proc);
546
    parameters = other.parameters;
547
    configProperties = other.configProperties;
548
    licensei = other.licensei;
549
    exception = other.exception;
550
    return *this;
551
}
552

    
553
SaxonProcessor::SaxonProcessor(const SaxonProcessor &other) {
554
    versionClass = other.versionClass;
555
    procClass = other.procClass;
556
    saxonCAPIClass = other.saxonCAPIClass;
557
    cwd = other.cwd;
558
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(other.proc);
559
    parameters = other.parameters;
560
    configProperties = other.configProperties;
561
    licensei = other.licensei;
562
    exception = other.exception;
563
}
564

    
565

    
566

    
567
void SaxonProcessor::setResourcesDirectory(const char *dir){}
568

    
569
XsltProcessor *SaxonProcessor::newXsltProcessor() {
570
    return (new XsltProcessor(this, cwd));
571
}
572

    
573
Xslt30Processor *SaxonProcessor::newXslt30Processor() {
574
    return (new Xslt30Processor(this, cwd));
575
}
576

    
577
XQueryProcessor *SaxonProcessor::newXQueryProcessor() {
578
    return (new XQueryProcessor(this, cwd));
579
}
580

    
581
XPathProcessor *SaxonProcessor::newXPathProcessor() {
582
    return (new XPathProcessor(this, cwd));
583
}
584

    
585
SchemaValidator *SaxonProcessor::newSchemaValidator() {
586
    if (licensei) {
587
        return (new SchemaValidator(this, cwd));
588
    } else {
589
        std::cerr << "\nError: Processor is not licensed for schema processing!" << std::endl;
590
        return nullptr;
591
    }
592
}
593

    
594

    
595
const char *SaxonProcessor::version() {
596
    if (versionStr == nullptr) {
597

    
598
        static jmethodID MID_version = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
599
                                                                                                       "getProductVersion",
600
                                                                                                       "(Lnet/sf/saxon/s9api/Processor;)Ljava/lang/String;");
601
        if (!MID_version) {
602
            std::cerr << "\nError: MyClassInDll " << "SaxonCAPI.getProductVersion()" << " not found" << std::endl;
603
            return nullptr;
604
        }
605

    
606
        jstring jstr = (jstring) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, MID_version,
607
                                                                                           proc));
608
        const char *tempVersionStr = SaxonProcessor::sxn_environ->env->GetStringUTFChars(jstr, nullptr);
609
        int verLen = strlen(tempVersionStr) + 22 + strlen(CVERSION) + 1;
610
        versionStr = new char[verLen];
611
        snprintf(versionStr, verLen, "Saxon/C %s %s %s", CVERSION, "running with", tempVersionStr);
612
        delete tempVersionStr;
613

    
614
    }
615

    
616
    return versionStr;
617
}
618

    
619
void SaxonProcessor::setcwd(const char *dir) {
620
    if(dir != nullptr) {
621
        cwd = std::string(dir);
622
    }
623
}
624

    
625
const char *SaxonProcessor::getcwd() {
626
    return cwd.c_str();
627
}
628

    
629

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

    
635
    if (!catalogMID) {
636
        std::cerr << "\nError: Saxonc." << "setCatalog()" << " not found" << std::endl;
637
        return;
638
    }
639
    if (catalogFile == nullptr) {
640

    
641
        return;
642
    }
643
    static jmethodID configMID = SaxonProcessor::sxn_environ->env->GetMethodID(procClass, "getUnderlyingConfiguration",
644
                                                                               "()Lnet/sf/saxon/Configuration;");
645

    
646
    if (!configMID) {
647
        std::cerr << "\nError: Saxonc." << "getUnderlyingConfiguration()" << " not found" << std::endl;
648
        return;
649
    }
650

    
651

    
652
    if (!proc) {
653
        createException("Processor is null in SaxonProcessor.setCatalog");
654
        return;
655
    }
656

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

    
659
    if (!configObj) {
660
        std::cout << "proc is null in SaxonProcessor setcatalog - config obj" << std::endl;
661
        return;
662
    }
663
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(xmlResolverClass, catalogMID,
664
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(catalogFile),
665
                                                           configObj, (jboolean) isTracing);
666
#ifdef DEBUG
667
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
668
#endif
669

    
670
}
671

    
672

    
673
XdmNode *SaxonProcessor::parseXmlFromString(const char *source) {
674

    
675
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlString",
676
                                                                                    "(Ljava/lang/String;Lnet/sf/saxon/s9api/Processor;Lnet/sf/saxon/s9api/SchemaValidator;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
677
    if (!mID) {
678
        std::cerr << "\nError: Saxonc." << "parseXmlString()" << " not found" << std::endl;
679
        return nullptr;
680
    }
681
//TODO SchemaValidator
682

    
683
    if(source == nullptr) {
684
        createException("Source string is NULL");
685
        return nullptr;
686
    }
687

    
688
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID,
689
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
690
                                                                                        cwd.c_str()), proc, nullptr,
691
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
692
                                                                                        source));
693
    if (xdmNodei) {
694
        XdmNode *value = new XdmNode(xdmNodei);
695
        return value;
696
    } else if (exceptionOccurred()) {
697
        createException();
698
    }
699

    
700
#ifdef DEBUG
701
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
702
#endif
703

    
704
    return nullptr;
705
}
706

    
707
int SaxonProcessor::getNodeKind(jobject obj) {
708
    jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmNode;");
709
    static jmethodID nodeKindMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
710
                                                                                             "getNodeKind",
711
                                                                                             "()Lnet/sf/saxon/s9api/XdmNodeKind;");
712
    if (!nodeKindMID) {
713
        std::cerr << "Error: MyClassInDll." << "getNodeKind" << " not found\n"
714
                  << std::endl;
715
        return 0;
716
    }
717

    
718
    jobject nodeKindObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(obj, nodeKindMID));
719
    if (!nodeKindObj) {
720

    
721
        return 0;
722
    }
723
    jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/option/cpp/XdmUtils;");
724

    
725
    jmethodID mID2 = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,
726
                                                                                     "convertNodeKindType",
727
                                                                                     "(Lnet/sf/saxon/s9api/XdmNodeKind;)I");
728

    
729
    if (!mID2) {
730
        std::cerr << "Error: MyClassInDll." << "convertNodeKindType" << " not found\n"
731
                  << std::endl;
732
        return 0;
733
    }
734
    if (!nodeKindObj) {
735
        return 0;
736
    }
737
    int nodeKind = (int) (SaxonProcessor::sxn_environ->env->CallStaticIntMethod(xdmUtilsClass, mID2, nodeKindObj));
738
    return nodeKind;
739
}
740

    
741

    
742
XdmNode *SaxonProcessor::parseXmlFromFile(const char *source) {
743

    
744
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlFile",
745
                                                                                    "(Lnet/sf/saxon/s9api/Processor;Ljava/lang/String;Lnet/sf/saxon/s9api/SchemaValidator;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
746
    if (!mID) {
747
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFile()" << " not found" << std::endl;
748
        return nullptr;
749
    }
750
//TODO SchemaValidator
751
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID, proc,
752
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
753
                                                                                        cwd.c_str()), nullptr,
754
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
755
                                                                                        source));
756
    if (exceptionOccurred()) {
757
        createException();
758

    
759
    } else {
760

    
761
        XdmNode *value = new XdmNode(xdmNodei);
762
        return value;
763
    }
764
    return nullptr;
765
}
766

    
767
XdmNode *SaxonProcessor::parseXmlFromUri(const char *source) {
768

    
769
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlFile",
770
                                                                                    "(Lnet/sf/saxon/s9api/Processor;Ljava/lang/String;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
771
    if (!mID) {
772
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFromUri()" << " not found" << std::endl;
773
        return nullptr;
774
    }
775
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID, proc,
776
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
777
                                                                                        ""),
778
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
779
                                                                                        source));
780
    if (exceptionOccurred()) {
781
        createException();
782
    } else {
783
        XdmNode *value = new XdmNode(xdmNodei);
784
        return value;
785
    }
786
    return nullptr;
787
}
788

    
789

    
790
/**
791
   * Set a configuration property.
792
   *
793
   * @param name of the property
794
   * @param value of the property
795
   */
796
void SaxonProcessor::setConfigurationProperty(const char *name, const char *value) {
797
    if (name != nullptr) {
798
        configProperties.insert(
799
                std::pair<std::string, std::string>(std::string(name), std::string((value == nullptr ? "" : value))));
800
    }
801
}
802

    
803
void SaxonProcessor::clearConfigurationProperties() {
804
    configProperties.clear();
805
}
806

    
807

    
808
void SaxonProcessor::release() {
809
    if (SaxonProcessor::jvmCreatedCPP != 0) {
810
        SaxonProcessor::jvmCreatedCPP = 0;
811
        //std::cerr<<"SaxonProc: JVM finalized calling !"<<std::endl;
812
        finalizeJavaRT(SaxonProcessor::sxn_environ->jvm);
813

    
814
        //delete SaxonProcessor::sxn_environ;
815
        /*clearParameters();
816
        clearProperties();*/
817
    } else {
818
#ifdef DEBUG
819
        std::cerr << "SaxonProc: JVM finalize not called!" << std::endl;
820
#endif
821
    }
822
}
823

    
824

    
825
/* ========= Factory method for Xdm ======== */
826

    
827
XdmAtomicValue *SaxonProcessor::makeStringValue(const char *str) {
828
    jobject obj = getJavaStringValue(SaxonProcessor::sxn_environ, str);
829
    static jmethodID mssID_atomic = nullptr;
830
    if (mssID_atomic == nullptr) {
831
        mssID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
832
                                                                                  "(Ljava/lang/String;)V"));
833
    }
834
    if (!mssID_atomic) {
835
        std::cerr << "XdmAtomic constructor (String)" << std::endl;
836
        return nullptr;
837
    }
838
    jobject obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mssID_atomic, obj));
839
    XdmAtomicValue *value = new XdmAtomicValue(obj2, "xs:string");
840
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
841
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj2);
842
    return value;
843
}
844

    
845
XdmAtomicValue *SaxonProcessor::makeStringValue(std::string str) {
846
    jobject obj = getJavaStringValue(SaxonProcessor::sxn_environ, str.c_str());
847
    static jmethodID msID_atomic = nullptr;
848
    if (msID_atomic == nullptr) {
849
        msID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
850
                                                                                 "(Ljava/lang/String;)V"));
851
    }
852
    if (!msID_atomic) {
853
        std::cerr << "XdmAtomic constructor (String)" << std::endl;
854
        return nullptr;
855
    }
856
    jobject obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, msID_atomic, obj));
857
    XdmAtomicValue *value = new XdmAtomicValue(obj2, "xs:string");
858
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
859
    return value;
860
}
861

    
862
XdmAtomicValue *SaxonProcessor::makeIntegerValue(int i) {
863
    //jobject obj = integerValue(*SaxonProcessor::sxn_environ, i);
864
    static jmethodID miiID_atomic = nullptr;
865
    if (miiID_atomic == nullptr) {
866
        miiID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
867
    }
868
    if (!miiID_atomic) {
869
        std::cerr << "XdmAtomic constructor (J)" << std::endl;
870
        return nullptr;
871
    }
872
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, miiID_atomic, (jlong) i));
873
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}integer");
874
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
875
    return value;
876
}
877

    
878
XdmAtomicValue *SaxonProcessor::makeDoubleValue(double d) {
879
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
880
    static jmethodID mdID_atomic = nullptr;
881
    if (mdID_atomic == nullptr) {
882
        mdID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(D)V"));
883
    }
884
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mdID_atomic, (jdouble) d));
885
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}double");
886
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
887
    return value;
888
}
889

    
890
XdmAtomicValue *SaxonProcessor::makeFloatValue(float d) {
891
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
892
    static jmethodID mfID_atomic = nullptr;
893
    if (mfID_atomic == nullptr) {
894
        mfID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(F)V"));
895
    }
896
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mfID_atomic, (jfloat) d));
897
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}float");
898
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
899
    return value;
900
}
901

    
902
XdmAtomicValue *SaxonProcessor::makeLongValue(long l) {
903
    //jobject obj = longValue(*SaxonProcessor::sxn_environ, l);
904
    static jmethodID mlID_atomic = nullptr;
905
    if (mlID_atomic == nullptr) {
906
        mlID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
907
    }
908
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mlID_atomic, (jlong) l));
909
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}long");
910
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
911
    return value;
912
}
913

    
914
XdmAtomicValue *SaxonProcessor::makeBooleanValue(bool b) {
915
    //jobject obj = booleanValue(*SaxonProcessor::sxn_environ, b);
916
    static jmethodID mID_atomic = nullptr;
917
    if (mID_atomic == nullptr) {
918
        mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(Z)V"));
919
    }
920
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, (jboolean) b));
921
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}boolean");
922
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
923
    return value;
924
}
925

    
926
XdmAtomicValue *SaxonProcessor::makeQNameValue(const char *str) {
927
    jobject val = xdmValueAsObj(SaxonProcessor::sxn_environ, "QName", str);
928
    XdmAtomicValue *value = new XdmAtomicValue(val, "QName");
929
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(val);
930
    return value;
931
}
932

    
933
XdmAtomicValue *SaxonProcessor::makeAtomicValue(const char *typei, const char *strValue) {
934
    jobject obj = xdmValueAsObj(SaxonProcessor::sxn_environ, typei, strValue);
935
    XdmAtomicValue *value = new XdmAtomicValue(obj, typei);
936
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
937
    return value;
938
}
939

    
940
const char *SaxonProcessor::getStringValue(XdmItem *item) {
941
    const char *result = stringValue(SaxonProcessor::sxn_environ, item->getUnderlyingValue());
942
#ifdef DEBUG
943
    if(result == nullptr) {
944
        std::cout<<"getStringValue of XdmItem is nullptr"<<std::endl;
945
    } else {
946
        std::cout<<"getStringValue of XdmItem is OK"<<std::endl;
947
    }
948
#endif
949

    
950
    return result;
951

    
952
}
953

    
954
#if CVERSION_API_NO >= 123
955

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

    
972

    
973
    jshortArray sArray = nullptr;
974

    
975
    sArray = SaxonProcessor::sxn_environ->env->NewShortArray((jint) length);
976
    jshort fill[length];
977
    for (int i = 0; i < length; i++) {
978
        fill[i] = input[i];
979
    }
980
    SaxonProcessor::sxn_environ->env->SetShortArrayRegion(sArray, 0, length, fill);
981

    
982
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmssID, sArray);
983
    if (!xdmArrayi) {
984
        std::cerr << "Error found when converting string to XdmArray";
985
        return nullptr;
986
    }
987

    
988
    if (exceptionOccurred()) {
989
        createException();
990
    } else {
991
        XdmArray *value = new XdmArray(xdmArrayi);
992
        return value;
993
    }
994
    return nullptr;
995
}
996

    
997

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

    
1014

    
1015
    jintArray iArray = nullptr;
1016

    
1017
    iArray = SaxonProcessor::sxn_environ->env->NewIntArray((jint) length);
1018
    jint fill[length];
1019
    for (int i = 0; i < length; i++) {
1020
        fill[i] = input[i];
1021
    }
1022
    SaxonProcessor::sxn_environ->env->SetIntArrayRegion(iArray, 0, length, fill);
1023

    
1024

    
1025
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiiID, iArray);
1026
    if (!xdmArrayi) {
1027
        std::cerr << "Error found when converting string to XdmArray";
1028
        return nullptr;
1029
    }
1030
    if (exceptionOccurred()) {
1031
        createException();
1032
    } else {
1033
        XdmArray *value = new XdmArray(xdmArrayi);
1034
        return value;
1035
    }
1036
    return nullptr;
1037

    
1038
}
1039

    
1040
XdmArray *SaxonProcessor::makeArray(long *input, int length) {
1041
    if (input == nullptr) {
1042
        std::cerr << "Error found when converting string to XdmArray";
1043
        return nullptr;
1044
    }
1045
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1046
    static jmethodID mmiID = nullptr;
1047
    if (mmiID == nullptr) {
1048
        mmiID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1049
                                                                                "([J)Lnet/sf/saxon/s9api/XdmArray;");
1050
    }
1051
    if (!mmiID) {
1052
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([J)" << " not found" << std::endl;
1053
        return nullptr;
1054
    }
1055

    
1056

    
1057
    jlongArray lArray = nullptr;
1058

    
1059
    lArray = SaxonProcessor::sxn_environ->env->NewLongArray((jint) length);
1060
    jlong fill[length];
1061
    for (int i = 0; i < length; i++) {
1062
        fill[i] = input[i];
1063
    }
1064
    SaxonProcessor::sxn_environ->env->SetLongArrayRegion(lArray, 0, length, fill);
1065

    
1066

    
1067
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiID, lArray);
1068
    if (!xdmArrayi) {
1069
        std::cerr << "Error found when converting string to XdmArray";
1070
        return nullptr;
1071
    }
1072
    if (exceptionOccurred()) {
1073
        createException();
1074
    } else {
1075
        XdmArray *value = new XdmArray(xdmArrayi);
1076
        return value;
1077
    }
1078
    return nullptr;
1079
}
1080

    
1081

    
1082
XdmArray *SaxonProcessor::makeArray(bool *input, int length) {
1083
    if (input == nullptr) {
1084
        std::cerr << "Error found when converting string to XdmArray";
1085
        return nullptr;
1086
    }
1087
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1088
    static jmethodID mmbID = nullptr;
1089
    if (mmbID == nullptr) {
1090
        mmbID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1091
                                                                                "([Z)Lnet/sf/saxon/s9api/XdmArray;");
1092
    }
1093
    if (!mmbID) {
1094
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([Z)" << " not found" << std::endl;
1095
        return nullptr;
1096
    }
1097

    
1098

    
1099
    jbooleanArray bArray = nullptr;
1100

    
1101
    bArray = SaxonProcessor::sxn_environ->env->NewBooleanArray((jint) length);
1102
    jboolean fill[length];
1103
    for (int i = 0; i < length; i++) {
1104
        fill[i] = input[i];
1105
    }
1106
    SaxonProcessor::sxn_environ->env->SetBooleanArrayRegion(bArray, 0, length, fill);
1107

    
1108

    
1109
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmbID, bArray);
1110
    if (!xdmArrayi) {
1111
        std::cerr << "Error found when converting string to XdmArray";
1112
        return nullptr;
1113
    }
1114
    if (exceptionOccurred()) {
1115
        createException();
1116
    } else {
1117
        XdmArray *value = new XdmArray(xdmArrayi);
1118
        return value;
1119
    }
1120
    return nullptr;
1121

    
1122

    
1123
}
1124

    
1125

    
1126
XdmArray *SaxonProcessor::makeArray(const char **input, int length) {
1127
    if (input == nullptr || length <= 0) {
1128
        std::cerr << "Error found when converting string to XdmArray";
1129
        return nullptr;
1130
    }
1131
    jobject obj = nullptr;
1132
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1133
    jmethodID mmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1134
                                                                                     "([Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmArray;");
1135

    
1136
    jmethodID mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
1137
                                                                                      "(Ljava/lang/String;)V"));
1138
    jobjectArray valueArray = nullptr;
1139
    jobject obj2 = nullptr;
1140
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length, xdmAtomicClass, 0);
1141
    for (int i = 0; i < length; i++) {
1142
        if (input[i] == nullptr) {
1143
            std::cerr << "Error found when converting string to XdmArray";
1144
            return nullptr;
1145
        }
1146
        obj = getJavaStringValue(SaxonProcessor::sxn_environ, input[i]);
1147
        obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, obj));
1148
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, obj2);
1149
    }
1150

    
1151
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmID, valueArray);
1152
    if (!xdmArrayi) {
1153
        std::cerr << "Error found when converting string to XdmArray";
1154
        return nullptr;
1155
    }
1156

    
1157
    if (exceptionOccurred()) {
1158
        checkAndCreateException(saxonCAPIClass);
1159
    } else {
1160
        XdmArray *value = new XdmArray(xdmArrayi);
1161
        return value;
1162
    }
1163
    return nullptr;
1164
}
1165

    
1166

    
1167
XdmMap *SaxonProcessor::makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap) {
1168
    jobjectArray keyArray = nullptr;
1169
    jobjectArray valueArray = nullptr;
1170
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
1171
                                      "java/lang/Object");
1172

    
1173
    int size = dataMap.size();
1174

    
1175
    if (size > 0) {
1176

    
1177
        keyArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1178
                                                                    objectClass, 0);
1179
        valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1180
                                                                      objectClass, 0);
1181
        int i = 0;
1182
        for (std::map<XdmAtomicValue *, XdmValue *>::iterator iter =
1183
                dataMap.begin(); iter != dataMap.end(); ++iter, i++) {
1184

    
1185

    
1186
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(keyArray, i,
1187
                                                                    (iter->first)->getUnderlyingValue());
1188

    
1189
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i,
1190
                                                                    (iter->second)->getUnderlyingValue());
1191

    
1192
        }
1193
        jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
1194
        jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass, "makeXdmMap",
1195
                                                                                         "([Lnet/sf/saxon/s9api/XdmAtomicValue;[Lnet/sf/saxon/s9api/XdmValue;)Lnet/sf/saxon/s9api/XdmMap;");
1196
        if (!xmID) {
1197
            std::cerr << "Error: SaxonDll." << "makeXdmMap"
1198
                      << " not found\n" << std::endl;
1199
            return nullptr;
1200
        }
1201

    
1202

    
1203
        jobject results = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
1204
                                                                                              keyArray, valueArray));
1205
        if (exceptionOccurred() || !results) {
1206
            checkAndCreateException(saxonCAPIClass);
1207
        } else {
1208
            return new XdmMap(results);
1209
        }
1210
    }
1211

    
1212
    return nullptr;
1213
}
1214

    
1215
#endif
1216

    
1217

    
(13-13/55)