Project

Profile

Help

Download (17.7 KB) Statistics
| Branch: | Revision:

he / src / main / c / Saxon.C.API / SaxonCXPath.c @ 02e2c377

1 72bf04c6 Norman Walsh
#include "SaxonCXPath.h"
2
3
4
5
6
    /**
7
     * Create boxed Boolean value
8
     * @param val - boolean value
9
     */
10
jobject booleanValue(sxnc_environment *environi, bool b){ 
11
        
12
         if(environi->env == NULL) {
13 ead48a5d O'Neil Delpratt
                printf("Error: Saxon-C env variable is NULL\n");
14 72bf04c6 Norman Walsh
                fflush (stdout);
15
                   return NULL;
16
         }
17
         jclass  booleanClass = lookForClass(environi->env, "java/lang/Boolean");
18
         static jmethodID MID_init = NULL;
19
         if(!MID_init) {
20
             MID_init = (jmethodID)findConstructor (environi->env, booleanClass, "(Z)V");
21
         }
22
         jobject booleanValue = (jobject)(*(environi->env))->NewObject(environi->env, booleanClass, MID_init, (jboolean)b);
23
               if (!booleanValue) {
24
                    printf("Error: failed to allocate Boolean object\n");
25
                fflush (stdout);
26
                return NULL;
27
               }
28
         return booleanValue;
29
}
30
31
    /**
32
     * Create an boxed Integer value
33
     * @param val - int value
34
     */
35
jobject integerValue(sxnc_environment *environi, int i){ 
36
        
37
         if(environi->env == NULL) {
38 ead48a5d O'Neil Delpratt
                printf("Error: Saxon-C env variable is NULL\n");
39 72bf04c6 Norman Walsh
                fflush (stdout);
40
                   return NULL;
41
         }
42
43
         jclass  integerClass = lookForClass(environi->env, "java/lang/Integer");
44
         /*static */ jmethodID intMID = NULL;
45
         //if(!intMID){
46
                intMID = (jmethodID)(*(environi->env))->GetMethodID (environi->env, integerClass, "<init>", "(I)V");
47
         //}
48
if(!intMID){
49
        printf("error in intMID");
50
}        
51
         jobject intValue = (*(environi->env))->NewObject(environi->env, integerClass, intMID, (jint)i);
52
               if (!intValue) {
53
                    printf("Error: failed to allocate Integer object\n");
54
printf("Value to build: %i",i);
55
                fflush (stdout);
56
//(*(environi->env))->ExceptionDescribe(environi->env); //remove line
57
                return NULL;
58
               }
59
         return intValue;
60
        }
61
62
63
    /**
64
     * Create an boxed Double value
65
     * @param val - double value
66
     */
67
jobject doubleValue(sxnc_environment *environi, double d){ 
68
        
69
         if(environi->env == NULL) {
70 ead48a5d O'Neil Delpratt
                printf("Error: Saxon-C env variable is NULL\n");
71 72bf04c6 Norman Walsh
                fflush (stdout);
72
                   return NULL;
73
         }
74
         jclass  doubleClass = lookForClass(environi->env, "java/lang/Double");
75
         static jmethodID dbID = NULL;
76
        if(!dbID) {
77
                dbID = (jmethodID)findConstructor (environi->env, doubleClass, "(D)V");
78
        }        
79
         jobject doubleValue = (jobject)(*(environi->env))->NewObject(environi->env, doubleClass, dbID, (jdouble)d);
80
               if (!doubleValue) {
81
                    printf("Error: failed to allocate Double object\n");
82
                fflush (stdout);
83
                return NULL;
84
               }
85
         return doubleValue;
86
        }
87
88
    /**
89
     * Create an boxed Float value
90
     * @param val - float value
91
     */
92
jobject floatValue(sxnc_environment *environi, float f){ 
93
        
94
         if(environi->env == NULL) {
95 ead48a5d O'Neil Delpratt
                printf("Error: Saxon-C env variable is NULL\n");
96 72bf04c6 Norman Walsh
                fflush (stdout);
97
                   return NULL;
98
         }
99
         jclass  floatClass = lookForClass(environi->env, "java/lang/Float");
100
         static jmethodID fID = NULL;
101
         if(!fID){
102
                 fID = (jmethodID)findConstructor (environi->env, floatClass, "(F)V");
103
         }
104
         jobject floatValue = (jobject)(*(environi->env))->NewObject(environi->env, floatClass, fID, (jfloat)f);
105
               if (!floatValue) {
106
                    printf("Error: failed to allocate float object\n");
107
                fflush (stdout);
108
                return NULL;
109
               }
110
         return floatValue;
111
        }
112
113
114
    /**
115
     * Create an boxed Long value
116
     * @param val - Long value
117
     */
118
jobject longValue(sxnc_environment *environi, long l){ 
119
        
120
         if(environi->env == NULL) {
121 ead48a5d O'Neil Delpratt
                printf("Error: Saxon-C env variable is NULL\n");
122 72bf04c6 Norman Walsh
                fflush (stdout);
123
                   return NULL;
124
         }
125
         jclass  longClass = lookForClass(environi->env, "java/lang/Long");
126
         static jmethodID lID = NULL;
127
         if(lID) {
128
                  lID = (jmethodID)findConstructor (environi->env, longClass, "(L)V");
129
        }        
130
         jobject longValue = (jobject)(*(environi->env))->NewObject(environi->env, longClass, lID, (jlong)l);
131
               if (!longValue) {
132
                    printf("Error: failed to allocate long object\n");
133
                fflush (stdout);
134
                return NULL;
135
               }
136
         return longValue;
137
        }
138
139
    /**
140
     * Create an boxed String value
141
     * @param val - as char array value
142
     */
143
jobject getJavaStringValue(sxnc_environment *environi, const char *str){ 
144
        
145
         if(environi->env == NULL) {
146 ead48a5d O'Neil Delpratt
                printf("Error: Saxon-C env variable is NULL\n");
147 72bf04c6 Norman Walsh
                fflush (stdout);
148
                   return NULL;
149
         }
150
        if(str == NULL) {
151
                return (*(environi->env))->NewStringUTF(environi->env, "");        
152
        }
153
        jstring jstrBuf = (*(environi->env))->NewStringUTF(environi->env, str);
154
         
155
               if (!jstrBuf) {
156
                    printf("Error: failed to allocate String object\n");
157
                fflush (stdout);
158
                return NULL;
159
               }
160
         return jstrBuf;
161
        }
162
163
164
165
jobject xdmValueAsObj(sxnc_environment *environi, const char* type, const char* str){ 
166
        
167
        jclass  saxoncClass = lookForClass(environi->env, "net/sf/saxon/option/cpp/SaxonCAPI");
168
         if(environi->env == NULL) {
169 ead48a5d O'Neil Delpratt
                printf("Error: Saxon-C env variable is NULL\n");
170 72bf04c6 Norman Walsh
                fflush (stdout);
171
                   return NULL;
172
         }
173
        char methodName[] = "createXdmAtomicItem";
174
            char args[] = "(Ljava/lang/String;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmValue;";
175
                
176
        static jmethodID MID_xdmValueo = NULL;
177
        if(!MID_xdmValueo) {
178
                MID_xdmValueo = (jmethodID)(*(environi->env))->GetStaticMethodID(environi->env, saxoncClass, methodName, args);
179
        }
180
       if (!MID_xdmValueo) {
181
          printf("\nError: MyClassInDll %s() not found\n",methodName);
182
            fflush (stdout);
183
          return NULL;
184
      }
185
   jobject resultObj = ((*(environi->env))->CallStaticObjectMethod(environi->env, saxoncClass, MID_xdmValueo, type, str));
186
   if(resultObj) {
187
        return resultObj;
188
   } 
189
   return NULL;
190
}
191
192
    /**
193
     * A Constructor. Create a XdmValue based on the target type. Conversion is applied if possible
194
     * @param type - specify target type of the value as the local name of the xsd built in type. For example 'gYearMonth' 
195
     * @param val - Value to convert
196
     */
197
sxnc_value * xdmValue(sxnc_environment *environi, const char* type, const char* str){ 
198
        
199
        jclass  saxoncClass = lookForClass(environi->env, "net/sf/saxon/option/cpp/SaxonCAPI");
200
         if(environi->env == NULL) {
201 ead48a5d O'Neil Delpratt
                printf("Error: Saxon-C env variable is NULL\n");
202 72bf04c6 Norman Walsh
                fflush (stdout);
203
                   return NULL;
204
         }
205
        char methodName[] = "createXdmAtomicValue";
206
            char args[] = "(Ljava/lang/String;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmValue;";
207
                
208
        static jmethodID MID_xdmValue = NULL;
209
        if(!MID_xdmValue) {
210
                MID_xdmValue = (jmethodID)(*(environi->env))->GetStaticMethodID(environi->env, saxoncClass, methodName, args);
211
        }
212
       if (!MID_xdmValue) {
213
          printf("\nError: MyClassInDll %s() not found\n",methodName);
214
            fflush (stdout);
215
          return NULL;
216
      }
217
   jobject resultObj = ((*(environi->env))->CallStaticObjectMethod(environi->env, saxoncClass, MID_xdmValue, type, str));
218
   if(resultObj) {
219
        sxnc_value* result = (sxnc_value *)malloc(sizeof(sxnc_value));
220
         result->xdmvalue = resultObj; 
221
         return result; 
222
   } 
223
   return NULL;
224
}
225
226
sxnc_value * evaluate(sxnc_environment *environi, sxnc_processor ** proc, char * cwd, char * xpathStr, sxnc_parameter *parameters, sxnc_property * properties, int parLen, int propLen){
227
        
228
        static jmethodID emID = NULL; //cache the methodID
229
        if(!proc){} // code to avoid warning of unused variable. TODO: Remove use of variable in next release
230
        jclass cppClass = lookForClass(environi->env, "net/sf/saxon/option/cpp/XPathProcessor");
231
232
        if(!cpp) {
233
                cpp = (jobject) createSaxonProcessor (environi->env, cppClass, "(Z)V", NULL, (jboolean)sxn_license);
234
        }
235
236
        if(emID == NULL) {
237
                emID = (jmethodID)(*(environi->env))->GetMethodID (environi->env, cppClass,"evaluateSingle", "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmItem;");
238
                if (!emID) {
239
                        printf("Error: MyClassInDll. evaluateSingle not found\n");
240
                        fflush (stdout);
241
                        return 0;
242
                 } 
243
        }
244
         
245
        jobjectArray stringArray = NULL;
246
        jobjectArray objectArray = NULL;
247
        int size = parLen + propLen+1; //We add one here for the resources-dir property
248
249
        if(size >0) {
250
           jclass objectClass = lookForClass(environi->env, "java/lang/Object");
251
           jclass stringClass = lookForClass(environi->env, "java/lang/String");
252
           objectArray = (*(environi->env))->NewObjectArray(environi->env, (jint)size, objectClass, 0 );
253
           stringArray = (*(environi->env))->NewObjectArray(environi->env, (jint)size, stringClass, 0 );
254
           if((!objectArray) || (!stringArray)) { return 0;}
255
           int i=0;
256
           for(i =0; i< parLen; i++) {
257
                
258
             (*(environi->env))->SetObjectArrayElement(environi->env, stringArray, i, (*(environi->env))->NewStringUTF(environi->env, parameters[i].name) );
259
             (*(environi->env))->SetObjectArrayElement(environi->env, objectArray, i, (jobject)(parameters[i].value) );
260
        
261
           }
262
263
           (*(environi->env))->SetObjectArrayElement(environi->env, stringArray, i, (*(environi->env))->NewStringUTF(environi->env,"resources"));
264
             (*(environi->env))->SetObjectArrayElement(environi->env, objectArray, i, (jobject)((*(environi->env))->NewStringUTF(environi->env, getResourceDirectory())) );
265
            i++;
266
           int j=0;        
267
             for(; j < propLen; j++, i++) {
268
             (*(environi->env))->SetObjectArrayElement(environi->env, stringArray, i, (*(environi->env))->NewStringUTF(environi->env, properties[j].name));
269
             (*(environi->env))->SetObjectArrayElement(environi->env, objectArray, i, (jobject)((*(environi->env))->NewStringUTF(environi->env, properties[j].value)) );
270
           }
271
          
272
        }
273
      jobject resultObj = (jstring)((*(environi->env))->CallObjectMethod(environi->env, cpp, emID, (cwd== NULL ? (*(environi->env))->NewStringUTF(environi->env, "") : (*(environi->env))->NewStringUTF(environi->env, cwd)), (*(environi->env))->NewStringUTF(environi->env,xpathStr), stringArray, objectArray ));
274
275
      (*(environi->env))->DeleteLocalRef(environi->env, objectArray);
276
      (*(environi->env))->DeleteLocalRef(environi->env, stringArray);
277
      if(resultObj) {
278
         sxnc_value* result = (sxnc_value *)malloc(sizeof(sxnc_value));
279
         result->xdmvalue = resultObj;  
280
        
281
         
282
        return result;
283
     }
284
285 4ee4355d O'Neil Delpratt
    checkForException(environi);
286 72bf04c6 Norman Walsh
    return 0;
287
288
}
289
290
291
bool effectiveBooleanValue(sxnc_environment *environi, sxnc_processor ** proc, char * cwd, char * xpathStr, sxnc_parameter *parameters, sxnc_property * properties, int parLen, int propLen){
292
        
293
        static jmethodID bmID = NULL; //cache the methodID
294
        if(!proc){} // code to avoid warning of unused variable. TODO: Remove use of variable in next release
295
        jclass cppClass = lookForClass(environi->env, "net/sf/saxon/option/cpp/XPathProcessor");
296
297
        if(!cpp) {
298
                cpp = (jobject) createSaxonProcessor (environi->env, cppClass, "(Z)V", NULL, (jboolean)sxn_license);
299
        }
300
301
        if(bmID == NULL) {
302
                bmID = (jmethodID)(*(environi->env))->GetMethodID (environi->env, cppClass,"effectiveBooleanValue", "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Z");
303
                if (!bmID) {
304
                        printf("Error: MyClassInDll. effectiveBooleanValue not found\n");
305
                        fflush (stdout);
306
                        return 0;
307
                 } 
308
        }
309
         
310
        jobjectArray stringArray = NULL;
311
        jobjectArray objectArray = NULL;
312
        int size = parLen + propLen+1; //We add one here for the resources-dir property
313
314
        if(size >0) {
315
           jclass objectClass = lookForClass(environi->env, "java/lang/Object");
316
           jclass stringClass = lookForClass(environi->env, "java/lang/String");
317
           objectArray = (*(environi->env))->NewObjectArray(environi->env, (jint)size, objectClass, 0 );
318
           stringArray = (*(environi->env))->NewObjectArray(environi->env, (jint)size, stringClass, 0 );
319
           if((!objectArray) || (!stringArray)) { 
320
                printf("Error: parameter and property arrays have some inconsistencies \n");
321
                fflush (stdout);                
322
                return 0; //false
323
           }
324
           int i=0;
325
           for(i =0; i< parLen; i++) {
326
                
327
             (*(environi->env))->SetObjectArrayElement(environi->env, stringArray, i, (*(environi->env))->NewStringUTF(environi->env, parameters[i].name) );
328
             (*(environi->env))->SetObjectArrayElement(environi->env, objectArray, i, (jobject)(parameters[i].value) );
329
        
330
           }
331
332
           (*(environi->env))->SetObjectArrayElement(environi->env, stringArray, i, (*(environi->env))->NewStringUTF(environi->env,"resources"));
333
             (*(environi->env))->SetObjectArrayElement(environi->env, objectArray, i, (jobject)((*(environi->env))->NewStringUTF(environi->env, getResourceDirectory())) );
334
            i++;
335
           int j=0;        
336
             for(; j<propLen; j++, i++) {
337
             (*(environi->env))->SetObjectArrayElement(environi->env, stringArray, i, (*(environi->env))->NewStringUTF(environi->env, properties[j].name));
338
             (*(environi->env))->SetObjectArrayElement(environi->env, objectArray, i, (jobject)((*(environi->env))->NewStringUTF(environi->env, properties[j].value)) );
339
           }
340
          
341
        }
342
      jboolean resultObj = (jboolean)((*(environi->env))->CallBooleanMethod(environi->env, cpp, bmID, (cwd== NULL ? (*(environi->env))->NewStringUTF(environi->env, "") : (*(environi->env))->NewStringUTF(environi->env, cwd)), (*(environi->env))->NewStringUTF(environi->env,xpathStr), (jobjectArray)stringArray, (jobjectArray)objectArray ));
343
344
      (*(environi->env))->DeleteLocalRef(environi->env, objectArray);
345
      (*(environi->env))->DeleteLocalRef(environi->env, stringArray);
346
      if(resultObj) {
347 4ee4355d O'Neil Delpratt
         checkForException(environi);
348 72bf04c6 Norman Walsh
        return resultObj;
349
     }
350
351 4ee4355d O'Neil Delpratt
    checkForException(environi);
352 72bf04c6 Norman Walsh
    return 0; //false
353
}
354
355
const char * getStringValue(sxnc_environment *environi, sxnc_value value){
356
        return stringValue(environi, value.xdmvalue);
357
}
358
359
int size(sxnc_environment *environi, sxnc_value val){
360
        
361
        jclass  xdmValueClass = lookForClass(environi->env, "net/sf/saxon/s9api/XdmValue");
362
         if(environi->env == NULL) {
363 ead48a5d O'Neil Delpratt
                printf("Error: Saxon-C env variable is NULL\n");
364 72bf04c6 Norman Walsh
                fflush (stdout);
365
                   return 0;
366
         }
367
        char methodName[] = "size";
368
            char args[] = "()I";
369
                
370
        static jmethodID MID_xdmValue = NULL;
371
        if(!MID_xdmValue) {
372
                MID_xdmValue = (jmethodID)(*(environi->env))->GetMethodID(environi->env, xdmValueClass, methodName, args);
373
        }
374
       if (!MID_xdmValue) {
375
          printf("\nError: Saxon-C %s() not found\n",methodName);
376
            fflush (stdout);
377
          return 0;
378
      }
379
      jint result = (jint)(*(environi->env))->CallIntMethod(environi->env,val.xdmvalue, MID_xdmValue);
380
          
381
        return result;
382
    
383
}
384
385
sxnc_value * itemAt(sxnc_environment *environi, sxnc_value val, int i){
386
        
387
        jclass  xdmValueClass = lookForClass(environi->env, "net/sf/saxon/s9api/XdmValue");
388
         if(environi->env == NULL) {
389 ead48a5d O'Neil Delpratt
                printf("Error: Saxon-C env variable is NULL\n");
390 72bf04c6 Norman Walsh
                fflush (stdout);
391
                   return 0;
392
         }
393
        char methodName[] = "itemAt";
394
            char args[] = "(I)Lnet/sf/saxon/s9api/XdmItem;";
395
                
396
        static jmethodID MID_xdmValue = NULL;
397
        if(!MID_xdmValue) {
398
                MID_xdmValue = (jmethodID)(*(environi->env))->GetMethodID(environi->env, xdmValueClass, methodName, args);
399
        }
400
       if (!MID_xdmValue) {
401
          printf("\nError: MyClassInDll %s() not found\n",methodName);
402
            fflush (stdout);
403
          return 0;
404
      }
405
      jobject xdmItemObj = (*(environi->env))->CallObjectMethod(environi->env,val.xdmvalue, MID_xdmValue, i);
406
      if(xdmItemObj) {   
407
         sxnc_value* result = (sxnc_value *)malloc(sizeof(sxnc_value));
408
         result->xdmvalue = xdmItemObj;  
409
        
410
        //checkForException(environi, cppClass, cpp);     
411
        return result;
412
     }
413
414
    return 0;
415
}
416
417
bool isAtomicvalue(sxnc_environment *environi, sxnc_value val){
418
        
419
jclass  xdmItemClass = lookForClass(environi->env, "net/sf/saxon/s9api/XdmItem");
420
         if(environi->env == NULL) {
421 ead48a5d O'Neil Delpratt
                printf("Error: Saxon-C env variable is NULL\n");
422 72bf04c6 Norman Walsh
                fflush (stdout);
423
                   return 0;
424
         }
425
        char methodName[] = "isAtomicValue";
426
            char args[] = "()Z";
427
                
428
        static jmethodID MID_xdmValue = NULL;
429
        if(!MID_xdmValue) {
430
                MID_xdmValue = (jmethodID)(*(environi->env))->GetMethodID(environi->env, xdmItemClass, methodName, args);
431
        }
432
       if (!MID_xdmValue) {
433
          printf("\nError: Saxon library - %s() not found\n",methodName);
434
            fflush (stdout);
435
          return 0;
436
      }
437
      jboolean result = (jboolean)(*(environi->env))->CallBooleanMethod(environi->env,val.xdmvalue, MID_xdmValue);
438
     return (bool)result;
439
}
440
441
int getIntegerValue(sxnc_environment *environi, sxnc_value value,  int failureVal){
442
        
443
        const char * valueStr = getStringValue(environi, value);
444 ead48a5d O'Neil Delpratt
        if(valueStr != NULL) {
445 72bf04c6 Norman Walsh
                int value = atoi(valueStr);
446
                if(value != 0) {
447
                        return value;
448
                }
449
        } 
450
451
        if (strcmp(valueStr,"0") == 0) {
452
                return 0;
453
        } else {
454
                return failureVal;        
455
        }}
456
457
bool getBooleanValue(sxnc_environment *environi, sxnc_value value){
458
        
459
        jclass booleanClass = lookForClass(environi->env,"java/lang/Boolean");
460
        static jmethodID strMID = NULL;
461
        if(!strMID) {
462
                strMID = (jmethodID)(*(environi->env))->GetMethodID(environi->env, booleanClass, "booleanValue", "()Z");
463
                if(!strMID) {
464
                          printf("\nError: Boolean %s() not found\n","booleanValue");
465
                            fflush (stdout);
466
                          return 0;//false
467
                }
468
        }
469
        jboolean result = (jboolean)((*(environi->env))->CallBooleanMethod(environi->env, value.xdmvalue, strMID));
470
        return (bool)result;
471
}
472
473
long getLongValue(sxnc_environment *environi, sxnc_value value,  long failureVal){
474
        
475
        const char * valueStr = getStringValue(environi, value);
476 ead48a5d O'Neil Delpratt
        if(valueStr != NULL) {
477 72bf04c6 Norman Walsh
                long value = atol(valueStr);
478
                if(value != 0) {
479
                        return value;
480
                }
481
        } 
482
483
        if (strcmp(valueStr,"0") == 0) {
484
                return 0;
485
        } else {
486
                return failureVal;        
487
        }
488
}
489
490
float getFloatValue(sxnc_environment *environi, sxnc_value value,  float failureVal){
491
        
492
        jclass floatClass = lookForClass(environi->env,"java/lang/Float");
493
        static jmethodID strMID = NULL;
494
        if(!strMID) {
495
                strMID = (jmethodID)(*(environi->env))->GetMethodID(environi->env, floatClass, "floatValue", "()F");
496
                if(!strMID) {
497
                          printf("\nError: Float %s() not found\n","floatValue");
498
                            fflush (stdout);
499
                          return 0;
500
                }
501
        }
502
        jfloat result = (jfloat)((*(environi->env))->CallFloatMethod(environi->env, value.xdmvalue, strMID));
503
        if ((*(environi->env))->ExceptionCheck(environi->env)) {
504
                return failureVal;
505
         }
506
        return (float)result;
507
508
509
}
510
511
double getDoubleValue(sxnc_environment *environi, sxnc_value value, double failureVal){
512
        
513
        const char * valueStr = getStringValue(environi, value);
514 ead48a5d O'Neil Delpratt
        if(valueStr != NULL) {
515 72bf04c6 Norman Walsh
                double value = atof(valueStr);
516
                if(value != 0) {
517
                        return value;
518
                }
519
        } 
520
521
        if (strcmp(valueStr,"0") == 0) {
522
                return 0;
523
        } else {
524
                return failureVal;        
525
        }
526
527
}