diff options
| author | Bob Jamison <ishmalius@gmail.com> | 2008-03-10 04:48:47 +0000 |
|---|---|---|
| committer | ishmal <ishmal@users.sourceforge.net> | 2008-03-10 04:48:47 +0000 |
| commit | 8d835e32a1f64150a61a0a37d42bb7f5bf1155bc (patch) | |
| tree | 7e8199cda7525c41c454c8600c2a267247eb3303 /src/bind/java | |
| parent | Update header (diff) | |
| download | inkscape-8d835e32a1f64150a61a0a37d42bb7f5bf1155bc.tar.gz inkscape-8d835e32a1f64150a61a0a37d42bb7f5bf1155bc.zip | |
Redirect stdout and stderr from scripts
(bzr r5022)
Diffstat (limited to 'src/bind/java')
| -rw-r--r-- | src/bind/java/org/inkscape/cmn/ScriptRunner.java | 114 |
1 files changed, 109 insertions, 5 deletions
diff --git a/src/bind/java/org/inkscape/cmn/ScriptRunner.java b/src/bind/java/org/inkscape/cmn/ScriptRunner.java index 1addfc72e..1214a66d4 100644 --- a/src/bind/java/org/inkscape/cmn/ScriptRunner.java +++ b/src/bind/java/org/inkscape/cmn/ScriptRunner.java @@ -5,12 +5,12 @@ * Authors: * Bob Jamison * - * Copyright (C) 2007 Bob Jamison + * Copyright (C) 2007-2008 Bob Jamison * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. + * version 3 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -26,15 +26,50 @@ package org.inkscape.cmn; import javax.script.*; import java.io.FileReader; +import java.io.PrintStream; +import java.io.OutputStream; import java.io.IOException; import javax.swing.JOptionPane; +/** + * Runs scripts + */ +public class ScriptRunner +{ +long backPtr; -public class ScriptRunner +/** + * Redirect stdout + */ +private native void stdOutWrite(long ptr, int b); +class StdOutStream extends OutputStream { +public void write(int b) +{ + stdOutWrite(backPtr, b); +} + +} + + + +/** + * Redirect stderr + */ +private native void stdErrWrite(long ptr, int b); +class StdErrStream extends OutputStream +{ + +public void write(int b) +{ + stdErrWrite(backPtr, b); +} + + +} static void err(String message) @@ -45,7 +80,14 @@ static void err(String message) -public static boolean run(String lang, String str) +/** + * Run a script buffer + * + * @param lang the scripting language to run + * @param str the script buffer to execute + * @return true if successful, else false + */ +public boolean run(String lang, String str) { ScriptEngineManager factory = new ScriptEngineManager(); // create JavaScript engine @@ -62,7 +104,15 @@ public static boolean run(String lang, String str) return true; } -public static boolean runFile(String lang, String fname) + +/** + * Run a script file + * + * @param lang the scripting language to run + * @param fname the script file to execute + * @return true if successful, else false + */ +public boolean runFile(String lang, String fname) { ScriptEngineManager factory = new ScriptEngineManager(); // create JavaScript engine @@ -101,5 +151,59 @@ public static boolean runFile(String lang, String fname) } +/** + * Constructor + * @param backPtr pointer back to the C context that called this + */ +public ScriptRunner(long backPtr) +{ + this.backPtr = backPtr; + System.setOut(new PrintStream(new StdOutStream())); + System.setErr(new PrintStream(new StdErrStream())); +} + + + +private static ScriptRunner _instance = null; + + +public static ScriptRunner getInstance(long backPtr) +{ + if (_instance == null) + _instance = new ScriptRunner(backPtr); + return _instance; +} + + +/** + * Run a script buffer + * + * @param backPtr pointer back to the C context that called this + * @param lang the scripting language to run + * @param str the script buffer to execute + * @return true if successful, else false + */ +public static boolean run(long ptr, String lang, String str) +{ + ScriptRunner runner = getInstance(ptr); + return runner.run(lang, str); +} + + +/** + * Run a script file + * + * @param backPtr pointer back to the C context that called this + * @param lang the scripting language to run + * @param fname the script file to execute + * @return true if successful, else false + */ +public static boolean runFile(long ptr, String lang, String fname) +{ + ScriptRunner runner = getInstance(ptr); + return runner.runFile(lang, fname); +} + + } |
