Project

Profile

Help

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

he / tags / 9.8.0.14 / hec / Saxon.C.API / XQueryProcessor.cpp @ 02f8308b

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(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XQueryEngine");
17

    
18

    
19
    cppXQ = createSaxonProcessor2 (SaxonProcessor::sxn_environ->env, cppClass, "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
20
    
21
#ifdef DEBUG
22
        jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
23
        SaxonProcessor::sxn_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) SaxonProcessor::sxn_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
                        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXQ, mID,
73
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(prefix),
74
                                        SaxonProcessor::sxn_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
                
147
        } else {
148

    
149
                for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
150
                        XdmValue * value = itr->second;
151
                        value->decrementRefCount();
152

    
153
                }
154
        }
155

    
156
        parameters.clear();
157
    }
158

    
159
   void XQueryProcessor::clearProperties(){
160
        properties.clear();
161
        //outputfile1.clear();
162
   }
163

    
164

    
165
   void XQueryProcessor::setcwd(const char* dir){
166
    cwdXQ = std::string(dir);
167
   }
168

    
169
    void XQueryProcessor::setQueryBaseURI(const char * baseURI){
170
        setProperty("base", baseURI);
171
    }
172

    
173
    void XQueryProcessor::executeQueryToFile(const char * infilename, const char * ofilename, const char * query){
174
        setProperty("resources", proc->getResourcesDirectory());  
175

    
176
        jmethodID mID = (jmethodID)SaxonProcessor::sxn_environ->env->GetMethodID (cppClass,"executeQueryToFile", "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
177
         if (!mID) {
178
        cout<<"Error: MyClassInDll."<<"executeQueryToFile"<<" not found\n"<<endl;
179
    } else {
180
        jobjectArray stringArray = NULL;
181
        jobjectArray objectArray = NULL;
182

    
183
        int size = parameters.size() + properties.size();
184
        if(query!= NULL) size++;
185
        if(infilename!= NULL) size++;        
186
        if(size >0) {
187

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

    
217
         SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXQ, mID, SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXQ.c_str()), SaxonProcessor::sxn_environ->env->NewStringUTF(ofilename), stringArray, objectArray );
218
          SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
219
          SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
220

    
221
        if(exceptionOccurred()) {
222
                        if(proc->exception != NULL) {
223
                                delete proc->exception;
224
                        }                
225
                proc->exception = proc->checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass, NULL);        
226
                proc->exceptionClear();
227
                           
228
             }
229
         
230
  }
231

    
232

    
233
   }
234

    
235

    
236
    XdmValue * XQueryProcessor::executeQueryToValue(const char * infilename, const char * query){
237
        setProperty("resources", proc->getResourcesDirectory()); 
238
 jmethodID mID = (jmethodID)SaxonProcessor::sxn_environ->env->GetMethodID (cppClass,"executeQueryToValue", "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
239
 if (!mID) {
240
        cout<<"Error: MyClassInDll."<<"executeQueryToValue"<<" not found\n"<<endl;
241
    } else {
242
        jobjectArray stringArray = NULL;
243
        jobjectArray objectArray = NULL;
244

    
245
        int size = parameters.size() + properties.size();
246
        if(query!= NULL) size++;
247
        if(infilename!= NULL) size++;
248
        if(size >0) {
249
           int i=0;
250
           jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
251
           jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
252
           objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray( (jint)size, objectClass, 0 );
253
           stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray( (jint)size, stringClass, 0 );
254

    
255
           if(query!= NULL) {
256
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF("qs") );
257
                  SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF(query));
258
             i++;        
259
           }
260
           if(infilename!= NULL) {
261
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF("s") );
262
                  SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF(infilename));
263
             i++;        
264
           }
265
           for(map<std::string, XdmValue* >::iterator iter=parameters.begin(); iter!=parameters.end(); ++iter, i++) {
266
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF( (iter->first).c_str() ) );
267
                bool checkCast = SaxonProcessor::sxn_environ->env->IsInstanceOf((iter->second)->getUnderlyingValue(proc), lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmValueForCpp") );
268
                if(( (bool)checkCast)==false ){
269
                        failure = "FAILURE in  array of XdmValueForCpp";
270
                } 
271
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, (jobject)((iter->second)->getUnderlyingValue(proc)) );
272
           }
273
             for(map<std::string, std::string >::iterator iter=properties.begin(); iter!=properties.end(); ++iter, i++) {
274
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF( (iter->first).c_str()  ));
275
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, (jobject)(SaxonProcessor::sxn_environ->env->NewStringUTF((iter->second).c_str())) );
276
           }
277
        }
278

    
279
          jobject result = (jobject)(SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXQ, mID, SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXQ.c_str()), stringArray, objectArray ));
280
          SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
281
          SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
282
    if(result) {
283
                jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
284
                jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
285
                jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
286
                XdmValue * xdmValue = NULL;
287
                if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
288
                                xdmValue = new XdmAtomicValue(result);
289
                                
290

    
291
                        } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
292
                                xdmValue = new XdmNode(result);
293

    
294
                        } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
295
                                return NULL;
296
                        } else {
297
                                xdmValue = new XdmValue(result);
298
                        }
299
        
300
        xdmValue->setProcessor(proc);
301
        return xdmValue;
302
     } else if(exceptionOccurred()) {
303
                        if(proc->exception != NULL) {
304
                                delete proc->exception;
305
                        }
306
                        proc->exception = proc->checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass, NULL);
307
                        proc->exceptionClear();
308
                           
309
                     } 
310
  }
311
  return NULL;
312

    
313
}
314

    
315
    const char * XQueryProcessor::executeQueryToString(const char * infilename, const char * query){
316
        setProperty("resources", proc->getResourcesDirectory()); 
317
 jmethodID mID = (jmethodID)SaxonProcessor::sxn_environ->env->GetMethodID (cppClass,"executeQueryToString", "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
318
 if (!mID) {
319
        cout<<"Error: MyClassInDll."<<"executeQueryToString"<<" not found\n"<<endl;
320
    } else {
321
        jobjectArray stringArray = NULL;
322
        jobjectArray objectArray = NULL;
323

    
324
        int size = parameters.size() + properties.size();
325
        if(query!= NULL) size++;
326
        if(infilename!= NULL) size++;
327
        if(size >0) {
328
           int i=0;
329
           jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
330
           jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
331
           objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray( (jint)size, objectClass, 0 );
332
           stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray( (jint)size, stringClass, 0 );
333

    
334
           if(query!= NULL) {
335
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF("qs") );
336
                  SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF(query));
337
             i++;        
338
           }
339
           if(infilename!= NULL) {
340
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF("s") );
341
                  SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF(infilename));
342
             i++;        
343
           }
344
           for(map<std::string, XdmValue* >::iterator iter=parameters.begin(); iter!=parameters.end(); ++iter, i++) {
345
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF( (iter->first).c_str() ) );
346
                bool checkCast = SaxonProcessor::sxn_environ->env->IsInstanceOf((iter->second)->getUnderlyingValue(proc), lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmValueForCpp") );
347
                if(( (bool)checkCast)==false ){
348
                        failure = "FAILURE in  array of XdmValueForCpp";
349
                } 
350
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, (jobject)((iter->second)->getUnderlyingValue(proc)) );
351
           }
352
             for(map<std::string, std::string >::iterator iter=properties.begin(); iter!=properties.end(); ++iter, i++) {
353
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( stringArray, i, SaxonProcessor::sxn_environ->env->NewStringUTF( (iter->first).c_str()  ));
354
             SaxonProcessor::sxn_environ->env->SetObjectArrayElement( objectArray, i, (jobject)(SaxonProcessor::sxn_environ->env->NewStringUTF((iter->second).c_str())) );
355
           }
356
        }
357

    
358
          jstring result = (jstring)(SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXQ, mID, SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXQ.c_str()), stringArray, objectArray ));
359
          SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
360
          SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
361

    
362
          if(result) {
363
             const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result, NULL);
364
            //return "result should be ok";            
365
            return str;
366
           } else if(exceptionOccurred()) {
367
                        if(proc->exception != NULL) {
368
                                delete proc->exception;
369
                        }
370
                        proc->exception = proc->checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass, NULL);
371
                        proc->exceptionClear();
372
                           
373
                     }
374
  }
375
  return NULL;
376

    
377

    
378
    }
379

    
380

    
381
    const char * XQueryProcessor::runQueryToString(){
382
        return executeQueryToString(NULL, NULL);        
383

    
384
    }
385

    
386

    
387
    XdmValue * XQueryProcessor::runQueryToValue(){
388
        return executeQueryToValue(NULL, NULL);
389
   }
390

    
391
    void XQueryProcessor::runQueryToFile(){
392
        executeQueryToFile(NULL, NULL, NULL);
393
   }
394

    
395
    void XQueryProcessor::setQueryFile(const char * ofile){
396
           //outputfile1 = std::string(ofile); 
397
           setProperty("q", ofile);
398
            queryFileExists = true;
399
    }
400

    
401
   void XQueryProcessor::setQueryContent(const char* content){
402
          // outputfile1 = std::string(content); 
403
           setProperty("qs", content);
404
            queryFileExists = false;
405
  }
406

    
407

    
408

    
409
void XQueryProcessor::exceptionClear(){
410
        if(proc->exception != NULL) {
411
                delete proc->exception;
412
                proc->exception = NULL;        
413
                SaxonProcessor::sxn_environ->env->ExceptionClear();
414
        }
415

    
416
   
417
 
418
}
419

    
420
bool XQueryProcessor::exceptionOccurred(){
421
        return proc->exceptionOccurred();
422

    
423
}
424

    
425

    
426
const char * XQueryProcessor::getErrorCode(int i) {
427
        if(proc->exception == NULL) {return NULL;}
428
        return proc->exception->getErrorCode(i);
429
}
430

    
431
const char * XQueryProcessor::getErrorMessage(int i ){
432
        if(proc->exception == NULL) {return NULL;}
433
        return proc->exception->getErrorMessage(i);
434
}
435

    
436
const char* XQueryProcessor::checkException(){
437
        /*if(proc->exception == NULL) {
438
                proc->exception = proc->checkForException(SaxonProcessor::sxn_environ->env, cppClass, cppXQ);
439
        }
440
        return proc->exception;*/
441
        return checkForException(*(SaxonProcessor::sxn_environ), cppClass, cppXQ);
442
}
443

    
444

    
445

    
446
int XQueryProcessor::exceptionCount(){
447
        if(proc->exception != NULL){
448
                return proc->exception->count();
449
        }
450
        return 0;
451
}
(19-19/45)