xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <Roland.Webe...@t-systems.com>
Subject AW: Batik and XmlHttpRequest / accessing Batik objects from javascript in svg
Date Thu, 14 Oct 2010 16:08:04 GMT
Hi again,

ok, maybe i should describe the problem again :-)

What I'm doing is to use JSVGCanvas inside an applet as an svg-viewer inside the browser.
In the applet's init()-method I create a new canvas (MySVGCanvas is as already explained simply
derived from JSVGCanvas in order to have access to the protected contextBridge-field of the
canvas, see Listing 2) and set the document of the canvas via canvas.setURI().

So what I try to achieve is that after the SVG is loaded into the canvas, the canvas object
itself (the java object) should be bound to the script-interpreter of the document (just as
Thomas Deweese pointed out earlier in this mail-thread). This should allow me to access the
canvas-object from inside the ecmascript of the SVG as a global variable named mycanvas. This
is done by calling my BindToInterpreter-method of class MySVGCanvas inside the DocumentLoaderListener.
Unfortunately, this throws an exception, because the internal document-field of the interpreter
is not set, it is null (Exception was posted before). The question is: What am I doing wrong,
why does the interpreter not have a document set (the svg is running perfectly inside the
canvas, it is displayed correctly and the ecmascript is working also). I hope this disruption
of the problem is more useful. All I want to do is bind a java-object to the interpreter in
order to access it from inside ecmascript.

Listing 1: The Applet

public class IntercomApplet extends JApplet {

    protected MySVGCanvas canvas;
    protected JSObject jso;

    public void init() {
        // Create a new JSVGCanvas.
        canvas = new MySVGCanvas();
        getContentPane().add("Center", canvas);

        try {
            // Set the document by using an URI
canvas.setURI (getWorkflowURL ());

            // Set the JSVGCanvas listeners.
            canvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
                public void documentLoadingStarted(SVGDocumentLoaderEvent e) {

                public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
                  System.out.println("### Document loaded");
                  // try to bind the canvas object und name myycanvas to the script interpreter
                  canvas.bindObjectToInterpreter("text/ecmascript", "mycanvas", canvas);

        } catch (Exception ex) {

    public String getWorkflowURL (){
      String codebase = getCodeBase().toString();
      String myurl = codebase + getParameter ("svgurl");
      return myurl;


Listing 2: MyCanvas

public class MySVGCanvas extends JSVGCanvas {

      public void bindObjectToInterpreter(String language, String name, Object obj){
            System.out.println("### Binding Object " + obj.toString() + " to interpreter.");
            Interpreter interpreter = this.bridgeContext.getInterpreter(language);
            interpreter.bindObject(name, obj);


Many thanks,


-----Ursprüngliche Nachricht-----
Von: Helder Magalhães [mailto:helder.magalhaes@gmail.com]
Gesendet: Freitag, 8. Oktober 2010 07:20
An: batik-users@xmlgraphics.apache.org
Betreff: Re: Batik and XmlHttpRequest / accessing Batik objects from javascript in svg

> Hi there,

Hi Roland,

> The difficulty is WHEN to bind the object. First try was to simply call it

> in the init()-Method of the Applet after using canvas.setURI(...). However,

> this method is async, so my bind-method will not yet find even a

> bridgeContext (it is null). Therefore, I thought it would be best to bind

> the object AFTER the document is loaded (via setURI) by using something

> like:


> Currently, this should bind the canvas (later it should be the applet) to

> the name "parentapplet" in the interpreter and I should be able to use this

> name as a global variable in my ecmascript, e.g. like alert(parentapplet);

> Correct?  However, an exception is thrown when binding the object:


> ### Document loaded


> ### Binding Object

> test.MySVGCanvas[,0,36,1000x964,alignmentX=0.0,alignmentY=0.0,border=,flags=288,maximumSize=,minimumSize=java.awt.Dimension[width=100,height=100],preferredSize=java.awt.Dimension[width=200,height=200]]

> to interpreter.


> java.lang.RuntimeException: Unknown document


>             at

> org.apache.batik.bridge.BridgeContext.getInterpreter(BridgeContext.java:558)


> Any suggestions what I'm doing wrong here?

I haven't quite understood your "document onload" mechanism but there

are a few good examples using "addSVGLoadEventDispatcherListener" and

"addEventListener" with "SVGLoad" [1]. You may also try running that

once the document is already "executing", using a runnable thread [2].

> Many thanks again,


> Roland

Hope this helps,


[1] http://xmlgraphics.apache.org/batik/using/scripting/java.html#Swing

[2] http://xmlgraphics.apache.org/batik/using/scripting/java.html#Threads


To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org

For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org

View raw message