Project

Profile

Help

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

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

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 55b80284 O'Neil Delpratt
18 766106fe O'Neil Delpratt
//#define DEBUG
19 72bf04c6 Norman Walsh
#ifdef DEBUG
20
#include <signal.h>
21
#endif
22 ead48a5d O'Neil Delpratt
23 72bf04c6 Norman Walsh
#include <stdio.h>
24
25
26 55b80284 O'Neil Delpratt
#if defined MEM_DEBUG
27
28
void* newImpl(std::size_t sz,char const* file, int line){
29
    static int counter{};
30
    void* ptr= std::malloc(sz);
31
    std::cerr << "Saxon/C Mem test: "<<file << ": " << line << " " <<  ptr << std::endl;
32
    myAlloc.push_back(ptr);
33
    return ptr;
34
}
35
36
void* operator new(std::size_t sz,char const* file, int line){
37
return newImpl(sz,file,line);
38
}
39
40
void* operator new [](std::size_t sz,char const* file, int line){
41
return newImpl(sz,file,line);
42
}
43
44
void operator delete(void* ptr) noexcept{
45
    auto ind= std::distance(myAlloc.begin(),std::find(myAlloc.begin(), myAlloc.end(),ptr));
46
    myAlloc[ind]= nullptr;
47
    std::free(ptr);
48
}
49
50 7728ae0b O'Neil Delpratt
void operator delete(void*, std::size_t) noexcept {
51
    auto ind= std::distance(myAlloc.begin(),std::find(myAlloc.begin(), myAlloc.end(),ptr));
52
    myAlloc[ind]= nullptr;
53
    std::free(ptr);
54
55
}
56
57 55b80284 O'Neil Delpratt
#define new new(__FILE__, __LINE__)
58
59
void SaxonProcessor::getInfo(){
60
61
    std::cout << std::endl;
62
63
    std::cout << "Allocation: " << std::endl;
64
    for (auto i: myAlloc){
65
        if (i != nullptr ) std::cout << " " << i << std::endl;
66
    }
67
68
    std::cout << std::endl;
69
70
}
71
72
#endif
73
74 72bf04c6 Norman Walsh
//jobject cpp;
75 ead48a5d O'Neil Delpratt
const char *failure;
76
sxnc_environment *SaxonProcessor::sxn_environ = 0;
77
int SaxonProcessor::jvmCreatedCPP = 0;
78 72bf04c6 Norman Walsh
79 ead48a5d O'Neil Delpratt
bool SaxonProcessor::exceptionOccurred() {
80 00082f6f O'Neil Delpratt
    return SaxonProcessor::sxn_environ->env->ExceptionCheck() || exception != nullptr;
81 72bf04c6 Norman Walsh
}
82
83 4ee4355d O'Neil Delpratt
const char *SaxonProcessor::checkException() {
84 ead48a5d O'Neil Delpratt
    const char *message = nullptr;
85 4ee4355d O'Neil Delpratt
    message = checkForException(sxn_environ);
86 ead48a5d O'Neil Delpratt
    return message;
87
}
88 72bf04c6 Norman Walsh
89 55b80284 O'Neil Delpratt
90
91 ead48a5d O'Neil Delpratt
SaxonApiException *SaxonProcessor::checkAndCreateException(jclass cppClass) {
92 00082f6f O'Neil Delpratt
    if (SaxonProcessor::sxn_environ->env->ExceptionCheck()) {
93 ead48a5d O'Neil Delpratt
        SaxonApiException *exception = checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass, nullptr);
94 72bf04c6 Norman Walsh
#ifdef DEBUG
95 ead48a5d O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->ExceptionDescribe();
96 72bf04c6 Norman Walsh
#endif
97 ead48a5d O'Neil Delpratt
        return exception;
98
    }
99
    return nullptr;
100 72bf04c6 Norman Walsh
}
101
102 8d5b64f3 O'Neil Delpratt
const char *SaxonProcessor::getErrorMessage() {
103
    if(exception == nullptr) {
104
        exception =  SaxonProcessor::checkForExceptionCPP(SaxonProcessor::sxn_environ->env,
105
                                                                            saxonCAPIClass,nullptr);
106
    }
107
    if (exception == nullptr) { return nullptr; }
108
    return exception->getMessage();
109
}
110
111 ead48a5d O'Neil Delpratt
void SaxonProcessor::exceptionClear() {
112 306962bc O'Neil Delpratt
    if(exceptionOccurred()) {
113
        SaxonProcessor::sxn_environ->env->ExceptionClear();
114
    }
115 01d6fdb6 O'Neil Delpratt
    std::cerr<< "saxonProc exceptionClear cp0"<<std::endl;
116 306962bc O'Neil Delpratt
    if (exception != nullptr) {
117 55b80284 O'Neil Delpratt
        std::cerr<< "saxonProc exception= "<< exception <<std::endl;
118 306962bc O'Neil Delpratt
        delete exception;
119 55b80284 O'Neil Delpratt
        exception = nullptr;
120 306962bc O'Neil Delpratt
    }
121 00082f6f O'Neil Delpratt
122 ead48a5d O'Neil Delpratt
}
123 72bf04c6 Norman Walsh
124
125
SaxonProcessor::SaxonProcessor() {
126
    licensei = false;
127
    SaxonProcessor(false);
128
}
129
130 766106fe O'Neil Delpratt
const char * SaxonProcessor::getResourcesDirectory() {
131
    return NULL;
132
}
133
134 306962bc O'Neil Delpratt
void SaxonProcessor::createException(const char * message) {
135
136 55b80284 O'Neil Delpratt
    exceptionClear();
137 306962bc O'Neil Delpratt
138
    if(message == nullptr) {
139
        exception = checkAndCreateException(saxonCAPIClass);
140
    } else {
141
        exception = new SaxonApiException(message);
142
    }
143
144
}
145
146 72bf04c6 Norman Walsh
147 306962bc O'Neil Delpratt
SaxonApiException * SaxonProcessor::checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject) {
148 72bf04c6 Norman Walsh
149
    if (env->ExceptionCheck()) {
150 ead48a5d O'Neil Delpratt
        std::string result1 = "";
151
        std::string errorCode = "";
152
        jthrowable exc = env->ExceptionOccurred();
153 72bf04c6 Norman Walsh
154 ead48a5d O'Neil Delpratt
#ifdef DEBUG
155
        env->ExceptionDescribe();
156 72bf04c6 Norman Walsh
#endif
157 ead48a5d O'Neil Delpratt
        jclass exccls(env->GetObjectClass(exc));
158 72bf04c6 Norman Walsh
        jclass clscls(env->FindClass("java/lang/Class"));
159
160
        jmethodID getName(env->GetMethodID(clscls, "getName", "()Ljava/lang/String;"));
161
        jstring name(static_cast<jstring>(env->CallObjectMethod(exccls, getName)));
162 ead48a5d O'Neil Delpratt
        char const *utfName(env->GetStringUTFChars(name, 0));
163
        result1 = (std::string(utfName));
164
        env->ReleaseStringUTFChars(name, utfName);
165
166
        jmethodID getMessage(env->GetMethodID(exccls, "getMessage", "()Ljava/lang/String;"));
167
        if (getMessage) {
168
169
            jstring message((jstring) (env->CallObjectMethod(exc, getMessage)));
170
            char const *utfMessage = nullptr;
171
            if (!message) {
172
                utfMessage = "";
173
                return nullptr;
174
            } else {
175
                utfMessage = (env->GetStringUTFChars(message, 0));
176
            }
177
            if (utfMessage != nullptr) {
178
                result1 = (result1 + " : ") + utfMessage;
179 72bf04c6 Norman Walsh
            }
180
181 ead48a5d O'Neil Delpratt
            env->ReleaseStringUTFChars(message, utfMessage);
182
183
            if (callingObject != nullptr && result1.compare(0, 43, "net.sf.saxon.s9api.SaxonApiException", 43) == 0) {
184
185
                jclass saxonApiExceptionClass(env->FindClass("net/sf/saxon/s9api/SaxonApiException"));
186
                static jmethodID lineNumID = nullptr;
187
                if (lineNumID == nullptr) {
188
                    lineNumID = env->GetMethodID(saxonApiExceptionClass, "getLinenumber", "()I");
189
                }
190
                static jmethodID ecID = nullptr;
191
                if (ecID == nullptr) {
192
                    ecID = env->GetMethodID(saxonApiExceptionClass, "getErrorCode", "()Ljnet/sf/saxon/s9api/QName;");
193
                }
194
                static jmethodID esysID = nullptr;
195
                if (esysID == nullptr) {
196
                    esysID = env->GetMethodID(saxonApiExceptionClass, "getSystemId", "()Ljava/lang/String;");
197
                }
198
199
200
                jobject errCodeQName = (jobject) (env->CallObjectMethod(exc, ecID));
201
                jstring errSystemID = (jstring) (env->CallObjectMethod(exc, esysID));
202
                int linenum = env->CallIntMethod(exc, lineNumID);
203
204
                jclass qnameClass(env->FindClass("net/sf/saxon/s9api/QName"));
205
                static jmethodID qnameStrID = nullptr;
206
                if (qnameStrID == nullptr) {
207
                    qnameStrID = env->GetMethodID(qnameClass, "toString", "()Ljava/lang/String;");
208
                }
209
210
                jstring qnameStr = (jstring) (env->CallObjectMethod(errCodeQName, qnameStrID));
211
212
213
                SaxonApiException *saxonExceptions = new SaxonApiException(result1.c_str(),
214
                                                                           (qnameStr ? env->GetStringUTFChars(qnameStr,
215
                                                                                                              0)
216
                                                                                     : nullptr),
217
                                                                           (errSystemID ? env->GetStringUTFChars(
218
                                                                                   errSystemID, 0) : nullptr), linenum);
219
220
                if (errCodeQName) {
221
                    env->DeleteLocalRef(errCodeQName);
222
                }
223
                if (errSystemID) {
224
                    env->DeleteLocalRef(errSystemID);
225
                }
226
                if (qnameStr) {
227
                    env->DeleteLocalRef(qnameStr);
228
                }
229
230
                if (message) {
231
                    env->DeleteLocalRef(message);
232
                }
233
                env->ExceptionClear();
234
                return saxonExceptions;
235
            }
236
        }
237
        SaxonApiException *saxonExceptions = new SaxonApiException(result1.c_str());
238
        //env->ExceptionDescribe();
239
        env->ExceptionClear();
240
        return saxonExceptions;
241
    }
242
    return nullptr;
243 72bf04c6 Norman Walsh
244
}
245
246
247 ead48a5d O'Neil Delpratt
SaxonProcessor::SaxonProcessor(bool l) {
248 72bf04c6 Norman Walsh
249 ead48a5d O'Neil Delpratt
    cwd = "";
250 72bf04c6 Norman Walsh
    licensei = l;
251 ead48a5d O'Neil Delpratt
    versionStr = nullptr;
252 01d6fdb6 O'Neil Delpratt
    exception = nullptr;
253 ead48a5d O'Neil Delpratt
254
    if (SaxonProcessor::jvmCreatedCPP == 0) {
255
        SaxonProcessor::jvmCreatedCPP = 1;
256
        SaxonProcessor::sxn_environ = new sxnc_environment;//(sxnc_environment *)malloc(sizeof(sxnc_environment));
257 72bf04c6 Norman Walsh
258
259 ead48a5d O'Neil Delpratt
        /*
260
         * First of all, load required component.
261
         * By the time of JET initialization, all components should be loaded.
262
         */
263 72bf04c6 Norman Walsh
264 ead48a5d O'Neil Delpratt
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
265 72bf04c6 Norman Walsh
266 ead48a5d O'Neil Delpratt
        /*
267
         * Initialize JET run-time.
268
         * The handle of loaded component is used to retrieve Invocation API.
269
         */
270
        initDefaultJavaRT(SaxonProcessor::sxn_environ);
271 72bf04c6 Norman Walsh
    } else {
272
#ifdef DEBUG
273 ead48a5d O'Neil Delpratt
        std::cerr<<"SaxonProc constructor: jvm exists! jvmCreatedCPP="<<jvmCreatedCPP<<std::endl;
274 72bf04c6 Norman Walsh
#endif
275
276 ead48a5d O'Neil Delpratt
    }
277
278 72bf04c6 Norman Walsh
279
    versionClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/Version");
280
    procClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/Processor");
281
    saxonCAPIClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/SaxonCAPI");
282 ead48a5d O'Neil Delpratt
283
    jobject proci = createSaxonProcessor(SaxonProcessor::sxn_environ->env, procClass, "(Z)V", nullptr, licensei);
284 61decfd8 Norman Walsh
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(proci);
285 ead48a5d O'Neil Delpratt
    if (!proc) {
286
        std::cout << "proc is nullptr in SaxonProcessor constructor" << std::endl;
287
    }
288 72bf04c6 Norman Walsh
289
    xdmAtomicClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
290
#ifdef DEBUG
291 ead48a5d O'Neil Delpratt
    jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "setDebugMode", "(Z)V");
292
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(saxonCAPIClass, debugMID, (jboolean)true);
293 72bf04c6 Norman Walsh
#endif
294
}
295
296 ead48a5d O'Neil Delpratt
SaxonProcessor::SaxonProcessor(const char *configFile) {
297
    cwd = "";
298
    versionStr = nullptr;
299 72bf04c6 Norman Walsh
300 ead48a5d O'Neil Delpratt
    if (SaxonProcessor::jvmCreatedCPP == 0) {
301
        SaxonProcessor::jvmCreatedCPP = 1;
302
        //SaxonProcessor::sxn_environ= new sxnc_environment;
303
        SaxonProcessor::sxn_environ = (sxnc_environment *) malloc(sizeof(sxnc_environment));
304 72bf04c6 Norman Walsh
305 ead48a5d O'Neil Delpratt
        /*
306
         * First of all, load required component.
307
         * By the time of JET initialization, all components should be loaded.
308
         */
309 72bf04c6 Norman Walsh
310 ead48a5d O'Neil Delpratt
        SaxonProcessor::sxn_environ->myDllHandle = loadDefaultDll();
311 72bf04c6 Norman Walsh
312 ead48a5d O'Neil Delpratt
        /*
313
         * Initialize JET run-time.
314
         * The handle of loaded component is used to retrieve Invocation API.
315
         */
316
        initDefaultJavaRT(SaxonProcessor::sxn_environ);
317 72bf04c6 Norman Walsh
    }
318 ead48a5d O'Neil Delpratt
319 72bf04c6 Norman Walsh
    versionClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/Version");
320
321
    procClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/Processor");
322
    saxonCAPIClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/SaxonCAPI");
323
324 ead48a5d O'Neil Delpratt
    static jmethodID mIDcreateProc = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
325
                                                                                                     "createSaxonProcessor",
326
                                                                                                     "(Ljava/lang/String;)Lnet/sf/saxon/s9api/Processor;");
327
    if (!mIDcreateProc) {
328
        std::cerr << "Error: SaxonDll." << "getPrimitiveTypeName"
329
                  << " not found\n" << std::endl;
330
        return;
331
    }
332
    jobject proci = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mIDcreateProc,
333
                                                                             SaxonProcessor::sxn_environ->env->NewStringUTF(
334
                                                                                     configFile));
335
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(proci);
336
    if (!proc) {
337
        checkAndCreateException(saxonCAPIClass);
338
        std::cerr << "Error: " << getDllname() << ". processor is nullptr in constructor(configFile)" << std::endl;
339
        return;
340
    }
341
342
    licensei = true;
343 72bf04c6 Norman Walsh
#ifdef DEBUG
344
345 ead48a5d O'Neil Delpratt
    std::cerr<<"SaxonProc constructor(configFile)"<<std::endl;
346 72bf04c6 Norman Walsh
#endif
347
    xdmAtomicClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
348
}
349
350 ead48a5d O'Neil Delpratt
SaxonProcessor::~SaxonProcessor() {
351
    clearConfigurationProperties();
352 85a9912e Dave Delpratt
    if(proc) {
353
            SaxonProcessor::sxn_environ->env->DeleteGlobalRef(proc);
354
    }
355 ead48a5d O'Neil Delpratt
    if (versionStr != nullptr) {
356
        delete versionStr;
357 55b80284 O'Neil Delpratt
        versionStr = nullptr;
358 ead48a5d O'Neil Delpratt
    }
359 306962bc O'Neil Delpratt
    exceptionClear();
360 ead48a5d O'Neil Delpratt
}
361 72bf04c6 Norman Walsh
362
363 ead48a5d O'Neil Delpratt
bool SaxonProcessor::isSchemaAwareProcessor() {
364
    if (!licensei) {
365
        return false;
366
    } else {
367
        static jmethodID MID_schema = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(procClass,
368
                                                                                                "isSchemaAware", "()Z");
369
        if (!MID_schema) {
370
            std::cerr << "\nError: Saxonc " << "SaxonProcessor.isSchemaAware()" << " not found" << std::endl;
371
            return false;
372
        }
373 72bf04c6 Norman Walsh
374 ead48a5d O'Neil Delpratt
        licensei = (jboolean) (SaxonProcessor::sxn_environ->env->CallBooleanMethod(proc, MID_schema));
375
        return licensei;
376 72bf04c6 Norman Walsh
377 ead48a5d O'Neil Delpratt
    }
378 72bf04c6 Norman Walsh
379
}
380
381 ead48a5d O'Neil Delpratt
void SaxonProcessor::applyConfigurationProperties() {
382
    if (configProperties.size() > 0) {
383
        int size = configProperties.size();
384
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
385
        jobjectArray stringArray1 = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size, stringClass, 0);
386
        jobjectArray stringArray2 = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size, stringClass, 0);
387
        static jmethodID mIDappConfig = nullptr;
388
        if (mIDappConfig == nullptr) {
389
            mIDappConfig = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
390
                                                                                           "applyToConfiguration",
391
                                                                                           "(Lnet/sf/saxon/s9api/Processor;[Ljava/lang/String;[Ljava/lang/String;)V");
392
            if (!mIDappConfig) {
393
                std::cerr << "Error: SaxonDll." << "applyToConfiguration"
394
                          << " not found\n" << std::endl;
395
                return;
396
            }
397
        }
398
        int i = 0;
399
        std::map<std::string, std::string>::iterator iter = configProperties.begin();
400
        for (iter = configProperties.begin(); iter != configProperties.end(); ++iter, i++) {
401
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray1, i,
402
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
403
                                                                            (iter->first).c_str()));
404
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray2, i,
405
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
406
                                                                            (iter->second).c_str()));
407
        }
408
        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mIDappConfig, proc, stringArray1,
409
                                                                 stringArray2);
410
        if (exceptionOccurred()) {
411 306962bc O'Neil Delpratt
            createException();
412 ead48a5d O'Neil Delpratt
        }
413
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray1);
414
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray2);
415
416
    }
417 72bf04c6 Norman Walsh
}
418
419
420 ead48a5d O'Neil Delpratt
jobjectArray SaxonProcessor::createJArray(XdmValue **values, int length) {
421
    jobjectArray valueArray = nullptr;
422 72bf04c6 Norman Walsh
423
    jclass xdmValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
424 ead48a5d O'Neil Delpratt
                                        "net/sf/saxon/s9api/XdmValue");
425 72bf04c6 Norman Walsh
426
427
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length,
428 ead48a5d O'Neil Delpratt
                                                                  xdmValueClass, 0);
429 72bf04c6 Norman Walsh
430 ead48a5d O'Neil Delpratt
    for (int i = 0; i < length; i++) {
431 72bf04c6 Norman Walsh
#ifdef DEBUG
432 ead48a5d O'Neil Delpratt
        std::string s1 = typeid(values[i]).name();
433
        std::cerr<<"In createJArray\nType of itr:"<<s1<<std::endl;
434 72bf04c6 Norman Walsh
435
436 ead48a5d O'Neil Delpratt
        jobject xx = values[i]->getUnderlyingValue();
437 72bf04c6 Norman Walsh
438 ead48a5d O'Neil Delpratt
        if(xx == nullptr) {
439
            std::cerr<<"value failed"<<std::endl;
440
        } else {
441 72bf04c6 Norman Walsh
442 ead48a5d O'Neil Delpratt
            std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
443
        }
444
        if(values[i]->getUnderlyingValue() == nullptr) {
445
            std::cerr<<"value["<<i<<"]->getUnderlyingValue() is nullptr"<<std::endl;
446
        }
447 72bf04c6 Norman Walsh
#endif
448 ead48a5d O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, values[i]->getUnderlyingValue());
449 72bf04c6 Norman Walsh
    }
450
    return valueArray;
451
452 ead48a5d O'Neil Delpratt
}
453 72bf04c6 Norman Walsh
454
455 ead48a5d O'Neil Delpratt
JParameters SaxonProcessor::createParameterJArray(std::map<std::string, XdmValue *> parameters,
456
                                                  std::map<std::string, std::string> properties) {
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() + properties.size();
466 72bf04c6 Norman Walsh
#ifdef DEBUG
467 ead48a5d O'Neil Delpratt
    std::cerr<<"Properties size: "<<properties.size()<<std::endl;
468
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
469 72bf04c6 Norman Walsh
#endif
470 ead48a5d O'Neil Delpratt
    if (size > 0) {
471
472
        comboArrays.objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
473
                                                                                   objectClass, 0);
474
        comboArrays.stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
475
                                                                                   stringClass, 0);
476
        int i = 0;
477
        for (std::map<std::string, XdmValue *>::iterator iter =
478
                parameters.begin(); iter != parameters.end(); ++iter, i++) {
479 72bf04c6 Norman Walsh
480
#ifdef DEBUG
481 ead48a5d O'Neil Delpratt
            std::cerr<<"map 1"<<std::endl;
482
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
483 72bf04c6 Norman Walsh
#endif
484 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
485
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
486
                                                                            (iter->first).c_str()));
487 72bf04c6 Norman Walsh
#ifdef DEBUG
488 ead48a5d O'Neil Delpratt
            std::string s1 = typeid(iter->second).name();
489
            std::cerr<<"Type of itr:"<<s1<<std::endl;
490
491
            if((iter->second) == nullptr) {std::cerr<<"iter->second is nullptr"<<std::endl;
492
            } else {
493
                std::cerr<<"getting underlying value"<<std::endl;
494
            jobject xx = (iter->second)->getUnderlyingValue();
495
496
            if(xx == nullptr) {
497
                std::cerr<<"value failed"<<std::endl;
498
            } else {
499
500
                std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
501
            }
502
            if((iter->second)->getUnderlyingValue() == nullptr) {
503
                std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
504
            }}
505 72bf04c6 Norman Walsh
#endif
506
507 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
508
                                                                    (iter->second)->getUnderlyingValue());
509 72bf04c6 Norman Walsh
510 ead48a5d O'Neil Delpratt
        }
511 72bf04c6 Norman Walsh
512 ead48a5d O'Neil Delpratt
        for (std::map<std::string, std::string>::iterator iter =
513
                properties.begin(); iter != properties.end(); ++iter, i++) {
514
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
515
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
516
                                                                            (iter->first).c_str()));
517
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
518
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
519
                                                                            (iter->second).c_str()));
520
        }
521 72bf04c6 Norman Walsh
522 ead48a5d O'Neil Delpratt
        return comboArrays;
523 72bf04c6 Norman Walsh
524 ead48a5d O'Neil Delpratt
    } else {
525
        return comboArrays;
526 72bf04c6 Norman Walsh
    }
527 ead48a5d O'Neil Delpratt
}
528 72bf04c6 Norman Walsh
529 ead48a5d O'Neil Delpratt
JParameters SaxonProcessor::createParameterJArray2(std::map<std::string, XdmValue *> parameters) {
530
    JParameters comboArrays;
531
    comboArrays.stringArray = nullptr;
532
    comboArrays.objectArray = nullptr;
533
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
534
                                      "java/lang/Object");
535
    jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
536
                                      "java/lang/String");
537 72bf04c6 Norman Walsh
538 ead48a5d O'Neil Delpratt
    int size = parameters.size();
539 72bf04c6 Norman Walsh
#ifdef DEBUG
540 ead48a5d O'Neil Delpratt
    std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
541 72bf04c6 Norman Walsh
#endif
542 ead48a5d O'Neil Delpratt
    if (size > 0) {
543 72bf04c6 Norman Walsh
544 ead48a5d O'Neil Delpratt
        comboArrays.objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
545
                                                                                   objectClass, 0);
546
        comboArrays.stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
547
                                                                                   stringClass, 0);
548
        int i = 0;
549
        for (std::map<std::string, XdmValue *>::iterator iter =
550
                parameters.begin(); iter != parameters.end(); ++iter, i++) {
551 72bf04c6 Norman Walsh
552
#ifdef DEBUG
553 ead48a5d O'Neil Delpratt
            std::cerr<<"map 1"<<std::endl;
554
            std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
555 72bf04c6 Norman Walsh
#endif
556 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.stringArray, i,
557
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
558
                                                                            (iter->first).c_str()));
559 72bf04c6 Norman Walsh
#ifdef DEBUG
560 ead48a5d O'Neil Delpratt
            std::string s1 = typeid(iter->second).name();
561
            std::cerr<<"Type of itr:"<<s1<<std::endl;
562
563
            if((iter->second) == nullptr) {std::cerr<<"iter->second is nullptr"<<std::endl;
564
            } else {
565
                std::cerr<<"getting underlying value"<<std::endl;
566
            jobject xx = (iter->second)->getUnderlyingValue();
567
568
            if(xx == nullptr) {
569
                std::cerr<<"value failed"<<std::endl;
570
            } else {
571
572
                std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
573
            }
574
            if((iter->second)->getUnderlyingValue() == nullptr) {
575
                std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
576
            }}
577 72bf04c6 Norman Walsh
#endif
578
579 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(comboArrays.objectArray, i,
580
                                                                    (iter->second)->getUnderlyingValue());
581 72bf04c6 Norman Walsh
582 ead48a5d O'Neil Delpratt
        }
583 72bf04c6 Norman Walsh
584
585 ead48a5d O'Neil Delpratt
        return comboArrays;
586 72bf04c6 Norman Walsh
587 ead48a5d O'Neil Delpratt
    } else {
588
        return comboArrays;
589 72bf04c6 Norman Walsh
    }
590 ead48a5d O'Neil Delpratt
}
591 72bf04c6 Norman Walsh
592
593 ead48a5d O'Neil Delpratt
SaxonProcessor &SaxonProcessor::operator=(const SaxonProcessor &other) {
594
    versionClass = other.versionClass;
595
    procClass = other.procClass;
596
    saxonCAPIClass = other.saxonCAPIClass;
597
    cwd = other.cwd;
598 9e416749 O'Neil Delpratt
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(other.proc);
599 ead48a5d O'Neil Delpratt
    parameters = other.parameters;
600
    configProperties = other.configProperties;
601
    licensei = other.licensei;
602
    exception = other.exception;
603
    return *this;
604 72bf04c6 Norman Walsh
}
605
606
SaxonProcessor::SaxonProcessor(const SaxonProcessor &other) {
607 ead48a5d O'Neil Delpratt
    versionClass = other.versionClass;
608
    procClass = other.procClass;
609
    saxonCAPIClass = other.saxonCAPIClass;
610
    cwd = other.cwd;
611 9e416749 O'Neil Delpratt
    proc = SaxonProcessor::sxn_environ->env->NewGlobalRef(other.proc);
612 ead48a5d O'Neil Delpratt
    parameters = other.parameters;
613
    configProperties = other.configProperties;
614
    licensei = other.licensei;
615
    exception = other.exception;
616 72bf04c6 Norman Walsh
}
617
618 03fcd3f9 O'Neil Delpratt
619
620
void SaxonProcessor::setResourcesDirectory(const char *dir){}
621
622 ead48a5d O'Neil Delpratt
XsltProcessor *SaxonProcessor::newXsltProcessor() {
623 72bf04c6 Norman Walsh
    return (new XsltProcessor(this, cwd));
624
}
625
626 ead48a5d O'Neil Delpratt
Xslt30Processor *SaxonProcessor::newXslt30Processor() {
627 72bf04c6 Norman Walsh
    return (new Xslt30Processor(this, cwd));
628
}
629
630 ead48a5d O'Neil Delpratt
XQueryProcessor *SaxonProcessor::newXQueryProcessor() {
631
    return (new XQueryProcessor(this, cwd));
632 72bf04c6 Norman Walsh
}
633
634 ead48a5d O'Neil Delpratt
XPathProcessor *SaxonProcessor::newXPathProcessor() {
635 72bf04c6 Norman Walsh
    return (new XPathProcessor(this, cwd));
636
}
637
638 ead48a5d O'Neil Delpratt
SchemaValidator *SaxonProcessor::newSchemaValidator() {
639
    if (licensei) {
640
        return (new SchemaValidator(this, cwd));
641
    } else {
642
        std::cerr << "\nError: Processor is not licensed for schema processing!" << std::endl;
643
        return nullptr;
644
    }
645 72bf04c6 Norman Walsh
}
646
647
648 ead48a5d O'Neil Delpratt
const char *SaxonProcessor::version() {
649
    if (versionStr == nullptr) {
650 72bf04c6 Norman Walsh
651 ead48a5d O'Neil Delpratt
        static jmethodID MID_version = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass,
652
                                                                                                       "getProductVersion",
653
                                                                                                       "(Lnet/sf/saxon/s9api/Processor;)Ljava/lang/String;");
654
        if (!MID_version) {
655
            std::cerr << "\nError: MyClassInDll " << "SaxonCAPI.getProductVersion()" << " not found" << std::endl;
656
            return nullptr;
657
        }
658 72bf04c6 Norman Walsh
659 01d6fdb6 O'Neil Delpratt
        if(proc == nullptr) {
660
            createException("The Java SaxonProcessor object (i.e. cppXQ) is NULL - Possible exception thrown");
661
            return nullptr;
662
        }
663
664 ead48a5d O'Neil Delpratt
        jstring jstr = (jstring) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, MID_version,
665
                                                                                           proc));
666
        const char *tempVersionStr = SaxonProcessor::sxn_environ->env->GetStringUTFChars(jstr, nullptr);
667
        int verLen = strlen(tempVersionStr) + 22 + strlen(CVERSION) + 1;
668
        versionStr = new char[verLen];
669
        snprintf(versionStr, verLen, "Saxon/C %s %s %s", CVERSION, "running with", tempVersionStr);
670 55b80284 O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->ReleaseStringUTFChars(jstr, tempVersionStr);
671
672 72bf04c6 Norman Walsh
673
    }
674
675
    return versionStr;
676
}
677
678 ead48a5d O'Neil Delpratt
void SaxonProcessor::setcwd(const char *dir) {
679 6b6a9706 O'Neil Delpratt
    if(dir != nullptr) {
680
        cwd = std::string(dir);
681
    }
682 72bf04c6 Norman Walsh
}
683
684 ead48a5d O'Neil Delpratt
const char *SaxonProcessor::getcwd() {
685
    return cwd.c_str();
686 72bf04c6 Norman Walsh
}
687
688
689 ead48a5d O'Neil Delpratt
void SaxonProcessor::setCatalog(const char *catalogFile, bool isTracing) {
690 9458d238 O'Neil Delpratt
    jclass xmlResolverClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/trans/XmlCatalogResolver");
691 ead48a5d O'Neil Delpratt
    static jmethodID catalogMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(xmlResolverClass, "setCatalog",
692
                                                                                      "(Ljava/lang/String;Lnet/sf/saxon/Configuration;Z)V");
693 72bf04c6 Norman Walsh
694 ead48a5d O'Neil Delpratt
    if (!catalogMID) {
695
        std::cerr << "\nError: Saxonc." << "setCatalog()" << " not found" << std::endl;
696
        return;
697
    }
698
    if (catalogFile == nullptr) {
699 72bf04c6 Norman Walsh
700
        return;
701 ead48a5d O'Neil Delpratt
    }
702
    static jmethodID configMID = SaxonProcessor::sxn_environ->env->GetMethodID(procClass, "getUnderlyingConfiguration",
703
                                                                               "()Lnet/sf/saxon/Configuration;");
704
705
    if (!configMID) {
706
        std::cerr << "\nError: Saxonc." << "getUnderlyingConfiguration()" << " not found" << std::endl;
707 72bf04c6 Norman Walsh
        return;
708 ead48a5d O'Neil Delpratt
    }
709
710 72bf04c6 Norman Walsh
711 ead48a5d O'Neil Delpratt
    if (!proc) {
712 306962bc O'Neil Delpratt
        createException("Processor is null in SaxonProcessor.setCatalog");
713 ead48a5d O'Neil Delpratt
        return;
714
    }
715 72bf04c6 Norman Walsh
716 ead48a5d O'Neil Delpratt
    jobject configObj = SaxonProcessor::sxn_environ->env->CallObjectMethod(proc, configMID);
717 72bf04c6 Norman Walsh
718 ead48a5d O'Neil Delpratt
    if (!configObj) {
719 9458d238 O'Neil Delpratt
        std::cout << "proc is null in SaxonProcessor setcatalog - config obj" << std::endl;
720 ead48a5d O'Neil Delpratt
        return;
721
    }
722
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(xmlResolverClass, catalogMID,
723
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(catalogFile),
724
                                                           configObj, (jboolean) isTracing);
725 72bf04c6 Norman Walsh
#ifdef DEBUG
726 ead48a5d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
727 72bf04c6 Norman Walsh
#endif
728 00082f6f O'Neil Delpratt
729 72bf04c6 Norman Walsh
}
730
731
732 ead48a5d O'Neil Delpratt
XdmNode *SaxonProcessor::parseXmlFromString(const char *source) {
733 72bf04c6 Norman Walsh
734 ead48a5d O'Neil Delpratt
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlString",
735
                                                                                    "(Ljava/lang/String;Lnet/sf/saxon/s9api/Processor;Lnet/sf/saxon/s9api/SchemaValidator;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
736 72bf04c6 Norman Walsh
    if (!mID) {
737 ead48a5d O'Neil Delpratt
        std::cerr << "\nError: Saxonc." << "parseXmlString()" << " not found" << std::endl;
738
        return nullptr;
739 72bf04c6 Norman Walsh
    }
740
//TODO SchemaValidator
741
742 306962bc O'Neil Delpratt
    if(source == nullptr) {
743
        createException("Source string is NULL");
744
        return nullptr;
745
    }
746
747 ead48a5d O'Neil Delpratt
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID,
748
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
749
                                                                                        cwd.c_str()), proc, nullptr,
750
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
751
                                                                                        source));
752
    if (xdmNodei) {
753 55b80284 O'Neil Delpratt
        XdmNode *value = nullptr;
754
        value = new XdmNode(xdmNodei);
755
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(xdmNodei);
756 ead48a5d O'Neil Delpratt
        return value;
757
    } else if (exceptionOccurred()) {
758 306962bc O'Neil Delpratt
        createException();
759 ead48a5d O'Neil Delpratt
    }
760
761 72bf04c6 Norman Walsh
#ifdef DEBUG
762 ead48a5d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->ExceptionDescribe();
763 72bf04c6 Norman Walsh
#endif
764
765 ead48a5d O'Neil Delpratt
    return nullptr;
766 72bf04c6 Norman Walsh
}
767
768 ead48a5d O'Neil Delpratt
int SaxonProcessor::getNodeKind(jobject obj) {
769
    jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmNode;");
770
    static jmethodID nodeKindMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
771
                                                                                             "getNodeKind",
772
                                                                                             "()Lnet/sf/saxon/s9api/XdmNodeKind;");
773
    if (!nodeKindMID) {
774
        std::cerr << "Error: MyClassInDll." << "getNodeKind" << " not found\n"
775
                  << std::endl;
776
        return 0;
777
    }
778
779
    jobject nodeKindObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(obj, nodeKindMID));
780
    if (!nodeKindObj) {
781 72bf04c6 Norman Walsh
782 ead48a5d O'Neil Delpratt
        return 0;
783
    }
784
    jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/option/cpp/XdmUtils;");
785
786
    jmethodID mID2 = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,
787
                                                                                     "convertNodeKindType",
788
                                                                                     "(Lnet/sf/saxon/s9api/XdmNodeKind;)I");
789
790
    if (!mID2) {
791
        std::cerr << "Error: MyClassInDll." << "convertNodeKindType" << " not found\n"
792
                  << std::endl;
793
        return 0;
794
    }
795
    if (!nodeKindObj) {
796
        return 0;
797
    }
798
    int nodeKind = (int) (SaxonProcessor::sxn_environ->env->CallStaticIntMethod(xdmUtilsClass, mID2, nodeKindObj));
799
    return nodeKind;
800
}
801 72bf04c6 Norman Walsh
802
803 ead48a5d O'Neil Delpratt
XdmNode *SaxonProcessor::parseXmlFromFile(const char *source) {
804
805
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlFile",
806
                                                                                    "(Lnet/sf/saxon/s9api/Processor;Ljava/lang/String;Lnet/sf/saxon/s9api/SchemaValidator;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
807 72bf04c6 Norman Walsh
    if (!mID) {
808 ead48a5d O'Neil Delpratt
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFile()" << " not found" << std::endl;
809
        return nullptr;
810 72bf04c6 Norman Walsh
    }
811
//TODO SchemaValidator
812 ead48a5d O'Neil Delpratt
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID, proc,
813
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
814
                                                                                        cwd.c_str()), nullptr,
815
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
816
                                                                                        source));
817
    if (exceptionOccurred()) {
818 306962bc O'Neil Delpratt
        createException();
819 ead48a5d O'Neil Delpratt
820
    } else {
821
822
        XdmNode *value = new XdmNode(xdmNodei);
823
        return value;
824
    }
825
    return nullptr;
826 72bf04c6 Norman Walsh
}
827
828 ead48a5d O'Neil Delpratt
XdmNode *SaxonProcessor::parseXmlFromUri(const char *source) {
829 72bf04c6 Norman Walsh
830 ead48a5d O'Neil Delpratt
    jmethodID mID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(saxonCAPIClass, "parseXmlFile",
831
                                                                                    "(Lnet/sf/saxon/s9api/Processor;Ljava/lang/String;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmNode;");
832 72bf04c6 Norman Walsh
    if (!mID) {
833 ead48a5d O'Neil Delpratt
        std::cerr << "\nError: Saxonc.Dll " << "parseXmlFromUri()" << " not found" << std::endl;
834
        return nullptr;
835
    }
836
    jobject xdmNodei = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, mID, proc,
837
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
838
                                                                                        ""),
839
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
840
                                                                                        source));
841
    if (exceptionOccurred()) {
842 306962bc O'Neil Delpratt
        createException();
843 ead48a5d O'Neil Delpratt
    } else {
844
        XdmNode *value = new XdmNode(xdmNodei);
845
        return value;
846
    }
847
    return nullptr;
848 72bf04c6 Norman Walsh
}
849
850
851 ead48a5d O'Neil Delpratt
/**
852
   * Set a configuration property.
853
   *
854
   * @param name of the property
855
   * @param value of the property
856
   */
857
void SaxonProcessor::setConfigurationProperty(const char *name, const char *value) {
858
    if (name != nullptr) {
859
        configProperties.insert(
860
                std::pair<std::string, std::string>(std::string(name), std::string((value == nullptr ? "" : value))));
861 72bf04c6 Norman Walsh
    }
862 ead48a5d O'Neil Delpratt
}
863 72bf04c6 Norman Walsh
864 ead48a5d O'Neil Delpratt
void SaxonProcessor::clearConfigurationProperties() {
865
    configProperties.clear();
866
}
867 72bf04c6 Norman Walsh
868
869 ead48a5d O'Neil Delpratt
void SaxonProcessor::release() {
870
    if (SaxonProcessor::jvmCreatedCPP != 0) {
871
        SaxonProcessor::jvmCreatedCPP = 0;
872
        //std::cerr<<"SaxonProc: JVM finalized calling !"<<std::endl;
873
        finalizeJavaRT(SaxonProcessor::sxn_environ->jvm);
874 72bf04c6 Norman Walsh
875 55b80284 O'Neil Delpratt
        delete SaxonProcessor::sxn_environ;
876 ead48a5d O'Neil Delpratt
        /*clearParameters();
877
        clearProperties();*/
878
    } else {
879 4ee4355d O'Neil Delpratt
#ifdef DEBUG
880 ead48a5d O'Neil Delpratt
        std::cerr << "SaxonProc: JVM finalize not called!" << std::endl;
881 4ee4355d O'Neil Delpratt
#endif
882 ead48a5d O'Neil Delpratt
    }
883 72bf04c6 Norman Walsh
}
884
885
886 ead48a5d O'Neil Delpratt
/* ========= Factory method for Xdm ======== */
887 72bf04c6 Norman Walsh
888 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeStringValue(const char *str) {
889
    jobject obj = getJavaStringValue(SaxonProcessor::sxn_environ, str);
890
    static jmethodID mssID_atomic = nullptr;
891
    if (mssID_atomic == nullptr) {
892
        mssID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
893
                                                                                  "(Ljava/lang/String;)V"));
894
    }
895
    if (!mssID_atomic) {
896
        std::cerr << "XdmAtomic constructor (String)" << std::endl;
897
        return nullptr;
898
    }
899
    jobject obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mssID_atomic, obj));
900
    XdmAtomicValue *value = new XdmAtomicValue(obj2, "xs:string");
901 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
902
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj2);
903 ead48a5d O'Neil Delpratt
    return value;
904
}
905 72bf04c6 Norman Walsh
906 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeStringValue(std::string str) {
907
    jobject obj = getJavaStringValue(SaxonProcessor::sxn_environ, str.c_str());
908
    static jmethodID msID_atomic = nullptr;
909
    if (msID_atomic == nullptr) {
910
        msID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
911
                                                                                 "(Ljava/lang/String;)V"));
912
    }
913
    if (!msID_atomic) {
914
        std::cerr << "XdmAtomic constructor (String)" << std::endl;
915
        return nullptr;
916
    }
917
    jobject obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, msID_atomic, obj));
918
    XdmAtomicValue *value = new XdmAtomicValue(obj2, "xs:string");
919 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
920 ead48a5d O'Neil Delpratt
    return value;
921
}
922 72bf04c6 Norman Walsh
923 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeIntegerValue(int i) {
924
    //jobject obj = integerValue(*SaxonProcessor::sxn_environ, i);
925
    static jmethodID miiID_atomic = nullptr;
926
    if (miiID_atomic == nullptr) {
927
        miiID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
928
    }
929
    if (!miiID_atomic) {
930
        std::cerr << "XdmAtomic constructor (J)" << std::endl;
931
        return nullptr;
932
    }
933
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, miiID_atomic, (jlong) i));
934
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}integer");
935 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
936 ead48a5d O'Neil Delpratt
    return value;
937
}
938
939
XdmAtomicValue *SaxonProcessor::makeDoubleValue(double d) {
940
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
941
    static jmethodID mdID_atomic = nullptr;
942
    if (mdID_atomic == nullptr) {
943
        mdID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(D)V"));
944 72bf04c6 Norman Walsh
    }
945 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mdID_atomic, (jdouble) d));
946
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}double");
947 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
948 ead48a5d O'Neil Delpratt
    return value;
949
}
950 72bf04c6 Norman Walsh
951 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeFloatValue(float d) {
952
    //jobject obj = doubleValue(*SaxonProcessor::sxn_environ, d);
953
    static jmethodID mfID_atomic = nullptr;
954
    if (mfID_atomic == nullptr) {
955
        mfID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(F)V"));
956 72bf04c6 Norman Walsh
    }
957 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mfID_atomic, (jfloat) d));
958
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}float");
959 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
960 ead48a5d O'Neil Delpratt
    return value;
961
}
962 72bf04c6 Norman Walsh
963 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeLongValue(long l) {
964
    //jobject obj = longValue(*SaxonProcessor::sxn_environ, l);
965
    static jmethodID mlID_atomic = nullptr;
966
    if (mlID_atomic == nullptr) {
967
        mlID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(J)V"));
968 72bf04c6 Norman Walsh
    }
969 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mlID_atomic, (jlong) l));
970
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}long");
971 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
972 ead48a5d O'Neil Delpratt
    return value;
973
}
974 72bf04c6 Norman Walsh
975 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeBooleanValue(bool b) {
976
    //jobject obj = booleanValue(*SaxonProcessor::sxn_environ, b);
977
    static jmethodID mID_atomic = nullptr;
978
    if (mID_atomic == nullptr) {
979
        mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>", "(Z)V"));
980 72bf04c6 Norman Walsh
    }
981 ead48a5d O'Neil Delpratt
    jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, (jboolean) b));
982
    XdmAtomicValue *value = new XdmAtomicValue(obj, "Q{http://www.w3.org/2001/XMLSchema}boolean");
983 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
984 ead48a5d O'Neil Delpratt
    return value;
985
}
986
987
XdmAtomicValue *SaxonProcessor::makeQNameValue(const char *str) {
988
    jobject val = xdmValueAsObj(SaxonProcessor::sxn_environ, "QName", str);
989
    XdmAtomicValue *value = new XdmAtomicValue(val, "QName");
990 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(val);
991 ead48a5d O'Neil Delpratt
    return value;
992
}
993 72bf04c6 Norman Walsh
994 ead48a5d O'Neil Delpratt
XdmAtomicValue *SaxonProcessor::makeAtomicValue(const char *typei, const char *strValue) {
995
    jobject obj = xdmValueAsObj(SaxonProcessor::sxn_environ, typei, strValue);
996
    XdmAtomicValue *value = new XdmAtomicValue(obj, typei);
997 4ee4355d O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
998 ead48a5d O'Neil Delpratt
    return value;
999
}
1000
1001
const char *SaxonProcessor::getStringValue(XdmItem *item) {
1002
    const char *result = stringValue(SaxonProcessor::sxn_environ, item->getUnderlyingValue());
1003 72bf04c6 Norman Walsh
#ifdef DEBUG
1004 ead48a5d O'Neil Delpratt
    if(result == nullptr) {
1005
        std::cout<<"getStringValue of XdmItem is nullptr"<<std::endl;
1006
    } else {
1007
        std::cout<<"getStringValue of XdmItem is OK"<<std::endl;
1008
    }
1009 72bf04c6 Norman Walsh
#endif
1010
1011 ead48a5d O'Neil Delpratt
    return result;
1012
1013
}
1014 72bf04c6 Norman Walsh
1015
#if CVERSION_API_NO >= 123
1016
1017 ead48a5d O'Neil Delpratt
XdmArray *SaxonProcessor::makeArray(short *input, int length) {
1018
    if (input == nullptr) {
1019
        std::cerr << "Error found when converting string to XdmArray" << std::endl;
1020
        return nullptr;
1021
    }
1022
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1023
    static jmethodID mmssID = nullptr;
1024
    if (mmssID == nullptr) {
1025
        mmssID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1026
                                                                                 "([S)Lnet/sf/saxon/s9api/XdmArray;");
1027
    }
1028
    if (!mmssID) {
1029
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([S)" << " not found" << std::endl;
1030
        return nullptr;
1031
    }
1032 72bf04c6 Norman Walsh
1033
1034 ead48a5d O'Neil Delpratt
    jshortArray sArray = nullptr;
1035 72bf04c6 Norman Walsh
1036 ead48a5d O'Neil Delpratt
    sArray = SaxonProcessor::sxn_environ->env->NewShortArray((jint) length);
1037
    jshort fill[length];
1038
    for (int i = 0; i < length; i++) {
1039
        fill[i] = input[i];
1040
    }
1041
    SaxonProcessor::sxn_environ->env->SetShortArrayRegion(sArray, 0, length, fill);
1042 72bf04c6 Norman Walsh
1043 ead48a5d O'Neil Delpratt
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmssID, sArray);
1044
    if (!xdmArrayi) {
1045
        std::cerr << "Error found when converting string to XdmArray";
1046
        return nullptr;
1047
    }
1048 72bf04c6 Norman Walsh
1049 ead48a5d O'Neil Delpratt
    if (exceptionOccurred()) {
1050 306962bc O'Neil Delpratt
        createException();
1051 ead48a5d O'Neil Delpratt
    } else {
1052
        XdmArray *value = new XdmArray(xdmArrayi);
1053
        return value;
1054
    }
1055
    return nullptr;
1056
}
1057 72bf04c6 Norman Walsh
1058
1059 ead48a5d O'Neil Delpratt
XdmArray *SaxonProcessor::makeArray(int *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 mmiiID = nullptr;
1066
    if (mmiiID == nullptr) {
1067
        mmiiID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1068
                                                                                 "([I)Lnet/sf/saxon/s9api/XdmArray;");
1069
    }
1070
    if (!mmiiID) {
1071
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([I)" << " not found" << std::endl;
1072
        return nullptr;
1073
    }
1074
1075
1076
    jintArray iArray = nullptr;
1077
1078
    iArray = SaxonProcessor::sxn_environ->env->NewIntArray((jint) length);
1079
    jint fill[length];
1080
    for (int i = 0; i < length; i++) {
1081
        fill[i] = input[i];
1082
    }
1083
    SaxonProcessor::sxn_environ->env->SetIntArrayRegion(iArray, 0, length, fill);
1084
1085
1086
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiiID, iArray);
1087
    if (!xdmArrayi) {
1088
        std::cerr << "Error found when converting string to XdmArray";
1089
        return nullptr;
1090
    }
1091
    if (exceptionOccurred()) {
1092 306962bc O'Neil Delpratt
        createException();
1093 ead48a5d O'Neil Delpratt
    } else {
1094
        XdmArray *value = new XdmArray(xdmArrayi);
1095
        return value;
1096
    }
1097
    return nullptr;
1098
1099
}
1100
1101
XdmArray *SaxonProcessor::makeArray(long *input, int length) {
1102
    if (input == nullptr) {
1103
        std::cerr << "Error found when converting string to XdmArray";
1104
        return nullptr;
1105
    }
1106
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1107
    static jmethodID mmiID = nullptr;
1108
    if (mmiID == nullptr) {
1109
        mmiID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1110
                                                                                "([J)Lnet/sf/saxon/s9api/XdmArray;");
1111
    }
1112
    if (!mmiID) {
1113
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([J)" << " not found" << std::endl;
1114
        return nullptr;
1115
    }
1116
1117
1118
    jlongArray lArray = nullptr;
1119
1120
    lArray = SaxonProcessor::sxn_environ->env->NewLongArray((jint) length);
1121
    jlong fill[length];
1122
    for (int i = 0; i < length; i++) {
1123
        fill[i] = input[i];
1124
    }
1125
    SaxonProcessor::sxn_environ->env->SetLongArrayRegion(lArray, 0, length, fill);
1126
1127
1128
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmiID, lArray);
1129
    if (!xdmArrayi) {
1130
        std::cerr << "Error found when converting string to XdmArray";
1131
        return nullptr;
1132
    }
1133
    if (exceptionOccurred()) {
1134 306962bc O'Neil Delpratt
        createException();
1135 ead48a5d O'Neil Delpratt
    } else {
1136
        XdmArray *value = new XdmArray(xdmArrayi);
1137
        return value;
1138
    }
1139
    return nullptr;
1140
}
1141
1142
1143
XdmArray *SaxonProcessor::makeArray(bool *input, int length) {
1144
    if (input == nullptr) {
1145
        std::cerr << "Error found when converting string to XdmArray";
1146
        return nullptr;
1147
    }
1148
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1149
    static jmethodID mmbID = nullptr;
1150
    if (mmbID == nullptr) {
1151
        mmbID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1152
                                                                                "([Z)Lnet/sf/saxon/s9api/XdmArray;");
1153
    }
1154
    if (!mmbID) {
1155
        std::cerr << "\nError: Saxonc.Dll " << "makeArray([Z)" << " not found" << std::endl;
1156
        return nullptr;
1157
    }
1158
1159
1160
    jbooleanArray bArray = nullptr;
1161
1162
    bArray = SaxonProcessor::sxn_environ->env->NewBooleanArray((jint) length);
1163
    jboolean fill[length];
1164
    for (int i = 0; i < length; i++) {
1165
        fill[i] = input[i];
1166
    }
1167
    SaxonProcessor::sxn_environ->env->SetBooleanArrayRegion(bArray, 0, length, fill);
1168
1169
1170
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmbID, bArray);
1171
    if (!xdmArrayi) {
1172
        std::cerr << "Error found when converting string to XdmArray";
1173
        return nullptr;
1174
    }
1175
    if (exceptionOccurred()) {
1176 306962bc O'Neil Delpratt
        createException();
1177 ead48a5d O'Neil Delpratt
    } else {
1178
        XdmArray *value = new XdmArray(xdmArrayi);
1179
        return value;
1180
    }
1181
    return nullptr;
1182
1183
1184
}
1185
1186
1187
XdmArray *SaxonProcessor::makeArray(const char **input, int length) {
1188
    if (input == nullptr || length <= 0) {
1189
        std::cerr << "Error found when converting string to XdmArray";
1190
        return nullptr;
1191
    }
1192
    jobject obj = nullptr;
1193
    jclass xdmArrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmArray;");
1194
    jmethodID mmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmArrayClass, "makeArray",
1195
                                                                                     "([Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmArray;");
1196
1197
    jmethodID mID_atomic = (jmethodID) (SaxonProcessor::sxn_environ->env->GetMethodID(xdmAtomicClass, "<init>",
1198
                                                                                      "(Ljava/lang/String;)V"));
1199
    jobjectArray valueArray = nullptr;
1200
    jobject obj2 = nullptr;
1201
    valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length, xdmAtomicClass, 0);
1202
    for (int i = 0; i < length; i++) {
1203
        if (input[i] == nullptr) {
1204
            std::cerr << "Error found when converting string to XdmArray";
1205
            return nullptr;
1206 72bf04c6 Norman Walsh
        }
1207 ead48a5d O'Neil Delpratt
        obj = getJavaStringValue(SaxonProcessor::sxn_environ, input[i]);
1208
        obj2 = (jobject) (SaxonProcessor::sxn_environ->env->NewObject(xdmAtomicClass, mID_atomic, obj));
1209
        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i, obj2);
1210
    }
1211 72bf04c6 Norman Walsh
1212 ead48a5d O'Neil Delpratt
    jobject xdmArrayi = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmArrayClass, mmID, valueArray);
1213
    if (!xdmArrayi) {
1214
        std::cerr << "Error found when converting string to XdmArray";
1215
        return nullptr;
1216
    }
1217 72bf04c6 Norman Walsh
1218 ead48a5d O'Neil Delpratt
    if (exceptionOccurred()) {
1219
        checkAndCreateException(saxonCAPIClass);
1220
    } else {
1221
        XdmArray *value = new XdmArray(xdmArrayi);
1222
        return value;
1223
    }
1224
    return nullptr;
1225
}
1226 72bf04c6 Norman Walsh
1227
1228 901b6eca O'Neil Delpratt
XdmMap *SaxonProcessor::makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap) {
1229 ead48a5d O'Neil Delpratt
    jobjectArray keyArray = nullptr;
1230
    jobjectArray valueArray = nullptr;
1231
    jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
1232
                                      "java/lang/Object");
1233 72bf04c6 Norman Walsh
1234 ead48a5d O'Neil Delpratt
    int size = dataMap.size();
1235 72bf04c6 Norman Walsh
1236 ead48a5d O'Neil Delpratt
    if (size > 0) {
1237 72bf04c6 Norman Walsh
1238 ead48a5d O'Neil Delpratt
        keyArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1239
                                                                    objectClass, 0);
1240
        valueArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1241
                                                                      objectClass, 0);
1242
        int i = 0;
1243
        for (std::map<XdmAtomicValue *, XdmValue *>::iterator iter =
1244
                dataMap.begin(); iter != dataMap.end(); ++iter, i++) {
1245 72bf04c6 Norman Walsh
1246
1247 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(keyArray, i,
1248
                                                                    (iter->first)->getUnderlyingValue());
1249 72bf04c6 Norman Walsh
1250 ead48a5d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(valueArray, i,
1251
                                                                    (iter->second)->getUnderlyingValue());
1252 72bf04c6 Norman Walsh
1253 ead48a5d O'Neil Delpratt
        }
1254
        jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
1255
        jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass, "makeXdmMap",
1256
                                                                                         "([Lnet/sf/saxon/s9api/XdmAtomicValue;[Lnet/sf/saxon/s9api/XdmValue;)Lnet/sf/saxon/s9api/XdmMap;");
1257
        if (!xmID) {
1258
            std::cerr << "Error: SaxonDll." << "makeXdmMap"
1259
                      << " not found\n" << std::endl;
1260
            return nullptr;
1261
        }
1262 72bf04c6 Norman Walsh
1263
1264 ead48a5d O'Neil Delpratt
        jobject results = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
1265
                                                                                              keyArray, valueArray));
1266 901b6eca O'Neil Delpratt
        if (exceptionOccurred() || !results) {
1267
            checkAndCreateException(saxonCAPIClass);
1268
        } else {
1269
            return new XdmMap(results);
1270
        }
1271 ead48a5d O'Neil Delpratt
    }
1272 72bf04c6 Norman Walsh
1273 901b6eca O'Neil Delpratt
    return nullptr;
1274 ead48a5d O'Neil Delpratt
}
1275 72bf04c6 Norman Walsh
1276
#endif
1277