Project

Profile

Help

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

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

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 4ee4355d O'Neil Delpratt
const char *SaxonProcessor::checkException() {
35 ead48a5d O'Neil Delpratt
    const char *message = nullptr;
36 4ee4355d O'Neil Delpratt
    message = checkForException(sxn_environ);
37 ead48a5d O'Neil Delpratt
    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 85a9912e Dave Delpratt
    if(proc) {
281
            SaxonProcessor::sxn_environ->env->DeleteGlobalRef(proc);
282
    }
283 ead48a5d O'Neil Delpratt
    if (versionStr != nullptr) {
284
        delete versionStr;
285
    }
286
}
287 72bf04c6 Norman Walsh
288
289 ead48a5d O'Neil Delpratt
bool SaxonProcessor::isSchemaAwareProcessor() {
290
    if (!licensei) {
291
        return false;
292
    } else {
293
        static jmethodID MID_schema = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(procClass,
294
                                                                                                "isSchemaAware", "()Z");
295
        if (!MID_schema) {
296
            std::cerr << "\nError: Saxonc " << "SaxonProcessor.isSchemaAware()" << " not found" << std::endl;
297
            return false;
298
        }
299 72bf04c6 Norman Walsh
300 ead48a5d O'Neil Delpratt
        licensei = (jboolean) (SaxonProcessor::sxn_environ->env->CallBooleanMethod(proc, MID_schema));
301
        return licensei;
302 72bf04c6 Norman Walsh
303 ead48a5d O'Neil Delpratt
    }
304 72bf04c6 Norman Walsh
305
}
306
307 ead48a5d O'Neil Delpratt
void SaxonProcessor::applyConfigurationProperties() {
308
    if (configProperties.size() > 0) {
309
        int size = configProperties.size();
310
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
311
        jobjectArray stringArray1 = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size, stringClass, 0);
312
        jobjectArray stringArray2 = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size, stringClass, 0);
313
        static jmethodID mIDappConfig = nullptr;
314
        if (mIDappConfig == nullptr) {
315
            mIDappConfig = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
316
                                                                                           "applyToConfiguration",
317
                                                                                           "(Lnet/sf/saxon/s9api/Processor;[Ljava/lang/String;[Ljava/lang/String;)V");
318
            if (!mIDappConfig) {
319
                std::cerr << "Error: SaxonDll." << "applyToConfiguration"
320
                          << " not found\n" << std::endl;
321
                return;
322
            }
323
        }
324
        int i = 0;
325
        std::map<std::string, std::string>::iterator iter = configProperties.begin();
326
        for (iter = configProperties.begin(); iter != configProperties.end(); ++iter, i++) {
327
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray1, i,
328
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
329
                                                                            (iter->first).c_str()));
330
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray2, i,
331
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
332
                                                                            (iter->second).c_str()));
333
        }
334
        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mIDappConfig, proc, stringArray1,
335
                                                                 stringArray2);
336
        if (exceptionOccurred()) {
337
            exception = checkAndCreateException(saxonCAPIClass);
338
            exceptionClear();
339
        }
340
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray1);
341
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray2);
342
343
    }
344 72bf04c6 Norman Walsh
}
345
346
347 ead48a5d O'Neil Delpratt
jobjectArray SaxonProcessor::createJArray(XdmValue **values, int length) {
348
    jobjectArray valueArray = nullptr;
349 72bf04c6 Norman Walsh
350
    jclass xdmValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
351 ead48a5d O'Neil Delpratt
                                        "net/sf/saxon/s9api/XdmValue");
352 72bf04c6 Norman Walsh
353
354
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length,
355 ead48a5d O'Neil Delpratt
                                                                  xdmValueClass, 0);
356 72bf04c6 Norman Walsh
357 ead48a5d O'Neil Delpratt
    for (int i = 0; i < length; i++) {
358 72bf04c6 Norman Walsh
#ifdef DEBUG
359 ead48a5d O'Neil Delpratt
        std::string s1 = typeid(values[i]).name();
360
        std::cerr<<"In createJArray\nType of itr:"<<s1<<std::endl;
361 72bf04c6 Norman Walsh
362
363 ead48a5d O'Neil Delpratt
        jobject xx = values[i]->getUnderlyingValue();
364 72bf04c6 Norman Walsh
365 ead48a5d O'Neil Delpratt
        if(xx == nullptr) {
366
            std::cerr<<"value failed"<<std::endl;
367
        } else {
368 72bf04c6 Norman Walsh
369 ead48a5d O'Neil Delpratt
            std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
370
        }
371
        if(values[i]->getUnderlyingValue() == nullptr) {
372
            std::cerr<<"value["<<i<<"]->getUnderlyingValue() is nullptr"<<std::endl;
373
        }
374 72bf04c6 Norman Walsh
#endif
375 ead48a5d O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, values[i]->getUnderlyingValue());
376 72bf04c6 Norman Walsh
    }
377
    return valueArray;
378
379 ead48a5d O'Neil Delpratt
}
380 72bf04c6 Norman Walsh
381
382 ead48a5d O'Neil Delpratt
JParameters SaxonProcessor::createParameterJArray(std::map<std::string, XdmValue *> parameters,
383
                                                  std::map<std::string, std::string> properties) {
384
    JParameters comboArrays;
385
    comboArrays.stringArray = nullptr;
386
    comboArrays.objectArray = nullptr;
387
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
388
                                      "java/lang/Object");
389
    jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
390
                                      "java/lang/String");
391 72bf04c6 Norman Walsh
392 ead48a5d O'Neil Delpratt
    int size = parameters.size() + properties.size();
393 72bf04c6 Norman Walsh
#ifdef DEBUG
394 ead48a5d O'Neil Delpratt
    std::cerr<<"Properties size: "<<properties.size()<<std::endl;
395
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
396 72bf04c6 Norman Walsh
#endif
397 ead48a5d O'Neil Delpratt
    if (size > 0) {
398
399
        comboArrays.objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
400
                                                                                   objectClass, 0);
401
        comboArrays.stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
402
                                                                                   stringClass, 0);
403
        int i = 0;
404
        for (std::map<std::string, XdmValue *>::iterator iter =
405
                parameters.begin(); iter != parameters.end(); ++iter, i++) {
406 72bf04c6 Norman Walsh
407
#ifdef DEBUG
408 ead48a5d O'Neil Delpratt
            std::cerr<<"map 1"<<std::endl;
409
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
410 72bf04c6 Norman Walsh
#endif
411 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
412
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
413
                                                                            (iter->first).c_str()));
414 72bf04c6 Norman Walsh
#ifdef DEBUG
415 ead48a5d O'Neil Delpratt
            std::string s1 = typeid(iter->second).name();
416
            std::cerr<<"Type of itr:"<<s1<<std::endl;
417
418
            if((iter->second) == nullptr) {std::cerr<<"iter->second is nullptr"<<std::endl;
419
            } else {
420
                std::cerr<<"getting underlying value"<<std::endl;
421
            jobject xx = (iter->second)->getUnderlyingValue();
422
423
            if(xx == nullptr) {
424
                std::cerr<<"value failed"<<std::endl;
425
            } else {
426
427
                std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
428
            }
429
            if((iter->second)->getUnderlyingValue() == nullptr) {
430
                std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
431
            }}
432 72bf04c6 Norman Walsh
#endif
433
434 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
435
                                                                    (iter->second)->getUnderlyingValue());
436 72bf04c6 Norman Walsh
437 ead48a5d O'Neil Delpratt
        }
438 72bf04c6 Norman Walsh
439 ead48a5d O'Neil Delpratt
        for (std::map<std::string, std::string>::iterator iter =
440
                properties.begin(); iter != properties.end(); ++iter, i++) {
441
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
442
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
443
                                                                            (iter->first).c_str()));
444
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
445
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
446
                                                                            (iter->second).c_str()));
447
        }
448 72bf04c6 Norman Walsh
449 ead48a5d O'Neil Delpratt
        return comboArrays;
450 72bf04c6 Norman Walsh
451 ead48a5d O'Neil Delpratt
    } else {
452
        return comboArrays;
453 72bf04c6 Norman Walsh
    }
454 ead48a5d O'Neil Delpratt
}
455 72bf04c6 Norman Walsh
456 ead48a5d O'Neil Delpratt
JParameters SaxonProcessor::createParameterJArray2(std::map<std::string, XdmValue *> parameters) {
457
    JParameters comboArrays;
458
    comboArrays.stringArray = nullptr;
459
    comboArrays.objectArray = nullptr;
460
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
461
                                      "java/lang/Object");
462
    jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
463
                                      "java/lang/String");
464 72bf04c6 Norman Walsh
465 ead48a5d O'Neil Delpratt
    int size = parameters.size();
466 72bf04c6 Norman Walsh
#ifdef DEBUG
467 ead48a5d O'Neil Delpratt
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
468 72bf04c6 Norman Walsh
#endif
469 ead48a5d O'Neil Delpratt
    if (size > 0) {
470 72bf04c6 Norman Walsh
471 ead48a5d O'Neil Delpratt
        comboArrays.objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
472
                                                                                   objectClass, 0);
473
        comboArrays.stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
474
                                                                                   stringClass, 0);
475
        int i = 0;
476
        for (std::map<std::string, XdmValue *>::iterator iter =
477
                parameters.begin(); iter != parameters.end(); ++iter, i++) {
478 72bf04c6 Norman Walsh
479
#ifdef DEBUG
480 ead48a5d O'Neil Delpratt
            std::cerr<<"map 1"<<std::endl;
481
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
482 72bf04c6 Norman Walsh
#endif
483 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
484
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
485
                                                                            (iter->first).c_str()));
486 72bf04c6 Norman Walsh
#ifdef DEBUG
487 ead48a5d O'Neil Delpratt
            std::string s1 = typeid(iter->second).name();
488
            std::cerr<<"Type of itr:"<<s1<<std::endl;
489
490
            if((iter->second) == nullptr) {std::cerr<<"iter->second is nullptr"<<std::endl;
491
            } else {
492
                std::cerr<<"getting underlying value"<<std::endl;
493
            jobject xx = (iter->second)->getUnderlyingValue();
494
495
            if(xx == nullptr) {
496
                std::cerr<<"value failed"<<std::endl;
497
            } else {
498
499
                std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
500
            }
501
            if((iter->second)->getUnderlyingValue() == nullptr) {
502
                std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
503
            }}
504 72bf04c6 Norman Walsh
#endif
505
506 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
507
                                                                    (iter->second)->getUnderlyingValue());
508 72bf04c6 Norman Walsh
509 ead48a5d O'Neil Delpratt
        }
510 72bf04c6 Norman Walsh
511
512 ead48a5d O'Neil Delpratt
        return comboArrays;
513 72bf04c6 Norman Walsh
514 ead48a5d O'Neil Delpratt
    } else {
515
        return comboArrays;
516 72bf04c6 Norman Walsh
    }
517 ead48a5d O'Neil Delpratt
}
518 72bf04c6 Norman Walsh
519
520 ead48a5d O'Neil Delpratt
SaxonProcessor &SaxonProcessor::operator=(const SaxonProcessor &other) {
521
    versionClass = other.versionClass;
522
    procClass = other.procClass;
523
    saxonCAPIClass = other.saxonCAPIClass;
524
    cwd = other.cwd;
525 9e416749 O'Neil Delpratt
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(other.proc);
526 ead48a5d O'Neil Delpratt
    parameters = other.parameters;
527
    configProperties = other.configProperties;
528
    licensei = other.licensei;
529
    exception = other.exception;
530
    return *this;
531 72bf04c6 Norman Walsh
}
532
533
SaxonProcessor::SaxonProcessor(const SaxonProcessor &other) {
534 ead48a5d O'Neil Delpratt
    versionClass = other.versionClass;
535
    procClass = other.procClass;
536
    saxonCAPIClass = other.saxonCAPIClass;
537
    cwd = other.cwd;
538 9e416749 O'Neil Delpratt
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(other.proc);
539 ead48a5d O'Neil Delpratt
    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 4ee4355d O'Neil Delpratt
#ifdef DEBUG
796 ead48a5d O'Neil Delpratt
        std::cerr << "SaxonProc: JVM finalize not called!" << std::endl;
797 4ee4355d O'Neil Delpratt
#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 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
818
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj2);
819 ead48a5d O'Neil Delpratt
    return value;
820
}
821 72bf04c6 Norman Walsh
822 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeStringValue(std::string str) {
823
    jobject obj = getJavaStringValue(SaxonProcessor::sxn_environ, str.c_str());
824
    static jmethodID msID_atomic = nullptr;
825
    if (msID_atomic == nullptr) {
826
        msID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
827
                                                                                 "(Ljava/lang/String;)V"));
828
    }
829
    if (!msID_atomic) {
830
        std::cerr << "XdmAtomic constructor (String)" << std::endl;
831
        return nullptr;
832
    }
833
    jobject obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, msID_atomic, obj));
834
    XdmAtomicValue *value = new XdmAtomicValue(obj2, "xs:string");
835 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
836 ead48a5d O'Neil Delpratt
    return value;
837
}
838 72bf04c6 Norman Walsh
839 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeIntegerValue(int i) {
840
    //jobject obj = integerValue(*SaxonProcessor::sxn_environ, i);
841
    static jmethodID miiID_atomic = nullptr;
842
    if (miiID_atomic == nullptr) {
843
        miiID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
844
    }
845
    if (!miiID_atomic) {
846
        std::cerr << "XdmAtomic constructor (J)" << std::endl;
847
        return nullptr;
848
    }
849
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, miiID_atomic, (jlong) i));
850
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}integer");
851 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
852 ead48a5d O'Neil Delpratt
    return value;
853
}
854
855
XdmAtomicValue *SaxonProcessor::makeDoubleValue(double d) {
856
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
857
    static jmethodID mdID_atomic = nullptr;
858
    if (mdID_atomic == nullptr) {
859
        mdID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(D)V"));
860 72bf04c6 Norman Walsh
    }
861 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mdID_atomic, (jdouble) d));
862
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}double");
863 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
864 ead48a5d O'Neil Delpratt
    return value;
865
}
866 72bf04c6 Norman Walsh
867 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeFloatValue(float d) {
868
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
869
    static jmethodID mfID_atomic = nullptr;
870
    if (mfID_atomic == nullptr) {
871
        mfID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(F)V"));
872 72bf04c6 Norman Walsh
    }
873 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mfID_atomic, (jfloat) d));
874
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}float");
875 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
876 ead48a5d O'Neil Delpratt
    return value;
877
}
878 72bf04c6 Norman Walsh
879 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeLongValue(long l) {
880
    //jobject obj = longValue(*SaxonProcessor::sxn_environ, l);
881
    static jmethodID mlID_atomic = nullptr;
882
    if (mlID_atomic == nullptr) {
883
        mlID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
884 72bf04c6 Norman Walsh
    }
885 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mlID_atomic, (jlong) l));
886
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}long");
887 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
888 ead48a5d O'Neil Delpratt
    return value;
889
}
890 72bf04c6 Norman Walsh
891 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeBooleanValue(bool b) {
892
    //jobject obj = booleanValue(*SaxonProcessor::sxn_environ, b);
893
    static jmethodID mID_atomic = nullptr;
894
    if (mID_atomic == nullptr) {
895
        mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(Z)V"));
896 72bf04c6 Norman Walsh
    }
897 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, (jboolean) b));
898
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}boolean");
899 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
900 ead48a5d O'Neil Delpratt
    return value;
901
}
902
903
XdmAtomicValue *SaxonProcessor::makeQNameValue(const char *str) {
904
    jobject val = xdmValueAsObj(SaxonProcessor::sxn_environ, "QName", str);
905
    XdmAtomicValue *value = new XdmAtomicValue(val, "QName");
906 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(val);
907 ead48a5d O'Neil Delpratt
    return value;
908
}
909 72bf04c6 Norman Walsh
910 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeAtomicValue(const char *typei, const char *strValue) {
911
    jobject obj = xdmValueAsObj(SaxonProcessor::sxn_environ, typei, strValue);
912
    XdmAtomicValue *value = new XdmAtomicValue(obj, typei);
913 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
914 ead48a5d O'Neil Delpratt
    return value;
915
}
916
917
const char *SaxonProcessor::getStringValue(XdmItem *item) {
918
    const char *result = stringValue(SaxonProcessor::sxn_environ, item->getUnderlyingValue());
919 72bf04c6 Norman Walsh
#ifdef DEBUG
920 ead48a5d O'Neil Delpratt
    if(result == nullptr) {
921
        std::cout<<"getStringValue of XdmItem is nullptr"<<std::endl;
922
    } else {
923
        std::cout<<"getStringValue of XdmItem is OK"<<std::endl;
924
    }
925 72bf04c6 Norman Walsh
#endif
926
927 ead48a5d O'Neil Delpratt
    return result;
928
929
}
930 72bf04c6 Norman Walsh
931
#if CVERSION_API_NO >= 123
932
933 ead48a5d O'Neil Delpratt
XdmArray *SaxonProcessor::makeArray(short *input, int length) {
934
    if (input == nullptr) {
935
        std::cerr << "Error found when converting string to XdmArray" << std::endl;
936
        return nullptr;
937
    }
938
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
939
    static jmethodID mmssID = nullptr;
940
    if (mmssID == nullptr) {
941
        mmssID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
942
                                                                                 "([S)Lnet/sf/saxon/s9api/XdmArray;");
943
    }
944
    if (!mmssID) {
945
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([S)" << " not found" << std::endl;
946
        return nullptr;
947
    }
948 72bf04c6 Norman Walsh
949
950 ead48a5d O'Neil Delpratt
    jshortArray sArray = nullptr;
951 72bf04c6 Norman Walsh
952 ead48a5d O'Neil Delpratt
    sArray = SaxonProcessor::sxn_environ->env->NewShortArray((jint) length);
953
    jshort fill[length];
954
    for (int i = 0; i < length; i++) {
955
        fill[i] = input[i];
956
    }
957
    SaxonProcessor::sxn_environ->env->SetShortArrayRegion(sArray, 0, length, fill);
958 72bf04c6 Norman Walsh
959 ead48a5d O'Neil Delpratt
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmssID, sArray);
960
    if (!xdmArrayi) {
961
        std::cerr << "Error found when converting string to XdmArray";
962
        return nullptr;
963
    }
964 72bf04c6 Norman Walsh
965 ead48a5d O'Neil Delpratt
    if (exceptionOccurred()) {
966
        exception = checkAndCreateException(saxonCAPIClass);
967
    } else {
968
        XdmArray *value = new XdmArray(xdmArrayi);
969
        return value;
970
    }
971
    return nullptr;
972
}
973 72bf04c6 Norman Walsh
974
975 ead48a5d O'Neil Delpratt
XdmArray *SaxonProcessor::makeArray(int *input, int length) {
976
    if (input == nullptr) {
977
        std::cerr << "Error found when converting string to XdmArray";
978
        return nullptr;
979
    }
980
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
981
    static jmethodID mmiiID = nullptr;
982
    if (mmiiID == nullptr) {
983
        mmiiID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
984
                                                                                 "([I)Lnet/sf/saxon/s9api/XdmArray;");
985
    }
986
    if (!mmiiID) {
987
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([I)" << " not found" << std::endl;
988
        return nullptr;
989
    }
990
991
992
    jintArray iArray = nullptr;
993
994
    iArray = SaxonProcessor::sxn_environ->env->NewIntArray((jint) length);
995
    jint fill[length];
996
    for (int i = 0; i < length; i++) {
997
        fill[i] = input[i];
998
    }
999
    SaxonProcessor::sxn_environ->env->SetIntArrayRegion(iArray, 0, length, fill);
1000
1001
1002
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiiID, iArray);
1003
    if (!xdmArrayi) {
1004
        std::cerr << "Error found when converting string to XdmArray";
1005
        return nullptr;
1006
    }
1007
    if (exceptionOccurred()) {
1008
        exception = checkAndCreateException(saxonCAPIClass);
1009
    } else {
1010
        XdmArray *value = new XdmArray(xdmArrayi);
1011
        return value;
1012
    }
1013
    return nullptr;
1014
1015
}
1016
1017
XdmArray *SaxonProcessor::makeArray(long *input, int length) {
1018
    if (input == nullptr) {
1019
        std::cerr << "Error found when converting string to XdmArray";
1020
        return nullptr;
1021
    }
1022
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1023
    static jmethodID mmiID = nullptr;
1024
    if (mmiID == nullptr) {
1025
        mmiID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1026
                                                                                "([J)Lnet/sf/saxon/s9api/XdmArray;");
1027
    }
1028
    if (!mmiID) {
1029
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([J)" << " not found" << std::endl;
1030
        return nullptr;
1031
    }
1032
1033
1034
    jlongArray lArray = nullptr;
1035
1036
    lArray = SaxonProcessor::sxn_environ->env->NewLongArray((jint) length);
1037
    jlong fill[length];
1038
    for (int i = 0; i < length; i++) {
1039
        fill[i] = input[i];
1040
    }
1041
    SaxonProcessor::sxn_environ->env->SetLongArrayRegion(lArray, 0, length, fill);
1042
1043
1044
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiID, lArray);
1045
    if (!xdmArrayi) {
1046
        std::cerr << "Error found when converting string to XdmArray";
1047
        return nullptr;
1048
    }
1049
    if (exceptionOccurred()) {
1050
        exception = checkAndCreateException(saxonCAPIClass);
1051
    } else {
1052
        XdmArray *value = new XdmArray(xdmArrayi);
1053
        return value;
1054
    }
1055
    return nullptr;
1056
}
1057
1058
1059
XdmArray *SaxonProcessor::makeArray(bool *input, int length) {
1060
    if (input == nullptr) {
1061
        std::cerr << "Error found when converting string to XdmArray";
1062
        return nullptr;
1063
    }
1064
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1065
    static jmethodID mmbID = nullptr;
1066
    if (mmbID == nullptr) {
1067
        mmbID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1068
                                                                                "([Z)Lnet/sf/saxon/s9api/XdmArray;");
1069
    }
1070
    if (!mmbID) {
1071
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([Z)" << " not found" << std::endl;
1072
        return nullptr;
1073
    }
1074
1075
1076
    jbooleanArray bArray = nullptr;
1077
1078
    bArray = SaxonProcessor::sxn_environ->env->NewBooleanArray((jint) length);
1079
    jboolean fill[length];
1080
    for (int i = 0; i < length; i++) {
1081
        fill[i] = input[i];
1082
    }
1083
    SaxonProcessor::sxn_environ->env->SetBooleanArrayRegion(bArray, 0, length, fill);
1084
1085
1086
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmbID, bArray);
1087
    if (!xdmArrayi) {
1088
        std::cerr << "Error found when converting string to XdmArray";
1089
        return nullptr;
1090
    }
1091
    if (exceptionOccurred()) {
1092
        exception = checkAndCreateException(saxonCAPIClass);
1093
    } else {
1094
        XdmArray *value = new XdmArray(xdmArrayi);
1095
        return value;
1096
    }
1097
    return nullptr;
1098
1099
1100
}
1101
1102
1103
XdmArray *SaxonProcessor::makeArray(const char **input, int length) {
1104
    if (input == nullptr || length <= 0) {
1105
        std::cerr << "Error found when converting string to XdmArray";
1106
        return nullptr;
1107
    }
1108
    jobject obj = nullptr;
1109
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1110
    jmethodID mmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1111
                                                                                     "([Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmArray;");
1112
1113
    jmethodID mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
1114
                                                                                      "(Ljava/lang/String;)V"));
1115
    jobjectArray valueArray = nullptr;
1116
    jobject obj2 = nullptr;
1117
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length, xdmAtomicClass, 0);
1118
    for (int i = 0; i < length; i++) {
1119
        if (input[i] == nullptr) {
1120
            std::cerr << "Error found when converting string to XdmArray";
1121
            return nullptr;
1122 72bf04c6 Norman Walsh
        }
1123 ead48a5d O'Neil Delpratt
        obj = getJavaStringValue(SaxonProcessor::sxn_environ, input[i]);
1124
        obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, obj));
1125
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, obj2);
1126
    }
1127 72bf04c6 Norman Walsh
1128 ead48a5d O'Neil Delpratt
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmID, valueArray);
1129
    if (!xdmArrayi) {
1130
        std::cerr << "Error found when converting string to XdmArray";
1131
        return nullptr;
1132
    }
1133 72bf04c6 Norman Walsh
1134 ead48a5d O'Neil Delpratt
    if (exceptionOccurred()) {
1135
        checkAndCreateException(saxonCAPIClass);
1136
    } else {
1137
        XdmArray *value = new XdmArray(xdmArrayi);
1138
        return value;
1139
    }
1140
    return nullptr;
1141
}
1142 72bf04c6 Norman Walsh
1143
1144 901b6eca O'Neil Delpratt
XdmMap *SaxonProcessor::makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap) {
1145 ead48a5d O'Neil Delpratt
    jobjectArray keyArray = nullptr;
1146
    jobjectArray valueArray = nullptr;
1147
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
1148
                                      "java/lang/Object");
1149 72bf04c6 Norman Walsh
1150 ead48a5d O'Neil Delpratt
    int size = dataMap.size();
1151 72bf04c6 Norman Walsh
1152 ead48a5d O'Neil Delpratt
    if (size > 0) {
1153 72bf04c6 Norman Walsh
1154 ead48a5d O'Neil Delpratt
        keyArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1155
                                                                    objectClass, 0);
1156
        valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1157
                                                                      objectClass, 0);
1158
        int i = 0;
1159
        for (std::map<XdmAtomicValue *, XdmValue *>::iterator iter =
1160
                dataMap.begin(); iter != dataMap.end(); ++iter, i++) {
1161 72bf04c6 Norman Walsh
1162
1163 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(keyArray, i,
1164
                                                                    (iter->first)->getUnderlyingValue());
1165 72bf04c6 Norman Walsh
1166 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i,
1167
                                                                    (iter->second)->getUnderlyingValue());
1168 72bf04c6 Norman Walsh
1169 ead48a5d O'Neil Delpratt
        }
1170
        jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
1171
        jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass, "makeXdmMap",
1172
                                                                                         "([Lnet/sf/saxon/s9api/XdmAtomicValue;[Lnet/sf/saxon/s9api/XdmValue;)Lnet/sf/saxon/s9api/XdmMap;");
1173
        if (!xmID) {
1174
            std::cerr << "Error: SaxonDll." << "makeXdmMap"
1175
                      << " not found\n" << std::endl;
1176
            return nullptr;
1177
        }
1178 72bf04c6 Norman Walsh
1179
1180 ead48a5d O'Neil Delpratt
        jobject results = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
1181
                                                                                              keyArray, valueArray));
1182 901b6eca O'Neil Delpratt
        if (exceptionOccurred() || !results) {
1183
            checkAndCreateException(saxonCAPIClass);
1184
        } else {
1185
            return new XdmMap(results);
1186
        }
1187 ead48a5d O'Neil Delpratt
    }
1188 72bf04c6 Norman Walsh
1189 901b6eca O'Neil Delpratt
    return nullptr;
1190 ead48a5d O'Neil Delpratt
}
1191 72bf04c6 Norman Walsh
1192
#endif
1193