From 132aed75d6bd4f10df1699a4ac8b328249a5b4b9 Mon Sep 17 00:00:00 2001 From: ishmal Date: Mon, 10 Mar 2008 04:48:47 +0000 Subject: [PATCH] Redirect stdout and stderr from scripts --- .../java/org/inkscape/cmn/ScriptRunner.java | 114 +++++++++++++++++- src/bind/javabind-private.h | 2 +- src/bind/javabind.cpp | 33 ++++- src/bind/javabind.h | 36 ++++++ 4 files changed, 176 insertions(+), 9 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); +} + + } diff --git a/src/bind/javabind-private.h b/src/bind/javabind-private.h index 18b041471..ce595fa85 100644 --- a/src/bind/javabind-private.h +++ b/src/bind/javabind-private.h @@ -7,7 +7,7 @@ * 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 diff --git a/src/bind/javabind.cpp b/src/bind/javabind.cpp index 36facee88..a25be353e 100644 --- a/src/bind/javabind.cpp +++ b/src/bind/javabind.cpp @@ -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 @@ -482,6 +482,25 @@ static void populateClassPath(const String &javaroot, } +static void stdOutWrite(jlong ptr, jint ch) +{ + JavaBinderyImpl *bind = (JavaBinderyImpl *)ptr; + bind->stdOut(ch); +} + +static void stdErrWrite(jlong ptr, jint ch) +{ + JavaBinderyImpl *bind = (JavaBinderyImpl *)ptr; + bind->stdErr(ch); +} + + +static JNINativeMethod scriptRunnerMethods[] = +{ +{ (char *)"stdOutWrite", (char *)"(JI)V", (void *)stdOutWrite }, +{ (char *)"stdErrWrite", (char *)"(JI)V", (void *)stdErrWrite }, +{ NULL, NULL, NULL } +}; bool JavaBinderyImpl::loadJVM() { @@ -510,7 +529,7 @@ bool JavaBinderyImpl::loadJVM() msg("Lib path is: '%s'", libpath.c_str()); JavaVMInitArgs vm_args; - JavaVMOption options[4]; + JavaVMOption options[2]; options[0].optionString = (char *)classpath.c_str(); options[1].optionString = (char *)libpath.c_str(); vm_args.version = JNI_VERSION_1_2; @@ -524,6 +543,11 @@ bool JavaBinderyImpl::loadJVM() return false; } + if (!registerNatives("org/inkscape/cmn/ScriptRunner", + scriptRunnerMethods)) + { + return false; + } return true; } @@ -551,6 +575,9 @@ bool JavaBinderyImpl::callStatic(int type, methodName.c_str(), signature.c_str()); return false; } + /** + * Assemble your parameters into a form usable by JNI + */ jvalue *jvals = new jvalue[params.size()]; for (unsigned int i=0 ; i