Project

Profile

Help

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

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

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
const char *SaxonProcessor::getErrorMessage() {
53
    if(exception == nullptr) {
54
        exception =  SaxonProcessor::checkForExceptionCPP(SaxonProcessor::sxn_environ->env,
55
                                                                            saxonCAPIClass,nullptr);
56
    }
57
    if (exception == nullptr) { return nullptr; }
58
    return exception->getMessage();
59
}
60

    
61
void SaxonProcessor::exceptionClear() {
62
    SaxonProcessor::sxn_environ->env->ExceptionClear();
63
    /* if(exception != nullptr && clearCPPException) {
64
        delete exception;
65
    } */
66
}
67

    
68

    
69
SaxonProcessor::SaxonProcessor() {
70
    licensei = false;
71
    SaxonProcessor(false);
72
}
73

    
74
const char * SaxonProcessor::getResourcesDirectory() {
75
    return NULL;
76
}
77

    
78

    
79
SaxonApiException *SaxonProcessor::checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject) {
80

    
81
    if (env->ExceptionCheck()) {
82
        std::string result1 = "";
83
        std::string errorCode = "";
84
        jthrowable exc = env->ExceptionOccurred();
85

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

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

    
98
        jmethodID getMessage(env->GetMethodID(exccls, "getMessage", "()Ljava/lang/String;"));
99
        if (getMessage) {
100

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

    
113
            env->ReleaseStringUTFChars(message, utfMessage);
114

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

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

    
131

    
132
                jobject errCodeQName = (jobject) (env->CallObjectMethod(exc, ecID));
133
                jstring errSystemID = (jstring) (env->CallObjectMethod(exc, esysID));
134
                int linenum = env->CallIntMethod(exc, lineNumID);
135

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

    
142
                jstring qnameStr = (jstring) (env->CallObjectMethod(errCodeQName, qnameStrID));
143

    
144

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

    
152
                if (errCodeQName) {
153
                    env->DeleteLocalRef(errCodeQName);
154
                }
155
                if (errSystemID) {
156
                    env->DeleteLocalRef(errSystemID);
157
                }
158
                if (qnameStr) {
159
                    env->DeleteLocalRef(qnameStr);
160
                }
161

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

    
176
}
177

    
178

    
179
SaxonProcessor::SaxonProcessor(bool l) {
180

    
181
    cwd = "";
182
    licensei = l;
183
    versionStr = nullptr;
184

    
185
    if (SaxonProcessor::jvmCreatedCPP == 0) {
186
        SaxonProcessor::jvmCreatedCPP = 1;
187
        SaxonProcessor::sxn_environ = new sxnc_environment;//(sxnc_environment *)malloc(sizeof(sxnc_environment));
188

    
189

    
190
        /*
191
         * First of all, load required component.
192
         * By the time of JET initialization, all components should be loaded.
193
         */
194

    
195
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
196

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

    
207
    }
208

    
209

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

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

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

    
227
SaxonProcessor::SaxonProcessor(const char *configFile) {
228
    cwd = "";
229
    versionStr = nullptr;
230

    
231
    if (SaxonProcessor::jvmCreatedCPP == 0) {
232
        SaxonProcessor::jvmCreatedCPP = 1;
233
        //SaxonProcessor::sxn_environ= new sxnc_environment;
234
        SaxonProcessor::sxn_environ = (sxnc_environment *) malloc(sizeof(sxnc_environment));
235

    
236
        /*
237
         * First of all, load required component.
238
         * By the time of JET initialization, all components should be loaded.
239
         */
240

    
241
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
242

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

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

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

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

    
273
    licensei = true;
274
#ifdef DEBUG
275

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

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

    
289

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

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

    
304
    }
305

    
306
}
307

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

    
344
    }
345
}
346

    
347

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

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

    
354

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

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

    
363

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

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

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

    
380
}
381

    
382

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

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

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

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

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

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

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

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

    
438
        }
439

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

    
450
        return comboArrays;
451

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

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

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

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

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

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

    
496
            if(xx == nullptr) {
497
                std::cerr<<"value failed"<<std::endl;
498
            } else {
499

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

    
507
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
508
                                                                    (iter->second)->getUnderlyingValue());
509

    
510
        }
511

    
512

    
513
        return comboArrays;
514

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

    
520

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

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

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

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

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

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

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

    
573

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

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

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

    
593
    }
594

    
595
    return versionStr;
596
}
597

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

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

    
608

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

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

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

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

    
630

    
631
    if (!proc) {
632
        std::cout << "proc is null in SaxonProcessor.setCatalog" << std::endl;
633
        return;
634
    }
635

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

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

    
650

    
651
XdmNode *SaxonProcessor::parseXmlFromString(const char *source) {
652

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

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

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

    
678
    return nullptr;
679
}
680

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

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

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

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

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

    
715

    
716
XdmNode *SaxonProcessor::parseXmlFromFile(const char *source) {
717

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

    
734
    } else {
735

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

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

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

    
764

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

    
778
void SaxonProcessor::clearConfigurationProperties() {
779
    configProperties.clear();
780
}
781

    
782

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

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

    
799

    
800
/* ========= Factory method for Xdm ======== */
801

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

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

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

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

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

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

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

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

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

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

    
915
    return result;
916

    
917
}
918

    
919
#if CVERSION_API_NO >= 123
920

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

    
937

    
938
    jshortArray sArray = nullptr;
939

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

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

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

    
962

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

    
979

    
980
    jintArray iArray = nullptr;
981

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

    
989

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

    
1003
}
1004

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

    
1021

    
1022
    jlongArray lArray = nullptr;
1023

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

    
1031

    
1032
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiID, lArray);
1033
    if (!xdmArrayi) {
1034
        std::cerr << "Error found when converting string to XdmArray";
1035
        return nullptr;
1036
    }
1037
    if (exceptionOccurred()) {
1038
        exception = checkAndCreateException(saxonCAPIClass);
1039
    } else {
1040
        XdmArray *value = new XdmArray(xdmArrayi);
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
        return value;
1084
    }
1085
    return nullptr;
1086

    
1087

    
1088
}
1089

    
1090

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

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

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

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

    
1131

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

    
1138
    int size = dataMap.size();
1139

    
1140
    if (size > 0) {
1141

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

    
1150

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

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

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

    
1167

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

    
1177
    return nullptr;
1178
}
1179

    
1180
#endif
1181

    
1182

    
(13-13/55)