4a880160aeae4e96081f25f1c204664ba3ee8c5a
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 "InkscapeScript.h"
29 #include <bind/javabind.h>
32 namespace Inkscape
33 {
34 namespace Extension
35 {
36 namespace Script
37 {
40 typedef Inkscape::Bind::Value Value;
43 /**
44 *
45 */
46 InkscapeScript::InkscapeScript()
47 {
48 }
53 /**
54 *
55 */
56 InkscapeScript::~InkscapeScript()
57 {
58 }
63 /**
64 * Interprets the script in the 'script' buffer,
65 * storing the stdout output in 'output', and any
66 * error messages in 'error.' Language is one of the
67 * enumerated types in ScriptLanguage above.
68 */
69 bool InkscapeScript::interpretScript(const Glib::ustring &script,
70 Glib::ustring &output,
71 Glib::ustring &error,
72 ScriptLanguage language)
73 {
74 #if USE_JAVA
75 const char *langname=NULL;
76 //if() instead of switch() lets us scope vars
77 if (language == InkscapeScript::JAVASCRIPT)
78 {
79 langname="Javascript";
80 }
81 else if (language == InkscapeScript::PYTHON)
82 {
83 langname="Python";
84 }
85 else if (language == InkscapeScript::RUBY)
86 {
87 langname="Ruby";
88 }
89 else
90 {
91 g_warning("interpretScript: Unknown Script Language type: %d\n",
92 language);
93 return false;
94 }
96 Inkscape::Bind::JavaBindery *binder =
97 Inkscape::Bind::JavaBindery::getInstance();
98 if (!binder->loadJVM()) //idempotent
99 {
100 g_warning("interpretScript: unable to start JVM\n");
101 return false;
102 }
103 std::vector<Value> parms;
104 Value retval;
105 Value parm;
106 parm.setString(langname);
107 parms.push_back(parm);
108 parm.setString(script);
109 parms.push_back(parm);
111 binder->stdOutClear();
112 binder->stdErrClear();
113 bool ret = binder->callStatic(Value::BIND_BOOLEAN,
114 "org/inkscape/cmn/ScriptRunner",
115 "run",
116 "(Ljava/lang/String;Ljava/lang/String;)Z",
117 parms,
118 retval);
119 output = binder->stdOutGet();
120 error = binder->stdErrGet();
122 if (!ret)
123 {
124 g_warning("interpretScript: failed\n");
125 return false;
126 }
128 return true;
129 #else // USE_JAVA
130 return false;
131 #endif // USE_JAVA
132 }
135 /**
136 * Interprets the script in the named file,
137 * storing the stdout output in 'output', and any
138 * error messages in 'error.' Language is one of the
139 * enumerated types in ScriptLanguage above.
140 */
141 bool InkscapeScript::interpretFile(const Glib::ustring &fname,
142 Glib::ustring &output,
143 Glib::ustring &error,
144 ScriptLanguage language)
145 {
146 #if USE_JAVA
147 const char *langname=NULL;
148 //if() instead of switch() lets us scope vars
149 if (language == InkscapeScript::JAVASCRIPT)
150 {
151 langname="Javascript";
152 }
153 else if (language == InkscapeScript::PYTHON)
154 {
155 langname="Python";
156 }
157 else if (language == InkscapeScript::RUBY)
158 {
159 langname="Ruby";
160 }
161 else
162 {
163 g_warning("interpretFile: Unknown Script Language type: %d\n",
164 language);
165 return false;
166 }
168 Inkscape::Bind::JavaBindery *binder =
169 Inkscape::Bind::JavaBindery::getInstance();
170 if (!binder->loadJVM()) //idempotent
171 {
172 g_warning("interpretFile: unable to start JVM\n");
173 return false;
174 }
175 std::vector<Value> parms;
176 Value retval;
177 Value parm;
178 parm.setString(langname);
179 parms.push_back(parm);
180 parm.setString(fname);
181 parms.push_back(parm);
183 binder->stdOutClear();
184 binder->stdErrClear();
185 bool ret = binder->callStatic(Value::BIND_BOOLEAN,
186 "org/inkscape/cmn/ScriptRunner",
187 "runFile",
188 "(Ljava/lang/String;Ljava/lang/String;)Z",
189 parms,
190 retval);
191 output = binder->stdOutGet();
192 error = binder->stdErrGet();
194 if (!ret)
195 {
196 g_warning("interpretFile: failed\n");
197 return false;
198 }
200 return true;
201 #else // USE_JAVA
202 return false;
203 #endif // USE_JAVA
204 }
214 } // namespace Script
215 } // namespace Extension
216 } // namespace Inkscape
218 //#########################################################################
219 //# E N D O F F I L E
220 //#########################################################################
222 /*
223 Local Variables:
224 mode:c++
225 c-file-style:"stroustrup"
226 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
227 indent-tabs-mode:nil
228 fill-column:99
229 End:
230 */
231 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :