Project

Profile

Help

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

he / latest9.6 / hec / Saxon.C.API / SaxonCGlue.c @ c6dcd97a

1
#include "SaxonCGlue.h"
2

    
3
jobject cpp;
4
/*
5
* Set Dll name. Also set the saxon resources directory. 
6
* If the SAXONC_HOME sxnc_environmental variable is set then use that as base.
7
*/
8
void setDllname(){
9
        if(getenv("SAXONC_HOME")!= NULL) {
10

    
11
                char * env = getenv("SAXONC_HOME");
12
                size_t env_len = strlen(env);
13
                size_t name_len =  15;
14
                size_t rDir_len = 11;
15
                //dllname =malloc(sizeof(char)*name_len);
16
                //resources_dir =malloc(sizeof(char)*rDir_len);
17
                memset(&dllname[0], 0, sizeof(dllname));
18
                memset(&resources_dir[0], 0, sizeof(resources_dir));
19
                strncat(resources_dir, env,  env_len);
20
                strncat(resources_dir, "/saxon-data", rDir_len);
21
                strncat(dllname, env, env_len);
22
                strncat(dllname, "/libsaxonhec.so", name_len); //rename according to product edition (-hec or -pec)
23
#ifdef DEBUG        
24
                printf("resources_dir: %s\n", resources_dir);        
25
                printf("envDir: %s\n", env);
26
                printf("size of env %i\n", strlen(env));
27
                printf("size of dllname %i\n", strlen(dllname));
28
                printf("dllName: %s\n", dllname);
29
                printf("resources_dir: %s\n", resources_dir);
30
#endif
31
        }
32
#ifdef DEBUG        
33
                printf("resources_dir: %s\n", resources_dir);        
34
                printf("size of dllname %i\n", strlen(dllname));
35
                printf("dllName: %s\n", dllname);
36
                printf("resources_dir: %s\n", resources_dir);
37
                printf("size of resources dir %i\n", strlen(resources_dir));
38
#endif         
39

    
40
}
41

    
42
char * getDllname(){
43
        return dllname;
44
}
45

    
46
/*
47
 * Load dll using the default setting in Saxon/C
48
 * Recommended method to use to load library
49
 */
50
HANDLE loadDefaultDll(){
51
        return loadDll(NULL);
52
}
53

    
54

    
55
/*
56
 * Load dll.
57
 */
58
HANDLE loadDll(char* name)
59
{
60
        if(name == NULL) {
61
                setDllname();
62
                name = getDllname();
63
                //perror("Error1: ");
64
        }
65

    
66
#if !(defined (__linux__) || (defined (__APPLE__) && defined(__MACH__)))
67
    HANDLE hDll = LoadLibrary (_T(name)); // Used for windows only
68
#else
69
    HANDLE hDll = LoadLibrary(name);
70
#endif
71

    
72
    if (!hDll) {
73
        printf ("Unable to load %s\n", name);
74
        perror("Error: ");
75
        exit(1);
76
    }
77
#ifdef DEBUG
78
    printf ("%s loaded\n", name);
79
#endif
80

    
81
    return hDll;
82
}
83

    
84

    
85

    
86
jint (JNICALL * JNI_GetDefaultJavaVMInitArgs_func) (void *args);
87
jint (JNICALL * JNI_CreateJavaVM_func) (JavaVM **pvm, void **penv, void *args);
88

    
89

    
90

    
91
void initDefaultJavaRT(sxnc_environment ** env){
92
        sxnc_environment *environ = *env;
93
        initJavaRT((environ->myDllHandle), &(environ->jvm), &(environ->env));
94
}
95

    
96
/*
97
 * Initialize JET run-time.
98
 */
99
void initJavaRT(HANDLE myDllHandle, JavaVM** pjvm, JNIEnv** penv)
100
{
101
    //perror ("initJavaRT - load Saxon/C library\n");
102
    int result;
103
    JavaVMInitArgs args;
104
    JNI_GetDefaultJavaVMInitArgs_func =
105
    (jint (JNICALL *) (void *args))
106
#if !(defined (__linux__) || (defined (__APPLE__) && defined(__MACH__)))
107
    GetProcAddress ((HMODULE)myDllHandle, "JNI_GetDefaultJavaVMInitArgs");
108
#else
109
    GetProcAddress (myDllHandle, "JNI_GetDefaultJavaVMInitArgs");
110
#endif
111
    
112
    JNI_CreateJavaVM_func =
113
    (jint (JNICALL *) (JavaVM **pvm, void **penv, void *args))
114
    
115
#if !(defined (__linux__) || (defined (__APPLE__) && defined(__MACH__)))
116
    GetProcAddress ((HMODULE)myDllHandle, "JNI_CreateJavaVM");
117
#else
118
    GetProcAddress (myDllHandle, "JNI_CreateJavaVM");
119
    
120
#endif
121
    
122
    if(!JNI_GetDefaultJavaVMInitArgs_func) {
123
        printf ("%s doesn't contain public JNI_GetDefaultJavaVMInitArgs\n", getDllname());
124
        exit (1);
125
    }
126
    
127
    if(!JNI_CreateJavaVM_func) {
128
        printf ("%s doesn't contain public JNI_CreateJavaVM\n", getDllname());
129
        exit (1);
130
    }
131
    
132
    memset (&args, 0, sizeof(args));
133
    
134
    args.version = JNI_VERSION_1_2;
135
    result = JNI_GetDefaultJavaVMInitArgs_func(&args);
136
    if (result != JNI_OK) {
137
        printf("JNI_GetDefaultJavaVMInitArgs() failed with result\n");
138
        exit(1);
139
    }  
140
    /*
141
     * NOTE: no JVM is actually created
142
     * this call to JNI_CreateJavaVM is intended for JET RT initialization
143
     */
144
    result = JNI_CreateJavaVM_func (pjvm, (void **)penv, &args);
145
    if (result != JNI_OK) {
146
        printf("JNI_CreateJavaVM() failed with result\n");
147
        exit(1);
148
    }
149

    
150
#ifdef DEBUG
151
    printf ("JET RT initialized\n");
152

    
153
#endif
154
    fflush (stdout);
155
}
156

    
157

    
158
/*
159
 * Look for class.
160
 */
161
jclass lookForClass (JNIEnv* penv, const char* name)
162
{
163
    jclass clazz = (*penv)->FindClass (penv, name);
164

    
165
    if (!clazz) {
166
        printf("Unable to find class %s\n", name);
167
        return NULL;
168
    }
169
#ifdef DEBUG
170
    printf ("Class %s found\n", name);
171
    fflush (stdout);
172
#endif
173

    
174
    return clazz;
175
}
176

    
177

    
178
/*
179
 * Create an object and invoke the instance method
180
 */
181
void invokeInstanceMethod (JNIEnv* penv, jclass myClassInDll, char * name, char * arguments)
182
{
183
    jmethodID MID_init, MID_name;
184
    jobject obj;
185

    
186
    MID_init = (*penv)->GetMethodID (penv, myClassInDll, "<init>", "()V");
187
    if (!MID_init) {
188
        printf("Error: MyClassInDll.<init>() not found\n");
189
        return;
190
    }
191

    
192
    obj = (*penv)->NewObject(penv, myClassInDll, MID_init);
193
    if (!obj) {
194
        printf("Error: failed to allocate an object\n");
195
        return;
196
    }
197

    
198
    MID_name = (*penv)->GetMethodID (penv, myClassInDll, name, arguments);
199

    
200
    if (!MID_name) {
201
        printf("Error: %s not found\n", name);
202
        return;
203
    }
204
    
205
    (*(penv))->CallVoidMethod (penv, obj, MID_name);
206
}
207

    
208

    
209

    
210

    
211
/*
212
 * Invoke the static method
213
 */
214
void invokeStaticMethod(JNIEnv* penv, jclass myClassInDll, char* name, char* arguments)
215
{
216
    jmethodID MID_name;
217

    
218
    MID_name = (*penv)->GetStaticMethodID(penv, myClassInDll, name, arguments);
219
    if (!MID_name) {
220
        printf("\nError: method %s not found\n", name);
221
        return;
222
    }
223
    
224
    (*penv)->CallStaticVoidMethod(penv, myClassInDll, MID_name);
225
}
226

    
227

    
228
/*
229
 * Find a constructor with a set arguments
230
 */
231
jmethodID findConstructor (JNIEnv* penv, jclass myClassInDll, char* arguments)
232
{
233
    jmethodID MID_initj;
234
    jobject obj;
235

    
236
    MID_initj = (jmethodID)(*penv)->GetMethodID (penv, myClassInDll, "<init>", arguments);
237
    if (!MID_initj) {
238
        printf("Error: MyClassInDll.<init>() not found\n");
239
        fflush (stdout);
240
        return 0;
241
    }
242

    
243
  return MID_initj;
244
}
245

    
246
/*
247
 * Create the Java SaxonProcessor
248
 */
249
jobject createSaxonProcessor (JNIEnv* penv, jclass myClassInDll, const char * arguments, jobject argument1, jboolean license)
250
{
251
    jmethodID MID_initi;
252

    
253
    jobject obj;
254

    
255
    MID_initi = (jmethodID)(*(penv))->GetMethodID (penv, myClassInDll, "<init>", arguments);
256
        if (!MID_initi) {
257
                printf("Error: MyClassInDll.<init>() not found\n");
258
                return NULL;
259
        }
260
     
261
    if(argument1) {
262

    
263
             obj = (jobject)(*(penv))->NewObject(penv, myClassInDll, MID_initi, argument1, license);
264
    } else {
265
        
266
      obj = (jobject)(*(penv))->NewObject(penv, myClassInDll, MID_initi, license);
267
     }
268
      if (!obj) {
269
        printf("Error: failed to allocate an object\n");
270
        return NULL;
271
      }
272
    return obj;
273
}
274

    
275

    
276
/*
277
 * Create the Java SaxonProcessor without boolean license argument
278
 */
279
jobject createSaxonProcessor2 (JNIEnv* penv, jclass myClassInDll, const char * arguments, jobject argument1)
280
{
281
    jmethodID MID_initi;
282

    
283
    jobject obj;
284

    
285
    MID_initi = (jmethodID)(*(penv))->GetMethodID (penv, myClassInDll, "<init>", arguments);
286
        if (!MID_initi) {
287
                printf("Error: MyClassInDll.<init>() not found\n");
288
                return NULL;
289
        }
290
     
291
    if(argument1) {
292

    
293
             obj = (jobject)(*(penv))->NewObject(penv, myClassInDll, MID_initi, argument1);
294
    } else {
295
        
296
      obj = (jobject)(*(penv))->NewObject(penv, myClassInDll, MID_initi);
297
     }
298
      if (!obj) {
299
        printf("Error: failed to allocate an object\n");
300
        return NULL;
301
      }
302
    return obj;
303
}
304

    
305

    
306
/*
307
 * Callback to check for exceptions. When called it returns the exception as a string 
308
 */
309
const char * checkForException(sxnc_environment environ, jclass callingClass,  jobject callingObject){
310

    
311
    if ((*(environ.env))->ExceptionCheck(environ.env)) {
312

    
313
        jthrowable exc = (*(environ.env))->ExceptionOccurred(environ.env);
314
#ifdef DEBUG
315
        (*(environ.env))->ExceptionDescribe(environ.env);
316
#endif
317
         if(exc) {
318
                printf("Exception Occurred!");
319
        }
320
        jclass exccls = (jclass) (*(environ.env))->GetObjectClass(environ.env, exc);
321

    
322
        jclass clscls = (jclass) (*(environ.env))->FindClass(environ.env, "java/lang/Class");
323
        
324
        jmethodID getName = (jmethodID)(*(environ.env))->GetMethodID(environ.env, clscls, "getName", "()Ljava/lang/String;");
325
        jstring name = (jstring)((*(environ.env))->CallObjectMethod(environ.env, exccls, getName));
326
        char const* utfName = (*(environ.env))->GetStringUTFChars(environ.env, name, NULL);
327
        
328
        if(callingObject != NULL && strcmp(utfName, "net.sf.saxon.s9api.SaxonApiException") == 0){
329
                  
330
                jclass saxonExcClass = (*(environ.env))->FindClass(environ.env, "java/lang/Throwable");
331
                
332
                jmethodID  getMessage =  (jmethodID)(*(environ.env))->GetMethodID(environ.env, exccls, "getMessage", "()Ljava/lang/String;");
333
                
334
        if(getMessage) {
335
            
336
            jstring message = (jstring)((*(environ.env))->CallObjectMethod(environ.env, exc, getMessage));
337
       
338
        if(!message){
339
        
340
        (*(environ.env))->ExceptionClear(environ.env);
341
        return 0;
342
        }
343
            char const* utfMessage= (*(environ.env))->GetStringUTFChars(environ.env, message, NULL);
344
      
345
            if(utfMessage != NULL) {
346
                (*(environ.env))->ReleaseStringUTFChars(environ.env, name, utfName);
347
            }
348
       
349
            (*(environ.env))->ExceptionClear(environ.env);
350
            return utfMessage;
351
        }
352
    }
353
     (*(environ.env))->ReleaseStringUTFChars(environ.env, name, utfName);      
354
    }
355
(*(environ.env))->ExceptionClear(environ.env);
356
    return NULL;
357
}
358

    
359

    
360
/*
361
 * Clean up and destroy Java VM to release memory used. 
362
 */
363
void finalizeJavaRT (JavaVM* jvm)
364
{
365

    
366
  if(!jvm){
367
          printf("\njvm is null\n");        
368
  }
369
  (*jvm)->DestroyJavaVM (jvm);
370
}
371

    
372

    
373
/*
374
 * Get a parameter from list 
375
 */
376
jobject getParameter(sxnc_parameter *parameters,  int parLen, const char* namespacei, const char * name){
377
        int i =0;
378
        for(i =0; i< parLen;i++) {
379
                if(strcmp(parameters[i].name, name) == 0)
380
                        return (jobject)parameters[i].value;                        
381
        }
382
        return NULL;
383
}
384

    
385

    
386
/*
387
 * Get a property from list 
388
 */
389
char* getProperty(sxnc_property * properties, int propLen, const char* namespacei, const char * name){
390
        int i =0;
391
        for(i =0; i< propLen;i++) {
392
                if(strcmp(properties[i].name, name) == 0)
393
                        return properties[i].value;                        
394
        }
395
        return 0;
396
}
397

    
398

    
399
/*
400
 * set a parameter 
401
 */
402
void setParameter(sxnc_parameter **parameters, int * parLen, int * parCap, const char * namespacei, const char * name, jobject value){
403

    
404
        if(getParameter(*parameters, (*parLen), "", name) != 0){
405
                return;                        
406
        }
407
        (*parLen)++;        
408
        if((*parLen) >= (*parCap)) {
409
                (*parCap) *=2; 
410
                sxnc_parameter* temp = (sxnc_parameter*)malloc(sizeof(sxnc_parameter)*(*parCap));        
411
                int i =0;
412
                for(i =0; i< (*parLen)-1;i++) {
413
                        temp[i] = (*parameters)[i];                        
414
                }
415
                free(parameters);
416
                parameters = &temp;
417
        }
418
        int nameLen = strlen(name)+7;        
419
        char *newName = malloc(sizeof(char)*nameLen);
420
          snprintf(newName, nameLen, "%s%s", "param:", name );
421
        (*parameters)[(*parLen)-1].name = (char*)newName;        
422
        (*parameters)[(*parLen)-1].value = (jobject)value;
423
}
424

    
425

    
426
/*
427
 * set a property 
428
 */
429
void setProperty(sxnc_property ** properties, int *propLen, int *propCap, const char* name, const char* value){        
430
        if(getProperty(*properties, (*propLen), "", name) != 0){
431
                return;                        
432
        }
433
                
434
        if(((*propLen)+1) >= (*propCap)) {
435
                (*propCap) *=2; 
436
                sxnc_property* temp = (sxnc_property*)malloc(sizeof(sxnc_property)*  (*propCap));
437
                int i =0;        
438
                for(i =0; i< (*propLen)-1;i++) {
439
                        temp[i] = (*properties)[i];                        
440
                }
441
                free(properties);
442
                properties = &temp;
443

    
444
        }
445
        int nameLen = strlen(name)+1;        
446
        char *newName = (char*)malloc(sizeof(char)*nameLen);
447
          snprintf(newName, nameLen, "%s", name );
448
        char *newValue = (char*)malloc(sizeof(char)*strlen(value)+1);
449
          snprintf(newValue, strlen(value)+1, "%s", value );
450
        (*properties)[(*propLen)].name = (char*)newName;        
451
        (*properties)[(*propLen)].value = (char*)newValue;
452
        (*propLen)++;
453
}
454

    
455
/*
456
 * clear parameter 
457
 */
458
void clearSettings(sxnc_parameter **parameters, int *parLen, sxnc_property ** properties, int *propLen){
459
        int i =0;
460
        free(*parameters);
461
        free(*parameters);
462
        /*for(i =0; i< parLen; i++){
463
                free((*parameters[i].name);
464
                free(parameters[i].value);
465
        }
466
        for(i =0; i< propLen; i++){
467
                free(properties[i].name);
468
                free(properties[i].value);
469
        }*/
470

    
471
            *parameters = (sxnc_parameter *)calloc(10, sizeof(sxnc_parameter));
472
            *properties = (sxnc_property *)calloc(10, sizeof(sxnc_property));
473
        (*parLen) = 0;
474
        (*propLen) = 0;
475
}
476

    
477

    
478
const char * stringValue(sxnc_environment environ, jobject value){
479
        jclass  objClass = lookForClass(environ.env, "java/lang/Object");
480
        static jmethodID strMID = NULL;
481
        if(!strMID) {
482
                strMID = (jmethodID)(*(environ.env))->GetMethodID(environ.env, objClass, "toString", "()Ljava/lang/String;");
483
                if(!strMID) {
484
                          printf("\nError: Object %s() not found\n","toString");
485
                            fflush (stdout);
486
                          return NULL;
487
                }
488
        }
489
        jstring result = (jstring)((*(environ.env))->CallObjectMethod(environ.env, value, strMID));
490
         if(result) {
491
                       const char * str = (*(environ.env))->GetStringUTFChars(environ.env, result, NULL);    
492
                return str;
493
        }
494

    
495
    //checkForException(environ, cppClass, cpp);
496
    return 0;
497
        
498
}
499

    
500

    
501

    
502

    
(2-2/37)