Project

Profile

Help

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

he / latest9.9 / hec / command / Transform.c @ 4364429b

1
#include "jni/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 FreeLibrary(x) dlclose(x, RTLD_LAZY)
11
    #define GetProcAddress(x,y) dlsym(x,y)
12
#else
13
    #include <windows.h>
14
#endif
15

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

    
28

    
29
char dllname[] =
30
#ifdef __linux__
31
    #ifdef EEC
32
         "/usr/lib/libsaxoneec.so";
33
    #elif defined PEC
34
            "/usr/lib/libsaxonpec.so";
35
    #else
36
            "/usr/lib/libsaxonhec.so";
37
    #endif
38
#elif defined __APPLE__
39
    #ifdef EEC
40
            "/usr/local/lib/libsaxoneec.dylib";
41
    #elif defined PEC
42
            "/usr/local/lib/libsaxonpec.dylib";
43
    #else
44
            "/usr/local/lib/libsaxonhec.dylib";
45
    #endif
46
#else
47
    #ifdef EEC
48
        "libsaxoneec.dll";
49
    #elif defined PEC
50
            "libsaxonpec.dll";
51
    #else
52
            "libsaxonhec.dll";
53
    #endif
54
#endif
55

    
56
*/
57
//===============================================================================================//
58
/*! <code>Environment</code>. This struct captures the jni, JVM and handler to the cross compiled Saxon/C library.
59
 * <p/>
60
 */
61
typedef struct {
62
                JNIEnv *env;
63
                HANDLE myDllHandle;
64
                JavaVM *jvm;
65
        } Environment;
66

    
67

    
68
//===============================================================================================//
69

    
70
/*! <code>MyParameter</code>. This struct captures details of paramaters used for the transformation as (string, value) pairs.
71
 * <p/>
72
 */
73
typedef struct {
74
                char* name;
75
                jobject value;
76
        } MyParameter;
77

    
78
//===============================================================================================//
79

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

    
88
jobject cpp;
89

    
90

    
91

    
92
const char * failure;
93
/*
94
 * Load dll.
95
 */
96
HANDLE loadDll(char* name)
97
{
98
    HANDLE hDll = LoadLibrary (name);
99

    
100
    if (!hDll) {
101
        printf ("Unable to load %s\n", name);
102
        exit(1);
103
    }
104
#ifdef DEBUG
105
    printf ("%s loaded\n", name);
106
#endif
107
    return hDll;
108
}
109

    
110

    
111
jint (JNICALL * JNI_GetDefaultJavaVMInitArgs_func) (void *args);
112
jint (JNICALL * JNI_CreateJavaVM_func) (JavaVM **pvm, void **penv, void *args);
113

    
114
/*
115
 * Initialize JET run-time.
116
 */
117
void initJavaRT(HANDLE myDllHandle, JavaVM** pjvm, JNIEnv** penv)
118
{
119
    int            result;
120
    JavaVMInitArgs args;
121

    
122
    JNI_GetDefaultJavaVMInitArgs_func = 
123
             (jint (JNICALL *) (void *args))
124
             GetProcAddress (myDllHandle, "JNI_GetDefaultJavaVMInitArgs");
125

    
126
    JNI_CreateJavaVM_func =
127
             (jint (JNICALL *) (JavaVM **pvm, void **penv, void *args))
128
             GetProcAddress (myDllHandle, "JNI_CreateJavaVM");
129

    
130
    if(!JNI_GetDefaultJavaVMInitArgs_func) {
131
        printf ("%s doesn't contain public JNI_GetDefaultJavaVMInitArgs\n", dllname);
132
        exit (1);
133
    }
134

    
135
    if(!JNI_CreateJavaVM_func) {
136
        printf ("%s doesn't contain public JNI_CreateJavaVM\n", dllname);
137
        exit (1);
138
    }
139

    
140
    memset (&args, 0, sizeof(args));
141

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

    
164

    
165
/*
166
 * Look for class.
167
 */
168
jclass lookForClass (JNIEnv* penv, char* name)
169
{
170
    jclass clazz = (*penv)->FindClass (penv, name);
171

    
172
    if (!clazz) {
173
        printf("Unable to find class %s\n", name);
174
        return NULL;
175
    }
176
#ifdef DEBUG
177
    printf ("Class %s found\n", name);
178
    fflush (stdout);
179
#endif
180

    
181
    return clazz;
182
}
183

    
184

    
185

    
186

    
187
jmethodID findConstructor (JNIEnv* penv, jclass myClassInDll, char* arguments)
188
{
189
    jmethodID MID_init, mID;
190
    jobject obj;
191

    
192
    MID_init = (jmethodID)(*penv)->GetMethodID (penv, myClassInDll, "<init>", arguments);
193
    if (!MID_init) {
194
        printf("Error: MyClassInDll.<init>() not found\n");
195
        fflush (stdout);
196
        return 0;
197
    }
198

    
199
  return MID_init;
200
}
201

    
202
jobject createObject (JNIEnv* penv, jclass myClassInDll, const char * arguments)
203
{
204
    jmethodID MID_init, mID;
205
    jobject obj;
206

    
207
    MID_init = (jmethodID)(*(penv))->GetMethodID (penv, myClassInDll, "<init>", arguments);
208
    if (!MID_init) {
209
        printf("Error: MyClassInDll.<init>() not found\n");
210
        return NULL;
211
    }
212

    
213
      obj = (jobject)(*(penv))->NewObject(penv, myClassInDll, MID_init, (jboolean)true);
214
      if (!obj) {
215
        printf("Error: failed to allocate an object\n");
216
        return NULL;
217
      }
218
    return obj;
219
}
220

    
221
void checkForException(Environment environ, jclass callingClass,  jobject callingObject){
222

    
223
    if ((*(environ.env))->ExceptionCheck(environ.env)) {
224
        char *  result1;
225
        const char * errorCode = NULL;
226
        jthrowable exc = (*(environ.env))->ExceptionOccurred(environ.env);
227
        (*(environ.env))->ExceptionDescribe(environ.env); //comment code
228
         jclass exccls = (jclass)(*(environ.env))->GetObjectClass(environ.env, exc);
229
        jclass clscls = (jclass)(*(environ.env))->FindClass(environ.env, "java/lang/Class");
230

    
231
        jmethodID getName = (jmethodID)(*(environ.env))->GetMethodID(environ.env, clscls, "getName", "()Ljava/lang/String;");
232
        jstring name =(jstring)((*(environ.env))->CallObjectMethod(environ.env, exccls, getName));
233
        char const* utfName = (char const*)(*(environ.env))->GetStringUTFChars(environ.env, name, 0);
234
        printf(utfName);
235

    
236
         jmethodID  getMessage = (jmethodID)(*(environ.env))->GetMethodID(environ.env, exccls, "getMessage", "()Ljava/lang/String;");
237
        if(getMessage) {
238

    
239
                jstring message = (jstring)((*(environ.env))->CallObjectMethod(environ.env, exc, getMessage));
240
                if(message) {                
241
                        char const* utfMessage = (char const*)(*(environ.env))->GetStringUTFChars(environ.env, message, 0);
242
                }
243
        
244
        }
245

    
246
     }
247
        //return NULL;
248

    
249
}
250

    
251

    
252
void finalizeJavaRT (JavaVM* jvm)
253
{
254
    (*jvm)->DestroyJavaVM (jvm);
255
}
256

    
257

    
258

    
259

    
260

    
261
int transform(Environment environ, int argc, const char* argv[]) {
262

    
263

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

    
287
   (*(environ.env))->CallStaticVoidMethod(environ.env, transClass, MID_foo, stringArray);
288
   
289
  (*(environ.env))->DeleteLocalRef(environ.env, stringArray);
290
        return 0;
291
}
292

    
293

    
294

    
295

    
296
int main( int argc, const char* argv[] )
297
{
298
    HANDLE myDllHandle;
299
    //JNIEnv *(environ.env);
300
    //JavaVM *jvm;
301
    jclass  myClassInDll;
302

    
303
    Environment environ;
304
    /*
305
     * First of all, load required component.
306
     * By the time of JET initialization, all components should be loaded.
307
     */
308
    environ.myDllHandle = loadDll (dllname);
309
   
310

    
311
    /*
312
     * Initialize JET run-time.
313
     * The handle of loaded component is used to retrieve Invocation API.
314
     */
315
    initJavaRT (environ.myDllHandle, &environ.jvm, &environ.env);
316
    transform(environ, argc, argv);        
317

    
318
  
319
fflush(stdout);
320
    /*
321
     * Finalize JET run-time.
322
     */
323
    finalizeJavaRT (environ.jvm);
324

    
325
    return 0;
326
}
(2-2/9)