Project

Profile

Help

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

he / src / main / c / Saxon.C.API / Validate.c @ 339c45ee

1
#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 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
/*! <code>Environment</code>. This struct captures the jni, JVM and handler to the cross compiled Saxon/C library.
41
 * <p/>
42
 */
43
typedef struct {
44
                JNIEnv *env;
45
                HANDLE myDllHandle;
46
                JavaVM *jvm;
47
        } Environment;
48

    
49

    
50
//===============================================================================================//
51

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

    
60
//===============================================================================================//
61

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

    
70
jobject cpp;
71

    
72

    
73

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

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

    
92

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

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

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

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

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

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

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

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

    
146

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

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

    
163
    return clazz;
164
}
165

    
166

    
167

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

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

    
180
  return MID_init;
181
}
182

    
183
jobject createObject (JNIEnv* penv, jclass myClassInDll, const 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
        return NULL;
192
    }
193

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

    
202
void checkForException(Environment environ, jclass callingClass,  jobject callingObject){
203

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

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

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

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

    
227
     }
228
        //return NULL;
229

    
230
}
231

    
232

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

    
238

    
239

    
240

    
241

    
242
int validate(Environment environ, int argc, const char* argv[]) {
243

    
244

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

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

    
274

    
275

    
276

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

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

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

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

    
306
    return 0;
307
}
(19-19/56)