Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XPathProcessor.cpp @ 6af44631

1
#include "XPathProcessor.h"
2
#include "XdmValue.h"
3
#include "XdmItem.h"
4
#include "XdmNode.h"
5
#include "XdmAtomicValue.h"
6
#include "XdmFunctionItem.h"
7
#include "XdmMap.h"
8
#include "XdmArray.h"
9

    
10
#ifdef MEM_DEBUG
11
#define new new(__FILE__, __LINE__)
12
#endif
13

    
14
XPathProcessor::XPathProcessor() {
15
        SaxonProcessor *p = new SaxonProcessor(false);
16
        XPathProcessor(p, "");
17
}
18

    
19
XPathProcessor::XPathProcessor(SaxonProcessor* p, std::string curr) {
20
        proc = p;
21

    
22
        /*
23
         * Look for class.
24
         */
25
        cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
26
                        "net/sf/saxon/option/cpp/XPathProcessor");
27
        if ((proc->proc) == nullptr) {
28
                std::cerr << "Processor is nullptr" << std::endl;
29
        }
30

    
31
    jobject tempcppXP = createSaxonProcessor2(SaxonProcessor::sxn_environ->env, cppClass,
32
                        "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
33

    
34

    
35
    if(tempcppXP) {
36
        cppXP = SaxonProcessor::sxn_environ->env->NewGlobalRef(tempcppXP);
37
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(tempcppXP);
38

    
39
    } else {
40
        createException("Error: Failed to create the XPathProcessor internal object");
41

    
42
    }
43

    
44

    
45

    
46
#ifdef DEBUG
47
        jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
48
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
49
#endif    
50

    
51
        exception = nullptr;
52
        if(!(proc->cwd.empty()) && curr.empty()){
53
                cwdXP = proc->cwd;
54
        } else {
55
                cwdXP = curr;
56
        }
57

    
58
}
59

    
60

    
61
XPathProcessor::~XPathProcessor(){
62
    clearProperties();
63
    clearParameters();
64

    
65
    SaxonProcessor::sxn_environ->env->DeleteGlobalRef(cppXP);
66
    exceptionClear();
67
    //delete contextItem;
68
}
69

    
70
XdmValue * XPathProcessor::evaluate(const char * xpathStr) {
71
        if (xpathStr == nullptr) {
72
                std::cerr << "Error:: XPath string cannot be empty or nullptr" << std::endl;
73
        return nullptr;
74
}
75

    
76
jmethodID mID =
77
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "evaluate",
78
                                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)[Lnet/sf/saxon/s9api/XdmValue;");
79
if (!mID) {
80
        std::cerr << "Error: "<<getDllname() << ".evaluate" << " not found\n"
81
                        << std::endl;
82

    
83
} else {
84
        jobjectArray stringArray = nullptr;
85
        jobjectArray objectArray = nullptr;
86
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
87
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
88

    
89
        int size = parameters.size() + properties.size();
90

    
91
        if (size > 0) {
92
                objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
93
                                objectClass, 0);
94
                stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
95
                                stringClass, 0);
96
                int i = 0;
97
                for (std::map<std::string, XdmValue*>::iterator iter = parameters.begin();
98
                                iter != parameters.end(); ++iter, i++) {
99
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
100
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
101
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
102
                                        (iter->second)->getUnderlyingValue());
103
                }
104
                for (std::map<std::string, std::string>::iterator iter = properties.begin();
105
                                iter != properties.end(); ++iter, i++) {
106
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
107
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
108
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
109
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->second).c_str()));
110
                }
111
        } 
112
        jobjectArray results = (jobjectArray)(
113
                        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXP, mID,
114
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXP.c_str()),
115
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(xpathStr), stringArray, objectArray));
116
        if(!results) {
117
                exception = proc->checkAndCreateException(cppClass);
118
                return nullptr;
119
        }
120
        
121
        int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
122
        if (size > 0) {
123
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
124
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
125
        }
126
        if (sizex>0) {
127
                jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
128
                jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
129
                jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
130
        jclass mapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
131
        jclass arrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmArray");
132

    
133
                XdmValue * value = new XdmValue();
134
                XdmItem * xdmItem = nullptr;
135
                for (int p=0; p < sizex; ++p) 
136
                {
137
                        jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
138

    
139
                        if(SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, atomicValueClass)           == JNI_TRUE) {
140
                                xdmItem = new XdmAtomicValue(resulti);
141

    
142
                        } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, nodeClass)           == JNI_TRUE) {
143
                                xdmItem = new XdmNode(resulti);
144

    
145
                        } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, functionItemClass)           == JNI_TRUE) {
146
                                xdmItem =  new XdmFunctionItem(resulti);
147
                        } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, mapClass)           == JNI_TRUE) {
148
                xdmItem =  new XdmMap(resulti);
149
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, arrayClass)           == JNI_TRUE) {
150
                xdmItem =  new XdmArray(resulti);
151
            }
152
            if(sizex == 1) {
153
                                delete value;
154
                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(results);
155
                                return xdmItem;                
156
                        }
157
                        
158
                        value->addXdmItem(xdmItem);
159
                }
160
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(results);
161
                return value;
162
        }
163
}
164
return nullptr;
165

    
166
}
167

    
168
XdmItem * XPathProcessor::evaluateSingle(const char * xpathStr) {
169
        if (xpathStr == nullptr) {
170
                std::cerr << "Error:: XPath string cannot be empty or nullptr" << std::endl;
171
             return nullptr;
172
        }
173

    
174
jmethodID mID =
175
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "evaluateSingle",
176
                                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmItem;");
177
if (!mID) {
178
        std::cerr << "Error: MyClassInDll." << "evaluateSingle" << " not found\n"
179
                        << std::endl;
180

    
181
} else {
182
        jobjectArray stringArray = nullptr;
183
        jobjectArray objectArray = nullptr;
184
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
185
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
186

    
187
        int size = parameters.size() + properties.size();
188
#ifdef DEBUG
189
                std::cerr<<"Properties size: "<<properties.size()<<std::endl;
190
                std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
191
                std::cerr<<"size:"<<size<<std::endl;
192
#endif
193
        if (size > 0) {
194
                objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
195
                                objectClass, 0);
196
                stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
197
                                stringClass, 0);
198
                int i = 0;
199
                for (std::map<std::string, XdmValue*>::iterator iter = parameters.begin();
200
                                iter != parameters.end(); ++iter, i++) {
201
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
202
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
203
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
204
                                        (iter->second)->getUnderlyingValue());
205
#ifdef DEBUG
206
                                std::string s1 = typeid(iter->second).name();
207
                                std::cerr<<"Type of itr:"<<s1<<std::endl;
208
                                jobject xx = (iter->second)->getUnderlyingValue();
209
                                if(xx == nullptr) {
210
                                        std::cerr<<"value failed"<<std::endl;
211
                                } else {
212

    
213
                                        std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
214
                                }
215
                                if((iter->second)->getUnderlyingValue() == nullptr) {
216
                                        std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
217
                                }
218
#endif
219
                }
220
                for (std::map<std::string, std::string>::iterator iter = properties.begin();
221
                                iter != properties.end(); ++iter, i++) {
222
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
223
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
224
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
225
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->second).c_str()));
226
                }
227
        }
228
        jobject result = (jobject)(
229
                        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXP, mID,
230
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXP.c_str()),
231
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(xpathStr), stringArray, objectArray));
232
        if (size > 0) {
233
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
234
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
235
        }
236
        if (result) {
237
                jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
238
                jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
239
                jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
240
                XdmItem * xdmItem = nullptr;
241
                if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
242
                        xdmItem = new XdmAtomicValue(result);
243

    
244
                } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
245
                        
246
                        xdmItem = new XdmNode(result);
247

    
248
                } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
249
                        return nullptr;
250
                }
251
                //SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
252
                return xdmItem;
253
        } else  {
254
                        exception = proc->checkAndCreateException(cppClass);
255
                           
256
                     }
257
}
258

    
259
return nullptr;
260

    
261
}
262

    
263
void XPathProcessor::setContextItem(XdmItem * item) {
264
            if(item != nullptr){
265
              parameters["node"] = (XdmValue *)item;
266
            }
267
}
268

    
269
void XPathProcessor::setContextFile(const char * filename) {
270
        if (filename != nullptr) {
271
                setProperty("s", filename);
272
        }
273
}
274

    
275

    
276
void XPathProcessor::declareNamespace(const char *prefix, const char * uri){
277
        if (prefix == nullptr || uri == nullptr) {
278
                return;
279
        }
280
        jmethodID mID =
281
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "declareNamespace",
282
                                "(Ljava/lang/String;Ljava/lang/String;)V");
283
        if (!mID) {
284
        std::cerr << "Error: "<<getDllname() << ".declareNameSpace" << " not found\n"
285
                        << std::endl;
286

    
287
        } else {
288
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXP, mID,
289
                                SaxonProcessor::sxn_environ->env->NewStringUTF(prefix),
290
                                SaxonProcessor::sxn_environ->env->NewStringUTF(uri));
291
                //proc->checkAndCreateException(cppClass);
292
        }
293

    
294
}
295

    
296
#if CVERSION_API_NO >= 121
297

    
298

    
299
void XPathProcessor::setBackwardsCompatible(bool option) {
300
    if (option) {
301
                        setProperty("backwardsCom:", "true");
302
    } else {
303
       properties.erase("backwardsCom:");
304
    }
305
}
306

    
307
void XPathProcessor::setCaching(bool caching){
308
    if (caching) {
309
                setProperty("caching:", "true");
310
    } else {
311
       properties.erase("caching:");
312
    }
313

    
314
}
315

    
316
void XPathProcessor::importSchemaNamespace(const char* uri){
317
      if (uri != nullptr) {
318
                       setProperty("importSN", uri);
319
      }
320
}
321

    
322

    
323
#endif
324

    
325

    
326
void XPathProcessor::setBaseURI(const char * uriStr) {
327
        if (uriStr == nullptr) {
328
                std::cerr << "Error:: XPath string cannot be empty or nullptr" << std::endl;
329
             return;
330
        }
331

    
332
jmethodID mID =
333
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "setBaseURI",
334
                                "(Ljava/lang/String;)V");
335
if (!mID) {
336
        std::cerr << "Error: Saxonc." << "setBaseURI" << " not found\n"
337
                        << std::endl;
338

    
339
} else {
340

    
341
        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXP, mID,
342
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(uriStr));
343
}
344

    
345
}
346

    
347
bool XPathProcessor::effectiveBooleanValue(const char * xpathStr) {
348
        if (xpathStr == nullptr) {
349
                std::cerr << "Error:: XPath string cannot be empty or nullptr" << std::endl;
350
             return false;
351
        }
352

    
353
jmethodID mID =
354
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "effectiveBooleanValue",
355
                                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Z");
356
if (!mID) {
357
        std::cerr << "Error: MyClassInDll." << "effectiveBooleanValue" << " not found\n"<< std::endl;
358

    
359
} else {
360
        jobjectArray stringArray = nullptr;
361
        jobjectArray objectArray = nullptr;
362
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
363
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
364

    
365
        int size = parameters.size() + properties.size();
366
        if (size > 0) {
367
                objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
368
                                objectClass, 0);
369
                stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
370
                                stringClass, 0);
371
                int i = 0;
372
                for (std::map<std::string, XdmValue*>::iterator iter = parameters.begin();
373
                                iter != parameters.end(); ++iter, i++) {
374
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
375
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
376
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
377
                                        (iter->second)->getUnderlyingValue());
378
                }
379
                for (std::map<std::string, std::string>::iterator iter = properties.begin();
380
                                iter != properties.end(); ++iter, i++) {
381
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
382
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
383
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
384
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->second).c_str()));
385
                }
386
        }
387
        jboolean result = (jboolean)(
388
                        SaxonProcessor::sxn_environ->env->CallBooleanMethod(cppXP, mID,
389
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXP.c_str()),
390
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(xpathStr), stringArray, objectArray));
391
        if (size > 0) {
392
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
393
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
394
        }
395
        exception = proc->checkAndCreateException(cppClass);
396
        return result;
397
}
398
return false;
399
}
400

    
401
void XPathProcessor::setParameter(const char * name, XdmValue* value) {
402
        if(value != nullptr){
403
                value->incrementRefCount();
404
                int s = parameters.size();
405
                std::string skey = "param:"+std::string(name);
406
                parameters[skey] = value;
407
                if(s == parameters.size()) {
408
            std::map<std::string, XdmValue*>::iterator it;
409
            it = parameters.find(skey);
410
            if (it != parameters.end()) {
411
                XdmValue * valuei = it->second;
412
                if(valuei != nullptr) {
413
                    valuei->decrementRefCount();
414
                    if (valuei->getRefCount() < 1) {
415
                        delete value;
416
                    }
417
                    parameters.erase(skey);
418
                    parameters[skey] = value;
419
                }
420
            }
421
                }
422
        }
423
}
424

    
425
bool XPathProcessor::removeParameter(const char * name) {
426
        return (bool)(parameters.erase("param:"+std::string(name)));
427
}
428

    
429
void XPathProcessor::setProperty(const char * name, const char * value) {
430
        if(name != nullptr) {
431
            int s = properties.size();
432
                std::string skey = std::string(name);
433
                properties.insert(std::pair<std::string, std::string>(skey, std::string((value == nullptr ? "" : value))));
434

    
435
                if(s == properties.size()) {
436
            std::map<std::string, std::string>::iterator it;
437
            it = properties.find(skey);
438
            if (it != properties.end()) {
439
                properties.erase(skey);
440
                properties[skey] = std::string((value == nullptr ? "" : value));
441
            }
442
                }
443
        }
444
}
445

    
446
void XPathProcessor::clearParameters(bool delVal) {
447
        if(delVal) {
448
        for (std::map<std::string, XdmValue *>::iterator itr = parameters.begin(); itr != parameters.end(); itr++) {
449
            XdmValue *value = itr->second;
450
            if (value != nullptr) {
451
#ifdef DEBUG
452
                std::cerr<<"XPathProc.clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
453
#endif
454
                    delete value;
455
            }
456
        }
457
    }
458
        parameters.clear();
459

    
460
}
461

    
462
void XPathProcessor::clearProperties() {
463
        properties.clear();
464
}
465

    
466

    
467
   void XPathProcessor::setcwd(const char* dir){
468
    cwdXP = std::string(dir);
469
   }
470

    
471
std::map<std::string,XdmValue*>& XPathProcessor::getParameters(){
472
        std::map<std::string,XdmValue*>& ptr = parameters;
473
        return ptr;
474
}
475

    
476
std::map<std::string,std::string>& XPathProcessor::getProperties(){
477
        std::map<std::string,std::string> &ptr = properties;
478
        return ptr;
479
}
480

    
481
void XPathProcessor::exceptionClear(){
482
    if(exception != nullptr) {
483
        delete exception;
484
        exception = nullptr;
485
        SaxonProcessor::sxn_environ->env->ExceptionClear();
486
    }
487

    
488
}
489

    
490
const char * XPathProcessor::getErrorCode() {
491
        if(exception == nullptr) {return nullptr;}
492
        return exception->getErrorCode();
493
}
494

    
495
const char * XPathProcessor::getErrorMessage(){
496
        if(exception == nullptr) {return nullptr;}
497
        return exception->getMessage();
498
}
499

    
500
    SaxonApiException * XPathProcessor::getException() {
501
        return exception;
502

    
503
}
504

    
505
bool XPathProcessor::exceptionOccurred() {
506
    return proc->exceptionOccurred() || exception != nullptr;
507
}
508

    
509

    
510

    
511
void XPathProcessor::createException(const char * message) {
512
    if(exception != nullptr) {
513
        delete exception;
514
        exception = nullptr;
515
    }
516
    if(message == nullptr) {
517
        exception = proc->checkAndCreateException(cppClass);
518
    } else {
519
        exception = new SaxonApiException(message);
520
    }
521

    
522
}
523

    
(20-20/51)