Project

Profile

Help

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

he / latest9.6 / hec / Saxon.C.API / XQueryProcessor.cpp @ 0608038f

1
#include "XQueryProcessor.h"
2
#include "XdmValue.h"
3
#include "XdmItem.h"
4
#include "XdmNode.h"
5
#include "XdmAtomicValue.h"
6

    
7
    XQueryProcessor::XQueryProcessor() {
8
        SaxonProcessor *p = new SaxonProcessor(false);
9
        XQueryProcessor(p, "");
10
     }
11

    
12
    XQueryProcessor::XQueryProcessor(SaxonProcessor *p, std::string curr) {
13
    proc = p;
14

    
15
  
16
     cppClass = lookForClass(environ->env, "net/sf/saxon/option/cpp/XQueryEngine");
17

    
18

    
19
    cppXQ = createSaxonProcessor2 (environ->env, cppClass, "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
20
    
21
#ifdef DEBUG
22
        jmethodID debugMID = environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
23
        environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
24
#endif
25

    
26
    proc->exception = NULL;
27
    outputfile1 = "";
28
        if(!(proc->cwd.empty()) && curr.empty()){
29
                cwdXQ = proc->cwd;
30
        } else {
31
                cwdXQ = curr;
32
        }
33
        queryFileExists = false;
34
}
35

    
36

    
37
std::map<std::string,XdmValue*>& XQueryProcessor::getParameters(){
38
        std::map<std::string,XdmValue*>& ptr = parameters;
39
        return ptr;
40
}
41

    
42
std::map<std::string,std::string>& XQueryProcessor::getProperties(){
43
        std::map<std::string,std::string> &ptr = properties;
44
        return ptr;
45
}
46

    
47

    
48
    /**
49
     * Set the source document for the query
50
    */
51
    void XQueryProcessor::setContextItem(XdmItem * value){
52
            if(value != NULL){
53
         value->incrementRefCount();
54
              parameters["node"] = (XdmValue *)value;
55
            }
56
    }
57

    
58

    
59
     void XQueryProcessor::declareNamespace(const char *prefix, const char * uri){
60
        if (prefix == NULL || uri == NULL) {
61
                return;
62
        }
63
        jmethodID mID =
64
                (jmethodID) environ->env->GetMethodID(cppClass, "declareNamespace",
65
                                "(Ljava/lang/String;Ljava/lang/String;)V");
66
        if (!mID) {
67
        cerr << "Error: MyClassInDll." << "declareNameSpace" << " not found\n"
68
                        << endl;
69

    
70
        } else {
71
        
72
                        environ->env->CallObjectMethod(cppXQ, mID,
73
                                        environ->env->NewStringUTF(prefix),
74
                                        environ->env->NewStringUTF(uri));
75
        }
76

    
77
}
78

    
79

    
80
    /**
81
     * Set the source document for the query
82
    */
83
    void XQueryProcessor::setContextItemFromFile(const char * ifile){
84
        setProperty("s", ifile);
85
    }
86

    
87
    /**
88
     * Set the output file where the result is sent
89
    */
90
    void XQueryProcessor::setOutputFile(const char* ofile){
91
       outputfile1 = std::string(ofile); 
92
       setProperty("o", ofile);
93
    }
94

    
95
    /**
96
     * Set a parameter value used in the query
97
     *
98
     * @param name  of the parameter, as a string
99
     * @param value of the query parameter, or null to clear a previously set value
100
     */
101
    void XQueryProcessor::setParameter(const char * name, XdmValue*value){
102
        if(value != NULL){
103
                value->incrementRefCount();
104
                parameters["param:"+string(name)] = value;
105
        } 
106
    }
107

    
108

    
109
    /**
110
     * Remove a parameter (name, value) pair
111
     *
112
     * @param namespacei currently not used
113
     * @param name  of the parameter
114
     * @return bool - outcome of the romoval
115
     */
116
    bool XQueryProcessor::removeParameter(const char * name){
117
        return (bool)(parameters.erase("param:"+string(name)));
118
    }
119
    /**
120
     * Set a property.
121
     *
122
     * @param name of the property
123
     * @param value of the property
124
     */
125
    void XQueryProcessor::setProperty(const char * name, const char * value){
126
#ifdef DEBUG        
127
        if(value == NULL) {
128
                std::cerr<<"XQueryProc setProperty is NULL"<<std::endl;
129
        }
130
#endif
131
        properties.insert(std::pair<std::string, std::string>(std::string(name), std::string((value== NULL ? "" : value))));
132
    }
133

    
134
    void XQueryProcessor::clearParameters(bool delVal){
135
        if(delVal){
136
                       for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
137
                        XdmValue * value = itr->second;
138
                        value->decrementRefCount();
139
#ifdef DEBUG
140
                        cout<<"XQueryProc.clearParameter() - XdmValue refCount="<<value->getRefCount()<<endl;
141
#endif
142
                        if(value != NULL && value->getRefCount() < 1){                
143
                                delete value;
144
                        }
145
                }
146
                parameters.clear();
147
        }
148
    }
149

    
150
   void XQueryProcessor::clearProperties(){
151
        properties.clear();
152
        outputfile1.clear();
153
   }
154

    
155

    
156
   void XQueryProcessor::setcwd(const char* dir){
157
    cwdXQ = std::string(dir);
158
   }
159

    
160
    void XQueryProcessor::setQueryBaseURI(const char * baseURI){
161
        setProperty("base", baseURI);
162
    }
163

    
164
    void XQueryProcessor::executeQueryToFile(const char * infilename, const char * ofilename, const char * query){
165
        setProperty("resources", proc->getResourcesDirectory());  
166

    
167
        jmethodID mID = (jmethodID)environ->env->GetMethodID (cppClass,"executeQueryToFile", "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
168
         if (!mID) {
169
        cout<<"Error: MyClassInDll."<<"executeQueryToFile"<<" not found\n"<<endl;
170
    } else {
171
        jobjectArray stringArray = NULL;
172
        jobjectArray objectArray = NULL;
173

    
174
        int size = parameters.size() + properties.size();
175
        if(query!= NULL) size++;
176
        if(infilename!= NULL) size++;        
177
        if(size >0) {
178

    
179
           int i=0;
180
           jclass objectClass = lookForClass(environ->env, "java/lang/Object");
181
           jclass stringClass = lookForClass(environ->env, "java/lang/String");
182
           objectArray = environ->env->NewObjectArray( (jint)size, objectClass, 0 );
183
           stringArray = environ->env->NewObjectArray( (jint)size, stringClass, 0 );
184
           if(query!= NULL) {
185
             environ->env->SetObjectArrayElement( stringArray, i, environ->env->NewStringUTF("qs") );
186
                  environ->env->SetObjectArrayElement( objectArray, i, environ->env->NewStringUTF(query));
187
             i++;        
188
           }
189
           if(infilename!= NULL) {
190
             environ->env->SetObjectArrayElement( stringArray, i, environ->env->NewStringUTF("s") );
191
                  environ->env->SetObjectArrayElement( objectArray, i, environ->env->NewStringUTF(infilename));
192
             i++;        
193
           }
194
           for(map<std::string, XdmValue* >::iterator iter=parameters.begin(); iter!=parameters.end(); ++iter, i++) {
195
             environ->env->SetObjectArrayElement( stringArray, i, environ->env->NewStringUTF( (iter->first).c_str() ) );
196
                bool checkCast = environ->env->IsInstanceOf((iter->second)->getUnderlyingValue(proc), lookForClass(environ->env, "net/sf/saxon/option/cpp/XdmValueForCpp") );
197
                if(( (bool)checkCast)==false ){
198
                        failure = "FAILURE in  array of XdmValueForCpp";
199
                } 
200
             environ->env->SetObjectArrayElement( objectArray, i, (jobject)((iter->second)->getUnderlyingValue(proc)) );
201
           }
202
             for(map<std::string, std::string >::iterator iter=properties.begin(); iter!=properties.end(); ++iter, i++) {
203
             environ->env->SetObjectArrayElement( stringArray, i, environ->env->NewStringUTF( (iter->first).c_str()  ));
204
             environ->env->SetObjectArrayElement( objectArray, i, (jobject)(environ->env->NewStringUTF((iter->second).c_str())) );
205
           }
206
        }
207

    
208
         environ->env->CallObjectMethod(cppXQ, mID, environ->env->NewStringUTF(cwdXQ.c_str()), environ->env->NewStringUTF(ofilename), stringArray, objectArray );
209
          environ->env->DeleteLocalRef(objectArray);
210
          environ->env->DeleteLocalRef(stringArray);
211

    
212
        if(exceptionOccurred()) {
213
                        if(proc->exception != NULL) {
214
                                delete proc->exception;
215
                        }                
216
                proc->exception = proc->checkForExceptionCPP(environ->env, cppClass, NULL);        
217
                proc->exceptionClear();
218
                           
219
             }
220
         
221
  }
222

    
223

    
224
   }
225

    
226

    
227
    XdmValue * XQueryProcessor::executeQueryToValue(const char * infilename, const char * query){
228
        setProperty("resources", proc->getResourcesDirectory()); 
229
 jmethodID mID = (jmethodID)environ->env->GetMethodID (cppClass,"executeQueryToValue", "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
230
 if (!mID) {
231
        cout<<"Error: MyClassInDll."<<"executeQueryToValue"<<" not found\n"<<endl;
232
    } else {
233
        jobjectArray stringArray = NULL;
234
        jobjectArray objectArray = NULL;
235

    
236
        int size = parameters.size() + properties.size();
237
        if(query!= NULL) size++;
238
        if(infilename!= NULL) size++;
239
        if(size >0) {
240
           int i=0;
241
           jclass objectClass = lookForClass(environ->env, "java/lang/Object");
242
           jclass stringClass = lookForClass(environ->env, "java/lang/String");
243
           objectArray = environ->env->NewObjectArray( (jint)size, objectClass, 0 );
244
           stringArray = environ->env->NewObjectArray( (jint)size, stringClass, 0 );
245

    
246
           if(query!= NULL) {
247
             environ->env->SetObjectArrayElement( stringArray, i, environ->env->NewStringUTF("qs") );
248
                  environ->env->SetObjectArrayElement( objectArray, i, environ->env->NewStringUTF(query));
249
             i++;        
250
           }
251
           if(infilename!= NULL) {
252
             environ->env->SetObjectArrayElement( stringArray, i, environ->env->NewStringUTF("s") );
253
                  environ->env->SetObjectArrayElement( objectArray, i, environ->env->NewStringUTF(infilename));
254
             i++;        
255
           }
256
           for(map<std::string, XdmValue* >::iterator iter=parameters.begin(); iter!=parameters.end(); ++iter, i++) {
257
             environ->env->SetObjectArrayElement( stringArray, i, environ->env->NewStringUTF( (iter->first).c_str() ) );
258
                bool checkCast = environ->env->IsInstanceOf((iter->second)->getUnderlyingValue(proc), lookForClass(environ->env, "net/sf/saxon/option/cpp/XdmValueForCpp") );
259
                if(( (bool)checkCast)==false ){
260
                        failure = "FAILURE in  array of XdmValueForCpp";
261
                } 
262
             environ->env->SetObjectArrayElement( objectArray, i, (jobject)((iter->second)->getUnderlyingValue(proc)) );
263
           }
264
             for(map<std::string, std::string >::iterator iter=properties.begin(); iter!=properties.end(); ++iter, i++) {
265
             environ->env->SetObjectArrayElement( stringArray, i, environ->env->NewStringUTF( (iter->first).c_str()  ));
266
             environ->env->SetObjectArrayElement( objectArray, i, (jobject)(environ->env->NewStringUTF((iter->second).c_str())) );
267
           }
268
        }
269

    
270
          jobject result = (jobject)(environ->env->CallObjectMethod(cppXQ, mID, environ->env->NewStringUTF(cwdXQ.c_str()), stringArray, objectArray ));
271
          environ->env->DeleteLocalRef(objectArray);
272
          environ->env->DeleteLocalRef(stringArray);
273
    if(result) {
274
                jclass atomicValueClass = lookForClass(environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
275
                jclass nodeClass = lookForClass(environ->env, "net/sf/saxon/s9api/XdmNode");
276
                jclass functionItemClass = lookForClass(environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
277
                XdmValue * xdmValue = NULL;
278
                if(environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
279
                                xdmValue = new XdmAtomicValue(result);
280
                                
281

    
282
                        } else if(environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
283
                                xdmValue = new XdmNode(result);
284

    
285
                        } else if (environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
286
                                return NULL;
287
                        } else {
288
                                xdmValue = new XdmValue(result);
289
                        }
290
        
291
        xdmValue->setProcessor(proc);
292
        return xdmValue;
293
     } else if(exceptionOccurred()) {
294
                        if(proc->exception != NULL) {
295
                                delete proc->exception;
296
                        }
297
                        proc->exception = proc->checkForExceptionCPP(environ->env, cppClass, NULL);
298
                        proc->exceptionClear();
299
                           
300
                     } 
301
  }
302
  return NULL;
303

    
304
}
305

    
306
    const char * XQueryProcessor::executeQueryToString(const char * infilename, const char * query){
307
        setProperty("resources", proc->getResourcesDirectory()); 
308
 jmethodID mID = (jmethodID)environ->env->GetMethodID (cppClass,"executeQueryToString", "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
309
 if (!mID) {
310
        cout<<"Error: MyClassInDll."<<"executeQueryToString"<<" not found\n"<<endl;
311
    } else {
312
        jobjectArray stringArray = NULL;
313
        jobjectArray objectArray = NULL;
314

    
315
        int size = parameters.size() + properties.size();
316
        if(query!= NULL) size++;
317
        if(infilename!= NULL) size++;
318
        if(size >0) {
319
           int i=0;
320
           jclass objectClass = lookForClass(environ->env, "java/lang/Object");
321
           jclass stringClass = lookForClass(environ->env, "java/lang/String");
322
           objectArray = environ->env->NewObjectArray( (jint)size, objectClass, 0 );
323
           stringArray = environ->env->NewObjectArray( (jint)size, stringClass, 0 );
324

    
325
           if(query!= NULL) {
326
             environ->env->SetObjectArrayElement( stringArray, i, environ->env->NewStringUTF("qs") );
327
                  environ->env->SetObjectArrayElement( objectArray, i, environ->env->NewStringUTF(query));
328
             i++;        
329
           }
330
           if(infilename!= NULL) {
331
             environ->env->SetObjectArrayElement( stringArray, i, environ->env->NewStringUTF("s") );
332
                  environ->env->SetObjectArrayElement( objectArray, i, environ->env->NewStringUTF(infilename));
333
             i++;        
334
           }
335
           for(map<std::string, XdmValue* >::iterator iter=parameters.begin(); iter!=parameters.end(); ++iter, i++) {
336
             environ->env->SetObjectArrayElement( stringArray, i, environ->env->NewStringUTF( (iter->first).c_str() ) );
337
                bool checkCast = environ->env->IsInstanceOf((iter->second)->getUnderlyingValue(proc), lookForClass(environ->env, "net/sf/saxon/option/cpp/XdmValueForCpp") );
338
                if(( (bool)checkCast)==false ){
339
                        failure = "FAILURE in  array of XdmValueForCpp";
340
                } 
341
             environ->env->SetObjectArrayElement( objectArray, i, (jobject)((iter->second)->getUnderlyingValue(proc)) );
342
           }
343
             for(map<std::string, std::string >::iterator iter=properties.begin(); iter!=properties.end(); ++iter, i++) {
344
             environ->env->SetObjectArrayElement( stringArray, i, environ->env->NewStringUTF( (iter->first).c_str()  ));
345
             environ->env->SetObjectArrayElement( objectArray, i, (jobject)(environ->env->NewStringUTF((iter->second).c_str())) );
346
           }
347
        }
348

    
349
          jstring result = (jstring)(environ->env->CallObjectMethod(cppXQ, mID, environ->env->NewStringUTF(cwdXQ.c_str()), stringArray, objectArray ));
350
          environ->env->DeleteLocalRef(objectArray);
351
          environ->env->DeleteLocalRef(stringArray);
352

    
353
          if(result) {
354
             const char * str = environ->env->GetStringUTFChars(result, NULL);
355
            //return "result should be ok";            
356
            return str;
357
           } else if(exceptionOccurred()) {
358
                        if(proc->exception != NULL) {
359
                                delete proc->exception;
360
                        }
361
                        proc->exception = proc->checkForExceptionCPP(environ->env, cppClass, NULL);
362
                        proc->exceptionClear();
363
                           
364
                     }
365
  }
366
  return NULL;
367

    
368

    
369
    }
370

    
371

    
372
    const char * XQueryProcessor::runQueryToString(){
373
        return executeQueryToString(NULL, NULL);        
374

    
375
    }
376

    
377

    
378
    XdmValue * XQueryProcessor::runQueryToValue(){
379
        return executeQueryToValue(NULL, NULL);
380
   }
381

    
382
    void XQueryProcessor::runQueryToFile(){
383
        executeQueryToFile(NULL, NULL, NULL);
384
   }
385

    
386
    void XQueryProcessor::setQueryFile(const char * ofile){
387
           outputfile1 = std::string(ofile); 
388
           setProperty("q", ofile);
389
            queryFileExists = true;
390
    }
391

    
392
   void XQueryProcessor::setQueryContent(const char* content){
393
           outputfile1 = std::string(content); 
394
           setProperty("qs", content);
395
            queryFileExists = false;
396
  }
397

    
398

    
399

    
400
void XQueryProcessor::exceptionClear(){
401
        if(proc->exception != NULL) {
402
                delete proc->exception;
403
                proc->exception = NULL;        
404
                environ->env->ExceptionClear();
405
        }
406

    
407
   
408
 
409
}
410

    
411
bool XQueryProcessor::exceptionOccurred(){
412
        return proc->exceptionOccurred();
413

    
414
}
415

    
416

    
417
const char * XQueryProcessor::getErrorCode(int i) {
418
        if(proc->exception == NULL) {return NULL;}
419
        return proc->exception->getErrorCode(i);
420
}
421

    
422
const char * XQueryProcessor::getErrorMessage(int i ){
423
        if(proc->exception == NULL) {return NULL;}
424
        return proc->exception->getErrorMessage(i);
425
}
426

    
427
const char* XQueryProcessor::checkException(){
428
        /*if(proc->exception == NULL) {
429
                proc->exception = proc->checkForException(environ->env, cppClass, cppXQ);
430
        }
431
        return proc->exception;*/
432
        return checkForException(*(environ), cppClass, cppXQ);
433
}
434

    
435

    
436

    
437
int XQueryProcessor::exceptionCount(){
438
        if(proc->exception != NULL){
439
                return proc->exception->count();
440
        }
441
        return 0;
442
}
(16-16/37)