1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
%include arrays_java.i
# Uncomment this if you wish to hace enums wrapped in an interface compatible
# with that generated by swig 1.3.21 (tests wont compile, though)
#%include enumsimple.swg
/* Mapscript library loader */
%pragma(java) jniclasscode=%{
static {
String library = System.getProperty("mapserver.library.name", "mapscript");
System.loadLibrary(library);
/* TODO Throw when return value not MS_SUCCESS? */
edu.umn.gis.mapscript.mapscript.msSetup();
}
%}
%typemap(jni) gdBuffer %{jbyteArray%}
%typemap(jtype) gdBuffer %{byte[]%}
%typemap(jstype) gdBuffer %{byte[]%}
%typemap(out) gdBuffer
%{ $result = SWIG_JavaArrayOutSchar(jenv, $1.data, $1.size);
if( $1.owns_data ) gdFree($1.data); %}
%typemap(javain) gdBuffer "$javainput"
%typemap(javaout) gdBuffer {
return $jnicall;
}
/* String conversion utility function */
%{
/*
These functions taken from: http://java.sun.com/docs/books/jni/html/other.html#26018
Umberto Nicoletti, umberto.nicoletti@gmail.com
Fix bug: http://mapserver.gis.umn.edu/bugs/show_bug.cgi?id=1753
*/
void JNU_ThrowByName(JNIEnv *env, const char *name, const char *msg)
{
jclass cls = (*env)->FindClass(env, name);
/* if cls is NULL, an exception has already been thrown */
if (cls != NULL) {
(*env)->ThrowNew(env, cls, msg);
}
/* free the local ref */
(*env)->DeleteLocalRef(env, cls);
}
char *JNU_GetStringNativeChars(JNIEnv *env, jstring jstr) {
jbyteArray bytes = 0;
jthrowable exc;
char *result = 0;
jclass jcls_str;
jmethodID MID_String_getBytes;
if (jstr == NULL) {
return NULL;
}
if ((*env)->EnsureLocalCapacity(env, 2) < 0) {
return 0; /* out of memory error */
}
jcls_str = (*env)->FindClass(env, "java/lang/String");
MID_String_getBytes = (*env)->GetMethodID(env, jcls_str, "getBytes", "()[B");
bytes = (*env)->CallObjectMethod(env, jstr,
MID_String_getBytes);
exc = (*env)->ExceptionOccurred(env);
if (!exc) {
jint len = (*env)->GetArrayLength(env, bytes);
result = (char *)malloc(len + 1);
if (result == 0) {
JNU_ThrowByName(env, "java/lang/OutOfMemoryError",0);
(*env)->DeleteLocalRef(env, bytes);
return 0;
}
(*env)->GetByteArrayRegion(env, bytes, 0, len,
(jbyte *)result);
result[len] = 0; /* NULL-terminate */
} else {
(*env)->DeleteLocalRef(env, exc);
}
(*env)->DeleteLocalRef(env, bytes);
return result;
}
jstring JNU_NewStringNative(JNIEnv *env, const char *str) {
jstring result;
jbyteArray bytes = 0;
int len;
jclass jcls_str;
jmethodID MID_String_init;
if (str == NULL) {
return NULL;
}
if ((*env)->EnsureLocalCapacity(env, 2) < 0) {
return NULL; /* out of memory error */
}
jcls_str = (*env)->FindClass(env, "java/lang/String");
MID_String_init = (*env)->GetMethodID(env, jcls_str, "<init>", "([B)V");
len = strlen(str);
bytes = (*env)->NewByteArray(env, len);
if (bytes != NULL) {
(*env)->SetByteArrayRegion(env, bytes, 0, len,
(jbyte *)str);
result = (*env)->NewObject(env, jcls_str,
MID_String_init, bytes);
(*env)->DeleteLocalRef(env, bytes);
return result;
} /* else fall through */
return NULL;
}
%}
%typemap(in) char * {
$1 = JNU_GetStringNativeChars(jenv, $input);
}
%typemap(out) char * {
$result = JNU_NewStringNative(jenv, $1);
}
|