Project

Profile

Help

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

he / latest9.9 / hec / Saxon.C.API / XPathProcessor.cpp @ 1117bc69

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(SaxonProcessor::sxn_environ->env,
19
                        "net/sf/saxon/option/cpp/XPathProcessor");
20
        if ((proc->proc) == NULL) {
21
                std::cerr << "Processor is NULL" << std::endl;
22
        }
23

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

    
27

    
28

    
29
#ifdef DEBUG
30
        jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
31
        SaxonProcessor::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
                std::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) SaxonProcessor::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
        std::cerr << "Error: "<<getDllname() << ".evaluate" << " not found\n"
54
                        << std::endl;
55

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

    
62
        int size = parameters.size() + properties.size();
63

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

    
104
                XdmValue * value = new XdmValue();
105
                XdmItem * xdmItem = NULL;
106
                for (int p=0; p < sizex; ++p) 
107
                {
108
                        jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
109
                        //value->addUnderlyingValue(resulti);
110

    
111
                        if(SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, atomicValueClass)           == JNI_TRUE) {
112
                                xdmItem = new XdmAtomicValue(resulti);
113

    
114
                        } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, nodeClass)           == JNI_TRUE) {
115
                                xdmItem = new XdmNode(resulti);
116

    
117
                        } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, functionItemClass)           == JNI_TRUE) {
118
                                xdmItem =  new XdmFunctionItem(resulti);
119
                        }
120
                        xdmItem->setProcessor(proc);
121
                        if(sizex == 1) {
122
                                value->decrementRefCount();
123
                                delete value;
124
                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(results);
125
                                return xdmItem;                
126
                        }
127
                        
128
                        value->addXdmItem(xdmItem);
129
                }
130
                value->setProcessor(proc);
131
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(results);
132
                return value;
133
        }
134
}
135
return NULL;
136

    
137
}
138

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

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

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

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

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

    
219
                } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
220
                        return NULL;
221
                }
222
                xdmItem->setProcessor(proc);
223
                //SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
224
                return xdmItem;
225
        } else  {
226
                proc->checkAndCreateException(cppClass);
227
                           
228
                     }
229
}
230

    
231
return NULL;
232

    
233
}
234

    
235
void XPathProcessor::setContextItem(XdmItem * item) {
236
        contextItem = item;
237
            if(item != NULL){
238
              parameters["node"] = (XdmValue *)item;
239
            }
240
}
241

    
242
void XPathProcessor::setContextFile(const char * filename) {
243
        if (filename != NULL) {
244
                setProperty("s", filename);
245
        }
246
}
247

    
248

    
249
void XPathProcessor::declareNamespace(const char *prefix, const char * uri){
250
        if (prefix == NULL || uri == NULL) {
251
                return;
252
        }
253
        jmethodID mID =
254
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "declareNamespace",
255
                                "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
256
        if (!mID) {
257
        std::cerr << "Error: "<<getDllname() << ".declareNameSpace" << " not found\n"
258
                        << std::endl;
259

    
260
        } else {
261
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXP, mID,
262
                                SaxonProcessor::sxn_environ->env->NewStringUTF(prefix),
263
                                SaxonProcessor::sxn_environ->env->NewStringUTF(uri));
264
                //proc->checkAndCreateException(cppClass);
265
        }
266

    
267
}
268

    
269
#if CVERSION_API_NO >= 121
270

    
271

    
272
void XPathProcessor::setBackwardsCompatible(bool option) {
273
    if (option) {
274
                        setProperty("backwardsCom:", "true");
275
    } else {
276
       properties.erase("backwardsCom:");
277
    }
278
}
279

    
280
void XPathProcessor::setCaching(bool caching){
281
    if (caching) {
282
                setProperty("caching:", "true");
283
    } else {
284
       properties.erase("caching:");
285
    }
286

    
287
}
288

    
289
void XPathProcessor::importSchemaNamespace(const char* uri){
290
      if (uri != NULL) {
291
                       setProperty("importSN", uri);
292
      }
293
}
294

    
295

    
296
#endif
297

    
298

    
299
void XPathProcessor::setBaseURI(const char * uriStr) {
300
        if (uriStr == NULL) {
301
                std::cerr << "Error:: XPath string cannot be empty or NULL" << std::endl;
302
             return;
303
        }
304
setProperty("resources", proc->getResourcesDirectory());
305
jmethodID mID =
306
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "setBaseURI",
307
                                "(Ljava/lang/String;)V");
308
if (!mID) {
309
        std::cerr << "Error: Saxonc." << "setBaseURI" << " not found\n"
310
                        << std::endl;
311

    
312
} else {
313

    
314
        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXP, mID,
315
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(uriStr));
316
}
317

    
318
}
319

    
320
bool XPathProcessor::effectiveBooleanValue(const char * xpathStr) {
321
        if (xpathStr == NULL) {
322
                std::cerr << "Error:: XPath string cannot be empty or NULL" << std::endl;
323
             return false;
324
        }
325
setProperty("resources", proc->getResourcesDirectory());
326
jmethodID mID =
327
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "effectiveBooleanValue",
328
                                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Z");
329
if (!mID) {
330
        std::cerr << "Error: MyClassInDll." << "effectiveBooleanValue" << " not found\n"<< std::endl;
331

    
332
} else {
333
        jobjectArray stringArray = NULL;
334
        jobjectArray objectArray = NULL;
335
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
336
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
337

    
338
        int size = parameters.size() + properties.size();
339
        if (size > 0) {
340
                objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
341
                                objectClass, 0);
342
                stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
343
                                stringClass, 0);
344
                int i = 0;
345
                for (std::map<std::string, XdmValue*>::iterator iter = parameters.begin();
346
                                iter != parameters.end(); ++iter, i++) {
347
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
348
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
349
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
350
                                        (iter->second)->getUnderlyingValue());
351
                }
352
                for (std::map<std::string, std::string>::iterator iter = properties.begin();
353
                                iter != properties.end(); ++iter, i++) {
354
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
355
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
356
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
357
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->second).c_str()));
358
                }
359
        }
360
        jboolean result = (jboolean)(
361
                        SaxonProcessor::sxn_environ->env->CallBooleanMethod(cppXP, mID,
362
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXP.c_str()),
363
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(xpathStr), stringArray, objectArray));
364
        if (size > 0) {
365
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
366
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
367
        }
368
        proc->checkAndCreateException(cppClass);
369
        return result;
370
}
371
return false;
372
}
373

    
374
void XPathProcessor::setParameter(const char * name, XdmValue* value) {
375
        if(value != NULL){
376
                value->incrementRefCount();
377
                int s = parameters.size();
378
                std::string skey = "param:"+std::string(name);
379
                parameters[skey] = value;
380
                if(s == parameters.size()) {
381
            std::map<std::string, XdmValue*>::iterator it;
382
            it = parameters.find(skey);
383
            if (it != parameters.end()) {
384
                XdmValue * valuei = it->second;
385
                valuei->decrementRefCount();
386
                if(valuei != NULL && valuei->getRefCount() < 1){
387
                    delete value;
388
                }
389
                parameters.erase(skey);
390
                parameters[skey] = value;
391
            }
392
                }
393
        }
394
}
395

    
396
bool XPathProcessor::removeParameter(const char * name) {
397
        return (bool)(parameters.erase("param:"+std::string(name)));
398
}
399

    
400
void XPathProcessor::setProperty(const char * name, const char * value) {
401
        if(name != NULL) {
402
            int s = properties.size();
403
                std::string skey = std::string(name);
404
                properties.insert(std::pair<std::string, std::string>(skey, std::string((value == NULL ? "" : value))));
405

    
406
                if(s == properties.size()) {
407
            std::map<std::string, std::string>::iterator it;
408
            it = properties.find(skey);
409
            if (it != properties.end()) {
410
                properties.erase(skey);
411
                properties[skey] = std::string((value == NULL ? "" : value));
412
            }
413
                }
414
        }
415
}
416

    
417
void XPathProcessor::clearParameters(bool delVal) {
418
        if(delVal){
419
                       for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
420
                        XdmValue * value = itr->second;
421
                        value->decrementRefCount();
422
#ifdef DEBUG
423
                        std::cerr<<"XPathProc.clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
424
#endif
425
                        if(value != NULL && value->getRefCount() < 1){                
426
                                delete value;
427
                        }
428
                }
429
                parameters.clear();
430
        }
431
}
432

    
433
void XPathProcessor::clearProperties() {
434
        properties.clear();
435
}
436

    
437

    
438
   void XPathProcessor::setcwd(const char* dir){
439
    cwdXP = std::string(dir);
440
   }
441

    
442
std::map<std::string,XdmValue*>& XPathProcessor::getParameters(){
443
        std::map<std::string,XdmValue*>& ptr = parameters;
444
        return ptr;
445
}
446

    
447
std::map<std::string,std::string>& XPathProcessor::getProperties(){
448
        std::map<std::string,std::string> &ptr = properties;
449
        return ptr;
450
}
451

    
452
void XPathProcessor::exceptionClear(){
453
        if(proc->exception != NULL) {
454
                delete proc->exception;
455
                proc->exception = NULL;        
456
        }
457

    
458
   SaxonProcessor::sxn_environ->env->ExceptionClear();
459
 
460
}
461

    
462
int XPathProcessor::exceptionCount(){
463
 if(proc->exception != NULL){
464
 return proc->exception->count();
465
 }
466
 return 0;
467
 }
468

    
469
const char * XPathProcessor::getErrorCode(int i) {
470
        if(proc->exception == NULL) {return NULL;}
471
        return proc->exception->getErrorCode(i);
472
}
473

    
474
const char * XPathProcessor::getErrorMessage(int i ){
475
        if(proc->exception == NULL) {return NULL;}
476
        return proc->exception->getErrorMessage(i);
477
}
478

    
479
    bool XPathProcessor::exceptionOccurred(){
480
        return proc->exceptionOccurred();
481
    }
482

    
483

    
484

    
485
    const char* XPathProcessor::checkException(){
486
        return proc->checkException(cppXP);
487
    }
488

    
(17-17/50)