Project

Profile

Help

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

he / latest9.8 / hec / command / Transform.c @ 52505fd1

1
#include <jni.h>
2

    
3
#if defined __linux__ || defined __APPLE__
4
    #include <stdlib.h>
5
    #include <string.h>
6
    #include <dlfcn.h>
7
    #include <stdio.h>  
8
    #define HANDLE void*
9
    #define LoadLibrary(x) dlopen(x, RTLD_LAZY)
10
    #define GetProcAddress(x,y) dlsym(x,y)
11
#else
12
    #include <windows.h>
13
#endif
14

    
15
#ifndef __cplusplus
16
    #ifndef _BOOL
17
        #include <stdbool.h>
18
        //typedef int bool;
19
        #define true 1
20
        #define false 0
21
    #else
22
        #define true 1
23
        #define false 0
24
    #endif
25
#endif
26

    
27

    
28
char dllname[] =
29
#ifdef __linux__
30
    #ifdef EEC
31
            "/usr/lib/libsaxoneec.so";
32
    #elif defined PEC
33
            "/usr/lib/libsaxonpec.so";
34
    #else
35
            "/usr/lib/libsaxonhec.so";
36
    #endif
37
#elif defined __APPLE__
38
    #ifdef EEC
39
            "/usr/local/lib/libsaxoneec.dylib";
40
    #elif defined PEC
41
            "/usr/local/lib/libsaxonpec.dylib";
42
    #else
43
            "/usr/local/lib/libsaxonhec.dylib";
44
    #endif
45
#else
46
    #ifdef EEC
47
        "libsaxoneec.dll";
48
    #elif defined PEC
49
            "libsaxonpec.dll";
50
    #else
51
            "libsaxonhec.dll";
52
    #endif
53
#endif
54
/*    #else
55
#ifdef EEC
56
        "Saxon-eec.dll";
57
#elif defined PEC
58
        "Saxon-pec.dll";
59
#else
60
        "Saxon-hec.dll";
61
#endif
62

63
#endif
64
*/
65
//===============================================================================================//
66
/*! <code>Environment</code>. This struct captures the jni, JVM and handler to the cross compiled Saxon/C library.
67
 * <p/>
68
 */
69
typedef struct {
70
                JNIEnv *env;
71
                HANDLE myDllHandle;
72
                JavaVM *jvm;
73
        } Environment;
74

    
75

    
76
//===============================================================================================//
77

    
78
/*! <code>MyParameter</code>. This struct captures details of paramaters used for the transformation as (string, value) pairs.
79
 * <p/>
80
 */
81
typedef struct {
82
                char* name;
83
                jobject value;
84
        } MyParameter;
85

    
86
//===============================================================================================//
87

    
88
/*! <code>MyProperty</code>. This struct captures details of properties used for the transformation as (string, string) pairs.
89
 * <p/>
90
 */
91
typedef struct {
92
                char * name;
93
                char * value;
94
        } MyProperty;
95

    
96
jobject cpp;
97

    
98

    
99

    
100
const char * failure;
101
/*
102
 * Load dll.
103
 */
104
HANDLE loadDll(char* name)
105
{
106
    HANDLE hDll = LoadLibrary (name);
107

    
108
    if (!hDll) {
109
        printf ("Unable to load %s\n", name);
110
        exit(1);
111
    }
112
#ifdef DEBUG
113
    printf ("%s loaded\n", name);
114
#endif
115
    return hDll;
116
}
117

    
118

    
119
jint (JNICALL * JNI_GetDefaultJavaVMInitArgs_func) (void *args);
120
jint (JNICALL * JNI_CreateJavaVM_func) (JavaVM **pvm, void **penv, void *args);
121

    
122
/*
123
 * Initialize JET run-time.
124
 */
125
void initJavaRT(HANDLE myDllHandle, JavaVM** pjvm, JNIEnv** penv)
126
{
127
    int            result;
128
    JavaVMInitArgs args;
129

    
130
    JNI_GetDefaultJavaVMInitArgs_func = 
131
             (jint (JNICALL *) (void *args))
132
             GetProcAddress (myDllHandle, "JNI_GetDefaultJavaVMInitArgs");
133

    
134
    JNI_CreateJavaVM_func =
135
             (jint (JNICALL *) (JavaVM **pvm, void **penv, void *args))
136
             GetProcAddress (myDllHandle, "JNI_CreateJavaVM");
137

    
138
    if(!JNI_GetDefaultJavaVMInitArgs_func) {
139
        printf ("%s doesn't contain public JNI_GetDefaultJavaVMInitArgs\n", dllname);
140
        exit (1);
141
    }
142

    
143
    if(!JNI_CreateJavaVM_func) {
144
        printf ("%s doesn't contain public JNI_CreateJavaVM\n", dllname);
145
        exit (1);
146
    }
147

    
148
    memset (&args, 0, sizeof(args));
149

    
150
    args.version = JNI_VERSION_1_2;
151
    result = JNI_GetDefaultJavaVMInitArgs_func(&args);
152
    if (result != JNI_OK) {
153
        printf ("JNI_GetDefaultJavaVMInitArgs() failed with result %d\n", result);
154
        exit(1);
155
    }
156
  
157
    /*
158
     * NOTE: no JVM is actually created
159
     * this call to JNI_CreateJavaVM is intended for JET RT initialization
160
     */
161
    result = JNI_CreateJavaVM_func (pjvm, (void **)penv, &args);
162
    if (result != JNI_OK) {
163
        printf ("JNI_CreateJavaVM() failed with result %d\n", result);
164
        exit(1);
165
    }
166
#ifdef DEBUG
167
    printf ("JET RT initialized\n");
168
    fflush (stdout);
169
#endif
170
}
171

    
172

    
173
/*
174
 * Look for class.
175
 */
176
jclass lookForClass (JNIEnv* penv, char* name)
177
{
178
    jclass clazz = (*penv)->FindClass (penv, name);
179

    
180
    if (!clazz) {
181
        printf("Unable to find class %s\n", name);
182
        return NULL;
183
    }
184
#ifdef DEBUG
185
    printf ("Class %s found\n", name);
186
    fflush (stdout);
187
#endif
188

    
189
    return clazz;
190
}
191

    
192

    
193

    
194

    
195
jmethodID findConstructor (JNIEnv* penv, jclass myClassInDll, char* arguments)
196
{
197
    jmethodID MID_init, mID;
198
    jobject obj;
199

    
200
    MID_init = (jmethodID)(*penv)->GetMethodID (penv, myClassInDll, "<init>", arguments);
201
    if (!MID_init) {
202
        printf("Error: MyClassInDll.<init>() not found\n");
203
        fflush (stdout);
204
        return 0;
205
    }
206

    
207
  return MID_init;
208
}
209

    
210
jobject createObject (JNIEnv* penv, jclass myClassInDll, const char * arguments)
211
{
212
    jmethodID MID_init, mID;
213
    jobject obj;
214

    
215
    MID_init = (jmethodID)(*(penv))->GetMethodID (penv, myClassInDll, "<init>", arguments);
216
    if (!MID_init) {
217
        printf("Error: MyClassInDll.<init>() not found\n");
218
        return NULL;
219
    }
220

    
221
      obj = (jobject)(*(penv))->NewObject(penv, myClassInDll, MID_init, (jboolean)true);
222
      if (!obj) {
223
        printf("Error: failed to allocate an object\n");
224
        return NULL;
225
      }
226
    return obj;
227
}
228

    
229
void checkForException(Environment environ, jclass callingClass,  jobject callingObject){
230

    
231
    if ((*(environ.env))->ExceptionCheck(environ.env)) {
232
        char *  result1;
233
        const char * errorCode = NULL;
234
        jthrowable exc = (*(environ.env))->ExceptionOccurred(environ.env);
235
        (*(environ.env))->ExceptionDescribe(environ.env); //comment code
236
         jclass exccls = (jclass)(*(environ.env))->GetObjectClass(environ.env, exc);
237
        jclass clscls = (jclass)(*(environ.env))->FindClass(environ.env, "java/lang/Class");
238

    
239
        jmethodID getName = (jmethodID)(*(environ.env))->GetMethodID(environ.env, clscls, "getName", "()Ljava/lang/String;");
240
        jstring name =(jstring)((*(environ.env))->CallObjectMethod(environ.env, exccls, getName));
241
        char const* utfName = (char const*)(*(environ.env))->GetStringUTFChars(environ.env, name, 0);
242
        printf(utfName);
243

    
244
         jmethodID  getMessage = (jmethodID)(*(environ.env))->GetMethodID(environ.env, exccls, "getMessage", "()Ljava/lang/String;");
245
        if(getMessage) {
246

    
247
                jstring message = (jstring)((*(environ.env))->CallObjectMethod(environ.env, exc, getMessage));
248
                if(message) {                
249
                        char const* utfMessage = (char const*)(*(environ.env))->GetStringUTFChars(environ.env, message, 0);
250
                }
251
        
252
        }
253

    
254
     }
255
        //return NULL;
256

    
257
}
258

    
259

    
260
void finalizeJavaRT (JavaVM* jvm)
261
{
262
    (*jvm)->DestroyJavaVM (jvm);
263
}
264

    
265

    
266

    
267

    
268

    
269
int transform(Environment environ, int argc, const char* argv[]) {
270

    
271

    
272
    jmethodID MID_foo;
273
    jclass transClass = lookForClass(environ.env, "net/sf/saxon/Transform");
274
    char methodName[] = "main";
275
    char args[] = "([Ljava/lang/String;)V";
276
    jobjectArray stringArray = NULL;
277
    MID_foo = (jmethodID)(*(environ.env))->GetStaticMethodID(environ.env, transClass, methodName, args);
278
    if (!MID_foo) {
279
        printf("\nError: MyClassInDll %s() not found\n",methodName);
280
        fflush (stdout);
281
        return -1;
282
    }
283
     if(argc < 2) {
284
        printf("\nError: Not enough arguments in Transform");
285
        return 0;
286
    }
287
           jclass stringClass = lookForClass(environ.env, "java/lang/String");
288
           stringArray = (*(environ.env))->NewObjectArray(environ.env, (jint)argc-1, stringClass, 0 );
289
           if(!stringArray) { return 0;}
290
  int i, j;
291
  for(i=1, j=0; i< argc; i++, j++) {
292
             (*(environ.env))->SetObjectArrayElement(environ.env, stringArray, j, (*(environ.env))->NewStringUTF(environ.env, argv[i]));
293
           }
294

    
295
   (*(environ.env))->CallStaticVoidMethod(environ.env, transClass, MID_foo, stringArray);
296
   
297
  (*(environ.env))->DeleteLocalRef(environ.env, stringArray);
298
        return 0;
299
}
300

    
301

    
302

    
303

    
304
int main( int argc, const char* argv[] )
305
{
306
    HANDLE myDllHandle;
307
    //JNIEnv *(environ.env);
308
    //JavaVM *jvm;
309
    jclass  myClassInDll;
310

    
311
    Environment environ;
312
    /*
313
     * First of all, load required component.
314
     * By the time of JET initialization, all components should be loaded.
315
     */
316
    environ.myDllHandle = loadDll (dllname);
317
   
318

    
319
    /*
320
     * Initialize JET run-time.
321
     * The handle of loaded component is used to retrieve Invocation API.
322
     */
323
    initJavaRT (environ.myDllHandle, &environ.jvm, &environ.env);
324
    transform(environ, argc, argv);        
325

    
326
  
327
fflush(stdout);
328
    /*
329
     * Finalize JET run-time.
330
     */
331
    finalizeJavaRT (environ.jvm);
332

    
333
    return 0;
334
}
(2-2/9)