1 /**
2 * @file
3 * @brief This is a simple mechanism to bind Inkscape to Java, and thence
4 * to all of the nice things that can be layered upon that.
5 *
6 * Authors:
7 * Bob Jamison
8 *
9 * Copyright (C) 2007-2008 Bob Jamison
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 3 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <jni.h>
33 #ifdef __WIN32__
34 #include <windows.h>
35 #else
36 #include <dlfcn.h>
37 #include <errno.h>
38 #endif
40 #include "javabind.h"
41 #include "javabind-private.h"
43 #include <dom/dom.h>
44 #include <dom/domimpl.h>
46 namespace Inkscape
47 {
48 namespace Bind
49 {
51 using namespace org::w3c::dom;
53 /**
54 * This file has the actual C++ --> Java bindings
55 * This file can get quite large!
56 */
58 /**
59 * This struct associates a class name with its native
60 * bindings. Since C++ does not allow "flexible" arrays,
61 * we will separate each of the tables into a JNINativeMethod
62 * array, and a class with a name and a pointer to that array.
63 */
64 typedef struct
65 {
66 const char *className;
67 JNINativeMethod *methods;
68 } NativeClass;
70 /**
71 * Although I dislike macros, this one seems reasonable
72 */
73 #define EXCEPTION (getExceptionString(env).c_str())
75 //########################################################################
76 //# BASE OBJECT
77 //########################################################################
79 static jmethodID _getPointer_id = NULL;
81 static jlong getPointer(JNIEnv *env, jobject obj)
82 {
83 if (!_getPointer_id)
84 {
85 _getPointer_id = env->GetMethodID(env->GetObjectClass(obj), "getPointer", "()J");
86 if (!_getPointer_id)
87 {
88 err("getPointer(): %s", EXCEPTION);
89 return 0;
90 }
91 }
92 jlong val = env->CallLongMethod(obj, _getPointer_id);
93 return val;
94 }
97 static jmethodID _setPointer_id = NULL;
99 static void setPointer(JNIEnv *env, jobject obj, jlong val)
100 {
101 if (!_setPointer_id)
102 {
103 _setPointer_id = env->GetMethodID(env->GetObjectClass(obj), "setPointer", "(J)V");
104 if (!_setPointer_id)
105 {
106 err("setPointer(): %s", EXCEPTION);
107 return;
108 }
109 }
110 env->CallVoidMethod(obj, _setPointer_id, val);
111 }
114 static void JNICALL BaseObject_construct
115 (JNIEnv *env, jobject obj)
116 {
117 setPointer(env, obj, 0L);
118 }
120 static void JNICALL BaseObject_destruct
121 (JNIEnv *env, jobject obj)
122 {
123 BaseObject *ptr = (BaseObject *)getPointer(env, obj);
124 if (ptr)
125 {
126 delete ptr;
127 }
128 setPointer(env, obj, 0L);
129 }
132 static JNINativeMethod nm_BaseObject[] =
133 {
134 { (char *)"construct", (char *)"()V", (void *)BaseObject_construct },
135 { (char *)"destruct", (char *)"()V", (void *)BaseObject_destruct },
136 { NULL, NULL, NULL }
137 };
139 static NativeClass nc_BaseObject =
140 {
141 "org/inkscape/cmn/BaseObject",
142 nm_BaseObject
143 };
145 //########################################################################
146 //# BASE OBJECT
147 //########################################################################
149 static void JNICALL DOMBase_construct
150 (JNIEnv *env, jobject obj)
151 {
152 setPointer(env, obj, 0L);
153 }
155 static void JNICALL DOMBase_destruct
156 (JNIEnv *env, jobject obj)
157 {
158 NodePtr *ptr = (NodePtr *)getPointer(env, obj);
159 if (ptr)
160 {
161 delete ptr;
162 }
163 setPointer(env, obj, 0L);
164 }
167 static JNINativeMethod nm_DOMBase[] =
168 {
169 { (char *)"construct", (char *)"()V", (void *)DOMBase_construct },
170 { (char *)"destruct", (char *)"()V", (void *)DOMBase_destruct },
171 { NULL, NULL, NULL }
172 };
174 static NativeClass nc_DOMBase =
175 {
176 "org/inkscape/dom/DOMBase",
177 nm_DOMBase
178 };
181 //########################################################################
182 //# DOMImplementation
183 //########################################################################
186 void JNICALL DOMImplementation_nCreateDocument
187 (JNIEnv *env, jobject obj)
188 {
189 DOMImplementationImpl domImpl;
190 DocumentTypePtr docType = domImpl.createDocumentType("", "", "");
191 DocumentPtr doc = domImpl.createDocument("", "", docType);
192 DocumentPtr *ptr = new DocumentPtr(doc);
193 setPointer(env, obj, (jlong)ptr);
194 }
198 static JNINativeMethod nm_DOMImplementation[] =
199 {
200 { (char *)"construct", (char *)"()V", (void *)DOMImplementation_nCreateDocument },
201 { NULL, NULL, NULL }
202 };
204 static NativeClass nc_DOMImplementation =
205 {
206 "org/inkscape/dom/DOMImplementation",
207 nm_DOMImplementation
208 };
212 //########################################################################
213 //# MAIN
214 //########################################################################
217 /**
218 * This is a table-of-tables, matching a class name to its
219 * table of native methods. We can probably think of a cleaner way
220 * of doing this
221 */
222 static NativeClass *allClasses[] =
223 {
224 &nc_BaseObject,
225 &nc_DOMBase,
226 &nc_DOMImplementation,
227 NULL
228 };
232 bool JavaBinderyImpl::doBinding()
233 {
234 for (NativeClass **nc = allClasses ; *nc ; nc++)
235 {
236 bool ret = registerNatives((*nc)->className, (*nc)->methods);
237 if (!ret)
238 {
239 err("Could not bind native methods");
240 return false;
241 }
242 }
243 return true;
244 }
249 } // namespace Bind
250 } // namespace Inkscape
252 //########################################################################
253 //# E N D O F F I L E
254 //########################################################################