Hi there
I am trying to call an applet method from script within an svg file. From
looking around the web, and especially this forum, the basic idea to achieve
this is to:
- Extend JSVGCanvas to allow access to the BridgeContext.
- Obtain the javascript interpreter from the BridgeContext.
- Create a mapping between the applet and new custom type.
- Call the custom type (applet mapping) from within svg script.
Here's my applet code:
public class SVGScriptApplet extends Applet {
private static class MyCanvas extends JSVGCanvas {
public void setBridgeContext(SVGOMDocument doc){
bridgeContext = createBridgeContext(doc);
}
public BridgeContext getBridgeContext() {
return bridgeContext;
}
}
public void init() {
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
try{
MyCanvas canvas = new MyCanvas();
URL url = new URL(getCodeBase(), "3D.svg");
SVGOMDocument svgDoc =
(SVGOMDocument)f.createDocument(url.toString());
canvas.setBridgeContext(svgDoc);
// 'Unknown document' exception encountered if the following 2
lines are not included
GVTBuilder builder = new GVTBuilder();
builder.build(canvas.getBridgeContext(), svgDoc);
Interpreter i =
canvas.getBridgeContext().getInterpreter("text/ecmascript");
i.bindObject("appletHost", this);
canvas.setDocument(svgDoc);
}catch (Exception e){
System.out.println(e);
}
}
public void doSomething() {
System.out.println("Do something!");
}
}
This applet loads the 3D.svg that comes in samples directory of Batik 1.7
release (in order to keep things simple and easily reproducable).
I have updated the 3D.svg file to reference the applet and call
'doSomething()' method:
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
id="body" width="640" height="480" viewBox="0 0 640 480">
<script type="text/ecmascript"><![CDATA[
.....
// Global initialization
function startAnimation(evt) {
appletHost.doSomething();
.....
}
....
Unfortunately the interpreter does not seem to take note of the mapping of
type of name 'appletHost' with the applet, and I get the following
exception;
org.mozilla.javascript.WrappedException: Wrapped
org.mozilla.javascript.EcmaError: ReferenceError: "appletHost" is not
defined. (Inline <script> http://localhost:8080/FO5/applet/3D.svg:-1#107)
(Event attribute http://localhost:8080/FO5/applet/3D.svg:-1 onload#1)
at
org.apache.batik.script.rhino.BatikSecurityController.callWithDomain(Unknown
Source)
at org.mozilla.javascript.Interpreter.interpret(Unknown Source)
at org.mozilla.javascript.InterpretedFunction.call(Unknown Source)
at org.mozilla.javascript.Interpreter.interpretLoop(Unknown Source)
at script(Event attribute http://localhost:8080/FO5/applet/3D.svg:-1
onload:1)
at org.mozilla.javascript.Interpreter.interpret(Unknown Source)
at org.mozilla.javascript.InterpretedFunction.call(Unknown Source)
at org.apache.batik.script.rhino.BatikSecurityController$1.run(Unknown
Source)
at java.security.AccessController.doPrivileged(Native Method)
at
org.apache.batik.script.rhino.BatikSecurityController.callWithDomain(Unknown
Source)
at org.mozilla.javascript.Interpreter.interpret(Unknown Source)
at org.mozilla.javascript.InterpretedFunction.call(Unknown Source)
at org.mozilla.javascript.ContextFactory.doTopCall(Unknown Source)
at org.mozilla.javascript.ScriptRuntime.doTopCall(Unknown Source)
at org.mozilla.javascript.InterpretedFunction.exec(Unknown Source)
at org.mozilla.javascript.Context.evaluateReader(Unknown Source)
at org.apache.batik.script.rhino.RhinoInterpreter$2.run(Unknown Source)
at org.mozilla.javascript.Context.call(Unknown Source)
Not really sure what I am doing wrong. Is there something I am missing?
Any help greatly appreciated
Pat
--
View this message in context:
http://www.nabble.com/Problem-calling-applet-method-from-script-in-svg-file-tf4378175.html#a12479719
Sent from the Batik - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]