Code

Split getException into method and function, so it can be used in native functions.
[inkscape.git] / src / bind / dobinding.cpp
1 /**
2  * This is a simple mechanism to bind Inkscape to Java, and thence
3  * to all of the nice things that can be layered upon that. 
4  *
5  * Authors:
6  *   Bob Jamison
7  *
8  * Copyright (C) 2007-2008 Bob Jamison
9  *
10  *  This library is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU Lesser General Public
12  *  License as published by the Free Software Foundation; either
13  *  version 3 of the License, or (at your option) any later version.
14  *
15  *  This library is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this library; if not, write to the Free Software
22  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23  */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <jni.h>
32 #ifdef __WIN32__
33 #include <windows.h>
34 #else
35 #include <dlfcn.h>
36 #include <errno.h>
37 #endif
39 #include "javabind.h"
40 #include "javabind-private.h"
42 #include <dom/dom.h>
43 #include <dom/domimpl.h>
45 namespace Inkscape
46 {
47 namespace Bind
48 {
50 using namespace org::w3c::dom;
52 /**
53  * This file has the actual C++ --> Java bindings
54  * This file can get quite large!
55  */  
57 /**
58  * This struct associates a class name with its native
59  * bindings.  Since C++ does not allow "flexible" arrays,
60  * we will separate each of the tables into a JNINativeMethod
61  * array, and a class with a name and a pointer to that array.  
62  */  
63 typedef struct
64 {
65     const char *className;
66     JNINativeMethod *methods;
67 } NativeClass;
70 //########################################################################
71 //# BASE OBJECT
72 //########################################################################
74 static jlong getPointer(JNIEnv *env, jobject obj)
75 {
76     jfieldID id = env->GetFieldID(env->GetObjectClass(obj), "_pointer", "J");
77     if (!id)
78         {
79         err("getPointer: %s", getException(env).c_str());
80         return 0;
81                 }
82     jlong val = env->GetLongField(obj, id);
83     return val;
84 }
86 static void setPointer(JNIEnv *env, jobject obj, jlong val)
87 {
88     jfieldID id = env->GetFieldID(env->GetObjectClass(obj), "_pointer", "J");
89     if (!id)
90         {
91         err("setPointer: %s", getException(env).c_str());
92         return;
93                 }
94     env->SetLongField(obj, id, val);
95 }
97 static void JNICALL BaseObject_construct
98   (JNIEnv *env, jobject obj)
99 {
100     setPointer(env, obj, 0L);
103 static void JNICALL BaseObject_destruct
104   (JNIEnv *env, jobject obj)
106     NodePtr *ptr = (NodePtr *)getPointer(env, obj);
107     if (ptr)
108         {
109         delete ptr;
110         }
111     setPointer(env, obj, 0L);
115 static JNINativeMethod nm_BaseObject[] =
117 { (char *)"construct", (char *)"()V", (void *)BaseObject_construct },
118 { (char *)"destruct",  (char *)"()V", (void *)BaseObject_destruct  },
119 { NULL,  NULL, NULL }
120 };
122 static NativeClass nc_BaseObject =
124     "org/inkscape/cmn/BaseObject",
125     nm_BaseObject
126 };
128 //########################################################################
129 //# BASE OBJECT
130 //########################################################################
132 static void JNICALL DOMBase_construct
133   (JNIEnv *env, jobject obj)
135     setPointer(env, obj, 0L);
138 static void JNICALL DOMBase_destruct
139   (JNIEnv *env, jobject obj)
141     NodePtr *ptr = (NodePtr *)getPointer(env, obj);
142     if (ptr)
143         {
144         delete ptr;
145         }
146     setPointer(env, obj, 0L);
150 static JNINativeMethod nm_DOMBase[] =
152 { (char *)"construct", (char *)"()V", (void *)DOMBase_construct },
153 { (char *)"destruct",  (char *)"()V", (void *)DOMBase_destruct  },
154 { NULL,  NULL, NULL }
155 };
157 static NativeClass nc_DOMBase =
159     "org/inkscape/dom/DOMBase",
160     nm_DOMBase
161 };
164 //########################################################################
165 //# DOMImplementation
166 //########################################################################
169 void JNICALL DOMImplementation_nCreateDocument
170   (JNIEnv *env, jobject obj)
172     DOMImplementationImpl domImpl;
173     DocumentTypePtr docType = domImpl.createDocumentType("", "", "");
174     DocumentPtr doc = domImpl.createDocument("", "", docType);
175     DocumentPtr *ptr = new DocumentPtr(doc);
176     setPointer(env, obj, (jlong)ptr);
181 static JNINativeMethod nm_DOMImplementation[] =
183 { (char *)"construct", (char *)"()V", (void *)DOMImplementation_nCreateDocument },
184 { NULL,  NULL, NULL }
185 };
187 static NativeClass nc_DOMImplementation =
189     "org/inkscape/dom/DOMImplementation",
190     nm_DOMImplementation
191 };
195 //########################################################################
196 //# MAIN
197 //########################################################################
200 /**
201  * This is a table-of-tables, matching a class name to its
202  * table of native methods.  We can probably think of a cleaner way
203  * of doing this
204  */   
205 static NativeClass *allClasses[] =
207     &nc_BaseObject,
208     &nc_DOMBase,
209     &nc_DOMImplementation,
210     NULL
211 };
215 bool JavaBinderyImpl::doBinding()
217     for (NativeClass **nc = allClasses ; *nc ; nc++)
218         {
219         bool ret = registerNatives((*nc)->className, (*nc)->methods);
220         if (!ret)
221             {
222             err("Could not bind native methods");
223             return false;
224             }
225         }
226     return true;
232 } // namespace Bind
233 } // namespace Inkscape
235 //########################################################################
236 //# E N D    O F    F I L E
237 //########################################################################