Project

Profile

Help

Download (46.9 KB) Statistics
| Branch: | Revision:

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

1 ead48a5d O'Neil Delpratt
#ifndef __linux__
2 72bf04c6 Norman Walsh
#ifndef __APPLE__
3 ead48a5d O'Neil Delpratt
//#include "stdafx.h"
4
#include <Tchar.h>
5 72bf04c6 Norman Walsh
#endif
6
#endif
7
8
#include "SaxonProcessor.h"
9
#include "XdmValue.h"
10
#include "XdmItem.h"
11
#include "XdmNode.h"
12 ead48a5d O'Neil Delpratt
#include "XdmFunctionItem.h"
13
#include "XdmMap.h"
14
#include "XdmArray.h"
15 72bf04c6 Norman Walsh
#include "XdmAtomicValue.h"
16
17 766106fe O'Neil Delpratt
//#define DEBUG
18 72bf04c6 Norman Walsh
#ifdef DEBUG
19
#include <signal.h>
20
#endif
21 ead48a5d O'Neil Delpratt
22 72bf04c6 Norman Walsh
#include <stdio.h>
23
24
25
//jobject cpp;
26 ead48a5d O'Neil Delpratt
const char *failure;
27
sxnc_environment *SaxonProcessor::sxn_environ = 0;
28
int SaxonProcessor::jvmCreatedCPP = 0;
29 72bf04c6 Norman Walsh
30 ead48a5d O'Neil Delpratt
bool SaxonProcessor::exceptionOccurred() {
31 00082f6f O'Neil Delpratt
    return SaxonProcessor::sxn_environ->env->ExceptionCheck() || exception != nullptr;
32 72bf04c6 Norman Walsh
}
33
34 ead48a5d O'Neil Delpratt
const char *SaxonProcessor::checkException(jobject cpp) {
35
    const char *message = nullptr;
36
    message = checkForException(sxn_environ, cpp);
37
    return message;
38
}
39 72bf04c6 Norman Walsh
40 ead48a5d O'Neil Delpratt
SaxonApiException *SaxonProcessor::checkAndCreateException(jclass cppClass) {
41 00082f6f O'Neil Delpratt
    if (SaxonProcessor::sxn_environ->env->ExceptionCheck()) {
42 ead48a5d O'Neil Delpratt
        SaxonApiException *exception = checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass, nullptr);
43 72bf04c6 Norman Walsh
#ifdef DEBUG
44 ead48a5d O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->ExceptionDescribe();
45 72bf04c6 Norman Walsh
#endif
46 ead48a5d O'Neil Delpratt
        return exception;
47
    }
48
    return nullptr;
49 72bf04c6 Norman Walsh
}
50
51 8d5b64f3 O'Neil Delpratt
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 ead48a5d O'Neil Delpratt
void SaxonProcessor::exceptionClear() {
61
    SaxonProcessor::sxn_environ->env->ExceptionClear();
62 00082f6f O'Neil Delpratt
63 ead48a5d O'Neil Delpratt
}
64 72bf04c6 Norman Walsh
65
66
SaxonProcessor::SaxonProcessor() {
67
    licensei = false;
68
    SaxonProcessor(false);
69
}
70
71 766106fe O'Neil Delpratt
const char * SaxonProcessor::getResourcesDirectory() {
72
    return NULL;
73
}
74
75 72bf04c6 Norman Walsh
76 ead48a5d O'Neil Delpratt
SaxonApiException *SaxonProcessor::checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject) {
77 72bf04c6 Norman Walsh
78
    if (env->ExceptionCheck()) {
79 ead48a5d O'Neil Delpratt
        std::string result1 = "";
80
        std::string errorCode = "";
81
        jthrowable exc = env->ExceptionOccurred();
82 72bf04c6 Norman Walsh
83 ead48a5d O'Neil Delpratt
#ifdef DEBUG
84
        env->ExceptionDescribe();
85 72bf04c6 Norman Walsh
#endif
86 ead48a5d O'Neil Delpratt
        jclass exccls(env->GetObjectClass(exc));
87 72bf04c6 Norman Walsh
        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 ead48a5d O'Neil Delpratt
        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 72bf04c6 Norman Walsh
            }
109
110 ead48a5d O'Neil Delpratt
            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 72bf04c6 Norman Walsh
173
}
174
175
176 ead48a5d O'Neil Delpratt
SaxonProcessor::SaxonProcessor(bool l) {
177 72bf04c6 Norman Walsh
178 ead48a5d O'Neil Delpratt
    cwd = "";
179 72bf04c6 Norman Walsh
    licensei = l;
180 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
186
187 ead48a5d O'Neil Delpratt
        /*
188
         * First of all, load required component.
189
         * By the time of JET initialization, all components should be loaded.
190
         */
191 72bf04c6 Norman Walsh
192 ead48a5d O'Neil Delpratt
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
193 72bf04c6 Norman Walsh
194 ead48a5d O'Neil Delpratt
        /*
195
         * Initialize JET run-time.
196
         * The handle of loaded component is used to retrieve Invocation API.
197
         */
198
        initDefaultJavaRT(SaxonProcessor::sxn_environ);
199 72bf04c6 Norman Walsh
    } else {
200
#ifdef DEBUG
201 ead48a5d O'Neil Delpratt
        std::cerr<<"SaxonProc constructor: jvm exists! jvmCreatedCPP="<<jvmCreatedCPP<<std::endl;
202 72bf04c6 Norman Walsh
#endif
203
204 ead48a5d O'Neil Delpratt
    }
205
206 72bf04c6 Norman Walsh
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 ead48a5d O'Neil Delpratt
211
    jobject proci = createSaxonProcessor(SaxonProcessor::sxn_environ->env, procClass, "(Z)V", nullptr, licensei);
212 61decfd8 Norman Walsh
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(proci);
213 ead48a5d O'Neil Delpratt
    if (!proc) {
214
        std::cout << "proc is nullptr in SaxonProcessor constructor" << std::endl;
215
    }
216 72bf04c6 Norman Walsh
217
    xdmAtomicClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
218
#ifdef DEBUG
219 ead48a5d O'Neil Delpratt
    jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "setDebugMode", "(Z)V");
220
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(saxonCAPIClass, debugMID, (jboolean)true);
221 72bf04c6 Norman Walsh
#endif
222
}
223
224 ead48a5d O'Neil Delpratt
SaxonProcessor::SaxonProcessor(const char *configFile) {
225
    cwd = "";
226
    versionStr = nullptr;
227 72bf04c6 Norman Walsh
228 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
233 ead48a5d O'Neil Delpratt
        /*
234
         * First of all, load required component.
235
         * By the time of JET initialization, all components should be loaded.
236
         */
237 72bf04c6 Norman Walsh
238 ead48a5d O'Neil Delpratt
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
239 72bf04c6 Norman Walsh
240 ead48a5d O'Neil Delpratt
        /*
241
         * Initialize JET run-time.
242
         * The handle of loaded component is used to retrieve Invocation API.
243
         */
244
        initDefaultJavaRT(SaxonProcessor::sxn_environ);
245 72bf04c6 Norman Walsh
    }
246 ead48a5d O'Neil Delpratt
247 72bf04c6 Norman Walsh
    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 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
#ifdef DEBUG
272
273 ead48a5d O'Neil Delpratt
    std::cerr<<"SaxonProc constructor(configFile)"<<std::endl;
274 72bf04c6 Norman Walsh
#endif
275
    xdmAtomicClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
276
}
277
278 ead48a5d O'Neil Delpratt
SaxonProcessor::~SaxonProcessor() {
279
    clearConfigurationProperties();
280
    SaxonProcessor::sxn_environ->env->DeleteGlobalRef(proc);
281
    if (versionStr != nullptr) {
282
        delete versionStr;
283
    }
284
}
285 72bf04c6 Norman Walsh
286
287 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
298 ead48a5d O'Neil Delpratt
        licensei = (jboolean) (SaxonProcessor::sxn_environ->env->CallBooleanMethod(proc, MID_schema));
299
        return licensei;
300 72bf04c6 Norman Walsh
301 ead48a5d O'Neil Delpratt
    }
302 72bf04c6 Norman Walsh
303
}
304
305 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
}
343
344
345 ead48a5d O'Neil Delpratt
jobjectArray SaxonProcessor::createJArray(XdmValue **values, int length) {
346
    jobjectArray valueArray = nullptr;
347 72bf04c6 Norman Walsh
348
    jclass xdmValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
349 ead48a5d O'Neil Delpratt
                                        "net/sf/saxon/s9api/XdmValue");
350 72bf04c6 Norman Walsh
351
352
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length,
353 ead48a5d O'Neil Delpratt
                                                                  xdmValueClass, 0);
354 72bf04c6 Norman Walsh
355 ead48a5d O'Neil Delpratt
    for (int i = 0; i < length; i++) {
356 72bf04c6 Norman Walsh
#ifdef DEBUG
357 ead48a5d O'Neil Delpratt
        std::string s1 = typeid(values[i]).name();
358
        std::cerr<<"In createJArray\nType of itr:"<<s1<<std::endl;
359 72bf04c6 Norman Walsh
360
361 ead48a5d O'Neil Delpratt
        jobject xx = values[i]->getUnderlyingValue();
362 72bf04c6 Norman Walsh
363 ead48a5d O'Neil Delpratt
        if(xx == nullptr) {
364
            std::cerr<<"value failed"<<std::endl;
365
        } else {
366 72bf04c6 Norman Walsh
367 ead48a5d O'Neil Delpratt
            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 72bf04c6 Norman Walsh
#endif
373 ead48a5d O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, values[i]->getUnderlyingValue());
374 72bf04c6 Norman Walsh
    }
375
    return valueArray;
376
377 ead48a5d O'Neil Delpratt
}
378 72bf04c6 Norman Walsh
379
380 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
390 ead48a5d O'Neil Delpratt
    int size = parameters.size() + properties.size();
391 72bf04c6 Norman Walsh
#ifdef DEBUG
392 ead48a5d O'Neil Delpratt
    std::cerr<<"Properties size: "<<properties.size()<<std::endl;
393
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
394 72bf04c6 Norman Walsh
#endif
395 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
405
#ifdef DEBUG
406 ead48a5d O'Neil Delpratt
            std::cerr<<"map 1"<<std::endl;
407
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
408 72bf04c6 Norman Walsh
#endif
409 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
410
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
411
                                                                            (iter->first).c_str()));
412 72bf04c6 Norman Walsh
#ifdef DEBUG
413 ead48a5d O'Neil Delpratt
            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 72bf04c6 Norman Walsh
#endif
431
432 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
433
                                                                    (iter->second)->getUnderlyingValue());
434 72bf04c6 Norman Walsh
435 ead48a5d O'Neil Delpratt
        }
436 72bf04c6 Norman Walsh
437 ead48a5d O'Neil Delpratt
        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 72bf04c6 Norman Walsh
447 ead48a5d O'Neil Delpratt
        return comboArrays;
448 72bf04c6 Norman Walsh
449 ead48a5d O'Neil Delpratt
    } else {
450
        return comboArrays;
451 72bf04c6 Norman Walsh
    }
452 ead48a5d O'Neil Delpratt
}
453 72bf04c6 Norman Walsh
454 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
463 ead48a5d O'Neil Delpratt
    int size = parameters.size();
464 72bf04c6 Norman Walsh
#ifdef DEBUG
465 ead48a5d O'Neil Delpratt
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
466 72bf04c6 Norman Walsh
#endif
467 ead48a5d O'Neil Delpratt
    if (size > 0) {
468 72bf04c6 Norman Walsh
469 ead48a5d O'Neil Delpratt
        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 72bf04c6 Norman Walsh
477
#ifdef DEBUG
478 ead48a5d O'Neil Delpratt
            std::cerr<<"map 1"<<std::endl;
479
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
480 72bf04c6 Norman Walsh
#endif
481 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
482
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
483
                                                                            (iter->first).c_str()));
484 72bf04c6 Norman Walsh
#ifdef DEBUG
485 ead48a5d O'Neil Delpratt
            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 72bf04c6 Norman Walsh
#endif
503
504 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
505
                                                                    (iter->second)->getUnderlyingValue());
506 72bf04c6 Norman Walsh
507 ead48a5d O'Neil Delpratt
        }
508 72bf04c6 Norman Walsh
509
510 ead48a5d O'Neil Delpratt
        return comboArrays;
511 72bf04c6 Norman Walsh
512 ead48a5d O'Neil Delpratt
    } else {
513
        return comboArrays;
514 72bf04c6 Norman Walsh
    }
515 ead48a5d O'Neil Delpratt
}
516 72bf04c6 Norman Walsh
517
518 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
}
531
532
SaxonProcessor::SaxonProcessor(const SaxonProcessor &other) {
533 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
}
544
545 03fcd3f9 O'Neil Delpratt
546
547
void SaxonProcessor::setResourcesDirectory(const char *dir){}
548
549 ead48a5d O'Neil Delpratt
XsltProcessor *SaxonProcessor::newXsltProcessor() {
550 72bf04c6 Norman Walsh
    return (new XsltProcessor(this, cwd));
551
}
552
553 ead48a5d O'Neil Delpratt
Xslt30Processor *SaxonProcessor::newXslt30Processor() {
554 72bf04c6 Norman Walsh
    return (new Xslt30Processor(this, cwd));
555
}
556
557 ead48a5d O'Neil Delpratt
XQueryProcessor *SaxonProcessor::newXQueryProcessor() {
558
    return (new XQueryProcessor(this, cwd));
559 72bf04c6 Norman Walsh
}
560
561 ead48a5d O'Neil Delpratt
XPathProcessor *SaxonProcessor::newXPathProcessor() {
562 72bf04c6 Norman Walsh
    return (new XPathProcessor(this, cwd));
563
}
564
565 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
}
573
574
575 ead48a5d O'Neil Delpratt
const char *SaxonProcessor::version() {
576
    if (versionStr == nullptr) {
577 72bf04c6 Norman Walsh
578 ead48a5d O'Neil Delpratt
        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 72bf04c6 Norman Walsh
586 ead48a5d O'Neil Delpratt
        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 72bf04c6 Norman Walsh
594
    }
595
596
    return versionStr;
597
}
598
599 ead48a5d O'Neil Delpratt
void SaxonProcessor::setcwd(const char *dir) {
600 6b6a9706 O'Neil Delpratt
    if(dir != nullptr) {
601
        cwd = std::string(dir);
602
    }
603 72bf04c6 Norman Walsh
}
604
605 ead48a5d O'Neil Delpratt
const char *SaxonProcessor::getcwd() {
606
    return cwd.c_str();
607 72bf04c6 Norman Walsh
}
608
609
610 ead48a5d O'Neil Delpratt
void SaxonProcessor::setCatalog(const char *catalogFile, bool isTracing) {
611 9458d238 O'Neil Delpratt
    jclass xmlResolverClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/trans/XmlCatalogResolver");
612 ead48a5d O'Neil Delpratt
    static jmethodID catalogMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(xmlResolverClass, "setCatalog",
613
                                                                                      "(Ljava/lang/String;Lnet/sf/saxon/Configuration;Z)V");
614 72bf04c6 Norman Walsh
615 ead48a5d O'Neil Delpratt
    if (!catalogMID) {
616
        std::cerr << "\nError: Saxonc." << "setCatalog()" << " not found" << std::endl;
617
        return;
618
    }
619
    if (catalogFile == nullptr) {
620 72bf04c6 Norman Walsh
621
        return;
622 ead48a5d O'Neil Delpratt
    }
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 72bf04c6 Norman Walsh
        return;
629 ead48a5d O'Neil Delpratt
    }
630
631 72bf04c6 Norman Walsh
632 ead48a5d O'Neil Delpratt
    if (!proc) {
633 00082f6f O'Neil Delpratt
        exception = new SaxonApiException("Processor is null in SaxonProcessor.setCatalog");
634 ead48a5d O'Neil Delpratt
        return;
635
    }
636 72bf04c6 Norman Walsh
637 ead48a5d O'Neil Delpratt
    jobject configObj = SaxonProcessor::sxn_environ->env->CallObjectMethod(proc, configMID);
638 72bf04c6 Norman Walsh
639 ead48a5d O'Neil Delpratt
    if (!configObj) {
640 9458d238 O'Neil Delpratt
        std::cout << "proc is null in SaxonProcessor setcatalog - config obj" << std::endl;
641 ead48a5d O'Neil Delpratt
        return;
642
    }
643
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(xmlResolverClass, catalogMID,
644
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(catalogFile),
645
                                                           configObj, (jboolean) isTracing);
646 72bf04c6 Norman Walsh
#ifdef DEBUG
647 ead48a5d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
648 72bf04c6 Norman Walsh
#endif
649 00082f6f O'Neil Delpratt
650 72bf04c6 Norman Walsh
}
651
652
653 ead48a5d O'Neil Delpratt
XdmNode *SaxonProcessor::parseXmlFromString(const char *source) {
654 72bf04c6 Norman Walsh
655 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
    if (!mID) {
658 ead48a5d O'Neil Delpratt
        std::cerr << "\nError: Saxonc." << "parseXmlString()" << " not found" << std::endl;
659
        return nullptr;
660 72bf04c6 Norman Walsh
    }
661
//TODO SchemaValidator
662
663 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
#ifdef DEBUG
677 ead48a5d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
678 72bf04c6 Norman Walsh
#endif
679
680 ead48a5d O'Neil Delpratt
    return nullptr;
681 72bf04c6 Norman Walsh
}
682
683 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
697 ead48a5d O'Neil Delpratt
        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 72bf04c6 Norman Walsh
717
718 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
    if (!mID) {
723 ead48a5d O'Neil Delpratt
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFile()" << " not found" << std::endl;
724
        return nullptr;
725 72bf04c6 Norman Walsh
    }
726
//TODO SchemaValidator
727 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
}
743
744 ead48a5d O'Neil Delpratt
XdmNode *SaxonProcessor::parseXmlFromUri(const char *source) {
745 72bf04c6 Norman Walsh
746 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
    if (!mID) {
749 ead48a5d O'Neil Delpratt
        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 72bf04c6 Norman Walsh
}
765
766
767 ead48a5d O'Neil Delpratt
/**
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 72bf04c6 Norman Walsh
    }
778 ead48a5d O'Neil Delpratt
}
779 72bf04c6 Norman Walsh
780 ead48a5d O'Neil Delpratt
void SaxonProcessor::clearConfigurationProperties() {
781
    configProperties.clear();
782
}
783 72bf04c6 Norman Walsh
784
785 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
791 ead48a5d O'Neil Delpratt
        //delete SaxonProcessor::sxn_environ;
792
        /*clearParameters();
793
        clearProperties();*/
794
    } else {
795 72bf04c6 Norman Walsh
//#ifdef DEBUG
796 ead48a5d O'Neil Delpratt
        std::cerr << "SaxonProc: JVM finalize not called!" << std::endl;
797 72bf04c6 Norman Walsh
//#endif
798 ead48a5d O'Neil Delpratt
    }
799 72bf04c6 Norman Walsh
}
800
801
802 ead48a5d O'Neil Delpratt
/* ========= Factory method for Xdm ======== */
803 72bf04c6 Norman Walsh
804 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
820 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
836 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
    }
857 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
862 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
    }
868 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
873 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
    }
879 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
884 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
    }
890 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
901 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
#ifdef DEBUG
910 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
#endif
916
917 ead48a5d O'Neil Delpratt
    return result;
918
919
}
920 72bf04c6 Norman Walsh
921
#if CVERSION_API_NO >= 123
922
923 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
939
940 ead48a5d O'Neil Delpratt
    jshortArray sArray = nullptr;
941 72bf04c6 Norman Walsh
942 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
949 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
955 ead48a5d O'Neil Delpratt
    if (exceptionOccurred()) {
956
        exception = checkAndCreateException(saxonCAPIClass);
957
    } else {
958
        XdmArray *value = new XdmArray(xdmArrayi);
959
        return value;
960
    }
961
    return nullptr;
962
}
963 72bf04c6 Norman Walsh
964
965 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
        }
1113 ead48a5d O'Neil Delpratt
        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 72bf04c6 Norman Walsh
1118 ead48a5d O'Neil Delpratt
    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 72bf04c6 Norman Walsh
1124 ead48a5d O'Neil Delpratt
    if (exceptionOccurred()) {
1125
        checkAndCreateException(saxonCAPIClass);
1126
    } else {
1127
        XdmArray *value = new XdmArray(xdmArrayi);
1128
        return value;
1129
    }
1130
    return nullptr;
1131
}
1132 72bf04c6 Norman Walsh
1133
1134 901b6eca O'Neil Delpratt
XdmMap *SaxonProcessor::makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap) {
1135 ead48a5d O'Neil Delpratt
    jobjectArray keyArray = nullptr;
1136
    jobjectArray valueArray = nullptr;
1137
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
1138
                                      "java/lang/Object");
1139 72bf04c6 Norman Walsh
1140 ead48a5d O'Neil Delpratt
    int size = dataMap.size();
1141 72bf04c6 Norman Walsh
1142 ead48a5d O'Neil Delpratt
    if (size > 0) {
1143 72bf04c6 Norman Walsh
1144 ead48a5d O'Neil Delpratt
        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 72bf04c6 Norman Walsh
1152
1153 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(keyArray, i,
1154
                                                                    (iter->first)->getUnderlyingValue());
1155 72bf04c6 Norman Walsh
1156 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i,
1157
                                                                    (iter->second)->getUnderlyingValue());
1158 72bf04c6 Norman Walsh
1159 ead48a5d O'Neil Delpratt
        }
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 72bf04c6 Norman Walsh
1169
1170 ead48a5d O'Neil Delpratt
        jobject results = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
1171
                                                                                              keyArray, valueArray));
1172 901b6eca O'Neil Delpratt
        if (exceptionOccurred() || !results) {
1173
            checkAndCreateException(saxonCAPIClass);
1174
        } else {
1175
            return new XdmMap(results);
1176
        }
1177 ead48a5d O'Neil Delpratt
    }
1178 72bf04c6 Norman Walsh
1179 901b6eca O'Neil Delpratt
    return nullptr;
1180 ead48a5d O'Neil Delpratt
}
1181 72bf04c6 Norman Walsh
1182
#endif
1183