Project

Profile

Help

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

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

    
65

    
66
//===============================================================================================//
67

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

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

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

    
86
jobject cpp;
87

    
88

    
89

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

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

    
108

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

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

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

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

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

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

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

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

    
162

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

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

    
179
    return clazz;
180
}
181

    
182

    
183

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

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

    
196
  return MID_init;
197
}
198

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

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

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

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

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

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

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

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

    
243
     }
244
        //return NULL;
245

    
246
}
247

    
248

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

    
254

    
255

    
256

    
257

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

    
260

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

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

    
290

    
291

    
292

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

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

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

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

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