Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XPathProcessor.cpp @ 7728ae0b

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
        cppXP = createSaxonProcessor2(SaxonProcessor::sxn_environ->env, cppClass,
32
                        "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
33

    
34

    
35

    
36
#ifdef DEBUG
37
        jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
38
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
39
#endif    
40

    
41
        exception = nullptr;
42
        if(!(proc->cwd.empty()) && curr.empty()){
43
                cwdXP = proc->cwd;
44
        } else {
45
                cwdXP = curr;
46
        }
47

    
48
}
49

    
50
XdmValue * XPathProcessor::evaluate(const char * xpathStr) {
51
        if (xpathStr == nullptr) {
52
                std::cerr << "Error:: XPath string cannot be empty or nullptr" << std::endl;
53
        return nullptr;
54
}
55

    
56
jmethodID mID =
57
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "evaluate",
58
                                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)[Lnet/sf/saxon/s9api/XdmValue;");
59
if (!mID) {
60
        std::cerr << "Error: "<<getDllname() << ".evaluate" << " not found\n"
61
                        << std::endl;
62

    
63
} else {
64
        jobjectArray stringArray = nullptr;
65
        jobjectArray objectArray = nullptr;
66
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
67
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
68

    
69
        int size = parameters.size() + properties.size();
70

    
71
        if (size > 0) {
72
                objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
73
                                objectClass, 0);
74
                stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
75
                                stringClass, 0);
76
                int i = 0;
77
                for (std::map<std::string, XdmValue*>::iterator iter = parameters.begin();
78
                                iter != parameters.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
                                        (iter->second)->getUnderlyingValue());
83
                }
84
                for (std::map<std::string, std::string>::iterator iter = properties.begin();
85
                                iter != properties.end(); ++iter, i++) {
86
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
87
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
88
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
89
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->second).c_str()));
90
                }
91
        } 
92
        jobjectArray results = (jobjectArray)(
93
                        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXP, mID,
94
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXP.c_str()),
95
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(xpathStr), stringArray, objectArray));
96
        if(!results) {
97
                exception = proc->checkAndCreateException(cppClass);
98
                return nullptr;
99
        }
100
        
101
        int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
102
        if (size > 0) {
103
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
104
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
105
        }
106
        if (sizex>0) {
107
                jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
108
                jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
109
                jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
110
        jclass mapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
111
        jclass arrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmArray");
112

    
113
                XdmValue * value = new XdmValue();
114
                XdmItem * xdmItem = nullptr;
115
                for (int p=0; p < sizex; ++p) 
116
                {
117
                        jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
118

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

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

    
125
                        } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, functionItemClass)           == JNI_TRUE) {
126
                                xdmItem =  new XdmFunctionItem(resulti);
127
                        } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, mapClass)           == JNI_TRUE) {
128
                xdmItem =  new XdmMap(resulti);
129
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, arrayClass)           == JNI_TRUE) {
130
                xdmItem =  new XdmArray(resulti);
131
            }
132
            if(sizex == 1) {
133
                                delete value;
134
                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(results);
135
                                return xdmItem;                
136
                        }
137
                        
138
                        value->addXdmItem(xdmItem);
139
                }
140
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(results);
141
                return value;
142
        }
143
}
144
return nullptr;
145

    
146
}
147

    
148
XdmItem * XPathProcessor::evaluateSingle(const char * xpathStr) {
149
        if (xpathStr == nullptr) {
150
                std::cerr << "Error:: XPath string cannot be empty or nullptr" << std::endl;
151
             return nullptr;
152
        }
153

    
154
jmethodID mID =
155
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "evaluateSingle",
156
                                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmItem;");
157
if (!mID) {
158
        std::cerr << "Error: MyClassInDll." << "evaluateSingle" << " not found\n"
159
                        << std::endl;
160

    
161
} else {
162
        jobjectArray stringArray = nullptr;
163
        jobjectArray objectArray = nullptr;
164
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
165
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
166

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

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

    
224
                } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
225
                        
226
                        xdmItem = new XdmNode(result);
227

    
228
                } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
229
                        return nullptr;
230
                }
231
                //SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
232
                return xdmItem;
233
        } else  {
234
                        exception = proc->checkAndCreateException(cppClass);
235
                           
236
                     }
237
}
238

    
239
return nullptr;
240

    
241
}
242

    
243
void XPathProcessor::setContextItem(XdmItem * item) {
244
        contextItem = item;
245
            if(item != nullptr){
246
              parameters["node"] = (XdmValue *)item;
247
            }
248
}
249

    
250
void XPathProcessor::setContextFile(const char * filename) {
251
        if (filename != nullptr) {
252
                setProperty("s", filename);
253
        }
254
}
255

    
256

    
257
void XPathProcessor::declareNamespace(const char *prefix, const char * uri){
258
        if (prefix == nullptr || uri == nullptr) {
259
                return;
260
        }
261
        jmethodID mID =
262
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "declareNamespace",
263
                                "(Ljava/lang/String;Ljava/lang/String;)V");
264
        if (!mID) {
265
        std::cerr << "Error: "<<getDllname() << ".declareNameSpace" << " not found\n"
266
                        << std::endl;
267

    
268
        } else {
269
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXP, mID,
270
                                SaxonProcessor::sxn_environ->env->NewStringUTF(prefix),
271
                                SaxonProcessor::sxn_environ->env->NewStringUTF(uri));
272
                //proc->checkAndCreateException(cppClass);
273
        }
274

    
275
}
276

    
277
#if CVERSION_API_NO >= 121
278

    
279

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

    
288
void XPathProcessor::setCaching(bool caching){
289
    if (caching) {
290
                setProperty("caching:", "true");
291
    } else {
292
       properties.erase("caching:");
293
    }
294

    
295
}
296

    
297
void XPathProcessor::importSchemaNamespace(const char* uri){
298
      if (uri != nullptr) {
299
                       setProperty("importSN", uri);
300
      }
301
}
302

    
303

    
304
#endif
305

    
306

    
307
void XPathProcessor::setBaseURI(const char * uriStr) {
308
        if (uriStr == nullptr) {
309
                std::cerr << "Error:: XPath string cannot be empty or nullptr" << std::endl;
310
             return;
311
        }
312

    
313
jmethodID mID =
314
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "setBaseURI",
315
                                "(Ljava/lang/String;)V");
316
if (!mID) {
317
        std::cerr << "Error: Saxonc." << "setBaseURI" << " not found\n"
318
                        << std::endl;
319

    
320
} else {
321

    
322
        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXP, mID,
323
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(uriStr));
324
}
325

    
326
}
327

    
328
bool XPathProcessor::effectiveBooleanValue(const char * xpathStr) {
329
        if (xpathStr == nullptr) {
330
                std::cerr << "Error:: XPath string cannot be empty or nullptr" << std::endl;
331
             return false;
332
        }
333

    
334
jmethodID mID =
335
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "effectiveBooleanValue",
336
                                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Z");
337
if (!mID) {
338
        std::cerr << "Error: MyClassInDll." << "effectiveBooleanValue" << " not found\n"<< std::endl;
339

    
340
} else {
341
        jobjectArray stringArray = nullptr;
342
        jobjectArray objectArray = nullptr;
343
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
344
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
345

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

    
382
void XPathProcessor::setParameter(const char * name, XdmValue* value) {
383
        if(value != nullptr){
384
                value->incrementRefCount();
385
                int s = parameters.size();
386
                std::string skey = "param:"+std::string(name);
387
                parameters[skey] = value;
388
                if(s == parameters.size()) {
389
            std::map<std::string, XdmValue*>::iterator it;
390
            it = parameters.find(skey);
391
            if (it != parameters.end()) {
392
                XdmValue * valuei = it->second;
393
                if(valuei != nullptr) {
394
                    valuei->decrementRefCount();
395
                    if (valuei->getRefCount() < 1) {
396
                        delete value;
397
                    }
398
                    parameters.erase(skey);
399
                    parameters[skey] = value;
400
                }
401
            }
402
                }
403
        }
404
}
405

    
406
bool XPathProcessor::removeParameter(const char * name) {
407
        return (bool)(parameters.erase("param:"+std::string(name)));
408
}
409

    
410
void XPathProcessor::setProperty(const char * name, const char * value) {
411
        if(name != nullptr) {
412
            int s = properties.size();
413
                std::string skey = std::string(name);
414
                properties.insert(std::pair<std::string, std::string>(skey, std::string((value == nullptr ? "" : value))));
415

    
416
                if(s == properties.size()) {
417
            std::map<std::string, std::string>::iterator it;
418
            it = properties.find(skey);
419
            if (it != properties.end()) {
420
                properties.erase(skey);
421
                properties[skey] = std::string((value == nullptr ? "" : value));
422
            }
423
                }
424
        }
425
}
426

    
427
void XPathProcessor::clearParameters(bool delVal) {
428
        if(delVal) {
429
        for (std::map<std::string, XdmValue *>::iterator itr = parameters.begin(); itr != parameters.end(); itr++) {
430
            XdmValue *value = itr->second;
431
            if (value != nullptr) {
432
                value->decrementRefCount();
433
#ifdef DEBUG
434
                std::cerr<<"XPathProc.clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
435
#endif
436
                if (value->getRefCount() < 1) {
437
                    delete value;
438
                }
439
            }
440
        }
441
    }
442
        parameters.clear();
443

    
444
}
445

    
446
void XPathProcessor::clearProperties() {
447
        properties.clear();
448
}
449

    
450

    
451
   void XPathProcessor::setcwd(const char* dir){
452
    cwdXP = std::string(dir);
453
   }
454

    
455
std::map<std::string,XdmValue*>& XPathProcessor::getParameters(){
456
        std::map<std::string,XdmValue*>& ptr = parameters;
457
        return ptr;
458
}
459

    
460
std::map<std::string,std::string>& XPathProcessor::getProperties(){
461
        std::map<std::string,std::string> &ptr = properties;
462
        return ptr;
463
}
464

    
465
void XPathProcessor::exceptionClear(){
466
        if(exception != nullptr) {
467
                delete proc->exception;
468
                exception = nullptr;
469
        }
470

    
471
   SaxonProcessor::sxn_environ->env->ExceptionClear();
472
 
473
}
474

    
475
const char * XPathProcessor::getErrorCode() {
476
        if(exception == nullptr) {return nullptr;}
477
        return exception->getErrorCode();
478
}
479

    
480
const char * XPathProcessor::getErrorMessage(){
481
        if(exception == nullptr) {return nullptr;}
482
        return exception->getMessage();
483
}
484

    
485
    SaxonApiException * XPathProcessor::getException() {
486
        return exception;
487

    
488
}
489

    
490
    bool XPathProcessor::exceptionOccurred(){
491
        return proc->exceptionOccurred();
492
    }
493

    
494

    
495

    
496
    const char* XPathProcessor::checkException(){
497
        return proc->checkException();
498
    }
499

    
(20-20/56)