Project

Profile

Help

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

he / src / main / c / Saxon.C.API / Transform.c @ a69dd173

1 72bf04c6 Norman Walsh
#include <jni.h>
2
3
#ifdef __linux__
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
typedef int bool;
17
#define true 1
18
#define false 0
19
20
21
char dllname[] =
22
    #ifdef __linux__
23
#ifdef EEC
24
        "/usr/lib/libsaxoneec.so";
25
#elif defined PEC
26
        "/usr/lib/libsaxonpec.so";
27
#else
28
        "/usr/lib/libsaxonhec.so";
29
#endif
30
    #else
31
#ifdef EEC
32
        "Saxon-eec.dll";
33
#elif defined PEC
34
        "Saxon-pec.dll";
35
#else
36
        "Saxon-hec.dll";
37
#endif
38
39
#endif
40
41
//===============================================================================================//
42
/*! <code>Environment</code>. This struct captures the jni, JVM and handler to the cross compiled Saxon/C library.
43
 * <p/>
44
 */
45
typedef struct {
46
                JNIEnv *env;
47
                HANDLE myDllHandle;
48
                JavaVM *jvm;
49
        } Environment;
50
51
52
//===============================================================================================//
53
54
/*! <code>MyParameter</code>. This struct captures details of paramaters used for the transformation as (string, value) pairs.
55
 * <p/>
56
 */
57
typedef struct {
58
                char* name;
59
                jobject value;
60
        } MyParameter;
61
62
//===============================================================================================//
63
64
/*! <code>MyProperty</code>. This struct captures details of properties used for the transformation as (string, string) pairs.
65
 * <p/>
66
 */
67
typedef struct {
68
                char * name;
69
                char * value;
70
        } MyProperty;
71
72
jobject cpp;
73
74
75
76
const char * failure;
77
/*
78
 * Load dll.
79
 */
80
HANDLE loadDll(char* name)
81
{
82
    HANDLE hDll = LoadLibrary (name);
83
84
    if (!hDll) {
85
        printf ("Unable to load %s\n", name);
86
        exit(1);
87
    }
88
#ifdef DEBUG
89
    printf ("%s loaded\n", name);
90
#endif
91
    return hDll;
92
}
93
94
95
jint (JNICALL * JNI_GetDefaultJavaVMInitArgs_func) (void *args);
96
jint (JNICALL * JNI_CreateJavaVM_func) (JavaVM **pvm, void **penv, void *args);
97
98
/*
99
 * Initialize JET run-time.
100
 */
101
void initJavaRT(HANDLE myDllHandle, JavaVM** pjvm, JNIEnv** penv)
102
{
103
    int            result;
104
    JavaVMInitArgs args;
105
106
    JNI_GetDefaultJavaVMInitArgs_func = 
107
             (jint (JNICALL *) (void *args))
108
             GetProcAddress (myDllHandle, "JNI_GetDefaultJavaVMInitArgs");
109
110
    JNI_CreateJavaVM_func =
111
             (jint (JNICALL *) (JavaVM **pvm, void **penv, void *args))
112
             GetProcAddress (myDllHandle, "JNI_CreateJavaVM");
113
114
    if(!JNI_GetDefaultJavaVMInitArgs_func) {
115
        printf ("%s doesn't contain public JNI_GetDefaultJavaVMInitArgs\n", dllname);
116
        exit (1);
117
    }
118
119
    if(!JNI_CreateJavaVM_func) {
120
        printf ("%s doesn't contain public JNI_CreateJavaVM\n", dllname);
121
        exit (1);
122
    }
123
124
    memset (&args, 0, sizeof(args));
125
126
    args.version = JNI_VERSION_1_2;
127
    result = JNI_GetDefaultJavaVMInitArgs_func(&args);
128
    if (result != JNI_OK) {
129
        printf ("JNI_GetDefaultJavaVMInitArgs() failed with result %d\n", result);
130
        exit(1);
131
    }
132
  
133
    /*
134
     * NOTE: no JVM is actually created
135
     * this call to JNI_CreateJavaVM is intended for JET RT initialization
136
     */
137
    result = JNI_CreateJavaVM_func (pjvm, (void **)penv, &args);
138
    if (result != JNI_OK) {
139
        printf ("JNI_CreateJavaVM() failed with result %d\n", result);
140
        exit(1);
141
    }
142
#ifdef DEBUG
143
    printf ("JET RT initialized\n");
144
    fflush (stdout);
145
#endif
146
}
147
148
149
/*
150
 * Look for class.
151
 */
152
jclass lookForClass (JNIEnv* penv, char* name)
153
{
154
    jclass clazz = (*penv)->FindClass (penv, name);
155
156
    if (!clazz) {
157
        printf("Unable to find class %s\n", name);
158
        return NULL;
159
    }
160
#ifdef DEBUG
161
    printf ("Class %s found\n", name);
162
    fflush (stdout);
163
#endif
164
165
    return clazz;
166
}
167
168
169
170
171
jmethodID findConstructor (JNIEnv* penv, jclass myClassInDll, char* arguments)
172
{
173
    jmethodID MID_init, mID;
174
    jobject obj;
175
176
    MID_init = (jmethodID)(*penv)->GetMethodID (penv, myClassInDll, "<init>", arguments);
177
    if (!MID_init) {
178
        printf("Error: MyClassInDll.<init>() not found\n");
179
        fflush (stdout);
180
        return 0;
181
    }
182
183
  return MID_init;
184
}
185
186
jobject createObject (JNIEnv* penv, jclass myClassInDll, const char * arguments)
187
{
188
    jmethodID MID_init, mID;
189
    jobject obj;
190
191
    MID_init = (jmethodID)(*(penv))->GetMethodID (penv, myClassInDll, "<init>", arguments);
192
    if (!MID_init) {
193
        printf("Error: MyClassInDll.<init>() not found\n");
194
        return NULL;
195
    }
196
197
      obj = (jobject)(*(penv))->NewObject(penv, myClassInDll, MID_init, (jboolean)true);
198
      if (!obj) {
199
        printf("Error: failed to allocate an object\n");
200
        return NULL;
201
      }
202
    return obj;
203
}
204
205
void checkForException(Environment environ, jclass callingClass,  jobject callingObject){
206
207
    if ((*(environ.env))->ExceptionCheck(environ.env)) {
208
        char *  result1;
209
        const char * errorCode = NULL;
210
        jthrowable exc = (*(environ.env))->ExceptionOccurred(environ.env);
211
        (*(environ.env))->ExceptionDescribe(environ.env); //comment code
212
         jclass exccls = (jclass)(*(environ.env))->GetObjectClass(environ.env, exc);
213
        jclass clscls = (jclass)(*(environ.env))->FindClass(environ.env, "java/lang/Class");
214
215
        jmethodID getName = (jmethodID)(*(environ.env))->GetMethodID(environ.env, clscls, "getName", "()Ljava/lang/String;");
216
        jstring name =(jstring)((*(environ.env))->CallObjectMethod(environ.env, exccls, getName));
217
        char const* utfName = (char const*)(*(environ.env))->GetStringUTFChars(environ.env, name, 0);
218
        printf(utfName);
219
220
         jmethodID  getMessage = (jmethodID)(*(environ.env))->GetMethodID(environ.env, exccls, "getMessage", "()Ljava/lang/String;");
221
        if(getMessage) {
222
223
                jstring message = (jstring)((*(environ.env))->CallObjectMethod(environ.env, exc, getMessage));
224
                if(message) {                
225
                        char const* utfMessage = (char const*)(*(environ.env))->GetStringUTFChars(environ.env, message, 0);
226
                }
227
        
228
        }
229
230
     }
231
        //return NULL;
232
233
}
234
235
236
void finalizeJavaRT (JavaVM* jvm)
237
{
238
    (*jvm)->DestroyJavaVM (jvm);
239
}
240
241
242
243
244
245
int transform(Environment environ, int argc, const char* argv[]) {
246
247
248
    jmethodID MID_foo;
249
    jclass transClass = lookForClass(environ.env, "net/sf/saxon/Transform");
250
    char methodName[] = "main";
251
    char args[] = "([Ljava/lang/String;)V";
252
    jobjectArray stringArray = NULL;
253
    MID_foo = (jmethodID)(*(environ.env))->GetStaticMethodID(environ.env, transClass, methodName, args);
254
    if (!MID_foo) {
255
        printf("\nError: MyClassInDll %s() not found\n",methodName);
256
        fflush (stdout);
257
        return -1;
258
    }
259
     if(argc < 2) {
260
        printf("\nError: Not enough arguments in Transform");
261
        return 0;
262
    }
263
           jclass stringClass = lookForClass(environ.env, "java/lang/String");
264
           stringArray = (*(environ.env))->NewObjectArray(environ.env, (jint)argc-1, stringClass, 0 );
265
           if(!stringArray) { return 0;}
266
  int i, j;
267
  for(i=1, j=0; i< argc; i++, j++) {
268
             (*(environ.env))->SetObjectArrayElement(environ.env, stringArray, j, (*(environ.env))->NewStringUTF(environ.env, argv[i]));
269
           }
270
271
   (*(environ.env))->CallStaticVoidMethod(environ.env, transClass, MID_foo, stringArray);
272
   
273
  (*(environ.env))->DeleteLocalRef(environ.env, stringArray);
274
        return 0;
275
}
276
277
278
279
280
int main( int argc, const char* argv[] )
281
{
282
    HANDLE myDllHandle;
283
    //JNIEnv *(environ.env);
284
    //JavaVM *jvm;
285
    jclass  myClassInDll;
286
287
    Environment environ;
288
    /*
289
     * First of all, load required component.
290
     * By the time of JET initialization, all components should be loaded.
291
     */
292
    environ.myDllHandle = loadDll (dllname);
293
   
294
295
    /*
296
     * Initialize JET run-time.
297
     * The handle of loaded component is used to retrieve Invocation API.
298
     */
299
    initJavaRT (environ.myDllHandle, &environ.jvm, &environ.env);
300
    transform(environ, argc, argv);        
301
302
  
303
fflush(stdout);
304
    /*
305
     * Finalize JET run-time.
306
     */
307
    finalizeJavaRT (environ.jvm);
308
309
    return 0;
310
}