Project

Profile

Help

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

he / latest9.6 / hec / Saxon.C.API / XPathProcessor.cpp @ 0af164b2

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

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

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

    
15
        /*
16
         * Look for class.
17
         */
18
        cppClass = lookForClass(sxn_environ->env,
19
                        "net/sf/saxon/option/cpp/XPathProcessor");
20
        if ((proc->proc) == NULL) {
21
                cerr << "Processor is NULL" << endl;
22
        }
23

    
24
        cppXP = createSaxonProcessor2(sxn_environ->env, cppClass,
25
                        "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
26

    
27

    
28

    
29
#ifdef DEBUG
30
        jmethodID debugMID = sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
31
        sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
32
#endif    
33

    
34
        proc->exception = NULL;
35
        if(!(proc->cwd.empty()) && curr.empty()){
36
                cwdXP = proc->cwd;
37
        } else {
38
                cwdXP = curr;
39
        }
40

    
41
}
42

    
43
XdmValue * XPathProcessor::evaluate(const char * xpathStr) {
44
        if (xpathStr == NULL) {
45
                cerr << "Error:: XPath string cannot be empty or NULL" << std::endl;
46
        return NULL;
47
}
48
setProperty("resources", proc->getResourcesDirectory());
49
jmethodID mID =
50
                (jmethodID) sxn_environ->env->GetMethodID(cppClass, "evaluate",
51
                                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)[Lnet/sf/saxon/s9api/XdmValue;");
52
if (!mID) {
53
        cerr << "Error: MyClassInDll." << "evaluate" << " not found\n"
54
                        << endl;
55

    
56
} else {
57
        jobjectArray stringArray = NULL;
58
        jobjectArray objectArray = NULL;
59
        jclass objectClass = lookForClass(sxn_environ->env, "java/lang/Object");
60
        jclass stringClass = lookForClass(sxn_environ->env, "java/lang/String");
61

    
62
        int size = parameters.size() + properties.size();
63
        if (size > 0) {
64
                objectArray = sxn_environ->env->NewObjectArray((jint) size,
65
                                objectClass, 0);
66
                stringArray = sxn_environ->env->NewObjectArray((jint) size,
67
                                stringClass, 0);
68
                int i = 0;
69
                for (map<std::string, XdmValue*>::iterator iter = parameters.begin();
70
                                iter != parameters.end(); ++iter, i++) {
71
                        sxn_environ->env->SetObjectArrayElement(stringArray, i,
72
                                        sxn_environ->env->NewStringUTF((iter->first).c_str()));
73
                        sxn_environ->env->SetObjectArrayElement(objectArray, i,
74
                                        (iter->second)->getUnderlyingValue(proc));
75
                }
76
                for (map<std::string, std::string>::iterator iter = properties.begin();
77
                                iter != properties.end(); ++iter, i++) {
78
                        sxn_environ->env->SetObjectArrayElement(stringArray, i,
79
                                        sxn_environ->env->NewStringUTF((iter->first).c_str()));
80
                        sxn_environ->env->SetObjectArrayElement(objectArray, i,
81
                                        sxn_environ->env->NewStringUTF((iter->second).c_str()));
82
                }
83
        }
84
        jobjectArray results = (jobjectArray)(
85
                        sxn_environ->env->CallObjectMethod(cppXP, mID,
86
                                        sxn_environ->env->NewStringUTF(cwdXP.c_str()),
87
                                        sxn_environ->env->NewStringUTF(xpathStr), stringArray, objectArray));
88
        if(!results) {
89
                if(exceptionOccurred()) {
90
                        if(proc->exception != NULL) {
91
                                delete proc->exception;
92
                        }
93
                        proc->exception = proc->checkForExceptionCPP(sxn_environ->env, cppClass, NULL);
94
                        proc->exceptionClear();
95
                        return NULL;
96
                           
97
                     }
98
        }
99
        
100
        int sizex = sxn_environ->env->GetArrayLength(results);
101
        if (size > 0) {
102
                sxn_environ->env->DeleteLocalRef(stringArray);
103
                sxn_environ->env->DeleteLocalRef(objectArray);
104
        }
105
        if (sizex>0) {
106
                jclass atomicValueClass = lookForClass(sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
107
                jclass nodeClass = lookForClass(sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
108
                jclass functionItemClass = lookForClass(sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
109

    
110
                XdmValue * value = new XdmValue();
111
                value->setProcessor(proc);
112
                XdmItem * xdmItem = NULL;
113
                for (int p=0; p < sizex; ++p) 
114
                {
115
                        jobject resulti = sxn_environ->env->GetObjectArrayElement(results, p);
116
                        //value->addUnderlyingValue(resulti);
117

    
118
                        if(sxn_environ->env->IsInstanceOf(resulti, atomicValueClass)           == JNI_TRUE) {
119
                                xdmItem = new XdmAtomicValue(resulti);
120
                                
121

    
122
                        } else if(sxn_environ->env->IsInstanceOf(resulti, nodeClass)           == JNI_TRUE) {
123
                                xdmItem = new XdmNode(resulti);
124

    
125

    
126
                        } else if (sxn_environ->env->IsInstanceOf(resulti, functionItemClass)           == JNI_TRUE) {
127
                                continue;
128
                        }
129
                        xdmItem->setProcessor(proc);
130
                        value->addXdmItem(xdmItem);
131
                }
132
                sxn_environ->env->DeleteLocalRef(results);
133
                return value;
134
        }
135
}
136
return NULL;
137

    
138
}
139

    
140
XdmItem * XPathProcessor::evaluateSingle(const char * xpathStr) {
141
        if (xpathStr == NULL) {
142
                cerr << "Error:: XPath string cannot be empty or NULL" << std::endl;
143
             return NULL;
144
        }
145
setProperty("resources", proc->getResourcesDirectory());
146
jmethodID mID =
147
                (jmethodID) sxn_environ->env->GetMethodID(cppClass, "evaluateSingle",
148
                                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmItem;");
149
if (!mID) {
150
        cerr << "Error: MyClassInDll." << "evaluateSingle" << " not found\n"
151
                        << endl;
152

    
153
} else {
154
        jobjectArray stringArray = NULL;
155
        jobjectArray objectArray = NULL;
156
        jclass objectClass = lookForClass(sxn_environ->env, "java/lang/Object");
157
        jclass stringClass = lookForClass(sxn_environ->env, "java/lang/String");
158

    
159
        int size = parameters.size() + properties.size();
160
#ifdef DEBUG
161
                cerr<<"Properties size: "<<properties.size()<<endl;
162
                cerr<<"Parameter size: "<<parameters.size()<<endl;
163
                cerr<<"size:"<<size<<endl;
164
#endif
165
        if (size > 0) {
166
                objectArray = sxn_environ->env->NewObjectArray((jint) size,
167
                                objectClass, 0);
168
                stringArray = sxn_environ->env->NewObjectArray((jint) size,
169
                                stringClass, 0);
170
                int i = 0;
171
                for (map<std::string, XdmValue*>::iterator iter = parameters.begin();
172
                                iter != parameters.end(); ++iter, i++) {
173
                        sxn_environ->env->SetObjectArrayElement(stringArray, i,
174
                                        sxn_environ->env->NewStringUTF((iter->first).c_str()));
175
                        sxn_environ->env->SetObjectArrayElement(objectArray, i,
176
                                        (iter->second)->getUnderlyingValue(proc));
177
#ifdef DEBUG
178
                                string s1 = typeid(iter->second).name();
179
                                cerr<<"Type of itr:"<<s1<<endl;
180
                                jobject xx = (iter->second)->getUnderlyingValue(proc);
181
                                if(xx == NULL) {
182
                                        cerr<<"value failed"<<endl;
183
                                } else {
184

    
185
                                        cerr<<"Type of value:"<<(typeid(xx).name())<<endl;
186
                                }
187
                                if((iter->second)->getUnderlyingValue(proc) == NULL) {
188
                                        cerr<<"(iter->second)->getUnderlyingValue() is NULL"<<endl;
189
                                }
190
#endif
191
                }
192
                for (map<std::string, std::string>::iterator iter = properties.begin();
193
                                iter != properties.end(); ++iter, i++) {
194
                        sxn_environ->env->SetObjectArrayElement(stringArray, i,
195
                                        sxn_environ->env->NewStringUTF((iter->first).c_str()));
196
                        sxn_environ->env->SetObjectArrayElement(objectArray, i,
197
                                        sxn_environ->env->NewStringUTF((iter->second).c_str()));
198
                }
199
        }
200
        jobject result = (jobject)(
201
                        sxn_environ->env->CallObjectMethod(cppXP, mID,
202
                                        sxn_environ->env->NewStringUTF(cwdXP.c_str()),
203
                                        sxn_environ->env->NewStringUTF(xpathStr), stringArray, objectArray));
204
        if (size > 0) {
205
                sxn_environ->env->DeleteLocalRef(stringArray);
206
                sxn_environ->env->DeleteLocalRef(objectArray);
207
        }
208
        if (result) {
209
                jclass atomicValueClass = lookForClass(sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
210
                jclass nodeClass = lookForClass(sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
211
                jclass functionItemClass = lookForClass(sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
212
                XdmItem * xdmItem = NULL;
213
                if(sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
214
                        xdmItem = new XdmAtomicValue(result);
215

    
216
                } else if(sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
217
                        
218
                        xdmItem = new XdmNode(result);
219

    
220
                } else if (sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
221
                        return NULL;
222
                }
223
                xdmItem->setProcessor(proc);
224
                return xdmItem;
225
        } else if(exceptionOccurred()) {
226
                        if(proc->exception != NULL) {
227
                                delete proc->exception;
228
                        }
229
                        proc->exception = proc->checkForExceptionCPP(sxn_environ->env, cppClass, NULL);
230
                        proc->exceptionClear();
231
                           
232
                     }
233
}
234

    
235
return NULL;
236

    
237
}
238

    
239
void XPathProcessor::setContextItem(XdmItem * item) {
240
        contextItem = item;
241
            if(item != NULL){
242
              parameters["node"] = (XdmValue *)item;
243
            }
244
}
245

    
246
void XPathProcessor::setContextFile(const char * filename) {
247
        if (filename != NULL) {
248
                setProperty("s", filename);
249
        }
250
}
251

    
252

    
253
//TODO test the declareNameSpace method
254
void XPathProcessor::declareNamespace(const char *prefix, const char * uri){
255
        if (prefix == NULL || uri == NULL) {
256
                return;
257
        }
258
        jmethodID mID =
259
                (jmethodID) sxn_environ->env->GetMethodID(cppClass, "declareNamespace",
260
                                "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
261
        if (!mID) {
262
        cerr << "Error: MyClassInDll." << "declareNameSpace" << " not found\n"
263
                        << endl;
264

    
265
        } else {
266
                        sxn_environ->env->CallObjectMethod(cppXP, mID,
267
                                        sxn_environ->env->NewStringUTF(prefix),
268
                                        sxn_environ->env->NewStringUTF(uri));
269
        }
270

    
271
}
272

    
273
void XPathProcessor::setBaseURI(const char * uriStr) {
274
        if (uriStr == NULL) {
275
                cerr << "Error:: XPath string cannot be empty or NULL" << std::endl;
276
             return;
277
        }
278
setProperty("resources", proc->getResourcesDirectory());
279
jmethodID mID =
280
                (jmethodID) sxn_environ->env->GetMethodID(cppClass, "setBaseURI",
281
                                "(Ljava/lang/String;)Z");
282
if (!mID) {
283
        cerr << "Error: MyClassInDll." << "setBaseURI" << " not found\n"
284
                        << endl;
285

    
286
} else {
287

    
288
        sxn_environ->env->CallObjectMethod(cppXP, mID,
289
                                        sxn_environ->env->NewStringUTF(uriStr));
290
}
291

    
292
}
293

    
294
bool XPathProcessor::effectiveBooleanValue(const char * xpathStr) {
295
        if (xpathStr == NULL) {
296
                cerr << "Error:: XPath string cannot be empty or NULL" << std::endl;
297
             return NULL;
298
        }
299
setProperty("resources", proc->getResourcesDirectory());
300
jmethodID mID =
301
                (jmethodID) sxn_environ->env->GetMethodID(cppClass, "effectiveBooleanValue",
302
                                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Z");
303
if (!mID) {
304
        cerr << "Error: MyClassInDll." << "effectiveBooleanValue" << " not found\n"
305
                        << endl;
306

    
307
} else {
308
        jobjectArray stringArray = NULL;
309
        jobjectArray objectArray = NULL;
310
        jclass objectClass = lookForClass(sxn_environ->env, "java/lang/Object");
311
        jclass stringClass = lookForClass(sxn_environ->env, "java/lang/String");
312

    
313
        int size = parameters.size() + properties.size();
314
        if (size > 0) {
315
                objectArray = sxn_environ->env->NewObjectArray((jint) size,
316
                                objectClass, 0);
317
                stringArray = sxn_environ->env->NewObjectArray((jint) size,
318
                                stringClass, 0);
319
                int i = 0;
320
                for (map<std::string, XdmValue*>::iterator iter = parameters.begin();
321
                                iter != parameters.end(); ++iter, i++) {
322
                        sxn_environ->env->SetObjectArrayElement(stringArray, i,
323
                                        sxn_environ->env->NewStringUTF((iter->first).c_str()));
324
                        sxn_environ->env->SetObjectArrayElement(objectArray, i,
325
                                        (iter->second)->getUnderlyingValue(proc));
326
                }
327
                for (map<std::string, std::string>::iterator iter = properties.begin();
328
                                iter != properties.end(); ++iter, i++) {
329
                        sxn_environ->env->SetObjectArrayElement(stringArray, i,
330
                                        sxn_environ->env->NewStringUTF((iter->first).c_str()));
331
                        sxn_environ->env->SetObjectArrayElement(objectArray, i,
332
                                        sxn_environ->env->NewStringUTF((iter->second).c_str()));
333
                }
334
        }
335
        jboolean result = (jboolean)(
336
                        sxn_environ->env->CallBooleanMethod(cppXP, mID,
337
                                        sxn_environ->env->NewStringUTF(cwdXP.c_str()),
338
                                        sxn_environ->env->NewStringUTF(xpathStr), stringArray, objectArray));
339
        if (size > 0) {
340
                sxn_environ->env->DeleteLocalRef(stringArray);
341
                sxn_environ->env->DeleteLocalRef(objectArray);
342
        }
343
        if(exceptionOccurred()) {
344
                        if(proc->exception != NULL) {
345
                                delete proc->exception;
346
                        }
347
                        proc->exception = proc->checkForExceptionCPP(sxn_environ->env, cppClass, NULL);
348
                        proc->exceptionClear();
349
                           
350
             }
351
        return result;
352
}
353
return NULL;
354
}
355

    
356
void XPathProcessor::setParameter(const char * name, XdmValue* value) {
357
        if(value != NULL){
358
                value->incrementRefCount();
359
                parameters["param:"+std::string(name)] = value;
360
        }
361
}
362

    
363
bool XPathProcessor::removeParameter(const char * name) {
364
        return (bool)(parameters.erase("param:"+std::string(name)));
365
}
366

    
367
void XPathProcessor::setProperty(const char * name, const char * value) {
368
#ifdef DEBUG        
369
        if(value == NULL) {
370
                std::cerr<<"XPathProc setProperty is NULL"<<std::endl;
371
        }
372
#endif
373
        properties.insert(std::pair<std::string, std::string>(std::string(name), std::string((value == NULL ? "" : value))));
374
}
375

    
376
void XPathProcessor::clearParameters(bool delVal) {
377
        if(delVal){
378
                       for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
379
                        XdmValue * value = itr->second;
380
                        value->decrementRefCount();
381
#ifdef DEBUG
382
                        cout<<"XPathProc.clearParameter() - XdmValue refCount="<<value->getRefCount()<<endl;
383
#endif
384
                        if(value != NULL && value->getRefCount() < 1){                
385
                                delete value;
386
                        }
387
                }
388
                parameters.clear();
389
        }
390
}
391

    
392
void XPathProcessor::clearProperties() {
393
        properties.clear();
394
}
395

    
396

    
397
   void XPathProcessor::setcwd(const char* dir){
398
    cwdXP = std::string(dir);
399
   }
400

    
401
std::map<std::string,XdmValue*>& XPathProcessor::getParameters(){
402
        std::map<std::string,XdmValue*>& ptr = parameters;
403
        return ptr;
404
}
405

    
406
std::map<std::string,std::string>& XPathProcessor::getProperties(){
407
        std::map<std::string,std::string> &ptr = properties;
408
        return ptr;
409
}
410

    
411
void XPathProcessor::exceptionClear(){
412
        if(proc->exception != NULL) {
413
                delete proc->exception;
414
                proc->exception = NULL;        
415
        }
416

    
417
   sxn_environ->env->ExceptionClear();
418
 
419
}
420

    
421
int XPathProcessor::exceptionCount(){
422
 if(proc->exception != NULL){
423
 return proc->exception->count();
424
 }
425
 return 0;
426
 }
427

    
428
const char * XPathProcessor::getErrorCode(int i) {
429
        if(proc->exception == NULL) {return NULL;}
430
        return proc->exception->getErrorCode(i);
431
}
432

    
433
const char * XPathProcessor::getErrorMessage(int i ){
434
        if(proc->exception == NULL) {return NULL;}
435
        return proc->exception->getErrorMessage(i);
436
}
437

    
438
    bool XPathProcessor::exceptionOccurred(){
439
        return proc->exceptionOccurred();
440
    }
441

    
442

    
443

    
444
    const char* XPathProcessor::checkException(){
445
        return checkForException(*(sxn_environ), cppClass, cppXP);
446
    }
447

    
(14-14/37)