You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Ro...@t-systems.com on 2010/09/17 14:45:30 UTC

Batik and XmlHttpRequest / accessing Batik objects from javascript in svg

Hi all,

this is my first post to this mailing list and I’m new to Batik and SVG ☺

My situation is as follows: I got an existing intranet application that uses SVG for a complex graph/workfloweditor. Currently this is running fine in IE 6+ IE 8 using the Adobe SVG Viewer Plugin 3.0. As support for this plugin has ended quite some time ago, my task is to find a suitable replacement and I stumpled across Batik which I find to be very powerful indeed.

However, I got 2 main problems in getting my existing SVGs run in an Applet with JSVGCanvas:

1) The Adobe Plugin is currently embedded into an HTML page via the embed-tag. This enables all scripts in the SVGs to access all Html-Elements/Javascripts  from the OUTER HTML page and vice versa. That means I can define a Javascript object in a script block on the HTML and use it inside the Javascript of the SVG out of the box. Or for example I can get hold of a DIV-Element from the outer HTML page by calling something like

parent.document.getElementById(<divId>);

from within javscript inside SVG.

No discussion on whether this is a good design or concept – fact is, that the Javascript in the existing SVGs heavily relies on Javascript blocks and Elements of the surrounding HTML page. My question is: Is there a way to achieve the same effect when using an Applet with JSVGCanvas to display the SVGs? Probably not.

However, I could surpass this by calling a method from my Applet from inside the SVG's Javascript (as I can get hold of Elements of the HTML page from my Applet). The question is: How to access The JSVGCanvas or the Applet object from javascript within SVG? Is there a way, maybe via the Rhino engine?

2) A related problem is the current communication. Within the Adobe plugin, I can access the browsers XmlHttpRequest object and use it for ajax-like communication from within the SVG. This doesn't work within the Applet with JSVGCanvas. Isn't there something like a lightweight XmlHttpRequest object available in Batik? If not, would it be possible to call some Java code from within the SVGs Javascript to build some similar communication and how does this work? Would security settings deny this (can't use signed Applets) if I only would connect to the same server/codedbase? Ajax-like communication within the svg is awesome, it makes the graph/workfloweditor quite fast with the Adobe Plugin - this would be a point Batik could really benefit from when it comes to developer and customer acceptance!

Many thanks in advance for any helping comments on these problems.

Greetings,
Roland

RE: Batik and XmlHttpRequest / accessing Batik objects from javascript in svg

Posted by Martin Gainty <mg...@hotmail.com>.
agreed 
 
with XMLHttpRequest be aware that your response is returned from XMLHttpRequest object thru JS function 
identified by onreadystatechange handler attribute e.g.
 
//this assumes you have a valid initialized XMLHttpRequest object called xmlHttpRequest and the send was successful
xmlHttpReq.onreadystatechange= function() { 
//check the status of the XMLHttpRequest for complete transfer 
if(xmlHttpRequest.readyState==4) 
{
//now that the reponse is complete check HTTP status
  if (xmlHttpRequest.status==200) 
  {  
  //do normal response stuff here including populating batik dom
  }
}
}
 
http://www.devx.com/webdev/Article/33024/1954

Martin Gainty 
______________________________________________ 
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.



 



To: batik-users@xmlgraphics.apache.org
CC: batik-users@xmlgraphics.apache.org
Subject: Re: Batik and XmlHttpRequest / accessing Batik objects from javascript in svg
From: thomas.deweese@kodak.com
Date: Wed, 29 Sep 2010 06:27:31 -0400

Hi Roland, 

<Ro...@t-systems.com> wrote on 09/17/2010 08:45:30 AM:

> However, I got 2 main problems in getting my existing SVGs run in an
> Applet with JSVGCanvas:
> 
> 1) The Adobe Plugin is currently embedded into an HTML page via the 
> embed-tag. This enables all scripts in the SVGs to access all Html-
> Elements/Javascripts  from the OUTER HTML page and vice versa. That 
> means I can define a Javascript object in a script block on the HTML
> and use it inside the Javascript of the SVG out of the box. Or for 
> example I can get hold of a DIV-Element from the outer HTML page by 
> calling something like
> 
> parent.document.getElementById(<divId>);
> 
> from within javscript inside SVG.
> 
> No discussion on whether this is a good design or concept – fact is,
> that the Javascript in the existing SVGs heavily relies on 
> Javascript blocks and Elements of the surrounding HTML page. My 
> question is: Is there a way to achieve the same effect when using an
> Applet with JSVGCanvas to display the SVGs? Probably not.

        Correct, I don't believe this is possible.  It's only possible in 
Adobe because they can actually run their scripts in the Browsers script 
engine such an option is not AFAIK available for Java. 

> However, I could surpass this by calling a method from my Applet 
> from inside the SVG's Javascript (as I can get hold of Elements of 
> the HTML page from my Applet). The question is: How to access The 
> JSVGCanvas or the Applet object from javascript within SVG? Is there
> a way, maybe via the Rhino engine?

        You will need to a little bit of setup in Java first.  Basically 
you need to bind the Applet and or JSVGCanvas object to a global in 
the JavaScript interpreter.  You can do this by calling 
'getInterpreter(String lang)' (lang should probably be "text/ecmascript") 
on the BridgeContext associated with the JSVGCanvas.  This will give 
you the Interpreter that is being used.  On that object you can call 
'bindObject(String name, Object obj)'. 

        That will bind 'obj' to 'name' as a global in the script environment. 
>From their Rhino does a decent job of exposing Java methods into JavaScript. 

> 2) A related problem is the current communication. Within the Adobe 
> plugin, I can access the browsers XmlHttpRequest object and use it 
> for ajax-like communication from within the SVG. This doesn't work 
> within the Applet with JSVGCanvas. Isn't there something like a 
> lightweight XmlHttpRequest object available in Batik? 

        Check 'getURL', and parseXML, they are generally a decent work 
alike (not quite as flexible). 

> If not, would it be possible to call some Java code from within the SVGs 
> Javascript to build some similar communication and how does this 
> work? Would security settings deny this (can't use signed Applets) 
> if I only would connect to the same server/codedbase? Ajax-like 
> communication within the svg is awesome, it makes the graph/
> workfloweditor quite fast with the Adobe Plugin - this would be a 
> point Batik could really benefit from when it comes to developer and
> customer acceptance!

        As I said above you can expose your own Java methods fairly 
easily.  You are bound by the Applet sandbox (as it Batik) so you 
are restricted in the servers you can connect to (of course you 
should have similar restrictions with XmlHttpRequest). 

Good luck.
 		 	   		  

Re: Batik and XmlHttpRequest / accessing Batik objects from javascript in svg

Posted by jonathan wood <jo...@gmail.com>.
Hello Roland,

  I have not tried to reproduce your problem, the items below are wild
guesses...

- Change the method call order ...

  1. set always dynamic
  2. assign listener.
  3. load doc.

  This may not be the issue since your listener is called, but it may
also be fortunate timing?


-  I've found it most opportune to start my document manipulations
using the following listener override...

mysvgCanvas.addGVTTreeBuilderListener(new GVTTreeBuilderAdapter() {
            @Override
            public void gvtBuildCompleted(GVTTreeBuilderEvent e) {
              // do stuff with doc here
            }
}





On Thu, Oct 14, 2010 at 12:08 PM,  <Ro...@t-systems.com> wrote:
> 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 ());
>
> canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
>
>
>
>             // 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) {
>
>             ex.printStackTrace();
>
>         }
>
>     }
>
>
>
>     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,
>
> Roland
>
>
>
>
>
> -----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:
>
> <snippet/>
>
>> 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,
>
>  Helder
>
>
>
>
>
> [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
>
>

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


AW: Batik and XmlHttpRequest / accessing Batik objects from javascript in svg

Posted by Ro...@t-systems.com.
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 ());
canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);

            // 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) {
            ex.printStackTrace();
        }
    }

    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,

Roland





-----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:

<snippet/>

> 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,

 Helder





[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



Re: Batik and XmlHttpRequest / accessing Batik objects from javascript in svg

Posted by Helder Magalhães <he...@gmail.com>.
> 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:
<snippet/>
> 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,
 Helder


[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


AW: Batik and XmlHttpRequest / accessing Batik objects from javascript in svg

Posted by Ro...@t-systems.com.
Hi there,

I hust tried to bind my applet to the script interpreter. Unfortunetaly, this does not work as expectet. I found the bridgeContext of the JSVGCanvas to be protected. Thus I derived a simple class from JSVGCanvas like this:

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);
      }
}

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:

            canvas.addSVGDocumentLoaderListener(new SVGDocumentLoaderAdapter() {
                public void documentLoadingStarted(SVGDocumentLoaderEvent e) {

                }
                public void documentLoadingCompleted(SVGDocumentLoaderEvent e) {
                  System.out.println("### Document loaded");
                  canvas.bindObjectToInterpreter("text/ecmascript", "parentapplet", canvas);
                }
            });

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)
            at test.MySVGCanvas.bindObjectToInterpreter(MySVGCanvas.java:15)
            at test.IntercomApplet$5.documentLoadingCompleted(IntercomApplet.java:109)
            at org.apache.batik.swing.svg.SVGDocumentLoader$2.dispatch(SVGDocumentLoader.java:145)
            at org.apache.batik.util.EventDispatcher.dispatchEvent(EventDispatcher.java:103)
            at org.apache.batik.util.EventDispatcher.fireEvent(EventDispatcher.java:87)
            at org.apache.batik.util.EventDispatcher$1.run(EventDispatcher.java:46)
            at java.awt.event.InvocationEvent.dispatch(Unknown Source)
            at java.awt.EventQueue.dispatchEvent(Unknown Source)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
            at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
            at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
            at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
            at java.awt.EventDispatchThread.run(Unknown Source)
This is because the "document" filed in the interpreter is null. But why? The scripting and the svg run fine, so it should have a document set, shouldn't it? I'll next check if this is caused by canvas.setURI(..) - does this not cause the document-field in the interpreter to be set?

Any suggestions what I'm doing wrong here? I must admit that my knowledge about Batik itself is still very poor - I mainly adjusted SVGs and ecmascript to be runnable inside Batik until now and simply use JSVGCanvas inside an applet as a viewer on the webpage. All logic is inside the SVGs and ecmascript.

Many thanks again,
Roland

________________________________
Von: thomas.deweese@kodak.com [mailto:thomas.deweese@kodak.com]
Gesendet: Mittwoch, 29. September 2010 12:28
An: batik-users@xmlgraphics.apache.org
Cc: batik-users@xmlgraphics.apache.org
Betreff: Re: Batik and XmlHttpRequest / accessing Batik objects from javascript in svg

Hi Roland,

<Ro...@t-systems.com> wrote on 09/17/2010 08:45:30 AM:

> However, I got 2 main problems in getting my existing SVGs run in an
> Applet with JSVGCanvas:
>
> 1) The Adobe Plugin is currently embedded into an HTML page via the
> embed-tag. This enables all scripts in the SVGs to access all Html-
> Elements/Javascripts  from the OUTER HTML page and vice versa. That
> means I can define a Javascript object in a script block on the HTML
> and use it inside the Javascript of the SVG out of the box. Or for
> example I can get hold of a DIV-Element from the outer HTML page by
> calling something like
>
> parent.document.getElementById(<divId>);
>
> from within javscript inside SVG.
>
> No discussion on whether this is a good design or concept - fact is,
> that the Javascript in the existing SVGs heavily relies on
> Javascript blocks and Elements of the surrounding HTML page. My
> question is: Is there a way to achieve the same effect when using an
> Applet with JSVGCanvas to display the SVGs? Probably not.

        Correct, I don't believe this is possible.  It's only possible in
Adobe because they can actually run their scripts in the Browsers script
engine such an option is not AFAIK available for Java.

> However, I could surpass this by calling a method from my Applet
> from inside the SVG's Javascript (as I can get hold of Elements of
> the HTML page from my Applet). The question is: How to access The
> JSVGCanvas or the Applet object from javascript within SVG? Is there
> a way, maybe via the Rhino engine?

        You will need to a little bit of setup in Java first.  Basically
you need to bind the Applet and or JSVGCanvas object to a global in
the JavaScript interpreter.  You can do this by calling
'getInterpreter(String lang)' (lang should probably be "text/ecmascript")
on the BridgeContext associated with the JSVGCanvas.  This will give
you the Interpreter that is being used.  On that object you can call
'bindObject(String name, Object obj)'.

        That will bind 'obj' to 'name' as a global in the script environment.
>From their Rhino does a decent job of exposing Java methods into JavaScript.

> 2) A related problem is the current communication. Within the Adobe
> plugin, I can access the browsers XmlHttpRequest object and use it
> for ajax-like communication from within the SVG. This doesn't work
> within the Applet with JSVGCanvas. Isn't there something like a
> lightweight XmlHttpRequest object available in Batik?

        Check 'getURL', and parseXML, they are generally a decent work
alike (not quite as flexible).

> If not, would it be possible to call some Java code from within the SVGs
> Javascript to build some similar communication and how does this
> work? Would security settings deny this (can't use signed Applets)
> if I only would connect to the same server/codedbase? Ajax-like
> communication within the svg is awesome, it makes the graph/
> workfloweditor quite fast with the Adobe Plugin - this would be a
> point Batik could really benefit from when it comes to developer and
> customer acceptance!

        As I said above you can expose your own Java methods fairly
easily.  You are bound by the Applet sandbox (as it Batik) so you
are restricted in the servers you can connect to (of course you
should have similar restrictions with XmlHttpRequest).

Good luck.

Re: Batik and XmlHttpRequest / accessing Batik objects from javascript in svg

Posted by Helder Magalhães <he...@gmail.com>.
Hi Roland,


> Regarding communication, I already use getURL() now. However, there seems to
> be a bug that has already been mentioned here, but was confirmed as fixed.
> Yet it still (or again) seems to exist: I cannot use getURL() inside an
> Applet (works fine in squiggle)  without granting a right to create a
> Classloader via a java.policy file in the user home directory:
>
> Please have a look here:
> http://old.nabble.com/Batik-Applet-and-getURL-XMLHttpRequest--td18105942.html

Yes, the post leads to revision 677245 [1], where the fix (by Thomas)
was committed.


> Ok, while rereading the above post, I realized that this fix is not included
> in the current release version 1.7 I use. Is that correct?

Right, that commit was made after the 1.7 release.


> The fix is from
> 2008 in 1.8Alpha ?! It seems there hasn’t there been any further major
> release since that?

Right again. It's in the current trunk code and (unfortunately) there
was no 1.7.1 release. Currently things are moving on and I hope there
will be a 1.8 release (see bug 50045 [2]) in a not-so-far future. ;-)


> Hmm, that’s sad, because I managed to handle several
> problems now – but the requirement of a java.policy file on the user’s
> computer is a real showstopper. How stable are current SVN-Releases that
> include the fix? Is it possible to use them in a productional environment?

IMHO using the SVN checkout and/or a nightly build [3] is already
better than the 1.7 release as there were a few notable bugs (such as
this one) fixed since the release. But of course that not being
released yet means exactly that. So it is possible to use in
production *but* you are the one which should evaluate and assume the
risk.

Nevertheless, if you are not willing to take the risk, you may simply
download/checkout the 1.7 release code and simply patch your working
copy (taking a look at the modification made by Thomas [1], it should
be quite similar if not exactly the same). ;-)


> Thanks again,
>
> Roland

Hope this helps,
 Helder


[1] http://svn.apache.org/viewvc?view=revision&revision=677245
[2] https://issues.apache.org/bugzilla/show_bug.cgi?id=50045
[3] http://xmlgraphics.apache.org/batik/download.cgi#Current+development+snapshot

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


AW: Batik and XmlHttpRequest / accessing Batik objects from javascript in svg

Posted by Ro...@t-systems.com.
Hi Thomas,

thank you very much for your valuable hints. I'll check it out.

Regarding communication, I already use getURL() now. However, there seems to be a bug that has already been mentioned here, but was confirmed as fixed. Yet it still (or again) seems to exist: I cannot use getURL() inside an Applet (works fine in squiggle)  without granting a right to create a Classloader via a java.policy file in the user home directory:

Please have a look here:

http://old.nabble.com/Batik-Applet-and-getURL-XMLHttpRequest--td18105942.html

Ok, while rereading the above post, I realized that this fix is not included in the current release version 1.7 I use. Is that correct? The fix is from 2008 in 1.8Alpha ?! It seems there hasn't there been any further major release since that? Hmm, that's sad, because I managed to handle several problems now - but the requirement of a java.policy file on the user's computer is a real showstopper. How stable are current SVN-Releases that include the fix? Is it possible to use them in a productional environment?

Thanks again,
Roland




________________________________
Von: thomas.deweese@kodak.com [mailto:thomas.deweese@kodak.com]
Gesendet: Mittwoch, 29. September 2010 12:28
An: batik-users@xmlgraphics.apache.org
Cc: batik-users@xmlgraphics.apache.org
Betreff: Re: Batik and XmlHttpRequest / accessing Batik objects from javascript in svg

Hi Roland,

<Ro...@t-systems.com> wrote on 09/17/2010 08:45:30 AM:

> However, I got 2 main problems in getting my existing SVGs run in an
> Applet with JSVGCanvas:
>
> 1) The Adobe Plugin is currently embedded into an HTML page via the
> embed-tag. This enables all scripts in the SVGs to access all Html-
> Elements/Javascripts  from the OUTER HTML page and vice versa. That
> means I can define a Javascript object in a script block on the HTML
> and use it inside the Javascript of the SVG out of the box. Or for
> example I can get hold of a DIV-Element from the outer HTML page by
> calling something like
>
> parent.document.getElementById(<divId>);
>
> from within javscript inside SVG.
>
> No discussion on whether this is a good design or concept - fact is,
> that the Javascript in the existing SVGs heavily relies on
> Javascript blocks and Elements of the surrounding HTML page. My
> question is: Is there a way to achieve the same effect when using an
> Applet with JSVGCanvas to display the SVGs? Probably not.

        Correct, I don't believe this is possible.  It's only possible in
Adobe because they can actually run their scripts in the Browsers script
engine such an option is not AFAIK available for Java.

> However, I could surpass this by calling a method from my Applet
> from inside the SVG's Javascript (as I can get hold of Elements of
> the HTML page from my Applet). The question is: How to access The
> JSVGCanvas or the Applet object from javascript within SVG? Is there
> a way, maybe via the Rhino engine?

        You will need to a little bit of setup in Java first.  Basically
you need to bind the Applet and or JSVGCanvas object to a global in
the JavaScript interpreter.  You can do this by calling
'getInterpreter(String lang)' (lang should probably be "text/ecmascript")
on the BridgeContext associated with the JSVGCanvas.  This will give
you the Interpreter that is being used.  On that object you can call
'bindObject(String name, Object obj)'.

        That will bind 'obj' to 'name' as a global in the script environment.
>From their Rhino does a decent job of exposing Java methods into JavaScript.

> 2) A related problem is the current communication. Within the Adobe
> plugin, I can access the browsers XmlHttpRequest object and use it
> for ajax-like communication from within the SVG. This doesn't work
> within the Applet with JSVGCanvas. Isn't there something like a
> lightweight XmlHttpRequest object available in Batik?

        Check 'getURL', and parseXML, they are generally a decent work
alike (not quite as flexible).

> If not, would it be possible to call some Java code from within the SVGs
> Javascript to build some similar communication and how does this
> work? Would security settings deny this (can't use signed Applets)
> if I only would connect to the same server/codedbase? Ajax-like
> communication within the svg is awesome, it makes the graph/
> workfloweditor quite fast with the Adobe Plugin - this would be a
> point Batik could really benefit from when it comes to developer and
> customer acceptance!

        As I said above you can expose your own Java methods fairly
easily.  You are bound by the Applet sandbox (as it Batik) so you
are restricted in the servers you can connect to (of course you
should have similar restrictions with XmlHttpRequest).

Good luck.

Re: Batik and XmlHttpRequest / accessing Batik objects from javascript in svg

Posted by th...@kodak.com.
Hi Roland,

<Ro...@t-systems.com> wrote on 09/17/2010 08:45:30 AM:

> However, I got 2 main problems in getting my existing SVGs run in an
> Applet with JSVGCanvas:
> 
> 1) The Adobe Plugin is currently embedded into an HTML page via the 
> embed-tag. This enables all scripts in the SVGs to access all Html-
> Elements/Javascripts  from the OUTER HTML page and vice versa. That 
> means I can define a Javascript object in a script block on the HTML
> and use it inside the Javascript of the SVG out of the box. Or for 
> example I can get hold of a DIV-Element from the outer HTML page by 
> calling something like
> 
> parent.document.getElementById(<divId>);
> 
> from within javscript inside SVG.
> 
> No discussion on whether this is a good design or concept – fact is,
> that the Javascript in the existing SVGs heavily relies on 
> Javascript blocks and Elements of the surrounding HTML page. My 
> question is: Is there a way to achieve the same effect when using an
> Applet with JSVGCanvas to display the SVGs? Probably not.

        Correct, I don't believe this is possible.  It's only possible in
Adobe because they can actually run their scripts in the Browsers script
engine such an option is not AFAIK available for Java.

> However, I could surpass this by calling a method from my Applet 
> from inside the SVG's Javascript (as I can get hold of Elements of 
> the HTML page from my Applet). The question is: How to access The 
> JSVGCanvas or the Applet object from javascript within SVG? Is there
> a way, maybe via the Rhino engine?

        You will need to a little bit of setup in Java first.  Basically
you need to bind the Applet and or JSVGCanvas object to a global in
the JavaScript interpreter.  You can do this by calling 
'getInterpreter(String lang)' (lang should probably be "text/ecmascript")
on the BridgeContext associated with the JSVGCanvas.  This will give 
you the Interpreter that is being used.  On that object you can call
'bindObject(String name, Object obj)'.

        That will bind 'obj' to 'name' as a global in the script 
environment.
From their Rhino does a decent job of exposing Java methods into 
JavaScript.

> 2) A related problem is the current communication. Within the Adobe 
> plugin, I can access the browsers XmlHttpRequest object and use it 
> for ajax-like communication from within the SVG. This doesn't work 
> within the Applet with JSVGCanvas. Isn't there something like a 
> lightweight XmlHttpRequest object available in Batik?

        Check 'getURL', and parseXML, they are generally a decent work 
alike (not quite as flexible).

> If not, would it be possible to call some Java code from within the SVGs 

> Javascript to build some similar communication and how does this 
> work? Would security settings deny this (can't use signed Applets) 
> if I only would connect to the same server/codedbase? Ajax-like 
> communication within the svg is awesome, it makes the graph/
> workfloweditor quite fast with the Adobe Plugin - this would be a 
> point Batik could really benefit from when it comes to developer and
> customer acceptance!

        As I said above you can expose your own Java methods fairly 
easily.  You are bound by the Applet sandbox (as it Batik) so you
are restricted in the servers you can connect to (of course you
should have similar restrictions with XmlHttpRequest).

Good luck.