Project

Profile

Help

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

he / latest9.6 / hec / Saxon.C.API / SaxonCXPath.c @ 6fc2978c

1
#include "SaxonCXPath.h"
2

    
3

    
4

    
5

    
6
    /**
7
     * Create boxed Boolean value
8
     * @param val - boolean value
9
     */
10
jobject booleanValue(sxnc_environment environ, bool b){ 
11
        
12
         if(environ.env == NULL) {
13
                printf("Error: Saxon-C env variable is null\n");
14
                fflush (stdout);
15
                   return NULL;
16
         }
17
         jclass  booleanClass = lookForClass(environ.env, "java/lang/Boolean");
18
         static jmethodID MID_init = NULL;
19
         if(!MID_init) {
20
             MID_init = (jmethodID)findConstructor (environ.env, booleanClass, "(Z)V");
21
         }
22
         jobject booleanValue = (jobject)(*(environ.env))->NewObject(environ.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 environ, int i){ 
36
         if(environ.env == NULL) {
37
                printf("Error: Saxon-C env variable is null\n");
38
                fflush (stdout);
39
                   return NULL;
40
         }
41

    
42
         jclass  integerClass = lookForClass(environ.env, "java/lang/Integer");
43
         /*static */ jmethodID intMID = NULL;
44
         //if(!intMID){
45
                intMID = (jmethodID)(*(environ.env))->GetMethodID (environ.env, integerClass, "<init>", "(I)V");
46
         //}
47
if(!intMID){
48
        printf("error in intMID");
49
}        
50
         jobject intValue = (*(environ.env))->NewObject(environ.env, integerClass, intMID, (jint)i);
51
               if (!intValue) {
52
                    printf("Error: failed to allocate Integer object\n");
53
printf("Value to build: %i",i);
54
                fflush (stdout);
55
//(*(environ.env))->ExceptionDescribe(environ.env); //remove line
56
                return NULL;
57
               }
58
         return intValue;
59
        }
60

    
61

    
62
    /**
63
     * Create an boxed Double value
64
     * @param val - double value
65
     */
66
jobject doubleValue(sxnc_environment environ, double d){ 
67
         if(environ.env == NULL) {
68
                printf("Error: Saxon-C env variable is null\n");
69
                fflush (stdout);
70
                   return NULL;
71
         }
72
         jclass  doubleClass = lookForClass(environ.env, "net/sf/saxon/s9api/XdmAtomicValue");
73
         static jmethodID dbID = NULL;
74
        if(!dbID) {
75
                dbID = (jmethodID)findConstructor (environ.env, doubleClass, "(D)V");
76
        }        
77
         jobject doubleValue = (jobject)(*(environ.env))->NewObject(environ.env, doubleClass, dbID, (jdouble)d);
78
               if (!doubleValue) {
79
                    printf("Error: failed to allocate Double object\n");
80
                fflush (stdout);
81
                return NULL;
82
               }
83
         return doubleValue;
84
        }
85

    
86
    /**
87
     * Create an boxed Float value
88
     * @param val - float value
89
     */
90
jobject floatValue(sxnc_environment environ, float f){ 
91
         if(environ.env == NULL) {
92
                printf("Error: Saxon-C env variable is null\n");
93
                fflush (stdout);
94
                   return NULL;
95
         }
96
         jclass  floatClass = lookForClass(environ.env, "java/lang/Float");
97
         static jmethodID fID = NULL;
98
         if(!fID){
99
                 fID = (jmethodID)findConstructor (environ.env, floatClass, "(F)V");
100
         }
101
         jobject floatValue = (jobject)(*(environ.env))->NewObject(environ.env, floatClass, fID, (jfloat)f);
102
               if (!floatValue) {
103
                    printf("Error: failed to allocate float object\n");
104
                fflush (stdout);
105
                return NULL;
106
               }
107
         return floatValue;
108
        }
109

    
110

    
111
    /**
112
     * Create an boxed Long value
113
     * @param val - Long value
114
     */
115
jobject longValue(sxnc_environment environ, long l){ 
116
         if(environ.env == NULL) {
117
                printf("Error: Saxon-C env variable is null\n");
118
                fflush (stdout);
119
                   return NULL;
120
         }
121
         jclass  longClass = lookForClass(environ.env, "java/lang/Long");
122
         static jmethodID lID = NULL;
123
         if(lID) {
124
                  lID = (jmethodID)findConstructor (environ.env, longClass, "(L)V");
125
        }        
126
         jobject longValue = (jobject)(*(environ.env))->NewObject(environ.env, longClass, lID, (jlong)l);
127
               if (!longValue) {
128
                    printf("Error: failed to allocate long object\n");
129
                fflush (stdout);
130
                return NULL;
131
               }
132
         return longValue;
133
        }
134

    
135
    /**
136
     * Create an boxed String value
137
     * @param val - as char array value
138
     */
139
jobject getJavaStringValue(sxnc_environment environ, const char *str){ 
140
         if(environ.env == NULL) {
141
                printf("Error: Saxon-C env variable is null\n");
142
                fflush (stdout);
143
                   return NULL;
144
         }
145
        if(str == NULL) {
146
                return (*(environ.env))->NewStringUTF(environ.env, "");        
147
        }
148
        jstring jstrBuf = (*(environ.env))->NewStringUTF(environ.env, str);
149
         
150
               if (!jstrBuf) {
151
                    printf("Error: failed to allocate String object\n");
152
                fflush (stdout);
153
                return NULL;
154
               }
155
         return jstrBuf;
156
        }
157

    
158

    
159

    
160
jobject xdmValueAsObj(sxnc_environment environ, const char* type, const char* str){ 
161
        jclass  saxoncClass = lookForClass(environ.env, "net/sf/saxon/option/cpp/SaxonCAPI");
162
         if(environ.env == NULL) {
163
                printf("Error: Saxon-C env variable is null\n");
164
                fflush (stdout);
165
                   return NULL;
166
         }
167
        char methodName[] = "createXdmAtomicItem";
168
            char args[] = "(Ljava/lang/String;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmValue;";
169
                
170
        static jmethodID MID_xdmValueo = NULL;
171
        if(!MID_xdmValueo) {
172
                MID_xdmValueo = (jmethodID)(*(environ.env))->GetStaticMethodID(environ.env, saxoncClass, methodName, args);
173
        }
174
       if (!MID_xdmValueo) {
175
          printf("\nError: MyClassInDll %s() not found\n",methodName);
176
            fflush (stdout);
177
          return NULL;
178
      }
179
   jobject resultObj = ((*(environ.env))->CallStaticObjectMethod(environ.env, saxoncClass, MID_xdmValueo, type, str));
180
   if(resultObj) {
181
        return resultObj;
182
   } 
183
   return NULL;
184
}
185

    
186
    /**
187
     * A Constructor. Create a XdmValue based on the target type. Conversion is applied if possible
188
     * @param type - specify target type of the value as the local name of the xsd built in type. For example 'gYearMonth' 
189
     * @param val - Value to convert
190
     */
191
sxnc_value * xdmValue(sxnc_environment environ, const char* type, const char* str){ 
192
        jclass  saxoncClass = lookForClass(environ.env, "net/sf/saxon/option/cpp/SaxonCAPI");
193
         if(environ.env == NULL) {
194
                printf("Error: Saxon-C env variable is null\n");
195
                fflush (stdout);
196
                   return NULL;
197
         }
198
        char methodName[] = "createXdmAtomicValue";
199
            char args[] = "(Ljava/lang/String;Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmValue;";
200
                
201
        static jmethodID MID_xdmValue = NULL;
202
        if(!MID_xdmValue) {
203
                MID_xdmValue = (jmethodID)(*(environ.env))->GetStaticMethodID(environ.env, saxoncClass, methodName, args);
204
        }
205
       if (!MID_xdmValue) {
206
          printf("\nError: MyClassInDll %s() not found\n",methodName);
207
            fflush (stdout);
208
          return NULL;
209
      }
210
   jobject resultObj = ((*(environ.env))->CallStaticObjectMethod(environ.env, saxoncClass, MID_xdmValue, type, str));
211
   if(resultObj) {
212
        sxnc_value* result = (sxnc_value *)malloc(sizeof(sxnc_value));
213
         result->xdmvalue = resultObj; 
214
         return result; 
215
   } 
216
   return NULL;
217
}
218

    
219
sxnc_value * evaluate(sxnc_environment environ, sxnc_processor ** proc, char * cwd, char * xpathStr, sxnc_parameter *parameters, sxnc_property * properties, int parLen, int propLen){
220
static jmethodID emID = NULL; //cache the methodID
221
        jclass cppClass = lookForClass(environ.env, "net/sf/saxon/option/cpp/XPathProcessor");
222

    
223
        if(!cpp) {
224
                cpp = (jobject) createSaxonProcessor (environ.env, cppClass, "(Z)V", NULL, (jboolean)license);
225
        }
226

    
227
        if(emID == NULL) {
228
                emID = (jmethodID)(*(environ.env))->GetMethodID (environ.env, cppClass,"evaluateSingle", "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmItem;");
229
                if (!emID) {
230
                        printf("Error: MyClassInDll. evaluateSingle not found\n");
231
                        fflush (stdout);
232
                        return 0;
233
                 } 
234
        }
235
         
236
        jobjectArray stringArray = NULL;
237
        jobjectArray objectArray = NULL;
238
        int size = parLen + propLen+1; //We add one here for the resources-dir property
239

    
240
        if(size >0) {
241
           jclass objectClass = lookForClass(environ.env, "java/lang/Object");
242
           jclass stringClass = lookForClass(environ.env, "java/lang/String");
243
           objectArray = (*(environ.env))->NewObjectArray(environ.env, (jint)size, objectClass, 0 );
244
           stringArray = (*(environ.env))->NewObjectArray(environ.env, (jint)size, stringClass, 0 );
245
           if((!objectArray) || (!stringArray)) { return 0;}
246
           int i=0;
247
           for(i =0; i< parLen; i++) {
248
                
249
             (*(environ.env))->SetObjectArrayElement(environ.env, stringArray, i, (*(environ.env))->NewStringUTF(environ.env, parameters[i].name) );
250
             (*(environ.env))->SetObjectArrayElement(environ.env, objectArray, i, (jobject)(parameters[i].value) );
251
        
252
           }
253

    
254
           (*(environ.env))->SetObjectArrayElement(environ.env, stringArray, i, (*(environ.env))->NewStringUTF(environ.env,"resources"));
255
             (*(environ.env))->SetObjectArrayElement(environ.env, objectArray, i, (jobject)((*(environ.env))->NewStringUTF(environ.env, getResourceDirectory())) );
256
            i++;
257
           int j=0;        
258
             for(; j < propLen; j++, i++) {
259
             (*(environ.env))->SetObjectArrayElement(environ.env, stringArray, i, (*(environ.env))->NewStringUTF(environ.env, properties[j].name));
260
             (*(environ.env))->SetObjectArrayElement(environ.env, objectArray, i, (jobject)((*(environ.env))->NewStringUTF(environ.env, properties[j].value)) );
261
           }
262
          
263
        }
264
      jobject resultObj = (jstring)((*(environ.env))->CallObjectMethod(environ.env, cpp, emID, (cwd== NULL ? (*(environ.env))->NewStringUTF(environ.env, "") : (*(environ.env))->NewStringUTF(environ.env, cwd)), (*(environ.env))->NewStringUTF(environ.env,xpathStr), stringArray, objectArray ));
265

    
266
      (*(environ.env))->DeleteLocalRef(environ.env, objectArray);
267
      (*(environ.env))->DeleteLocalRef(environ.env, stringArray);
268
      if(resultObj) {
269
         sxnc_value* result = (sxnc_value *)malloc(sizeof(sxnc_value));
270
         result->xdmvalue = resultObj;  
271
        
272
        //checkForException(environ, cppClass, cpp);     
273
        return result;
274
     }
275

    
276
    checkForException(environ, cppClass, cpp);
277
    return 0;
278

    
279
}
280

    
281

    
282
bool effectiveBooleanValue(sxnc_environment environ, sxnc_processor ** proc, char * cwd, char * xpathStr, sxnc_parameter *parameters, sxnc_property * properties, int parLen, int propLen){
283
        static jmethodID bmID = NULL; //cache the methodID
284
        jclass cppClass = lookForClass(environ.env, "net/sf/saxon/option/cpp/XPathProcessor");
285

    
286
        if(!cpp) {
287
                cpp = (jobject) createSaxonProcessor (environ.env, cppClass, "(Z)V", NULL, (jboolean)license);
288
        }
289

    
290
        if(bmID == NULL) {
291
                bmID = (jmethodID)(*(environ.env))->GetMethodID (environ.env, cppClass,"effectiveBooleanValue", "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Z");
292
                if (!bmID) {
293
                        printf("Error: MyClassInDll. effectiveBooleanValue not found\n");
294
                        fflush (stdout);
295
                        return 0;
296
                 } 
297
        }
298
         
299
        jobjectArray stringArray = NULL;
300
        jobjectArray objectArray = NULL;
301
        int size = parLen + propLen+1; //We add one here for the resources-dir property
302

    
303
        if(size >0) {
304
           jclass objectClass = lookForClass(environ.env, "java/lang/Object");
305
           jclass stringClass = lookForClass(environ.env, "java/lang/String");
306
           objectArray = (*(environ.env))->NewObjectArray(environ.env, (jint)size, objectClass, 0 );
307
           stringArray = (*(environ.env))->NewObjectArray(environ.env, (jint)size, stringClass, 0 );
308
           if((!objectArray) || (!stringArray)) { 
309
                printf("Error: parameter and property arrays have some inconsistencies \n");
310
                fflush (stdout);                
311
                return false;}
312
           int i=0;
313
           for(i =0; i< parLen; i++) {
314
                
315
             (*(environ.env))->SetObjectArrayElement(environ.env, stringArray, i, (*(environ.env))->NewStringUTF(environ.env, parameters[i].name) );
316
             (*(environ.env))->SetObjectArrayElement(environ.env, objectArray, i, (jobject)(parameters[i].value) );
317
        
318
           }
319

    
320
           (*(environ.env))->SetObjectArrayElement(environ.env, stringArray, i, (*(environ.env))->NewStringUTF(environ.env,"resources"));
321
             (*(environ.env))->SetObjectArrayElement(environ.env, objectArray, i, (jobject)((*(environ.env))->NewStringUTF(environ.env, getResourceDirectory())) );
322
            i++;
323
           int j=0;        
324
             for(; j<propLen; j++, i++) {
325
             (*(environ.env))->SetObjectArrayElement(environ.env, stringArray, i, (*(environ.env))->NewStringUTF(environ.env, properties[j].name));
326
             (*(environ.env))->SetObjectArrayElement(environ.env, objectArray, i, (jobject)((*(environ.env))->NewStringUTF(environ.env, properties[j].value)) );
327
           }
328
          
329
        }
330
      jboolean resultObj = (jboolean)((*(environ.env))->CallBooleanMethod(environ.env, cpp, bmID, (cwd== NULL ? (*(environ.env))->NewStringUTF(environ.env, "") : (*(environ.env))->NewStringUTF(environ.env, cwd)), (*(environ.env))->NewStringUTF(environ.env,xpathStr), (jobjectArray)stringArray, (jobjectArray)objectArray ));
331

    
332
      (*(environ.env))->DeleteLocalRef(environ.env, objectArray);
333
      (*(environ.env))->DeleteLocalRef(environ.env, stringArray);
334
      if(resultObj) {
335
         checkForException(environ, cppClass, cpp);    
336
        return resultObj;
337
     }
338

    
339
    checkForException(environ, cppClass, cpp);
340
    return false;
341
}
342

    
343
const char * getStringValue(sxnc_environment environ, sxnc_value value){
344
        return stringValue(environ, value.xdmvalue);
345
}
346

    
347
int size(sxnc_environment environ, sxnc_value val){
348
        //TODO write method up
349
        return 0;
350
}
351

    
352
sxnc_value * itemAt(sxnc_environment environ, sxnc_value val, int i){
353
jclass  xdmValueClass = lookForClass(environ.env, "net/sf/saxon/s9api/XdmValue");
354
         if(environ.env == NULL) {
355
                printf("Error: Saxon-C env variable is null\n");
356
                fflush (stdout);
357
                   return NULL;
358
         }
359
        char methodName[] = "itemAt";
360
            char args[] = "(I)Lnet/sf/saxon/s9api/XdmItem;";
361
                
362
        static jmethodID MID_xdmValue = NULL;
363
        if(!MID_xdmValue) {
364
                MID_xdmValue = (jmethodID)(*(environ.env))->GetMethodID(environ.env, xdmValueClass, methodName, args);
365
        }
366
       if (!MID_xdmValue) {
367
          printf("\nError: MyClassInDll %s() not found\n",methodName);
368
            fflush (stdout);
369
          return NULL;
370
      }
371
      jobject xdmItemObj = (*(environ.env))->CallObjectMethod(environ.env,val.xdmvalue, MID_xdmValue, i);
372
      if(xdmItemObj) {   
373
         sxnc_value* result = (sxnc_value *)malloc(sizeof(sxnc_value));
374
         result->xdmvalue = xdmItemObj;  
375
        
376
        //checkForException(environ, cppClass, cpp);     
377
        return result;
378
     }
379

    
380
    checkForException(environ, xdmValueClass, val.xdmvalue);
381
    return NULL;
382
}
383

    
384
bool isAtomicvalue(sxnc_value value){
385
//TODO
386
        return false;
387
}
388

    
389
int getIntegerValue(sxnc_environment environ, sxnc_value value,  int failureVal){
390
        const char * valueStr = getStringValue(environ, value);
391
        if(valueStr != NULL) {                
392
                int value = atoi(valueStr);
393
                if(value != 0) {
394
                        return value;
395
                }
396
        } 
397

    
398
        if (strcmp(valueStr,"0") == 0) {
399
                return 0;
400
        } else {
401
                return failureVal;        
402
        }}
403

    
404
bool getBooleanValue(sxnc_environment environ, sxnc_value value){return false;}
405

    
406
long getLongValue(sxnc_environment environ, sxnc_value value,  long failureVal){
407
        const char * valueStr = getStringValue(environ, value);
408
        if(valueStr != NULL) {                
409
                long value = atol(valueStr);
410
                if(value != 0) {
411
                        return value;
412
                }
413
        } 
414

    
415
        if (strcmp(valueStr,"0") == 0) {
416
                return 0;
417
        } else {
418
                return failureVal;        
419
        }
420
}
421

    
422
float getFloatValue(sxnc_environment environ, sxnc_value value,  float failureVal){return 0;}
423

    
424
double getDoubleValue(sxnc_environment environ, sxnc_value value, double failureVal){
425
        const char * valueStr = getStringValue(environ, value);
426
        if(valueStr != NULL) {                
427
                double value = atof(valueStr);
428
                if(value != 0) {
429
                        return value;
430
                }
431
        } 
432

    
433
        if (strcmp(valueStr,"0") == 0) {
434
                return 0;
435
        } else {
436
                return failureVal;        
437
        }
438

    
439
}
440

    
(6-6/37)