Project

Profile

Help

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

he / latest9.8 / hec / command / Transform.c @ b5a62f2e

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
typedef int bool;
16
#define true 1
17
#define false 0
18

    
19

    
20
char dllname[] =
21
   // #ifdef __linux__
22
#ifdef EEC
23
        "/usr/lib/libsaxoneec.so";
24
#elif defined PEC
25
        "/usr/lib/libsaxonpec.so";
26
#else
27
        "/usr/lib/libsaxonhec.so";
28
#endif
29
/*    #else
30
#ifdef EEC
31
        "Saxon-eec.dll";
32
#elif defined PEC
33
        "Saxon-pec.dll";
34
#else
35
        "Saxon-hec.dll";
36
#endif
37

38
#endif
39
*/
40
//===============================================================================================//
41
/*! <code>Environment</code>. This struct captures the jni, JVM and handler to the cross compiled Saxon/C library.
42
 * <p/>
43
 */
44
typedef struct {
45
                JNIEnv *env;
46
                HANDLE myDllHandle;
47
                JavaVM *jvm;
48
        } Environment;
49

    
50

    
51
//===============================================================================================//
52

    
53
/*! <code>MyParameter</code>. This struct captures details of paramaters used for the transformation as (string, value) pairs.
54
 * <p/>
55
 */
56
typedef struct {
57
                char* name;
58
                jobject value;
59
        } MyParameter;
60

    
61
//===============================================================================================//
62

    
63
/*! <code>MyProperty</code>. This struct captures details of properties used for the transformation as (string, string) pairs.
64
 * <p/>
65
 */
66
typedef struct {
67
                char * name;
68
                char * value;
69
        } MyProperty;
70

    
71
jobject cpp;
72

    
73

    
74

    
75
const char * failure;
76
/*
77
 * Load dll.
78
 */
79
HANDLE loadDll(char* name)
80
{
81
    HANDLE hDll = LoadLibrary (name);
82

    
83
    if (!hDll) {
84
        printf ("Unable to load %s\n", name);
85
        exit(1);
86
    }
87
#ifdef DEBUG
88
    printf ("%s loaded\n", name);
89
#endif
90
    return hDll;
91
}
92

    
93

    
94
jint (JNICALL * JNI_GetDefaultJavaVMInitArgs_func) (void *args);
95
jint (JNICALL * JNI_CreateJavaVM_func) (JavaVM **pvm, void **penv, void *args);
96

    
97
/*
98
 * Initialize JET run-time.
99
 */
100
void initJavaRT(HANDLE myDllHandle, JavaVM** pjvm, JNIEnv** penv)
101
{
102
    int            result;
103
    JavaVMInitArgs args;
104

    
105
    JNI_GetDefaultJavaVMInitArgs_func = 
106
             (jint (JNICALL *) (void *args))
107
             GetProcAddress (myDllHandle, "JNI_GetDefaultJavaVMInitArgs");
108

    
109
    JNI_CreateJavaVM_func =
110
             (jint (JNICALL *) (JavaVM **pvm, void **penv, void *args))
111
             GetProcAddress (myDllHandle, "JNI_CreateJavaVM");
112

    
113
    if(!JNI_GetDefaultJavaVMInitArgs_func) {
114
        printf ("%s doesn't contain public JNI_GetDefaultJavaVMInitArgs\n", dllname);
115
        exit (1);
116
    }
117

    
118
    if(!JNI_CreateJavaVM_func) {
119
        printf ("%s doesn't contain public JNI_CreateJavaVM\n", dllname);
120
        exit (1);
121
    }
122

    
123
    memset (&args, 0, sizeof(args));
124

    
125
    args.version = JNI_VERSION_1_2;
126
    result = JNI_GetDefaultJavaVMInitArgs_func(&args);
127
    if (result != JNI_OK) {
128
        printf ("JNI_GetDefaultJavaVMInitArgs() failed with result %d\n", result);
129
        exit(1);
130
    }
131
  
132
    /*
133
     * NOTE: no JVM is actually created
134
     * this call to JNI_CreateJavaVM is intended for JET RT initialization
135
     */
136
    result = JNI_CreateJavaVM_func (pjvm, (void **)penv, &args);
137
    if (result != JNI_OK) {
138
        printf ("JNI_CreateJavaVM() failed with result %d\n", result);
139
        exit(1);
140
    }
141
#ifdef DEBUG
142
    printf ("JET RT initialized\n");
143
    fflush (stdout);
144
#endif
145
}
146

    
147

    
148
/*
149
 * Look for class.
150
 */
151
jclass lookForClass (JNIEnv* penv, char* name)
152
{
153
    jclass clazz = (*penv)->FindClass (penv, name);
154

    
155
    if (!clazz) {
156
        printf("Unable to find class %s\n", name);
157
        return NULL;
158
    }
159
#ifdef DEBUG
160
    printf ("Class %s found\n", name);
161
    fflush (stdout);
162
#endif
163

    
164
    return clazz;
165
}
166

    
167

    
168

    
169

    
170
jmethodID findConstructor (JNIEnv* penv, jclass myClassInDll, char* arguments)
171
{
172
    jmethodID MID_init, mID;
173
    jobject obj;
174

    
175
    MID_init = (jmethodID)(*penv)->GetMethodID (penv, myClassInDll, "<init>", arguments);
176
    if (!MID_init) {
177
        printf("Error: MyClassInDll.<init>() not found\n");
178
        fflush (stdout);
179
        return 0;
180
    }
181

    
182
  return MID_init;
183
}
184

    
185
jobject createObject (JNIEnv* penv, jclass myClassInDll, const char * arguments)
186
{
187
    jmethodID MID_init, mID;
188
    jobject obj;
189

    
190
    MID_init = (jmethodID)(*(penv))->GetMethodID (penv, myClassInDll, "<init>", arguments);
191
    if (!MID_init) {
192
        printf("Error: MyClassInDll.<init>() not found\n");
193
        return NULL;
194
    }
195

    
196
      obj = (jobject)(*(penv))->NewObject(penv, myClassInDll, MID_init, (jboolean)true);
197
      if (!obj) {
198
        printf("Error: failed to allocate an object\n");
199
        return NULL;
200
      }
201
    return obj;
202
}
203

    
204
void checkForException(Environment environ, jclass callingClass,  jobject callingObject){
205

    
206
    if ((*(environ.env))->ExceptionCheck(environ.env)) {
207
        char *  result1;
208
        const char * errorCode = NULL;
209
        jthrowable exc = (*(environ.env))->ExceptionOccurred(environ.env);
210
        (*(environ.env))->ExceptionDescribe(environ.env); //comment code
211
         jclass exccls = (jclass)(*(environ.env))->GetObjectClass(environ.env, exc);
212
        jclass clscls = (jclass)(*(environ.env))->FindClass(environ.env, "java/lang/Class");
213

    
214
        jmethodID getName = (jmethodID)(*(environ.env))->GetMethodID(environ.env, clscls, "getName", "()Ljava/lang/String;");
215
        jstring name =(jstring)((*(environ.env))->CallObjectMethod(environ.env, exccls, getName));
216
        char const* utfName = (char const*)(*(environ.env))->GetStringUTFChars(environ.env, name, 0);
217
        printf(utfName);
218

    
219
         jmethodID  getMessage = (jmethodID)(*(environ.env))->GetMethodID(environ.env, exccls, "getMessage", "()Ljava/lang/String;");
220
        if(getMessage) {
221

    
222
                jstring message = (jstring)((*(environ.env))->CallObjectMethod(environ.env, exc, getMessage));
223
                if(message) {                
224
                        char const* utfMessage = (char const*)(*(environ.env))->GetStringUTFChars(environ.env, message, 0);
225
                }
226
        
227
        }
228

    
229
     }
230
        //return NULL;
231

    
232
}
233

    
234

    
235
void finalizeJavaRT (JavaVM* jvm)
236
{
237
    (*jvm)->DestroyJavaVM (jvm);
238
}
239

    
240

    
241

    
242

    
243

    
244
int transform(Environment environ, int argc, const char* argv[]) {
245

    
246

    
247
    jmethodID MID_foo;
248
    jclass transClass = lookForClass(environ.env, "net/sf/saxon/Transform");
249
    char methodName[] = "main";
250
    char args[] = "([Ljava/lang/String;)V";
251
    jobjectArray stringArray = NULL;
252
    MID_foo = (jmethodID)(*(environ.env))->GetStaticMethodID(environ.env, transClass, methodName, args);
253
    if (!MID_foo) {
254
        printf("\nError: MyClassInDll %s() not found\n",methodName);
255
        fflush (stdout);
256
        return -1;
257
    }
258
     if(argc < 2) {
259
        printf("\nError: Not enough arguments in Transform");
260
        return 0;
261
    }
262
           jclass stringClass = lookForClass(environ.env, "java/lang/String");
263
           stringArray = (*(environ.env))->NewObjectArray(environ.env, (jint)argc-1, stringClass, 0 );
264
           if(!stringArray) { return 0;}
265
  int i, j;
266
  for(i=1, j=0; i< argc; i++, j++) {
267
             (*(environ.env))->SetObjectArrayElement(environ.env, stringArray, j, (*(environ.env))->NewStringUTF(environ.env, argv[i]));
268
           }
269

    
270
   (*(environ.env))->CallStaticVoidMethod(environ.env, transClass, MID_foo, stringArray);
271
   
272
  (*(environ.env))->DeleteLocalRef(environ.env, stringArray);
273
        return 0;
274
}
275

    
276

    
277

    
278

    
279
int main( int argc, const char* argv[] )
280
{
281
    HANDLE myDllHandle;
282
    //JNIEnv *(environ.env);
283
    //JavaVM *jvm;
284
    jclass  myClassInDll;
285

    
286
    Environment environ;
287
    /*
288
     * First of all, load required component.
289
     * By the time of JET initialization, all components should be loaded.
290
     */
291
    environ.myDllHandle = loadDll (dllname);
292
   
293

    
294
    /*
295
     * Initialize JET run-time.
296
     * The handle of loaded component is used to retrieve Invocation API.
297
     */
298
    initJavaRT (environ.myDllHandle, &environ.jvm, &environ.env);
299
    transform(environ, argc, argv);        
300

    
301
  
302
fflush(stdout);
303
    /*
304
     * Finalize JET run-time.
305
     */
306
    finalizeJavaRT (environ.jvm);
307

    
308
    return 0;
309
}
(2-2/9)