1 
2 %include arrays_java.i
3 
4 /*
5   Uncomment this if you wish to hace enums wrapped in an interface compatible
6   with that generated by swig 1.3.21 (tests wont compile, though)
7   %include enumsimple.swg
8 */
9 
10 /* Mapscript library loader */
11 
12 %pragma(java) jniclasscode=%{
13     static {
14         String  library = System.getProperty("mapserver.library.name", "javamapscript");
15 
16         System.loadLibrary(library);
17         /* TODO Throw when return value not MS_SUCCESS? */
18         edu.umn.gis.mapscript.mapscript.msSetup();
19     }
20 %}
21 
22 %typemap(jni) gdBuffer    %{jbyteArray%}
23 %typemap(jtype) gdBuffer  %{byte[]%}
24 %typemap(jstype) gdBuffer %{byte[]%}
25 
26 %typemap(out) gdBuffer
27 %{ $result = SWIG_JavaArrayOutSchar(jenv, $1.data, $1.size);
28    if( $1.owns_data ) msFree($1.data); %}
29 
30 %typemap(javain) gdBuffer "$javainput"
31 %typemap(javaout) gdBuffer {
32     return $jnicall;
33 }
34 
35 /* String conversion utility function */
36 
37 %{
38 /*
39 	These functions taken from: http://java.sun.com/docs/books/jni/html/other.html#26018
40 	Umberto Nicoletti, umberto.nicoletti@gmail.com
41 
42 	Fix bug: http://mapserver.gis.umn.edu/bugs/show_bug.cgi?id=1753
43 */
44 
45 
JNU_ThrowByName(JNIEnv * env,const char * name,const char * msg)46 void JNU_ThrowByName(JNIEnv *env, const char *name, const char *msg)
47 {
48 	jclass cls = (*env)->FindClass(env, name);
49 	/* if cls is NULL, an exception has already been thrown */
50 	if (cls != NULL) {
51 		(*env)->ThrowNew(env, cls, msg);
52 	}
53 	/* free the local ref */
54 	(*env)->DeleteLocalRef(env, cls);
55 }
56 
JNU_GetStringNativeChars(JNIEnv * env,jstring jstr)57 char *JNU_GetStringNativeChars(JNIEnv *env, jstring jstr) {
58 	jbyteArray bytes = 0;
59 	jthrowable exc;
60 	char *result = 0;
61 	jclass jcls_str;
62 	jmethodID MID_String_getBytes;
63 
64 	if (jstr == NULL) {
65 		return NULL;
66 	}
67 
68 	if ((*env)->EnsureLocalCapacity(env, 2) < 0) {
69 		return 0; /* out of memory error */
70 	}
71 
72   	jcls_str = (*env)->FindClass(env, "java/lang/String");
73     	MID_String_getBytes = (*env)->GetMethodID(env, jcls_str, "getBytes", "()[B");
74 
75 	bytes = (*env)->CallObjectMethod(env, jstr,
76                                       MID_String_getBytes);
77 	exc = (*env)->ExceptionOccurred(env);
78 	if (!exc) {
79     		jint len = (*env)->GetArrayLength(env, bytes);
80     		result = (char *)malloc(len + 1);
81     		if (result == 0) {
82         		JNU_ThrowByName(env, "java/lang/OutOfMemoryError",0);
83         		(*env)->DeleteLocalRef(env, bytes);
84         		return 0;
85     		}
86     		(*env)->GetByteArrayRegion(env, bytes, 0, len,
87                                (jbyte *)result);
88 	    	result[len] = 0; /* NULL-terminate */
89 	} else {
90     		(*env)->DeleteLocalRef(env, exc);
91 	}
92 	(*env)->DeleteLocalRef(env, bytes);
93 	return result;
94 }
95 
JNU_NewStringNative(JNIEnv * env,const char * str)96 jstring JNU_NewStringNative(JNIEnv *env, const char *str) {
97 	jstring result;
98 	jbyteArray bytes = 0;
99 	int len;
100 	jclass jcls_str;
101 	jmethodID MID_String_init;
102 
103 	if (str == NULL) {
104 		return NULL;
105 	}
106 
107 	if ((*env)->EnsureLocalCapacity(env, 2) < 0) {
108 	    return NULL; /* out of memory error */
109 	}
110 	jcls_str = (*env)->FindClass(env, "java/lang/String");
111 	MID_String_init = (*env)->GetMethodID(env, jcls_str, "<init>", "([B)V");
112 
113 	len = strlen(str);
114 	bytes = (*env)->NewByteArray(env, len);
115 	if (bytes != NULL) {
116 	    (*env)->SetByteArrayRegion(env, bytes, 0, len,
117 	                            (jbyte *)str);
118 	    result = (*env)->NewObject(env, jcls_str,
119 	                            MID_String_init, bytes);
120 	    (*env)->DeleteLocalRef(env, bytes);
121 	    return result;
122 	} /* else fall through */
123 	return NULL;
124 }
125 
126 %}
127 
128 %typemap(in) char * {
129 	$1 = JNU_GetStringNativeChars(jenv, $input);
130 }
131 
132 /* The default mapping would use ReleaseStringUTFChars to release the
133 memory allocated with JNU_GetStringNativeChars which causes a
134 memory corruption. (#3491) */
135 %typemap(freearg, noblock=1) char * { if ($1) free($1); }
136 
137 %typemap(out) char * {
138 	$result = JNU_NewStringNative(jenv, $1);
139 }
140 
141 /*
142 ===============================================================================
143 RFC-24 implementation follows
144 ===============================================================================
145    Modified constructor according to:
146    - cache population and sync, item 3.2
147 */
148 %typemap(javaconstruct) layerObj(mapObj map) %{ {
149         this($imcall, true);
150         if (map != null) {
151                 this.map=map;
152         }
153 }
154 %}
155 
156 %typemap(javaconstruct) classObj(layerObj layer) %{ {
157         this($imcall, true);
158         if (layer != null) {
159                 this.layer=layer;
160         }
161 }
162 %}
163 
164 %typemap(javaout) int insertLayer {
165         // call the C API
166         int actualIndex=$jnicall;
167         /* Store parent reference, item 3.2 */
168         layer.map=this;
169         return actualIndex;
170 }
171 
172 %typemap(javaout) layerObj* getLayer {
173         // call the C API
174         long cPtr=$jnicall;
175         layerObj layer = null;
176         if (cPtr != 0) {
177         	layer=new layerObj(cPtr, true);
178 	        /* Store parent reference, item 3.2 */
179 	        layer.map=this;
180         }
181         return layer;
182 }
183 
184 %typemap(javaout) layerObj* getLayerByName {
185         // call the C API
186         long cPtr=$jnicall;
187         layerObj layer = null;
188         if (cPtr != 0) {
189         	layer=new layerObj(cPtr, true);
190 	        /* Store parent reference, item 3.2 */
191 	        layer.map=this;
192         }
193         return layer;
194 }
195 
196 %typemap(javaout) int insertClass {
197         // call the C API
198         int actualIndex=$jnicall;
199         /* Store parent reference, item 3.2 */
200         classobj.layer=this;
201         return actualIndex;
202 }
203 
204 %typemap(javaout) classObj* getClass {
205         // call the C API
206         long cPtr=$jnicall;
207         classObj clazz = null;
208         if (cPtr != 0) {
209         	clazz=new classObj(cPtr, true);
210 	        /* Store parent reference, item 3.2 */
211 	        clazz.layer=this;
212         }
213         return clazz;
214 }
215 
216 %typemap(javacode) struct layerObj %{
217         /* parent reference, RFC-24 item 3.2 */
218         mapObj map=null;
219 %}
220 
221 %typemap(javacode) struct classObj %{
222         /* parent reference, RFC-24 item 3.2 */
223         layerObj layer=null;
224 %}
225