hillion 02/02/11 23:08:17
Modified: sources/org/apache/batik/bridge BridgeEventSupport.java
ScriptingEnvironment.java
sources/org/apache/batik/script/rhino RhinoInterpreter.java
Added: sources/org/apache/batik/script JavaFunction.java
sources/org/apache/batik/script/rhino RhinoFunction.java
Log:
'setTimeout' now works with optional parameters.
Revision Changes Path
1.21 +8 -21
xml-batik/sources/org/apache/batik/bridge/BridgeEventSupport.java
Index: BridgeEventSupport.java
===================================================================
RCS file:
/home/cvs/xml-batik/sources/org/apache/batik/bridge/BridgeEventSupport.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- BridgeEventSupport.java 11 Feb 2002 13:14:29 -0000 1.20
+++ BridgeEventSupport.java 12 Feb 2002 07:08:17 -0000 1.21
@@ -56,7 +56,7 @@
* on the GVT root to propagate GVT events to the DOM.
* @author <a href="mailto:[EMAIL PROTECTED]>Christophe Jolif</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Stephane Hillion</a>
- * @version $Id: BridgeEventSupport.java,v 1.20 2002/02/11 13:14:29 hillion Exp $
+ * @version $Id: BridgeEventSupport.java,v 1.21 2002/02/12 07:08:17 hillion Exp $
*/
class BridgeEventSupport implements SVGConstants {
private static final String[] EVENT_ATTRIBUTES_GRAPHICS = {
@@ -315,29 +315,18 @@
final ScriptingEnvironment se = ctx.getUpdateManager().
getScriptingEnvironment();
- // add a function definition 'alert'
- final Interpreter inter =
- ctx.getInterpreterPool().getInterpreter(doc, "text/ecmascript");
- if (inter != null) {
- try {
- javax.swing.JOptionPane pane = new javax.swing.JOptionPane();
- inter.bindObject("pane", pane);
- inter.evaluate("function alert(msg) { pane.showMessageDialog(null,
msg); }");
-
- inter.bindObject("scriptEnv", se);
- inter.evaluate("function setTimeout(s, t) {
scriptEnv.pauseScript(t); scriptEnv.runScript(s, 'text/ecmascript', null); }");
- } catch (Exception ex) {
- // nothing to do
- ex.printStackTrace();
- }
- }
-
+ String lang = null;
for (int i = 0; i < list.getLength(); i++) {
language = (selement = (Element)list.item(i)).
getAttribute("type");
final Interpreter interpret =
ctx.getInterpreterPool().getInterpreter(doc, language);
if (interpret != null) {
+ if (language != lang) {
+ se.setEnvironment(interpret, language);
+ }
+ lang = language;
+
final StringBuffer script = new StringBuffer();
for (Node n = selement.getFirstChild(); n != null;
n = n.getNextSibling()) {
@@ -503,8 +492,6 @@
}
public static class ScriptCaller implements EventListener {
- private static String EVENT_NAME = "evt";
-
private String script = null;
private BridgeContext context;
private String language;
@@ -520,7 +507,7 @@
public void handleEvent(Event evt) {
ScriptingEnvironment se =
context.getUpdateManager().getScriptingEnvironment();
- se.runScript(script, language, evt);
+ se.runEventHandler(script, evt, language);
}
}
}
1.2 +154 -28
xml-batik/sources/org/apache/batik/bridge/ScriptingEnvironment.java
Index: ScriptingEnvironment.java
===================================================================
RCS file:
/home/cvs/xml-batik/sources/org/apache/batik/bridge/ScriptingEnvironment.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ScriptingEnvironment.java 11 Feb 2002 13:14:29 -0000 1.1
+++ ScriptingEnvironment.java 12 Feb 2002 07:08:17 -0000 1.2
@@ -16,6 +16,7 @@
import org.apache.batik.script.Interpreter;
import org.apache.batik.script.InterpreterException;
import org.apache.batik.script.InterpreterPool;
+import org.apache.batik.script.JavaFunction;
import org.apache.batik.util.Lock;
import org.apache.batik.util.RunnableQueue;
@@ -27,11 +28,12 @@
* This class contains the informations needed by the SVG scripting.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Stephane Hillion</a>
- * @version $Id: ScriptingEnvironment.java,v 1.1 2002/02/11 13:14:29 hillion Exp $
+ * @version $Id: ScriptingEnvironment.java,v 1.2 2002/02/12 07:08:17 hillion Exp $
*/
public class ScriptingEnvironment {
private final static String EVENT_NAME = "evt";
+ private final static String ARG_NAME = "arg__";
/**
* The scripting lock.
@@ -54,11 +56,6 @@
protected RunnableQueue updateRunnableQueue;
/**
- * The active scripting threads.
- */
- protected List scripts = Collections.synchronizedList(new LinkedList());
-
- /**
* Whether the scripts must be suspended.
*/
protected volatile boolean suspended;
@@ -69,6 +66,11 @@
protected Object suspendLock = new Object();
/**
+ * The alert function.
+ */
+ protected JavaFunction alertFunction;
+
+ /**
* Creates a new ScriptingEnvironment.
* @param um The update manager.
*/
@@ -80,10 +82,60 @@
}
/**
- * Runs a script.
+ * Sets the environment of the given interpreter.
+ */
+ public void setEnvironment(Interpreter interp, String lang) {
+ interp.bindObject("alert", createAlertFunction());
+ interp.bindObject("setTimeout", createSetTimeoutFunction(lang));
+ }
+
+ private JavaFunction createAlertFunction() {
+ if (alertFunction == null) {
+ alertFunction = new JavaFunction() {
+ public Class[] getParameterTypes() {
+ return new Class[] { String.class };
+ }
+ public Object call(Object[] arguments) {
+ javax.swing.JOptionPane.showMessageDialog
+ (null, (String)arguments[0]);
+ return null;
+ }
+ };
+ }
+ return alertFunction;
+ }
+
+
+ private final static Class[] parameters = { String.class, Long.TYPE };
+ private JavaFunction createSetTimeoutFunction(final String lang) {
+ return new JavaFunction() {
+ public Class[] getParameterTypes() {
+ return parameters;
+ }
+ public Object call(Object[] args) {
+ pauseScript(((Long)args[1]).longValue());
+ Object[] fargs = new Object[args.length - 2];
+ for (int i = 0; i < fargs.length; i++) {
+ fargs[i] = args[i + 2];
+ }
+ runFunction((String)args[0], fargs, lang);
+ return null;
+ }
+ };
+ }
+
+ /**
+ * Runs an event handler.
*/
- public void runScript(String script, String lang, Event evt) {
- new ScriptingThread(script, lang, evt).start();
+ public void runEventHandler(String script, Event evt, String lang) {
+ new EventHandlerThread(script, evt, lang).start();
+ }
+
+ /**
+ * Runs a function.
+ */
+ public void runFunction(String function, Object[] args, String lang) {
+ new FunctionCallThread(function, args, lang).start();
}
/**
@@ -170,51 +222,125 @@
}
/**
- * To run a script.
+ * To run a piece of script.
*/
- protected class ScriptingThread extends Thread {
- protected String script;
- protected Event event;
+ protected abstract class ScriptingThread extends Thread {
protected UserAgent userAgent;
protected Interpreter interpreter;
- public ScriptingThread(String script, String lang, Event evt) {
- this.script = script;
- event = evt;
+
+ /**
+ * Creates a new scripting thread.
+ */
+ public ScriptingThread(String lang) {
BridgeContext bc = updateManager.getBridgeContext();
userAgent = bc.getUserAgent();
Document doc = updateManager.getDocument();
interpreter = bc.getInterpreterPool().getInterpreter(doc, lang);
-
if (interpreter == null) {
if (userAgent != null) {
- userAgent.displayError(new Exception("unknow language: "+
- lang));
+ userAgent.displayError
+ (new Exception("unknow language: " + lang));
}
}
}
+
+ /**
+ * The main method.
+ */
public void run() {
if (interpreter != null) {
beginScript();
- scripts.add(this);
-
- if (event != null) {
- interpreter.bindObject(EVENT_NAME, event);
- }
+
try {
- interpreter.evaluate(script);
+ interpreter.evaluate(getScript());
} catch (InterpreterException ie) {
Exception ex = ie.getException();
if (ex instanceof StopScriptException) {
- scripts.remove(this);
return;
}
if (userAgent != null) {
userAgent.displayError((ex != null) ? ex : ie);
}
+ } finally {
+ try {
+ endScript();
+ } catch (StopScriptException e) {
+ }
}
- scripts.remove(this);
- endScript();
}
+ }
+
+ /**
+ * Returns the script to execute.
+ */
+ protected abstract String getScript();
+
+ }
+
+ /**
+ * To run a function.
+ */
+ protected class FunctionCallThread extends ScriptingThread {
+ protected String function;
+ protected Object[] arguments;
+
+ /**
+ * Creates a new FunctionCallThread.
+ */
+ public FunctionCallThread(String fname, Object[] args, String lang) {
+ super(lang);
+ function = fname;
+ arguments = args;
+ }
+
+ /**
+ * Returns the script to execute.
+ */
+ protected String getScript() {
+ if (function.endsWith("()")) {
+ function = function.substring(0, function.length() - 2);
+ }
+ StringBuffer sb = new StringBuffer(function);
+
+ sb.append("(");
+ if (arguments.length > 0) {
+ String s = ARG_NAME + 0;
+ sb.append(s);
+ interpreter.bindObject(s, arguments[0]);
+ for (int i = 1; i < arguments.length; i++) {
+ s = ARG_NAME + i;
+ sb.append(",");
+ sb.append(s);
+ interpreter.bindObject(s, arguments[i]);
+ }
+ }
+ sb.append(")");
+ return sb.toString();
+ }
+ }
+
+ /**
+ * To run an event handler.
+ */
+ protected class EventHandlerThread extends ScriptingThread {
+ protected String script;
+ protected Event event;
+
+ /**
+ * Creates a new EventHandlerThread.
+ */
+ public EventHandlerThread(String script, Event evt, String lang) {
+ super(lang);
+ this.script = script;
+ event = evt;
+ }
+
+ /**
+ * Returns the script to execute.
+ */
+ protected String getScript() {
+ interpreter.bindObject(EVENT_NAME, event);
+ return script;
}
}
1.1 xml-batik/sources/org/apache/batik/script/JavaFunction.java
Index: JavaFunction.java
===================================================================
/*****************************************************************************
* Copyright (C) The Apache Software Foundation. All rights reserved. *
* ------------------------------------------------------------------------- *
* This software is published under the terms of the Apache Software License *
* version 1.1, a copy of which has been included with this distribution in *
* the LICENSE file. *
*****************************************************************************/
package org.apache.batik.script;
/**
* This interface represents an object which is transformed to a
* function when binded to an interpreter.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Stephane Hillion</a>
* @version $Id: JavaFunction.java,v 1.1 2002/02/12 07:08:17 hillion Exp $
*/
public interface JavaFunction {
/**
* Returns the know parameter types.
*/
Class[] getParameterTypes();
/**
* Called by the interpreter. Arguments are casted to the types
* returned by <code>getParameterTypes()</code>
*/
Object call(Object[] arguments);
}
1.11 +16 -3
xml-batik/sources/org/apache/batik/script/rhino/RhinoInterpreter.java
Index: RhinoInterpreter.java
===================================================================
RCS file:
/home/cvs/xml-batik/sources/org/apache/batik/script/rhino/RhinoInterpreter.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- RhinoInterpreter.java 11 Feb 2002 13:14:29 -0000 1.10
+++ RhinoInterpreter.java 12 Feb 2002 07:08:17 -0000 1.11
@@ -20,6 +20,7 @@
import org.apache.batik.script.Interpreter;
import org.apache.batik.script.InterpreterException;
+import org.apache.batik.script.JavaFunction;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
@@ -36,7 +37,7 @@
* A simple implementation of <code>Interpreter</code> interface to use
* Rhino ECMAScript interpreter.
* @author <a href="mailto:[EMAIL PROTECTED]">Christophe Jolif</a>
- * @version $Id: RhinoInterpreter.java,v 1.10 2002/02/11 13:14:29 hillion Exp $
+ * @version $Id: RhinoInterpreter.java,v 1.11 2002/02/12 07:08:17 hillion Exp $
*/
public class RhinoInterpreter implements Interpreter {
private static String[] TO_BE_IMPORTED = {
@@ -231,8 +232,20 @@
* @param object the Java object
*/
public void bindObject(String name, Object object) {
- Scriptable jsObject = Context.toObject(object, globalObject);
- globalObject.put(name, globalObject, jsObject);
+ Context ctx = Context.enter();
+ ctx.setWrapHandler(wrapHandler);
+ try {
+ if (object instanceof JavaFunction) {
+ JavaFunction jf = (JavaFunction)object;
+ globalObject.put(name, globalObject,
+ new RhinoFunction(jf, globalObject));
+ return;
+ }
+ Scriptable jsObject = Context.toObject(object, globalObject);
+ globalObject.put(name, globalObject, jsObject);
+ } finally {
+ Context.exit();
+ }
}
/**
1.1
xml-batik/sources/org/apache/batik/script/rhino/RhinoFunction.java
Index: RhinoFunction.java
===================================================================
/*****************************************************************************
* Copyright (C) The Apache Software Foundation. All rights reserved. *
* ------------------------------------------------------------------------- *
* This software is published under the terms of the Apache Software License *
* version 1.1, a copy of which has been included with this distribution in *
* the LICENSE file. *
*****************************************************************************/
package org.apache.batik.script.rhino;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.JavaScriptException;
import org.mozilla.javascript.NativeFunction;
import org.mozilla.javascript.NativeJavaObject;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.Undefined;
import org.mozilla.javascript.WrappedException;
import org.apache.batik.script.JavaFunction;
/**
* This class is used to call a JavaFunction.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Stephane Hillion</a>
* @version $Id: RhinoFunction.java,v 1.1 2002/02/12 07:08:17 hillion Exp $
*/
public class RhinoFunction extends NativeFunction {
/**
* The JavaFunction to call.
*/
protected JavaFunction javaFunction;
/**
* The parameter types.
*/
protected Class[] paramTypes;
/**
* The global object.
*/
protected ScriptableObject globalObject;
/**
* Creates a new RhinoFunction.
*/
public RhinoFunction(JavaFunction jf,
ScriptableObject glob) {
javaFunction = jf;
paramTypes = jf.getParameterTypes();
globalObject = glob;
}
/**
* Called by the interpreter.
*/
public Object call(Context cx,
Scriptable scope,
Scriptable thisObj,
Object[] args) throws JavaScriptException {
Object[] jargs = new Object[args.length];
for (int i = 0; i < paramTypes.length; i++) {
args[i] = NativeJavaObject.coerceType(paramTypes[i],
args[i]);
}
Object result = javaFunction.call(args);
if (result == null) {
return Undefined.instance;
} else {
return Context.toObject(result, globalObject);
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]