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 */
58 //########################################################################
59 //# BASE OBJECT
60 //########################################################################
62 static jlong getPointer(JNIEnv *env, jobject obj)
63 {
64 jfieldID id = env->GetFieldID(env->GetObjectClass(obj), "_pointer", "J");
65 jlong val = env->GetLongField(obj, id);
66 return val;
67 }
69 static void setPointer(JNIEnv *env, jobject obj, jlong val)
70 {
71 jfieldID id = env->GetFieldID(env->GetObjectClass(obj), "_pointer", "J");
72 env->SetLongField(obj, id, val);
73 }
75 static void JNICALL DOMBase_construct
76 (JNIEnv *env, jobject obj)
77 {
78 setPointer(env, obj, 0L);
79 }
81 static void JNICALL DOMBase_destruct
82 (JNIEnv *env, jobject obj)
83 {
84 NodePtr *ptr = (NodePtr *)getPointer(env, obj);
85 if (ptr)
86 {
87 delete ptr;
88 }
89 setPointer(env, obj, 0L);
90 }
93 static JNINativeMethod DOMBaseMethods[] =
94 {
95 { (char *)"construct", (char *)"()V", (void *)DOMBase_construct },
96 { (char *)"destruct", (char *)"()V", (void *)DOMBase_destruct },
97 { NULL, NULL, NULL }
98 };
100 //########################################################################
101 //# DOMImplementation
102 //########################################################################
105 void JNICALL DOMImplementation_nCreateDocument
106 (JNIEnv *env, jobject obj)
107 {
108 DOMImplementationImpl domImpl;
109 DocumentTypePtr docType = domImpl.createDocumentType("", "", "");
110 DocumentPtr doc = domImpl.createDocument("", "", docType);
111 DocumentPtr *ptr = new DocumentPtr(doc);
112 setPointer(env, obj, (jlong)ptr);
113 }
117 static JNINativeMethod DOMImplementationMethods[] =
118 {
119 { (char *)"construct", (char *)"()V", (void *)DOMImplementation_nCreateDocument },
120 { NULL, NULL, NULL }
121 };
125 //########################################################################
126 //# MAIN
127 //########################################################################
128 typedef struct
129 {
130 const char *className;
131 JNINativeMethod *methods;
132 } NativeEntry;
135 static NativeEntry nativeEntries[] =
136 {
137 { "org/inkscape/dom/DOMBase", DOMBaseMethods },
138 { "org/inkscape/dom/DOMImplementation", DOMImplementationMethods },
139 { NULL, NULL }
140 };
144 bool JavaBinderyImpl::doBinding()
145 {
146 for (NativeEntry *ne = nativeEntries ; ne->className ; ne++)
147 {
148 bool ret = registerNatives(ne->className, ne->methods);
149 if (!ret)
150 {
151 err("Could not bind native methods");
152 return false;
153 }
154 }
155 return true;
156 }
161 } // namespace Bind
162 } // namespace Inkscape
164 //########################################################################
165 //# E N D O F F I L E
166 //########################################################################