Project

Profile

Help

Maintenance. Planio will be undergoing a scheduled maintenance this weekend. Between Saturday, July 24 at 9:00 UTC and Sunday, July 25, 22:00 UTC your account might observe occasional downtimes which may last up to several minutes in some cases.

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

he / src / main / c / Saxon.C.API / SaxonProcessor.cpp @ 03fcd3f9

1
#ifndef __linux__
2
#ifndef __APPLE__
3
//#include "stdafx.h"
4
#include <Tchar.h>
5
#endif
6
#endif
7

    
8
#include "SaxonProcessor.h"
9
#include "XdmValue.h"
10
#include "XdmItem.h"
11
#include "XdmNode.h"
12
#include "XdmFunctionItem.h"
13
#include "XdmMap.h"
14
#include "XdmArray.h"
15
#include "XdmAtomicValue.h"
16

    
17
//#define DEBUG
18
#ifdef DEBUG
19
#include <signal.h>
20
#endif
21

    
22
#include <stdio.h>
23

    
24

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

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

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

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

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

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

    
63
}
64

    
65

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

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

    
75

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

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

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

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

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

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

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

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

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

    
128

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

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

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

    
141

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

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

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

    
173
}
174

    
175

    
176
SaxonProcessor::SaxonProcessor(bool l) {
177

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

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

    
186

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

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

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

    
204
    }
205

    
206

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

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

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

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

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

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

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

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

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

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

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

    
270
    licensei = true;
271
#ifdef DEBUG
272

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

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

    
286

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

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

    
301
    }
302

    
303
}
304

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

    
341
    }
342
}
343

    
344

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

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

    
351

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

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

    
360

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

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

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

    
377
}
378

    
379

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

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

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

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

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

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

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

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

    
435
        }
436

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

    
447
        return comboArrays;
448

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

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

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

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

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

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

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

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

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

    
507
        }
508

    
509

    
510
        return comboArrays;
511

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

    
517

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

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

    
545

    
546

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

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

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

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

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

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

    
574

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

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

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

    
594
    }
595

    
596
    return versionStr;
597
}
598

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

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

    
609

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

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

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

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

    
631

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

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

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

    
650
}
651

    
652

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

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

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

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

    
680
    return nullptr;
681
}
682

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

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

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

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

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

    
717

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

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

    
736
    } else {
737

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

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

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

    
766

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

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

    
784

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

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

    
801

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

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

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

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

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

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

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

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

    
895
XdmAtomicValue *SaxonProcessor::makeQNameValue(const char *str) {
896
    jobject val = xdmValueAsObj(SaxonProcessor::sxn_environ, "QName", str);
897
    XdmAtomicValue *value = new XdmAtomicValue(val, "QName");
898
    return value;
899
}
900

    
901
XdmAtomicValue *SaxonProcessor::makeAtomicValue(const char *typei, const char *strValue) {
902
    jobject obj = xdmValueAsObj(SaxonProcessor::sxn_environ, typei, strValue);
903
    XdmAtomicValue *value = new XdmAtomicValue(obj, typei);
904
    return value;
905
}
906

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

    
917
    return result;
918

    
919
}
920

    
921
#if CVERSION_API_NO >= 123
922

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

    
939

    
940
    jshortArray sArray = nullptr;
941

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

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

    
955
    if (exceptionOccurred()) {
956
        exception = checkAndCreateException(saxonCAPIClass);
957
    } else {
958
        XdmArray *value = new XdmArray(xdmArrayi);
959
        return value;
960
    }
961
    return nullptr;
962
}
963

    
964

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

    
981

    
982
    jintArray iArray = nullptr;
983

    
984
    iArray = SaxonProcessor::sxn_environ->env->NewIntArray((jint) length);
985
    jint fill[length];
986
    for (int i = 0; i < length; i++) {
987
        fill[i] = input[i];
988
    }
989
    SaxonProcessor::sxn_environ->env->SetIntArrayRegion(iArray, 0, length, fill);
990

    
991

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

    
1005
}
1006

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

    
1023

    
1024
    jlongArray lArray = nullptr;
1025

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

    
1033

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

    
1048

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

    
1065

    
1066
    jbooleanArray bArray = nullptr;
1067

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

    
1075

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

    
1089

    
1090
}
1091

    
1092

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

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

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

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

    
1133

    
1134
XdmMap *SaxonProcessor::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
        if (exceptionOccurred() || !results) {
1173
            checkAndCreateException(saxonCAPIClass);
1174
        } else {
1175
            return new XdmMap(results);
1176
        }
1177
    }
1178

    
1179
    return nullptr;
1180
}
1181

    
1182
#endif
1183

    
1184

    
(13-13/55)