Project

Profile

Help

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

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

1 72bf04c6 Norman Walsh
#include "XQueryProcessor.h"
2
#include "XdmValue.h"
3
#include "XdmItem.h"
4
#include "XdmNode.h"
5
#include "XdmAtomicValue.h"
6 01d6fdb6 O'Neil Delpratt
#include "XdmFunctionItem.h"
7
#include "XdmMap.h"
8
#include "XdmArray.h"
9
10
11
#ifdef MEM_DEBUG
12
#define new new(__FILE__, __LINE__)
13
#endif
14 72bf04c6 Norman Walsh
15
    XQueryProcessor::XQueryProcessor() {
16
        SaxonProcessor *p = new SaxonProcessor(false);
17
        XQueryProcessor(p, "");
18
     }
19
20 01d6fdb6 O'Neil Delpratt
21
    XQueryProcessor::~XQueryProcessor(){
22
        clearProperties();
23
        clearParameters();
24
        SaxonProcessor::sxn_environ->env->DeleteGlobalRef(cppXQ);
25
        cwdXQ.erase();
26
        exceptionClear();
27
    }
28
29 72bf04c6 Norman Walsh
    XQueryProcessor::XQueryProcessor(SaxonProcessor *p, std::string curr) {
30
    proc = p;
31 01d6fdb6 O'Neil Delpratt
        exception = nullptr;
32 72bf04c6 Norman Walsh
     cppClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XQueryEngine");
33
34
35 01d6fdb6 O'Neil Delpratt
    jobject tempcppXQ = createSaxonProcessor2 (SaxonProcessor::sxn_environ->env, cppClass, "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
36
    if(tempcppXQ) {
37
        cppXQ = SaxonProcessor::sxn_environ->env->NewGlobalRef(tempcppXQ);
38
    } else {
39
        createException("Error: Failed to create the Xslt30Processor internal object");
40
        std::cerr<<"Error in XQueryProcessor XXXXXXX"<<std::endl;
41
    }
42 72bf04c6 Norman Walsh
    
43
#ifdef DEBUG
44
        jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
45
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
46
#endif
47
48 01d6fdb6 O'Neil Delpratt
49 72bf04c6 Norman Walsh
   // outputfile1 = "";
50
        if(!(proc->cwd.empty()) && curr.empty()){
51
                cwdXQ = proc->cwd;
52
        } else {
53
                cwdXQ = curr;
54
        }
55
}
56
57
58
59
XQueryProcessor::XQueryProcessor(const XQueryProcessor &other) {
60
    cwdXQ = other.cwdXQ;
61
        proc = other.proc; //TODO check thread safety
62
        jclass  cppClass;
63
        jobject cppXQ;
64
        std::map<std::string,XdmValue*> parameters; /*!< map of parameters used for the transformation as (string, value) pairs */
65
        std::map<std::string,std::string> properties;
66
67
68
}
69
70
71 ead48a5d O'Neil Delpratt
XQueryProcessor * XQueryProcessor::clone() {
72 72bf04c6 Norman Walsh
      XQueryProcessor * proc = new XQueryProcessor(*this);
73
      return proc;
74
75
76
}
77
78
79
std::map<std::string,XdmValue*>& XQueryProcessor::getParameters(){
80
        std::map<std::string,XdmValue*>& ptr = parameters;
81
        return ptr;
82
}
83
84
std::map<std::string,std::string>& XQueryProcessor::getProperties(){
85
        std::map<std::string,std::string> &ptr = properties;
86
        return ptr;
87
}
88
89
90
    /**
91
     * Set the source document for the query
92
    */
93
    void XQueryProcessor::setContextItem(XdmItem * value){
94 ead48a5d O'Neil Delpratt
            if(value != nullptr){
95 72bf04c6 Norman Walsh
         value->incrementRefCount();
96
              parameters["node"] = (XdmValue *)value;
97
            }
98
    }
99
100
101
     void XQueryProcessor::declareNamespace(const char *prefix, const char * uri){
102 ead48a5d O'Neil Delpratt
        if (prefix == nullptr || uri == nullptr) {
103 72bf04c6 Norman Walsh
                    return;
104
        }  else {
105
            //setProperty("ns-prefix", uri);
106
             int s = properties.size();
107 ead48a5d O'Neil Delpratt
             std::string skey = std::string("ns-prefix:") + prefix;
108 72bf04c6 Norman Walsh
             properties.insert(std::pair<std::string, std::string>(skey, std::string(uri)));
109
110
             if(s == properties.size()) {
111
                 std::map<std::string, std::string>::iterator it;
112
                 it = properties.find(skey);
113
                 if (it != properties.end()) {
114
                       properties.erase(skey);
115
                       properties[skey] = std::string(uri);
116
                 }
117
             }
118
119
        }
120
}
121
122
123
    /**
124
     * Set the source document for the query
125
    */
126
    void XQueryProcessor::setContextItemFromFile(const char * ifile){
127
        setProperty("s", ifile);
128
    }
129
130
    /**
131
     * Set the output file where the result is sent
132
    */
133
    void XQueryProcessor::setOutputFile(const char* ofile){
134
      // outputfile1 = std::string(ofile); 
135
       setProperty("o", ofile);
136
    }
137
138
    /**
139
     * Set a parameter value used in the query
140
     *
141
     * @param name  of the parameter, as a string
142 ead48a5d O'Neil Delpratt
     * @param value of the query parameter, or nullptr to clear a previously set value
143 72bf04c6 Norman Walsh
     */
144
    void XQueryProcessor::setParameter(const char * name, XdmValue*value){
145 ead48a5d O'Neil Delpratt
        if(value != nullptr){
146 72bf04c6 Norman Walsh
                value->incrementRefCount();
147
                int s = parameters.size();
148 ead48a5d O'Neil Delpratt
                std::string skey = "param:"+std::string(name);
149 72bf04c6 Norman Walsh
                parameters[skey] = value;
150
                if(s == parameters.size()) {
151
            std::map<std::string, XdmValue*>::iterator it;
152
            it = parameters.find(skey);
153
            if (it != parameters.end()) {
154
                XdmValue * valuei = it->second;
155 146cfacb O'Neil Delpratt
                if(valuei != nullptr) {
156
                    valuei->decrementRefCount();
157
                    if (valuei->getRefCount() < 1) {
158
                        delete value;
159
                    }
160
                    parameters.erase(skey);
161
                    parameters[skey] = value;
162 72bf04c6 Norman Walsh
                }
163
            }
164
                }
165
        } 
166
    }
167
168
169
    /**
170
     * Remove a parameter (name, value) pair
171
     *
172
     * @param namespacei currently not used
173
     * @param name  of the parameter
174
     * @return bool - outcome of the romoval
175
     */
176
    bool XQueryProcessor::removeParameter(const char * name){
177
        return (bool)(parameters.erase("param:"+std::string(name)));
178
    }
179
    /**
180
     * Set a property.
181
     *
182
     * @param name of the property
183
     * @param value of the property
184
     */
185
    void XQueryProcessor::setProperty(const char * name, const char * value){
186
#ifdef DEBUG        
187 ead48a5d O'Neil Delpratt
        if(value == nullptr) {
188
                std::cerr<<"XQueryProc setProperty is nullptr"<<std::endl;
189 72bf04c6 Norman Walsh
        }
190
#endif
191 ead48a5d O'Neil Delpratt
            if(name != nullptr) {
192 72bf04c6 Norman Walsh
                int s = properties.size();
193 79d12c83 O'Neil Delpratt
                    std::string skey = std::string(name);
194 ead48a5d O'Neil Delpratt
                    properties.insert(std::pair<std::string, std::string>(skey, std::string((value == nullptr ? "" : value))));
195 72bf04c6 Norman Walsh
196
                    if(s == properties.size()) {
197
                std::map<std::string, std::string>::iterator it;
198
                it = properties.find(skey);
199
                if (it != properties.end()) {
200
                    properties.erase(skey);
201 ead48a5d O'Neil Delpratt
                    properties[skey] = std::string((value == nullptr ? "" : value));
202 72bf04c6 Norman Walsh
                }
203
                    }
204
            }
205
206
207
208
    }
209
210
    void XQueryProcessor::clearParameters(bool delVal){
211
        if(delVal){
212 146cfacb O'Neil Delpratt
                       for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++) {
213
                XdmValue *value = itr->second;
214
                if (value != nullptr) {
215 72bf04c6 Norman Walsh
#ifdef DEBUG
216 146cfacb O'Neil Delpratt
                    std::cerr<<"XQueryProc.clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
217 72bf04c6 Norman Walsh
#endif
218 146cfacb O'Neil Delpratt
                        delete value;
219 01d6fdb6 O'Neil Delpratt
                        value = nullptr;
220 146cfacb O'Neil Delpratt
                }
221
            }
222 72bf04c6 Norman Walsh
        }
223
        parameters.clear();
224
    }
225
226
   void XQueryProcessor::clearProperties(){
227
        properties.clear();
228
        //outputfile1.clear();
229
   }
230
231
232
   void XQueryProcessor::setcwd(const char* dir){
233
    cwdXQ = std::string(dir);
234
   }
235
236
    void XQueryProcessor::setQueryBaseURI(const char * baseURI){
237
        setProperty("base", baseURI);
238
    }
239
240
241
    void XQueryProcessor::setUpdating(bool updating){
242
     
243
            jmethodID mID =
244
                    (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "setUpdating",
245
                                    "(Z)V");
246
            if (!mID) {
247
            std::cerr << "Error: Saxonc library." << "setUpdating" << " not found\n"
248
                            << std::endl;
249
250
            } else {
251
252
                            SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXQ, mID,
253
                                            (jboolean)updating);
254
            }
255
256
    }
257
258
    void XQueryProcessor::executeQueryToFile(const char * infilename, const char * ofilename, const char * query){
259 ead48a5d O'Neil Delpratt
260 72bf04c6 Norman Walsh
261
        jmethodID mID = (jmethodID)SaxonProcessor::sxn_environ->env->GetMethodID (cppClass,"executeQueryToFile", "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
262
         if (!mID) {
263 7728ae0b O'Neil Delpratt
        std::cerr<<"Error: "<<getDllname()<<".executeQueryToFile"<<" not found\n"<<std::endl;
264 72bf04c6 Norman Walsh
    } else {
265 01d6fdb6 O'Neil Delpratt
        if(cppXQ == nullptr) {
266
#if defined(DEBUG)
267
            std::cerr << "The Java XQueryEngine object (i.e. cppXQ) is NULL - Possible exception thrown" << std::endl;
268
#endif
269
            createException("The Java XQueryEngine object (i.e. cppXQ) is NULL - Possible exception thrown");
270
            return;
271
        }
272 ead48a5d O'Neil Delpratt
        jobjectArray stringArray = nullptr;
273
        jobjectArray objectArray = nullptr;
274 72bf04c6 Norman Walsh
275
        int size = parameters.size() + properties.size();
276 ead48a5d O'Neil Delpratt
        if(query!= nullptr) size++;
277
        if(infilename!= nullptr) size++;
278 72bf04c6 Norman Walsh
        if(size >0) {
279
280
           int i=0;
281 01d6fdb6 O'Neil Delpratt
           jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
282 72bf04c6 Norman Walsh
           jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
283
           objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray( (jint)size, objectClass, 0 );
284
           stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray( (jint)size, stringClass, 0 );
285 ead48a5d O'Neil Delpratt
           if(query!= nullptr) {
286 72bf04c6 Norman Walsh
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF("qs") );
287
                  SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF(query));
288
             i++;        
289
           }
290 ead48a5d O'Neil Delpratt
           if(infilename!= nullptr) {
291 72bf04c6 Norman Walsh
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF("s") );
292
                  SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF(infilename));
293
             i++;        
294
           }
295
           for(std::map<std::string, XdmValue* >::iterator iter=parameters.begin(); iter!=parameters.end(); ++iter, i++) {
296
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF( (iter->first).c_str() ) );
297
                bool checkCast = SaxonProcessor::sxn_environ->env->IsInstanceOf((iter->second)->getUnderlyingValue(), lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmValueForCpp") );
298
299
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, (jobject)((iter->second)->getUnderlyingValue()) );
300
           }
301
             for(std::map<std::string, std::string >::iterator iter=properties.begin(); iter!=properties.end(); ++iter, i++) {
302
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF( (iter->first).c_str()  ));
303
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, (jobject)(SaxonProcessor::sxn_environ->env->NewStringUTF((iter->second).c_str())) );
304
           }
305
        }
306
307 01d6fdb6 O'Neil Delpratt
         SaxonProcessor::sxn_environ->env->CallVoidMethod(cppXQ, mID, SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXQ.c_str()), SaxonProcessor::sxn_environ->env->NewStringUTF(ofilename), stringArray, objectArray );
308 72bf04c6 Norman Walsh
          SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
309
          SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
310
311 01d6fdb6 O'Neil Delpratt
          if(proc->exceptionOccurred()) {
312
          createException();
313
      }
314 72bf04c6 Norman Walsh
         
315
  }
316
317
318
   }
319
320
321
    XdmValue * XQueryProcessor::executeQueryToValue(const char * infilename, const char * query){
322 ead48a5d O'Neil Delpratt
323 72bf04c6 Norman Walsh
 jmethodID mID = (jmethodID)SaxonProcessor::sxn_environ->env->GetMethodID (cppClass,"executeQueryToValue", "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
324
 if (!mID) {
325
        std::cerr<<"Error: MyClassInDll."<<"executeQueryToValue"<<" not found\n"<<std::endl;
326
    } else {
327 01d6fdb6 O'Neil Delpratt
     if(cppXQ == nullptr) {
328
#if defined(DEBUG)
329
         std::cerr << "The Java XQueryEngine object (i.e. cppXQ) is NULL - Possible exception thrown" << std::endl;
330
#endif
331
         createException("The Java XQueryEngine object (i.e. cppXQ) is NULL - Possible exception thrown");
332
         return nullptr;
333
     }
334 ead48a5d O'Neil Delpratt
        jobjectArray stringArray = nullptr;
335
        jobjectArray objectArray = nullptr;
336 72bf04c6 Norman Walsh
337
        int size = parameters.size() + properties.size();
338 ead48a5d O'Neil Delpratt
        if(query!= nullptr) size++;
339
        if(infilename!= nullptr) size++;
340 72bf04c6 Norman Walsh
        if(size >0) {
341
           int i=0;
342
           jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
343
           jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
344
           objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray( (jint)size, objectClass, 0 );
345
           stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray( (jint)size, stringClass, 0 );
346
347 ead48a5d O'Neil Delpratt
           if(query!= nullptr) {
348 72bf04c6 Norman Walsh
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF("qs") );
349
                  SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF(query));
350
             i++;        
351
           }
352 ead48a5d O'Neil Delpratt
           if(infilename!= nullptr) {
353 72bf04c6 Norman Walsh
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF("s") );
354
                  SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF(infilename));
355
             i++;        
356
           }
357
           for(std::map<std::string, XdmValue* >::iterator iter=parameters.begin(); iter!=parameters.end(); ++iter, i++) {
358
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF( (iter->first).c_str() ) );
359
                bool checkCast = SaxonProcessor::sxn_environ->env->IsInstanceOf((iter->second)->getUnderlyingValue(), lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmValueForCpp") );
360
361
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, (jobject)((iter->second)->getUnderlyingValue()) );
362
           }
363
             for(std::map<std::string, std::string >::iterator iter=properties.begin(); iter!=properties.end(); ++iter, i++) {
364
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF( (iter->first).c_str()  ));
365
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, (jobject)(SaxonProcessor::sxn_environ->env->NewStringUTF((iter->second).c_str())) );
366
           }
367
        }
368
369
          jobject result = (jobject)(SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXQ, mID, SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXQ.c_str()), stringArray, objectArray ));
370
          SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
371
          SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
372
    if(result) {
373
                jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
374
                jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
375
                jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
376 01d6fdb6 O'Neil Delpratt
        jclass mapItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
377
                                           "net/sf/saxon/s9api/XdmMap");
378
        jclass arrayItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
379
                                             "net/sf/saxon/s9api/XdmArray");
380 ead48a5d O'Neil Delpratt
                XdmValue * value = nullptr;
381
                XdmItem * xdmItem = nullptr;
382 72bf04c6 Norman Walsh
383
                if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
384
                                xdmItem = new XdmAtomicValue(result);
385
                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
386
                                return xdmItem;
387
388
                        } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
389
                                xdmItem = new XdmNode(result);
390
                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
391
                                return xdmItem;
392
                        
393
                        } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
394 01d6fdb6 O'Neil Delpratt
                xdmItem = new XdmFunctionItem(result);
395
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
396
                return xdmItem;
397
                         } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, mapItemClass) == JNI_TRUE) {
398
            xdmItem = new XdmMap(result);
399
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
400
        return xdmItem;
401
        } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, arrayItemClass) == JNI_TRUE) {
402
        xdmItem = new XdmArray(result);
403
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
404
            return xdmItem;
405
        } else {
406 72bf04c6 Norman Walsh
                                value = new XdmValue(result, true);
407
                                return value;
408
                        }
409
                        value = new XdmValue();
410
                        value->addXdmItem(xdmItem);
411
                        return value;
412
     } else {
413
           
414
            exception = proc->checkAndCreateException(cppClass);
415
             } 
416
  }
417 ead48a5d O'Neil Delpratt
  return nullptr;
418 72bf04c6 Norman Walsh
419
}
420
421
    const char * XQueryProcessor::executeQueryToString(const char * infilename, const char * query){
422 ead48a5d O'Neil Delpratt
423 01d6fdb6 O'Neil Delpratt
 jmethodID mID = (jmethodID)SaxonProcessor::sxn_environ->env->GetMethodID (cppClass,"executeQueryToString", "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)[B");
424 72bf04c6 Norman Walsh
 if (!mID) {
425
        std::cerr<<"Error: MyClassInDll."<<"executeQueryToString"<<" not found\n"<<std::endl;
426
    } else {
427 01d6fdb6 O'Neil Delpratt
     if(cppXQ == nullptr) {
428
#if defined(DEBUG)
429
         std::cerr << "The Java XQueryEngine object (i.e. cppXQ) is NULL - Possible exception thrown" << std::endl;
430
#endif
431
         createException("The Java XQueryEngine object (i.e. cppXQ) is NULL - Possible exception thrown");
432
         return nullptr;
433
     }
434 ead48a5d O'Neil Delpratt
        jobjectArray stringArray = nullptr;
435
        jobjectArray objectArray = nullptr;
436 72bf04c6 Norman Walsh
437
        int size = parameters.size() + properties.size();
438 ead48a5d O'Neil Delpratt
        if(query!= nullptr) size++;
439
        if(infilename!= nullptr) size++;
440 72bf04c6 Norman Walsh
        if(size >0) {
441
           int i=0;
442
           jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
443
           jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
444
           objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray( (jint)size, objectClass, 0 );
445
           stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray( (jint)size, stringClass, 0 );
446
447 ead48a5d O'Neil Delpratt
           if(query!= nullptr) {
448 72bf04c6 Norman Walsh
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF("qs") );
449
                  SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF(query));
450
             i++;        
451
           }
452 ead48a5d O'Neil Delpratt
           if(infilename!= nullptr) {
453 72bf04c6 Norman Walsh
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF("s") );
454
                  SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF(infilename));
455
             i++;        
456
           }
457
           for(std::map<std::string, XdmValue* >::iterator iter=parameters.begin(); iter!=parameters.end(); ++iter, i++) {
458
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF( (iter->first).c_str() ) );
459
                bool checkCast = SaxonProcessor::sxn_environ->env->IsInstanceOf((iter->second)->getUnderlyingValue(), lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmValueForCpp") );
460
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, (jobject)((iter->second)->getUnderlyingValue()) );
461
           }
462
             for(std::map<std::string, std::string >::iterator iter=properties.begin(); iter!=properties.end(); ++iter, i++) {
463
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF( (iter->first).c_str()  ));
464
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, (jobject)(SaxonProcessor::sxn_environ->env->NewStringUTF((iter->second).c_str())) );
465
           }
466
        }
467
468 01d6fdb6 O'Neil Delpratt
     jbyteArray result = nullptr;
469
     jobject obj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXQ, mID, SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXQ.c_str()), stringArray, objectArray ));
470 72bf04c6 Norman Walsh
          SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
471
          SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
472
473 01d6fdb6 O'Neil Delpratt
     if (obj) {
474
         result = (jbyteArray) obj;
475
     }
476
     if (result) {
477
         jboolean isCopy = false;
478
         jbyte* b = SaxonProcessor::sxn_environ->env->GetByteArrayElements(result, &isCopy);
479
         jsize num_bytes = SaxonProcessor::sxn_environ->env->GetArrayLength(result);
480
481
         const char *str = new char[num_bytes];
482
         memcpy ((void *) str, b , num_bytes );
483
484
         SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
485
         return str;
486
     } else {
487
                    createException();
488 72bf04c6 Norman Walsh
                           
489
                     }
490
  }
491 ead48a5d O'Neil Delpratt
  return nullptr;
492 72bf04c6 Norman Walsh
493
494
    }
495
496
497
    const char * XQueryProcessor::runQueryToString(){
498 ead48a5d O'Neil Delpratt
        return executeQueryToString(nullptr, nullptr);
499 72bf04c6 Norman Walsh
500
    }
501
502
503
    XdmValue * XQueryProcessor::runQueryToValue(){
504 ead48a5d O'Neil Delpratt
        return executeQueryToValue(nullptr, nullptr);
505 72bf04c6 Norman Walsh
   }
506
507
    void XQueryProcessor::runQueryToFile(){
508 ead48a5d O'Neil Delpratt
        executeQueryToFile(nullptr, nullptr, nullptr);
509 72bf04c6 Norman Walsh
   }
510
511
    void XQueryProcessor::setQueryFile(const char * ofile){
512
           //outputfile1 = std::string(ofile); 
513
           setProperty("q", ofile);
514
    }
515
516
   void XQueryProcessor::setQueryContent(const char* content){
517
          // outputfile1 = std::string(content); 
518
           setProperty("qs", content);
519
  }
520
521
522
523
void XQueryProcessor::exceptionClear(){
524 01d6fdb6 O'Neil Delpratt
    SaxonProcessor::sxn_environ->env->ExceptionClear();
525 ead48a5d O'Neil Delpratt
        if(exception != nullptr) {
526 72bf04c6 Norman Walsh
                delete exception;
527 ead48a5d O'Neil Delpratt
                exception = nullptr;
528 01d6fdb6 O'Neil Delpratt
529 72bf04c6 Norman Walsh
        }
530
531 01d6fdb6 O'Neil Delpratt
532 72bf04c6 Norman Walsh
   
533
 
534
}
535
536
bool XQueryProcessor::exceptionOccurred(){
537 01d6fdb6 O'Neil Delpratt
    return proc->exceptionOccurred() || exception != nullptr;
538 72bf04c6 Norman Walsh
}
539
540
541
const char * XQueryProcessor::getErrorCode() {
542 ead48a5d O'Neil Delpratt
        if(exception == nullptr) {return nullptr;}
543 72bf04c6 Norman Walsh
        return exception->getErrorCode();
544
}
545
546
const char * XQueryProcessor::getErrorMessage(){
547 ead48a5d O'Neil Delpratt
        if(exception == nullptr) {return nullptr;}
548
        return exception->getMessage();
549 72bf04c6 Norman Walsh
}
550
551
const char* XQueryProcessor::checkException(){
552 ead48a5d O'Neil Delpratt
        /*if(proc->exception == nullptr) {
553 72bf04c6 Norman Walsh
                proc->exception = proc->checkForException(SaxonProcessor::sxn_environ->env, cppClass, cppXQ);
554
        }
555
        return proc->exception;*/
556 4ee4355d O'Neil Delpratt
        return proc->checkException();
557 72bf04c6 Norman Walsh
}
558
559
560
561 79d12c83 O'Neil Delpratt
SaxonApiException* XQueryProcessor::getException() {
562
    return exception;
563
}
564 01d6fdb6 O'Neil Delpratt
565
void XQueryProcessor::createException(const char * message) {
566
    if(exception != nullptr) {
567
        delete exception;
568
        exception = nullptr;
569
    }
570
    if(message == nullptr) {
571
        exception = proc->checkAndCreateException(cppClass);
572
    } else {
573
        exception = new SaxonApiException(message);
574
    }
575
576
}