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 const char *langname=NULL;
75 //if() instead of switch() lets us scope vars
76 if (language == InkscapeScript::JAVASCRIPT)
77 {
78 langname="javascript";
79 }
80 else if (language == InkscapeScript::PYTHON)
81 {
82 langname="python";
83 }
84 else if (language == InkscapeScript::RUBY)
85 {
86 langname="ruby";
87 }
88 else
89 {
90 g_warning("interpretScript: Unknown Script Language type: %d\n",
91 language);
92 return false;
93 }
95 Inkscape::Bind::JavaBindery *binder =
96 Inkscape::Bind::JavaBindery::getInstance();
97 if (!binder->loadJVM()) //idempotent
98 {
99 g_warning("interpretScript: unable to start JVM\n");
100 return false;
101 }
102 std::vector<Value> parms;
103 Value retval;
104 Value parm;
105 parm.setString(langname);
106 parms.push_back(parm);
107 parm.setString(script);
108 parms.push_back(parm);
110 //binder->stdOutClear();
111 //binder->stdErrClear();
112 bool ret = binder->callStatic(Value::BIND_BOOLEAN,
113 "org/inkscape/cmn/ScriptRunner",
114 "run",
115 "(Ljava/lang/String;Ljava/lang/String;)Z",
116 parms,
117 retval);
118 //output = binder->stdOutGet();
119 //error = binder->stdErrGet();
121 if (!ret)
122 {
123 g_warning("interpretScript: failed\n");
124 return false;
125 }
127 return true;
128 }
131 /**
132 * Interprets the script in the named file,
133 * storing the stdout output in 'output', and any
134 * error messages in 'error.' Language is one of the
135 * enumerated types in ScriptLanguage above.
136 */
137 bool InkscapeScript::interpretFile(const Glib::ustring &fname,
138 Glib::ustring &output,
139 Glib::ustring &error,
140 ScriptLanguage language)
141 {
142 const char *langname=NULL;
143 //if() instead of switch() lets us scope vars
144 if (language == InkscapeScript::JAVASCRIPT)
145 {
146 langname="Javascript";
147 }
148 else if (language == InkscapeScript::PYTHON)
149 {
150 langname="Python";
151 }
152 else if (language == InkscapeScript::RUBY)
153 {
154 langname="Ruby";
155 }
156 else
157 {
158 g_warning("interpretFile: Unknown Script Language type: %d\n",
159 language);
160 return false;
161 }
163 Inkscape::Bind::JavaBindery *binder =
164 Inkscape::Bind::JavaBindery::getInstance();
165 if (!binder->loadJVM()) //idempotent
166 {
167 g_warning("interpretFile: unable to start JVM\n");
168 return false;
169 }
170 std::vector<Value> parms;
171 Value retval;
172 Value parm;
173 parm.setString(langname);
174 parms.push_back(parm);
175 parm.setString(fname);
176 parms.push_back(parm);
178 //binder->stdOutClear();
179 //binder->stdErrClear();
180 bool ret = binder->callStatic(Value::BIND_BOOLEAN,
181 "org/inkscape/cmn/ScriptRunner",
182 "runFile",
183 "(Ljava/lang/String;Ljava/lang/String;)Z",
184 parms,
185 retval);
186 //output = binder->stdOutGet();
187 //error = binder->stdErrGet();
189 if (!ret)
190 {
191 g_warning("interpretFile: failed\n");
192 return false;
193 }
195 return true;
196 }
206 } // namespace Script
207 } // namespace Extension
208 } // namespace Inkscape
210 //#########################################################################
211 //# E N D O F F I L E
212 //#########################################################################
214 /*
215 Local Variables:
216 mode:c++
217 c-file-style:"stroustrup"
218 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
219 indent-tabs-mode:nil
220 fill-column:99
221 End:
222 */
223 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :