/
javamodule.i
222 lines (187 loc) · 5.7 KB
/
javamodule.i
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
%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", "javamapscript");
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 ) msFree($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);
}
/* The default mapping would use ReleaseStringUTFChars to release the
memory allocated with JNU_GetStringNativeChars which causes a
memory corruption. (#3491) */
%typemap(freearg, noblock=1) char * { if ($1) free($1); }
%typemap(out) char * {
$result = JNU_NewStringNative(jenv, $1);
}
/*
===============================================================================
RFC-24 implementation follows
===============================================================================
Modified constructor according to:
- cache population and sync, item 3.2
*/
%typemap(javaconstruct) layerObj(mapObj map) %{ {
this($imcall, true);
if (map != null) {
this.map=map;
}
}
%}
%typemap(javaconstruct) classObj(layerObj layer) %{ {
this($imcall, true);
if (layer != null) {
this.layer=layer;
}
}
%}
%typemap(javaout) int insertLayer {
// call the C API
int actualIndex=$jnicall;
/* Store parent reference, item 3.2 */
layer.map=this;
return actualIndex;
}
%typemap(javaout) layerObj* getLayer {
// call the C API
long cPtr=$jnicall;
layerObj layer = null;
if (cPtr != 0) {
layer=new layerObj(cPtr, true);
/* Store parent reference, item 3.2 */
layer.map=this;
}
return layer;
}
%typemap(javaout) layerObj* getLayerByName {
// call the C API
long cPtr=$jnicall;
layerObj layer = null;
if (cPtr != 0) {
layer=new layerObj(cPtr, true);
/* Store parent reference, item 3.2 */
layer.map=this;
}
return layer;
}
%typemap(javaout) int insertClass {
// call the C API
int actualIndex=$jnicall;
/* Store parent reference, item 3.2 */
classobj.layer=this;
return actualIndex;
}
%typemap(javaout) classObj* getClass {
// call the C API
long cPtr=$jnicall;
classObj clazz = null;
if (cPtr != 0) {
clazz=new classObj(cPtr, true);
/* Store parent reference, item 3.2 */
clazz.layer=this;
}
return clazz;
}
%typemap(javacode) struct layerObj %{
/* parent reference, RFC-24 item 3.2 */
mapObj map=null;
%}
%typemap(javacode) struct classObj %{
/* parent reference, RFC-24 item 3.2 */
layerObj layer=null;
%}