summaryrefslogtreecommitdiffstats
path: root/src/bind
diff options
context:
space:
mode:
authorBob Jamison <ishmalius@gmail.com>2008-03-10 04:48:47 +0000
committerishmal <ishmal@users.sourceforge.net>2008-03-10 04:48:47 +0000
commit8d835e32a1f64150a61a0a37d42bb7f5bf1155bc (patch)
tree7e8199cda7525c41c454c8600c2a267247eb3303 /src/bind
parentUpdate header (diff)
downloadinkscape-8d835e32a1f64150a61a0a37d42bb7f5bf1155bc.tar.gz
inkscape-8d835e32a1f64150a61a0a37d42bb7f5bf1155bc.zip
Redirect stdout and stderr from scripts
(bzr r5022)
Diffstat (limited to 'src/bind')
-rw-r--r--src/bind/java/org/inkscape/cmn/ScriptRunner.java114
-rw-r--r--src/bind/javabind-private.h2
-rw-r--r--src/bind/javabind.cpp33
-rw-r--r--src/bind/javabind.h36
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<params.size() ; i++)
{
diff --git a/src/bind/javabind.h b/src/bind/javabind.h
index 24db22e20..f01627a15 100644
--- a/src/bind/javabind.h
+++ b/src/bind/javabind.h
@@ -260,12 +260,48 @@ public:
{
return false;
}
+
+ virtual String stdOutGet()
+ {
+ return stdOutBuf;
+ }
+
+ virtual void stdOutClear()
+ {
+ stdOutBuf.clear();
+ }
+
+ virtual String stdErrGet()
+ {
+ return stdErrBuf;
+ }
+
+ virtual void stdErrClear()
+ {
+ stdErrBuf.clear();
+ }
+
+ virtual void stdOut(int ch)
+ {
+ stdOutBuf.push_back((char)ch);
+ }
+
+ virtual void stdErr(int ch)
+ {
+ stdErrBuf.push_back((char)ch);
+ }
/**
* Return a singleton instance of this bindery
*/
static JavaBindery *getInstance();
+
+protected:
+
+
+ String stdOutBuf;
+ String stdErrBuf;
};