Project

Profile

Help

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

he / latest9.8 / hec / command / Validate.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
//===============================================================================================//
55
/*! <code>Environment</code>. This struct captures the jni, JVM and handler to the cross compiled Saxon/C library.
56
 * <p/>
57
 */
58
typedef struct {
59
                JNIEnv *env;
60
                HANDLE myDllHandle;
61
                JavaVM *jvm;
62
        } Environment;
63

    
64

    
65
//===============================================================================================//
66

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

    
75
//===============================================================================================//
76

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

    
85
jobject cpp;
86

    
87

    
88

    
89
const char * failure;
90
/*
91
 * Load dll.
92
 */
93
HANDLE loadDll(char* name)
94
{
95
    HANDLE hDll = LoadLibrary (name);
96

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

    
107

    
108
jint (JNICALL * JNI_GetDefaultJavaVMInitArgs_func) (void *args);
109
jint (JNICALL * JNI_CreateJavaVM_func) (JavaVM **pvm, void **penv, void *args);
110

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

    
119
    JNI_GetDefaultJavaVMInitArgs_func = 
120
             (jint (JNICALL *) (void *args))
121
             GetProcAddress (myDllHandle, "JNI_GetDefaultJavaVMInitArgs");
122

    
123
    JNI_CreateJavaVM_func =
124
             (jint (JNICALL *) (JavaVM **pvm, void **penv, void *args))
125
             GetProcAddress (myDllHandle, "JNI_CreateJavaVM");
126

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

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

    
137
    memset (&args, 0, sizeof(args));
138

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

    
161

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

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

    
178
    return clazz;
179
}
180

    
181

    
182

    
183
jmethodID findConstructor (JNIEnv* penv, jclass myClassInDll, char* arguments)
184
{
185
    jmethodID MID_init, mID;
186
    jobject obj;
187

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

    
195
  return MID_init;
196
}
197

    
198
jobject createObject (JNIEnv* penv, jclass myClassInDll, const char * arguments)
199
{
200
    jmethodID MID_init, mID;
201
    jobject obj;
202

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

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

    
217
void checkForException(Environment environ, jclass callingClass,  jobject callingObject){
218

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

    
227
        jmethodID getName = (jmethodID)(*(environ.env))->GetMethodID(environ.env, clscls, "getName", "()Ljava/lang/String;");
228
        jstring name =(jstring)((*(environ.env))->CallObjectMethod(environ.env, exccls, getName));
229
        char const* utfName = (char const*)(*(environ.env))->GetStringUTFChars(environ.env, name, 0);
230
        printf(utfName);
231

    
232
         jmethodID  getMessage = (jmethodID)(*(environ.env))->GetMethodID(environ.env, exccls, "getMessage", "()Ljava/lang/String;");
233
        if(getMessage) {
234

    
235
                jstring message = (jstring)((*(environ.env))->CallObjectMethod(environ.env, exc, getMessage));
236
                if(message) {                
237
                        char const* utfMessage = (char const*)(*(environ.env))->GetStringUTFChars(environ.env, message, 0);
238
                }
239
        
240
        }
241

    
242
     }
243
        //return NULL;
244

    
245
}
246

    
247

    
248
void finalizeJavaRT (JavaVM* jvm)
249
{
250
    (*jvm)->DestroyJavaVM (jvm);
251
}
252

    
253

    
254

    
255

    
256

    
257
int validate(Environment environ, int argc, const char* argv[]) {
258

    
259

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

    
283
   (*(environ.env))->CallStaticVoidMethod(environ.env, transClass, MID_foo, stringArray);
284
   
285
  (*(environ.env))->DeleteLocalRef(environ.env, stringArray);
286
        return 0;
287
}
288

    
289

    
290

    
291

    
292
int main( int argc, const char* argv[] )
293
{
294
    HANDLE myDllHandle;
295
    //JNIEnv *(environ.env);
296
    //JavaVM *jvm;
297
    jclass  myClassInDll;
298

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

    
307
    /*
308
     * Initialize JET run-time.
309
     * The handle of loaded component is used to retrieve Invocation API.
310
     */
311
    initJavaRT (environ.myDllHandle, &environ.jvm, &environ.env);
312
    validate(environ, argc, argv);        
313

    
314
  
315
fflush(stdout);
316
    /*
317
     * Finalize JET run-time.
318
     */
319
    finalizeJavaRT (environ.jvm);
320

    
321
    return 0;
322
}
(3-3/9)