Project

Profile

Help

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

he / src / main / c / Saxon.C.API / Query.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 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
    #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
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 query(Environment environ, int argc, const char* argv[]) {
243

    
244

    
245
    jmethodID MID_foo;
246
    jclass transClass = lookForClass(environ.env, "net/sf/saxon/Query");
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 Query");
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
    query(environ, argc, argv);        
298

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

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