Code

fix "last effect settings..." that used to run the effect immediately.
[inkscape.git] / src / bind / java / org / inkscape / cmn / ScriptRunner.java
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  */
25 package org.inkscape.cmn;
27 import javax.script.*;
28 import java.io.FileReader;
29 import java.io.PrintStream;
30 import java.io.OutputStream;
31 import java.io.IOException;
32 import javax.swing.JOptionPane;
35 /**
36  * Runs scripts
37  */
38 public class ScriptRunner
39 {
40 long backPtr;
43 /**
44  * Redirect stdout
45  */
46 private native void stdOutWrite(long ptr, int b);
47 class StdOutStream extends OutputStream
48 {
50 public void write(int b)
51 {
52     stdOutWrite(backPtr, b);
53 }
55 }
59 /**
60  * Redirect stderr
61  */
62 private native void stdErrWrite(long ptr, int b);
63 class StdErrStream extends OutputStream
64 {
66 public void write(int b)
67 {
68     stdErrWrite(backPtr, b);
69 }
72 }
75 static void err(String message)
76 {
77     JOptionPane.showMessageDialog(null, message,
78                "Script Error", JOptionPane.ERROR_MESSAGE);
79 }
83 /**
84  * Run a script buffer
85  *
86  * @param lang the scripting language to run
87  * @param str the script buffer to execute
88  * @return true if successful, else false
89  */
90 public boolean run(String lang, String str)
91 {
92     ScriptEngineManager factory = new ScriptEngineManager();
93     // create JavaScript engine
94     ScriptEngine engine = factory.getEngineByName(lang);
95     // evaluate JavaScript code from given file - specified by first argument
96     try
97         {
98         engine.eval(str);
99         }
100     catch (javax.script.ScriptException e)
101         {
102         err("Executing script: " + e);
103         }
104     return true;
108 /**
109  * Run a script file
110  *
111  * @param lang the scripting language to run
112  * @param fname the script file to execute
113  * @return true if successful, else false
114  */
115 public boolean runFile(String lang, String fname)
117     ScriptEngineManager factory = new ScriptEngineManager();
118     // create JavaScript engine
119     ScriptEngine engine = factory.getEngineByName(lang);
120     // evaluate JavaScript code from given file - specified by first argument
121     FileReader in = null;
122     boolean ret = true;
123     try
124         {
125         in = new FileReader(fname);
126         }
127     catch (java.io.IOException e)
128         {
129         err("Executing file: " + e);
130         return false;
131         }
132     try
133         {
134         engine.eval(in);
135         }
136     catch (javax.script.ScriptException e)
137         {
138         err("Executing file: " + e);
139         ret = false;
140         }
141     try
142         {
143         in.close();
144         }
145     catch (java.io.IOException e)
146         {
147         err("Executing file: " + e);
148         return false;
149         }
150     return ret;
154 /**
155  * Constructor
156  * @param backPtr pointer back to the C context that called this
157  */
158 public ScriptRunner(long backPtr)
160     this.backPtr = backPtr;
161     System.setOut(new PrintStream(new StdOutStream()));
162     System.setErr(new PrintStream(new StdErrStream()));
167 private static ScriptRunner _instance = null;
170 public static ScriptRunner getInstance(long backPtr)
172     if (_instance == null)
173         _instance = new ScriptRunner(backPtr);
174     return _instance;
178 /**
179  * Run a script buffer
180  *
181  * @param backPtr pointer back to the C context that called this
182  * @param lang the scripting language to run
183  * @param str the script buffer to execute
184  * @return true if successful, else false
185  */
186 public static boolean run(long ptr, String lang, String str)
188     ScriptRunner runner = getInstance(ptr);
189     return runner.run(lang, str);
193 /**
194  * Run a script file
195  *
196  * @param backPtr pointer back to the C context that called this
197  * @param lang the scripting language to run
198  * @param fname the script file to execute
199  * @return true if successful, else false
200  */
201 public static boolean runFile(long ptr, String lang, String fname)
203     ScriptRunner runner = getInstance(ptr);
204     return runner.runFile(lang, fname);