Project

Profile

Help

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

he / latest10 / hec / Saxon.C.API / SaxonProcessor.cpp @ 6304fb8b

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
    bool found = SaxonProcessor::sxn_environ->env->ExceptionCheck();
32
    return found;
33
}
34

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

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

    
52
void SaxonProcessor::exceptionClear() {
53
    SaxonProcessor::sxn_environ->env->ExceptionClear();
54
    /* if(exception != nullptr && clearCPPException) {
55
        delete exception;
56
    } */
57
}
58

    
59

    
60
SaxonProcessor::SaxonProcessor() {
61
    licensei = false;
62
    SaxonProcessor(false);
63
}
64

    
65

    
66
SaxonApiException *SaxonProcessor::checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject) {
67

    
68
    if (env->ExceptionCheck()) {
69
        std::string result1 = "";
70
        std::string errorCode = "";
71
        jthrowable exc = env->ExceptionOccurred();
72

    
73
#ifdef DEBUG
74
        env->ExceptionDescribe();
75
#endif
76
        jclass exccls(env->GetObjectClass(exc));
77
        jclass clscls(env->FindClass("java/lang/Class"));
78

    
79
        jmethodID getName(env->GetMethodID(clscls, "getName", "()Ljava/lang/String;"));
80
        jstring name(static_cast<jstring>(env->CallObjectMethod(exccls, getName)));
81
        char const *utfName(env->GetStringUTFChars(name, 0));
82
        result1 = (std::string(utfName));
83
        env->ReleaseStringUTFChars(name, utfName);
84

    
85
        jmethodID getMessage(env->GetMethodID(exccls, "getMessage", "()Ljava/lang/String;"));
86
        if (getMessage) {
87

    
88
            jstring message((jstring) (env->CallObjectMethod(exc, getMessage)));
89
            char const *utfMessage = nullptr;
90
            if (!message) {
91
                utfMessage = "";
92
                return nullptr;
93
            } else {
94
                utfMessage = (env->GetStringUTFChars(message, 0));
95
            }
96
            if (utfMessage != nullptr) {
97
                result1 = (result1 + " : ") + utfMessage;
98
            }
99

    
100
            env->ReleaseStringUTFChars(message, utfMessage);
101

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

    
104
                jclass saxonApiExceptionClass(env->FindClass("net/sf/saxon/s9api/SaxonApiException"));
105
                static jmethodID lineNumID = nullptr;
106
                if (lineNumID == nullptr) {
107
                    lineNumID = env->GetMethodID(saxonApiExceptionClass, "getLinenumber", "()I");
108
                }
109
                static jmethodID ecID = nullptr;
110
                if (ecID == nullptr) {
111
                    ecID = env->GetMethodID(saxonApiExceptionClass, "getErrorCode", "()Ljnet/sf/saxon/s9api/QName;");
112
                }
113
                static jmethodID esysID = nullptr;
114
                if (esysID == nullptr) {
115
                    esysID = env->GetMethodID(saxonApiExceptionClass, "getSystemId", "()Ljava/lang/String;");
116
                }
117

    
118

    
119
                jobject errCodeQName = (jobject) (env->CallObjectMethod(exc, ecID));
120
                jstring errSystemID = (jstring) (env->CallObjectMethod(exc, esysID));
121
                int linenum = env->CallIntMethod(exc, lineNumID);
122

    
123
                jclass qnameClass(env->FindClass("net/sf/saxon/s9api/QName"));
124
                static jmethodID qnameStrID = nullptr;
125
                if (qnameStrID == nullptr) {
126
                    qnameStrID = env->GetMethodID(qnameClass, "toString", "()Ljava/lang/String;");
127
                }
128

    
129
                jstring qnameStr = (jstring) (env->CallObjectMethod(errCodeQName, qnameStrID));
130

    
131

    
132
                SaxonApiException *saxonExceptions = new SaxonApiException(result1.c_str(),
133
                                                                           (qnameStr ? env->GetStringUTFChars(qnameStr,
134
                                                                                                              0)
135
                                                                                     : nullptr),
136
                                                                           (errSystemID ? env->GetStringUTFChars(
137
                                                                                   errSystemID, 0) : nullptr), linenum);
138

    
139
                if (errCodeQName) {
140
                    env->DeleteLocalRef(errCodeQName);
141
                }
142
                if (errSystemID) {
143
                    env->DeleteLocalRef(errSystemID);
144
                }
145
                if (qnameStr) {
146
                    env->DeleteLocalRef(qnameStr);
147
                }
148

    
149
                if (message) {
150
                    env->DeleteLocalRef(message);
151
                }
152
                env->ExceptionClear();
153
                return saxonExceptions;
154
            }
155
        }
156
        SaxonApiException *saxonExceptions = new SaxonApiException(result1.c_str());
157
        //env->ExceptionDescribe();
158
        env->ExceptionClear();
159
        return saxonExceptions;
160
    }
161
    return nullptr;
162

    
163
}
164

    
165

    
166
SaxonProcessor::SaxonProcessor(bool l) {
167

    
168
    cwd = "";
169
    licensei = l;
170
    versionStr = nullptr;
171

    
172
    if (SaxonProcessor::jvmCreatedCPP == 0) {
173
        SaxonProcessor::jvmCreatedCPP = 1;
174
        SaxonProcessor::sxn_environ = new sxnc_environment;//(sxnc_environment *)malloc(sizeof(sxnc_environment));
175

    
176

    
177
        /*
178
         * First of all, load required component.
179
         * By the time of JET initialization, all components should be loaded.
180
         */
181

    
182
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
183

    
184
        /*
185
         * Initialize JET run-time.
186
         * The handle of loaded component is used to retrieve Invocation API.
187
         */
188
        initDefaultJavaRT(SaxonProcessor::sxn_environ);
189
    } else {
190
#ifdef DEBUG
191
        std::cerr<<"SaxonProc constructor: jvm exists! jvmCreatedCPP="<<jvmCreatedCPP<<std::endl;
192
#endif
193

    
194
    }
195

    
196

    
197
    versionClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/Version");
198
    procClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/Processor");
199
    saxonCAPIClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/SaxonCAPI");
200

    
201
    jobject proci = createSaxonProcessor(SaxonProcessor::sxn_environ->env, procClass, "(Z)V", nullptr, licensei);
202
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(proci);
203
    if (!proc) {
204
        std::cout << "proc is nullptr in SaxonProcessor constructor" << std::endl;
205
    }
206

    
207
    xdmAtomicClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
208
#ifdef DEBUG
209
    jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "setDebugMode", "(Z)V");
210
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(saxonCAPIClass, debugMID, (jboolean)true);
211
#endif
212
}
213

    
214
SaxonProcessor::SaxonProcessor(const char *configFile) {
215
    cwd = "";
216
    versionStr = nullptr;
217

    
218
    if (SaxonProcessor::jvmCreatedCPP == 0) {
219
        SaxonProcessor::jvmCreatedCPP = 1;
220
        //SaxonProcessor::sxn_environ= new sxnc_environment;
221
        SaxonProcessor::sxn_environ = (sxnc_environment *) malloc(sizeof(sxnc_environment));
222

    
223
        /*
224
         * First of all, load required component.
225
         * By the time of JET initialization, all components should be loaded.
226
         */
227

    
228
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
229

    
230
        /*
231
         * Initialize JET run-time.
232
         * The handle of loaded component is used to retrieve Invocation API.
233
         */
234
        initDefaultJavaRT(SaxonProcessor::sxn_environ);
235
    }
236

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

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

    
242
    static jmethodID mIDcreateProc = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
243
                                                                                                     "createSaxonProcessor",
244
                                                                                                     "(Ljava/lang/String;)Lnet/sf/saxon/s9api/Processor;");
245
    if (!mIDcreateProc) {
246
        std::cerr << "Error: SaxonDll." << "getPrimitiveTypeName"
247
                  << " not found\n" << std::endl;
248
        return;
249
    }
250
    jobject proci = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mIDcreateProc,
251
                                                                             SaxonProcessor::sxn_environ->env->NewStringUTF(
252
                                                                                     configFile));
253
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(proci);
254
    if (!proc) {
255
        checkAndCreateException(saxonCAPIClass);
256
        std::cerr << "Error: " << getDllname() << ". processor is nullptr in constructor(configFile)" << std::endl;
257
        return;
258
    }
259

    
260
    licensei = true;
261
#ifdef DEBUG
262

    
263
    std::cerr<<"SaxonProc constructor(configFile)"<<std::endl;
264
#endif
265
    xdmAtomicClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
266
}
267

    
268
SaxonProcessor::~SaxonProcessor() {
269
    clearConfigurationProperties();
270
    SaxonProcessor::sxn_environ->env->DeleteGlobalRef(proc);
271
    if (versionStr != nullptr) {
272
        delete versionStr;
273
    }
274
}
275

    
276

    
277
bool SaxonProcessor::isSchemaAwareProcessor() {
278
    if (!licensei) {
279
        return false;
280
    } else {
281
        static jmethodID MID_schema = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(procClass,
282
                                                                                                "isSchemaAware", "()Z");
283
        if (!MID_schema) {
284
            std::cerr << "\nError: Saxonc " << "SaxonProcessor.isSchemaAware()" << " not found" << std::endl;
285
            return false;
286
        }
287

    
288
        licensei = (jboolean) (SaxonProcessor::sxn_environ->env->CallBooleanMethod(proc, MID_schema));
289
        return licensei;
290

    
291
    }
292

    
293
}
294

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

    
331
    }
332
}
333

    
334

    
335
jobjectArray SaxonProcessor::createJArray(XdmValue **values, int length) {
336
    jobjectArray valueArray = nullptr;
337

    
338
    jclass xdmValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
339
                                        "net/sf/saxon/s9api/XdmValue");
340

    
341

    
342
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length,
343
                                                                  xdmValueClass, 0);
344

    
345
    for (int i = 0; i < length; i++) {
346
#ifdef DEBUG
347
        std::string s1 = typeid(values[i]).name();
348
        std::cerr<<"In createJArray\nType of itr:"<<s1<<std::endl;
349

    
350

    
351
        jobject xx = values[i]->getUnderlyingValue();
352

    
353
        if(xx == nullptr) {
354
            std::cerr<<"value failed"<<std::endl;
355
        } else {
356

    
357
            std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
358
        }
359
        if(values[i]->getUnderlyingValue() == nullptr) {
360
            std::cerr<<"value["<<i<<"]->getUnderlyingValue() is nullptr"<<std::endl;
361
        }
362
#endif
363
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, values[i]->getUnderlyingValue());
364
    }
365
    return valueArray;
366

    
367
}
368

    
369

    
370
JParameters SaxonProcessor::createParameterJArray(std::map<std::string, XdmValue *> parameters,
371
                                                  std::map<std::string, std::string> properties) {
372
    JParameters comboArrays;
373
    comboArrays.stringArray = nullptr;
374
    comboArrays.objectArray = nullptr;
375
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
376
                                      "java/lang/Object");
377
    jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
378
                                      "java/lang/String");
379

    
380
    int size = parameters.size() + properties.size();
381
#ifdef DEBUG
382
    std::cerr<<"Properties size: "<<properties.size()<<std::endl;
383
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
384
#endif
385
    if (size > 0) {
386

    
387
        comboArrays.objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
388
                                                                                   objectClass, 0);
389
        comboArrays.stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
390
                                                                                   stringClass, 0);
391
        int i = 0;
392
        for (std::map<std::string, XdmValue *>::iterator iter =
393
                parameters.begin(); iter != parameters.end(); ++iter, i++) {
394

    
395
#ifdef DEBUG
396
            std::cerr<<"map 1"<<std::endl;
397
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
398
#endif
399
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
400
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
401
                                                                            (iter->first).c_str()));
402
#ifdef DEBUG
403
            std::string s1 = typeid(iter->second).name();
404
            std::cerr<<"Type of itr:"<<s1<<std::endl;
405

    
406
            if((iter->second) == nullptr) {std::cerr<<"iter->second is nullptr"<<std::endl;
407
            } else {
408
                std::cerr<<"getting underlying value"<<std::endl;
409
            jobject xx = (iter->second)->getUnderlyingValue();
410

    
411
            if(xx == nullptr) {
412
                std::cerr<<"value failed"<<std::endl;
413
            } else {
414

    
415
                std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
416
            }
417
            if((iter->second)->getUnderlyingValue() == nullptr) {
418
                std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
419
            }}
420
#endif
421

    
422
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
423
                                                                    (iter->second)->getUnderlyingValue());
424

    
425
        }
426

    
427
        for (std::map<std::string, std::string>::iterator iter =
428
                properties.begin(); iter != properties.end(); ++iter, i++) {
429
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
430
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
431
                                                                            (iter->first).c_str()));
432
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
433
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
434
                                                                            (iter->second).c_str()));
435
        }
436

    
437
        return comboArrays;
438

    
439
    } else {
440
        return comboArrays;
441
    }
442
}
443

    
444
JParameters SaxonProcessor::createParameterJArray2(std::map<std::string, XdmValue *> parameters) {
445
    JParameters comboArrays;
446
    comboArrays.stringArray = nullptr;
447
    comboArrays.objectArray = nullptr;
448
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
449
                                      "java/lang/Object");
450
    jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
451
                                      "java/lang/String");
452

    
453
    int size = parameters.size();
454
#ifdef DEBUG
455
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
456
#endif
457
    if (size > 0) {
458

    
459
        comboArrays.objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
460
                                                                                   objectClass, 0);
461
        comboArrays.stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
462
                                                                                   stringClass, 0);
463
        int i = 0;
464
        for (std::map<std::string, XdmValue *>::iterator iter =
465
                parameters.begin(); iter != parameters.end(); ++iter, i++) {
466

    
467
#ifdef DEBUG
468
            std::cerr<<"map 1"<<std::endl;
469
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
470
#endif
471
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
472
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
473
                                                                            (iter->first).c_str()));
474
#ifdef DEBUG
475
            std::string s1 = typeid(iter->second).name();
476
            std::cerr<<"Type of itr:"<<s1<<std::endl;
477

    
478
            if((iter->second) == nullptr) {std::cerr<<"iter->second is nullptr"<<std::endl;
479
            } else {
480
                std::cerr<<"getting underlying value"<<std::endl;
481
            jobject xx = (iter->second)->getUnderlyingValue();
482

    
483
            if(xx == nullptr) {
484
                std::cerr<<"value failed"<<std::endl;
485
            } else {
486

    
487
                std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
488
            }
489
            if((iter->second)->getUnderlyingValue() == nullptr) {
490
                std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
491
            }}
492
#endif
493

    
494
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
495
                                                                    (iter->second)->getUnderlyingValue());
496

    
497
        }
498

    
499

    
500
        return comboArrays;
501

    
502
    } else {
503
        return comboArrays;
504
    }
505
}
506

    
507

    
508
SaxonProcessor &SaxonProcessor::operator=(const SaxonProcessor &other) {
509
    versionClass = other.versionClass;
510
    procClass = other.procClass;
511
    saxonCAPIClass = other.saxonCAPIClass;
512
    cwd = other.cwd;
513
    proc = other.proc;
514
    //SaxonProcessor::sxn_environ= other.environ;
515
    parameters = other.parameters;
516
    configProperties = other.configProperties;
517
    licensei = other.licensei;
518
    exception = other.exception;
519
    return *this;
520
}
521

    
522
SaxonProcessor::SaxonProcessor(const SaxonProcessor &other) {
523
    versionClass = other.versionClass;
524
    procClass = other.procClass;
525
    saxonCAPIClass = other.saxonCAPIClass;
526
    cwd = other.cwd;
527
    proc = other.proc;
528
    //SaxonProcessor::sxn_environ= other.environ;
529
    parameters = other.parameters;
530
    configProperties = other.configProperties;
531
    licensei = other.licensei;
532
    exception = other.exception;
533
}
534

    
535
XsltProcessor *SaxonProcessor::newXsltProcessor() {
536
    return (new XsltProcessor(this, cwd));
537
}
538

    
539
Xslt30Processor *SaxonProcessor::newXslt30Processor() {
540
    return (new Xslt30Processor(this, cwd));
541
}
542

    
543
XQueryProcessor *SaxonProcessor::newXQueryProcessor() {
544
    return (new XQueryProcessor(this, cwd));
545
}
546

    
547
XPathProcessor *SaxonProcessor::newXPathProcessor() {
548
    return (new XPathProcessor(this, cwd));
549
}
550

    
551
SchemaValidator *SaxonProcessor::newSchemaValidator() {
552
    if (licensei) {
553
        return (new SchemaValidator(this, cwd));
554
    } else {
555
        std::cerr << "\nError: Processor is not licensed for schema processing!" << std::endl;
556
        return nullptr;
557
    }
558
}
559

    
560

    
561
const char *SaxonProcessor::version() {
562
    if (versionStr == nullptr) {
563

    
564
        static jmethodID MID_version = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
565
                                                                                                       "getProductVersion",
566
                                                                                                       "(Lnet/sf/saxon/s9api/Processor;)Ljava/lang/String;");
567
        if (!MID_version) {
568
            std::cerr << "\nError: MyClassInDll " << "SaxonCAPI.getProductVersion()" << " not found" << std::endl;
569
            return nullptr;
570
        }
571

    
572
        jstring jstr = (jstring) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, MID_version,
573
                                                                                           proc));
574
        const char *tempVersionStr = SaxonProcessor::sxn_environ->env->GetStringUTFChars(jstr, nullptr);
575
        int verLen = strlen(tempVersionStr) + 22 + strlen(CVERSION) + 1;
576
        versionStr = new char[verLen];
577
        snprintf(versionStr, verLen, "Saxon/C %s %s %s", CVERSION, "running with", tempVersionStr);
578
        delete tempVersionStr;
579

    
580
    }
581

    
582
    return versionStr;
583
}
584

    
585
void SaxonProcessor::setcwd(const char *dir) {
586
    cwd = std::string(dir);
587
}
588

    
589
const char *SaxonProcessor::getcwd() {
590
    return cwd.c_str();
591
}
592

    
593

    
594
void SaxonProcessor::setCatalog(const char *catalogFile, bool isTracing) {
595
    jclass xmlResolverClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/trans/XmlCatalogResolver;");
596
    static jmethodID catalogMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(xmlResolverClass, "setCatalog",
597
                                                                                      "(Ljava/lang/String;Lnet/sf/saxon/Configuration;Z)V");
598

    
599
    if (!catalogMID) {
600
        std::cerr << "\nError: Saxonc." << "setCatalog()" << " not found" << std::endl;
601
        return;
602
    }
603
    if (catalogFile == nullptr) {
604

    
605
        return;
606
    }
607
    static jmethodID configMID = SaxonProcessor::sxn_environ->env->GetMethodID(procClass, "getUnderlyingConfiguration",
608
                                                                               "()Lnet/sf/saxon/Configuration;");
609

    
610
    if (!configMID) {
611
        std::cerr << "\nError: Saxonc." << "getUnderlyingConfiguration()" << " not found" << std::endl;
612
        return;
613
    }
614

    
615

    
616
    if (!proc) {
617
        std::cout << "proc is nullptr in SaxonProcessorsetCatalog" << std::endl;
618
        return;
619
    }
620

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

    
623
    if (!configObj) {
624
        std::cout << "proc is nullptr in SaxonProcessor setcatalog - config obj" << std::endl;
625
        return;
626
    }
627
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(xmlResolverClass, catalogMID,
628
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(catalogFile),
629
                                                           configObj, (jboolean) isTracing);
630
#ifdef DEBUG
631
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
632
#endif
633
}
634

    
635

    
636
XdmNode *SaxonProcessor::parseXmlFromString(const char *source) {
637

    
638
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlString",
639
                                                                                    "(Ljava/lang/String;Lnet/sf/saxon/s9api/Processor;Lnet/sf/saxon/s9api/SchemaValidator;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
640
    if (!mID) {
641
        std::cerr << "\nError: Saxonc." << "parseXmlString()" << " not found" << std::endl;
642
        return nullptr;
643
    }
644
//TODO SchemaValidator
645

    
646
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID,
647
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
648
                                                                                        cwd.c_str()), proc, nullptr,
649
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
650
                                                                                        source));
651
    if (xdmNodei) {
652
        XdmNode *value = new XdmNode(xdmNodei);
653
        value->setProcessor(this);
654
        return value;
655
    } else if (exceptionOccurred()) {
656
        exception = checkAndCreateException(saxonCAPIClass);
657
        exceptionClear();
658
    }
659

    
660
#ifdef DEBUG
661
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
662
#endif
663

    
664
    return nullptr;
665
}
666

    
667
int SaxonProcessor::getNodeKind(jobject obj) {
668
    jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmNode;");
669
    static jmethodID nodeKindMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
670
                                                                                             "getNodeKind",
671
                                                                                             "()Lnet/sf/saxon/s9api/XdmNodeKind;");
672
    if (!nodeKindMID) {
673
        std::cerr << "Error: MyClassInDll." << "getNodeKind" << " not found\n"
674
                  << std::endl;
675
        return 0;
676
    }
677

    
678
    jobject nodeKindObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(obj, nodeKindMID));
679
    if (!nodeKindObj) {
680

    
681
        return 0;
682
    }
683
    jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/option/cpp/XdmUtils;");
684

    
685
    jmethodID mID2 = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,
686
                                                                                     "convertNodeKindType",
687
                                                                                     "(Lnet/sf/saxon/s9api/XdmNodeKind;)I");
688

    
689
    if (!mID2) {
690
        std::cerr << "Error: MyClassInDll." << "convertNodeKindType" << " not found\n"
691
                  << std::endl;
692
        return 0;
693
    }
694
    if (!nodeKindObj) {
695
        return 0;
696
    }
697
    int nodeKind = (int) (SaxonProcessor::sxn_environ->env->CallStaticIntMethod(xdmUtilsClass, mID2, nodeKindObj));
698
    return nodeKind;
699
}
700

    
701

    
702
XdmNode *SaxonProcessor::parseXmlFromFile(const char *source) {
703

    
704
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlFile",
705
                                                                                    "(Lnet/sf/saxon/s9api/Processor;Ljava/lang/String;Lnet/sf/saxon/s9api/SchemaValidator;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
706
    if (!mID) {
707
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFile()" << " not found" << std::endl;
708
        return nullptr;
709
    }
710
//TODO SchemaValidator
711
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID, proc,
712
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
713
                                                                                        cwd.c_str()), nullptr,
714
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
715
                                                                                        source));
716
    if (exceptionOccurred()) {
717
        exception = checkAndCreateException(saxonCAPIClass);
718
        exceptionClear();
719

    
720
    } else {
721

    
722
        XdmNode *value = new XdmNode(xdmNodei);
723
        value->setProcessor(this);
724
        return value;
725
    }
726
    return nullptr;
727
}
728

    
729
XdmNode *SaxonProcessor::parseXmlFromUri(const char *source) {
730

    
731
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlFile",
732
                                                                                    "(Lnet/sf/saxon/s9api/Processor;Ljava/lang/String;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
733
    if (!mID) {
734
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFromUri()" << " not found" << std::endl;
735
        return nullptr;
736
    }
737
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID, proc,
738
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
739
                                                                                        ""),
740
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
741
                                                                                        source));
742
    if (exceptionOccurred()) {
743
        exception = checkAndCreateException(saxonCAPIClass);
744
    } else {
745
        XdmNode *value = new XdmNode(xdmNodei);
746
        value->setProcessor(this);
747
        return value;
748
    }
749
    return nullptr;
750
}
751

    
752

    
753
/**
754
   * Set a configuration property.
755
   *
756
   * @param name of the property
757
   * @param value of the property
758
   */
759
void SaxonProcessor::setConfigurationProperty(const char *name, const char *value) {
760
    if (name != nullptr) {
761
        configProperties.insert(
762
                std::pair<std::string, std::string>(std::string(name), std::string((value == nullptr ? "" : value))));
763
    }
764
}
765

    
766
void SaxonProcessor::clearConfigurationProperties() {
767
    configProperties.clear();
768
}
769

    
770

    
771
void SaxonProcessor::release() {
772
    if (SaxonProcessor::jvmCreatedCPP != 0) {
773
        SaxonProcessor::jvmCreatedCPP = 0;
774
        //std::cerr<<"SaxonProc: JVM finalized calling !"<<std::endl;
775
        finalizeJavaRT(SaxonProcessor::sxn_environ->jvm);
776

    
777
        //delete SaxonProcessor::sxn_environ;
778
        /*clearParameters();
779
        clearProperties();*/
780
    } else {
781
//#ifdef DEBUG
782
        std::cerr << "SaxonProc: JVM finalize not called!" << std::endl;
783
//#endif
784
    }
785
}
786

    
787

    
788
/* ========= Factory method for Xdm ======== */
789

    
790
XdmAtomicValue *SaxonProcessor::makeStringValue(const char *str) {
791
    jobject obj = getJavaStringValue(SaxonProcessor::sxn_environ, str);
792
    static jmethodID mssID_atomic = nullptr;
793
    if (mssID_atomic == nullptr) {
794
        mssID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
795
                                                                                  "(Ljava/lang/String;)V"));
796
    }
797
    if (!mssID_atomic) {
798
        std::cerr << "XdmAtomic constructor (String)" << std::endl;
799
        return nullptr;
800
    }
801
    jobject obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mssID_atomic, obj));
802
    XdmAtomicValue *value = new XdmAtomicValue(obj2, "xs:string");
803
    value->setProcessor(this);
804
    return value;
805
}
806

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

    
824
XdmAtomicValue *SaxonProcessor::makeIntegerValue(int i) {
825
    //jobject obj = integerValue(*SaxonProcessor::sxn_environ, i);
826
    static jmethodID miiID_atomic = nullptr;
827
    if (miiID_atomic == nullptr) {
828
        miiID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
829
    }
830
    if (!miiID_atomic) {
831
        std::cerr << "XdmAtomic constructor (J)" << std::endl;
832
        return nullptr;
833
    }
834
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, miiID_atomic, (jlong) i));
835
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}integer");
836
    value->setProcessor(this);
837
    return value;
838
}
839

    
840
XdmAtomicValue *SaxonProcessor::makeDoubleValue(double d) {
841
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
842
    static jmethodID mdID_atomic = nullptr;
843
    if (mdID_atomic == nullptr) {
844
        mdID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(D)V"));
845
    }
846
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mdID_atomic, (jdouble) d));
847
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}double");
848
    value->setProcessor(this);
849
    return value;
850
}
851

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

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

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

    
888
XdmAtomicValue *SaxonProcessor::makeQNameValue(const char *str) {
889
    jobject val = xdmValueAsObj(SaxonProcessor::sxn_environ, "QName", str);
890
    XdmAtomicValue *value = new XdmAtomicValue(val, "QName");
891
    value->setProcessor(this);
892
    return value;
893
}
894

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

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

    
912
    return result;
913

    
914
}
915

    
916
#if CVERSION_API_NO >= 123
917

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

    
934

    
935
    jshortArray sArray = nullptr;
936

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

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

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

    
960

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

    
977

    
978
    jintArray iArray = nullptr;
979

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

    
987

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

    
1002
}
1003

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

    
1020

    
1021
    jlongArray lArray = nullptr;
1022

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

    
1030

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

    
1046

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

    
1063

    
1064
    jbooleanArray bArray = nullptr;
1065

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

    
1073

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

    
1088

    
1089
}
1090

    
1091

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

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

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

    
1123
    if (exceptionOccurred()) {
1124
        checkAndCreateException(saxonCAPIClass);
1125
    } else {
1126
        XdmArray *value = new XdmArray(xdmArrayi);
1127
        value->setProcessor(this);
1128
        return value;
1129
    }
1130
    return nullptr;
1131
}
1132

    
1133

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

    
1140
    int size = dataMap.size();
1141

    
1142
    if (size > 0) {
1143

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

    
1152

    
1153
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(keyArray, i,
1154
                                                                    (iter->first)->getUnderlyingValue());
1155

    
1156
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i,
1157
                                                                    (iter->second)->getUnderlyingValue());
1158

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

    
1169

    
1170
        jobject results = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
1171
                                                                                              keyArray, valueArray));
1172

    
1173
    }
1174

    
1175

    
1176
}
1177

    
1178
#endif
1179

    
1180

    
(12-12/49)