Code

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