Project

Profile

Help

Revision ac6fa66c

Added by O'Neil Delpratt almost 6 years ago

Added the file Query.c

View differences:

latest9.6/hec/Saxon.C.API/Query.c
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
        "Saxon-hec.dll";
32
    #endif
33

  
34
//===============================================================================================//
35
/*! <code>Environment</code>. This struct captures the jni, JVM and handler to the cross compiled Saxon/C library.
36
 * <p/>
37
 */
38
typedef struct {
39
		JNIEnv *env;
40
		HANDLE myDllHandle;
41
		JavaVM *jvm;
42
	} Environment;
43

  
44

  
45
//===============================================================================================//
46

  
47
/*! <code>MyParameter</code>. This struct captures details of paramaters used for the transformation as (string, value) pairs.
48
 * <p/>
49
 */
50
typedef struct {
51
		char* name;
52
		jobject value;
53
	} MyParameter;
54

  
55
//===============================================================================================//
56

  
57
/*! <code>MyProperty</code>. This struct captures details of properties used for the transformation as (string, string) pairs.
58
 * <p/>
59
 */
60
typedef struct {
61
		char * name;
62
		char * value;
63
	} MyProperty;
64

  
65
jobject cpp;
66

  
67

  
68

  
69
const char * failure;
70
/*
71
 * Load dll.
72
 */
73
HANDLE loadDll(char* name)
74
{
75
    HANDLE hDll = LoadLibrary (name);
76

  
77
    if (!hDll) {
78
        printf ("Unable to load %s\n", name);
79
        exit(1);
80
    }
81
#ifdef DEBUG
82
    printf ("%s loaded\n", name);
83
#endif
84
    return hDll;
85
}
86

  
87

  
88
jint (JNICALL * JNI_GetDefaultJavaVMInitArgs_func) (void *args);
89
jint (JNICALL * JNI_CreateJavaVM_func) (JavaVM **pvm, void **penv, void *args);
90

  
91
/*
92
 * Initialize JET run-time.
93
 */
94
void initJavaRT(HANDLE myDllHandle, JavaVM** pjvm, JNIEnv** penv)
95
{
96
    int            result;
97
    JavaVMInitArgs args;
98

  
99
    JNI_GetDefaultJavaVMInitArgs_func = 
100
             (jint (JNICALL *) (void *args))
101
             GetProcAddress (myDllHandle, "JNI_GetDefaultJavaVMInitArgs");
102

  
103
    JNI_CreateJavaVM_func =
104
             (jint (JNICALL *) (JavaVM **pvm, void **penv, void *args))
105
             GetProcAddress (myDllHandle, "JNI_CreateJavaVM");
106

  
107
    if(!JNI_GetDefaultJavaVMInitArgs_func) {
108
        printf ("%s doesn't contain public JNI_GetDefaultJavaVMInitArgs\n", dllname);
109
        exit (1);
110
    }
111

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

  
117
    memset (&args, 0, sizeof(args));
118

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

  
141

  
142
/*
143
 * Look for class.
144
 */
145
jclass lookForClass (JNIEnv* penv, char* name)
146
{
147
    jclass clazz = (*penv)->FindClass (penv, name);
148

  
149
    if (!clazz) {
150
        printf("Unable to find class %s\n", name);
151
	return NULL;
152
    }
153
#ifdef DEBUG
154
    printf ("Class %s found\n", name);
155
    fflush (stdout);
156
#endif
157

  
158
    return clazz;
159
}
160

  
161

  
162
/*
163
 * Create an object and invoke the "ifoo" instance method
164
 */
165
void invokeInstanceMethod (JNIEnv* penv, jclass myClassInDll)
166
{
167
    jmethodID MID_init, MID_ifoo;
168
    jobject obj;
169

  
170
    MID_init = (*penv)->GetMethodID (penv, myClassInDll, "<init>", "()V");
171
    if (!MID_init) {
172
        printf("Error: MyClassInDll.<init>() not found\n");
173
        return;
174
    }
175

  
176
    obj = (*penv)->NewObject(penv, myClassInDll, MID_init);
177
    if (!obj) {
178
        printf("Error: failed to allocate an object\n");
179
        return;
180
    }
181

  
182
    MID_ifoo = (*penv)->GetMethodID (penv, myClassInDll, "ifoo", "()V");
183

  
184
    if (!MID_ifoo) {
185
        printf("Error: MyClassInDll.ifoo() not found\n");
186
        return;
187
    }
188
    
189
    (*(penv))->CallVoidMethod (penv, obj, MID_ifoo);
190
}
191

  
192

  
193

  
194
/*
195
 * Invoke the "foo" static method
196
 */
197
void invokeStaticMethod(JNIEnv* penv, jclass myClassInDll)
198
{
199
    jmethodID MID_foo;
200

  
201
    MID_foo = (*penv)->GetStaticMethodID(penv, myClassInDll, "foo", "()V");
202
    if (!MID_foo) {
203
        printf("\nError: MyClassInDll.foo() not found\n");
204
        return;
205
    }
206
    
207
    (*penv)->CallStaticVoidMethod(penv, myClassInDll, MID_foo);
208
}
209

  
210
jmethodID findConstructor (JNIEnv* penv, jclass myClassInDll, char* arguments)
211
{
212
    jmethodID MID_init, mID;
213
    jobject obj;
214

  
215
    MID_init = (jmethodID)(*penv)->GetMethodID (penv, myClassInDll, "<init>", arguments);
216
    if (!MID_init) {
217
        printf("Error: MyClassInDll.<init>() not found\n");
218
	fflush (stdout);
219
        return 0;
220
    }
221

  
222
  return MID_init;
223
}
224

  
225
jobject createObject (JNIEnv* penv, jclass myClassInDll, const char * arguments)
226
{
227
    jmethodID MID_init, mID;
228
    jobject obj;
229

  
230
    MID_init = (jmethodID)(*(penv))->GetMethodID (penv, myClassInDll, "<init>", arguments);
231
    if (!MID_init) {
232
        printf("Error: MyClassInDll.<init>() not found\n");
233
        return NULL;
234
    }
235

  
236
      obj = (jobject)(*(penv))->NewObject(penv, myClassInDll, MID_init, (jboolean)true);
237
      if (!obj) {
238
        printf("Error: failed to allocate an object\n");
239
        return NULL;
240
      }
241
    return obj;
242
}
243

  
244
void checkForException(Environment environ, jclass callingClass,  jobject callingObject){
245

  
246
    if ((*(environ.env))->ExceptionCheck(environ.env)) {
247
	char *  result1;
248
	const char * errorCode = NULL;
249
	jthrowable exc = (*(environ.env))->ExceptionOccurred(environ.env);
250
	(*(environ.env))->ExceptionDescribe(environ.env); //comment code
251
	 jclass exccls = (jclass)(*(environ.env))->GetObjectClass(environ.env, exc);
252
        jclass clscls = (jclass)(*(environ.env))->FindClass(environ.env, "java/lang/Class");
253

  
254
        jmethodID getName = (jmethodID)(*(environ.env))->GetMethodID(environ.env, clscls, "getName", "()Ljava/lang/String;");
255
        jstring name =(jstring)((*(environ.env))->CallObjectMethod(environ.env, exccls, getName));
256
        char const* utfName = (char const*)(*(environ.env))->GetStringUTFChars(environ.env, name, 0);
257
	printf(utfName);
258

  
259
	 jmethodID  getMessage = (jmethodID)(*(environ.env))->GetMethodID(environ.env, exccls, "getMessage", "()Ljava/lang/String;");
260
	if(getMessage) {
261

  
262
		jstring message = (jstring)((*(environ.env))->CallObjectMethod(environ.env, exc, getMessage));
263
		if(message) {        	
264
			char const* utfMessage = (char const*)(*(environ.env))->GetStringUTFChars(environ.env, message, 0);
265
		}
266
	
267
	}
268

  
269
     }
270
	//return NULL;
271

  
272
}
273

  
274

  
275
void finalizeJavaRT (JavaVM* jvm)
276
{
277
    (*jvm)->DestroyJavaVM (jvm);
278
}
279

  
280

  
281

  
282

  
283

  
284
int query(Environment environ, int argc, const char* argv[]) {
285

  
286

  
287
    jmethodID MID_foo;
288
    jclass transClass = lookForClass(environ.env, "net/sf/saxon/Query");
289
    char methodName[] = "main";
290
    char args[] = "([Ljava/lang/String;)V";
291
    jobjectArray stringArray = NULL;
292
    MID_foo = (jmethodID)(*(environ.env))->GetStaticMethodID(environ.env, transClass, methodName, args);
293
    if (!MID_foo) {
294
	printf("\nError: MyClassInDll %s() not found\n",methodName);
295
	fflush (stdout);
296
        return -1;
297
    }
298
     if(argc < 2) {
299
	printf("\nError: Not enough arguments in Query");
300
	return 0;
301
    }
302
	   jclass stringClass = lookForClass(environ.env, "java/lang/String");
303
	   stringArray = (*(environ.env))->NewObjectArray(environ.env, (jint)argc-1, stringClass, 0 );
304
	   if(!stringArray) { return 0;}
305
  int i, j;
306
  for(i=1, j=0; i< argc; i++, j++) {
307
	     (*(environ.env))->SetObjectArrayElement(environ.env, stringArray, j, (*(environ.env))->NewStringUTF(environ.env, argv[i]));
308
	   }
309

  
310
   (*(environ.env))->CallStaticVoidMethod(environ.env, transClass, MID_foo, stringArray);
311
   
312
  (*(environ.env))->DeleteLocalRef(environ.env, stringArray);
313
	return 0;
314
}
315

  
316

  
317

  
318

  
319
int main( int argc, const char* argv[] )
320
{
321
    HANDLE myDllHandle;
322
    //JNIEnv *(environ.env);
323
    //JavaVM *jvm;
324
    jclass  myClassInDll;
325

  
326
    Environment environ;
327
    /*
328
     * First of all, load required component.
329
     * By the time of JET initialization, all components should be loaded.
330
     */
331
    environ.myDllHandle = loadDll (dllname);
332
   
333

  
334
    /*
335
     * Initialize JET run-time.
336
     * The handle of loaded component is used to retrieve Invocation API.
337
     */
338
    initJavaRT (environ.myDllHandle, &environ.jvm, &environ.env);
339
    query(environ, argc, argv);	
340

  
341
  
342
fflush(stdout);
343
    /*
344
     * Finalize JET run-time.
345
     */
346
    finalizeJavaRT (environ.jvm);
347

  
348
    return 0;
349
}

Also available in: Unified diff