Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XPathProcessor.cpp @ 146cfacb

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
XPathProcessor::XPathProcessor() {
11
        SaxonProcessor *p = new SaxonProcessor(false);
12
        XPathProcessor(p, "");
13
}
14

    
15
XPathProcessor::XPathProcessor(SaxonProcessor* p, std::string curr) {
16
        proc = p;
17

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

    
27
        cppXP = createSaxonProcessor2(SaxonProcessor::sxn_environ->env, cppClass,
28
                        "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
29

    
30

    
31

    
32
#ifdef DEBUG
33
        jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
34
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
35
#endif    
36

    
37
        exception = nullptr;
38
        if(!(proc->cwd.empty()) && curr.empty()){
39
                cwdXP = proc->cwd;
40
        } else {
41
                cwdXP = curr;
42
        }
43

    
44
}
45

    
46
XdmValue * XPathProcessor::evaluate(const char * xpathStr) {
47
        if (xpathStr == nullptr) {
48
                std::cerr << "Error:: XPath string cannot be empty or nullptr" << std::endl;
49
        return nullptr;
50
}
51

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

    
59
} else {
60
        jobjectArray stringArray = nullptr;
61
        jobjectArray objectArray = nullptr;
62
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
63
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
64

    
65
        int size = parameters.size() + properties.size();
66

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

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

    
115
                        if(SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, atomicValueClass)           == JNI_TRUE) {
116
                                xdmItem = new XdmAtomicValue(resulti);
117

    
118
                        } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, nodeClass)           == JNI_TRUE) {
119
                                xdmItem = new XdmNode(resulti);
120

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

    
142
}
143

    
144
XdmItem * XPathProcessor::evaluateSingle(const char * xpathStr) {
145
        if (xpathStr == nullptr) {
146
                std::cerr << "Error:: XPath string cannot be empty or nullptr" << std::endl;
147
             return nullptr;
148
        }
149

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

    
157
} else {
158
        jobjectArray stringArray = nullptr;
159
        jobjectArray objectArray = nullptr;
160
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
161
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
162

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

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

    
220
                } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
221
                        
222
                        xdmItem = new XdmNode(result);
223

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

    
235
return nullptr;
236

    
237
}
238

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

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

    
252

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

    
264
        } else {
265
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXP, mID,
266
                                SaxonProcessor::sxn_environ->env->NewStringUTF(prefix),
267
                                SaxonProcessor::sxn_environ->env->NewStringUTF(uri));
268
                //proc->checkAndCreateException(cppClass);
269
        }
270

    
271
}
272

    
273
#if CVERSION_API_NO >= 121
274

    
275

    
276
void XPathProcessor::setBackwardsCompatible(bool option) {
277
    if (option) {
278
                        setProperty("backwardsCom:", "true");
279
    } else {
280
       properties.erase("backwardsCom:");
281
    }
282
}
283

    
284
void XPathProcessor::setCaching(bool caching){
285
    if (caching) {
286
                setProperty("caching:", "true");
287
    } else {
288
       properties.erase("caching:");
289
    }
290

    
291
}
292

    
293
void XPathProcessor::importSchemaNamespace(const char* uri){
294
      if (uri != nullptr) {
295
                       setProperty("importSN", uri);
296
      }
297
}
298

    
299

    
300
#endif
301

    
302

    
303
void XPathProcessor::setBaseURI(const char * uriStr) {
304
        if (uriStr == nullptr) {
305
                std::cerr << "Error:: XPath string cannot be empty or nullptr" << std::endl;
306
             return;
307
        }
308

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

    
316
} else {
317

    
318
        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXP, mID,
319
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(uriStr));
320
}
321

    
322
}
323

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

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

    
336
} else {
337
        jobjectArray stringArray = nullptr;
338
        jobjectArray objectArray = nullptr;
339
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
340
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
341

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

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

    
402
bool XPathProcessor::removeParameter(const char * name) {
403
        return (bool)(parameters.erase("param:"+std::string(name)));
404
}
405

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

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

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

    
440
}
441

    
442
void XPathProcessor::clearProperties() {
443
        properties.clear();
444
}
445

    
446

    
447
   void XPathProcessor::setcwd(const char* dir){
448
    cwdXP = std::string(dir);
449
   }
450

    
451
std::map<std::string,XdmValue*>& XPathProcessor::getParameters(){
452
        std::map<std::string,XdmValue*>& ptr = parameters;
453
        return ptr;
454
}
455

    
456
std::map<std::string,std::string>& XPathProcessor::getProperties(){
457
        std::map<std::string,std::string> &ptr = properties;
458
        return ptr;
459
}
460

    
461
void XPathProcessor::exceptionClear(){
462
        if(exception != nullptr) {
463
                delete proc->exception;
464
                exception = nullptr;
465
        }
466

    
467
   SaxonProcessor::sxn_environ->env->ExceptionClear();
468
 
469
}
470

    
471
const char * XPathProcessor::getErrorCode() {
472
        if(exception == nullptr) {return nullptr;}
473
        return exception->getErrorCode();
474
}
475

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

    
481
    SaxonApiException * XPathProcessor::getException() {
482
        return exception;
483

    
484
}
485

    
486
    bool XPathProcessor::exceptionOccurred(){
487
        return proc->exceptionOccurred();
488
    }
489

    
490

    
491

    
492
    const char* XPathProcessor::checkException(){
493
        return proc->checkException(cppXP);
494
    }
495

    
(19-19/55)