xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rana30 <indraneel.bis...@gmail.com>
Subject Batik can not run the ecmascript
Date Tue, 10 Nov 2009 15:11:59 GMT

1. Create a Document from a SVG file ( say WithScript.svg ) which contains
the green box.
2. Add this Document in the SVGCanvas.
3. Add a EventListener green box.
4. Onclick event of the green box add a yellow box and ecmascript in the
existing document.
                      

When user clicks on the green box, the yellow box would be added in the
existing SVG document. In this activity the ecmascript also gets added in
the existing SVG document. The ecmascript only change the border color of
the green box when yellow box will be clicked.

The ecmascript: 
 
<script type="text/ecmascript">
//<![CDATA[
				
function Init()
{
    rect = document.getElementById("MyBox");
    rect.setAttribute("stroke", "red");
}
			
// ]]>
</script>
	

But when I clicked on the yellow box the following error occurred.
 
 org.mozilla.javascript.EcmaError: ReferenceError: "Init" is not defined.
(Event attribute file://localhost/C:/13060ESPE07/docWithoutScript.svg:0
onclick#1)
	at
org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3557)
	at
org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3535)
	at
org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3620)
	at
org.mozilla.javascript.ScriptRuntime.getNameFunctionAndThis(ScriptRuntime.java:2085)
	at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:95)
	at org.mozilla.javascript.gen.c2._c0(Event attribute
file://localhost/C:/13060ESPE07/docWithoutScript.svg:0 onclick:1)
	at org.mozilla.javascript.gen.c2.call(Event attribute
file://localhost/C:/13060ESPE07/docWithoutScript.svg:0 onclick)
	at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:401)
	at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3003)
	at org.mozilla.javascript.gen.c2.call(Event attribute
file://localhost/C:/13060ESPE07/docWithoutScript.svg:0 onclick)
	at org.mozilla.javascript.gen.c2.exec(Event attribute
file://localhost/C:/13060ESPE07/docWithoutScript.svg:0 onclick)
	at org.mozilla.javascript.Context.evaluateReader(Context.java:1119)
	at
org.apache.batik.script.rhino.RhinoInterpreter$2.run(RhinoInterpreter.java:261)
	at org.mozilla.javascript.Context.call(Context.java:499)
	at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:511)
	at
org.apache.batik.script.rhino.RhinoInterpreter.evaluate(RhinoInterpreter.java:271)
	at
org.apache.batik.bridge.ScriptingEnvironment.runEventHandler(ScriptingEnvironment.java:400)
	at
org.apache.batik.bridge.ScriptingEnvironment$ScriptingEventListener.handleEvent(ScriptingEnvironment.java:1325)
	at
org.apache.batik.dom.events.EventSupport.fireEventListeners(EventSupport.java:324)
	at
org.apache.batik.dom.events.EventSupport.fireEventListeners(EventSupport.java:366)
	at
org.apache.batik.dom.events.EventSupport.dispatchEvent(EventSupport.java:276)
	at org.apache.batik.dom.AbstractNode.dispatchEvent(AbstractNode.java:1014)
	at
org.apache.batik.bridge.BridgeEventSupport$Listener.dispatchMouseEvent(BridgeEventSupport.java:405)
	at
org.apache.batik.bridge.BridgeEventSupport$Listener.dispatchMouseEvent(BridgeEventSupport.java:341)
	at
org.apache.batik.bridge.BridgeEventSupport$Listener.mouseClicked(BridgeEventSupport.java:249)
	at
org.apache.batik.gvt.event.AbstractAWTEventDispatcher.processMouseEvent(AbstractAWTEventDispatcher.java:602)
	at
org.apache.batik.gvt.event.AbstractAWTEventDispatcher.dispatchMouseEvent(AbstractAWTEventDispatcher.java:545)
	at
org.apache.batik.gvt.event.AbstractAWTEventDispatcher.dispatchEvent(AbstractAWTEventDispatcher.java:387)
	at
org.apache.batik.gvt.event.AbstractAWTEventDispatcher.mouseClicked(AbstractAWTEventDispatcher.java:201)
	at
org.apache.batik.swing.svg.AbstractJSVGComponent$17.run(AbstractJSVGComponent.java:2107)
	at org.apache.batik.util.RunnableQueue.run(RunnableQueue.java:237)
	at java.lang.Thread.run(Thread.java:595)


The ecmascript is not running .

Following is the Java code: 

public class SVGApplication {

    public static void main(String[] args) {
        new SVGApplication();
    }

    JFrame frame;
    JSVGScrollPane mySVGScrollPane;
    SVGCanvas canvas;
    Document document;
    Window window;

    public SVGApplication() {
    	
        final Semaphore docUpdateSemaphore =
        	new Semaphore(1);
        
        frame = new JFrame();
        canvas = new SVGCanvas();
        // Forces the canvas to always be dynamic even if the current
        // document does not contain scripting or animation.
        canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
        canvas.addSVGLoadEventDispatcherListener
            (new SVGLoadEventDispatcherAdapter() {
                    public void svgLoadEventDispatchStarted
                        (SVGLoadEventDispatcherEvent e) {
                        // At this time the document is available...
                        document = canvas.getSVGDocument();
                        // ...and the window object too.
                        UpdateManager um = canvas.getUpdateManager();
                        
                        window = um.
                            getScriptingEnvironment().createWindow();
                        // Registers the listeners on the document
                        // just before the SVGLoad event is
                        // dispatched.
                        registerListeners();
                        // It is time to pack the frame.
                        frame.pack();
                    }
                });
        
        mySVGScrollPane = new JSVGScrollPane(canvas);
        mySVGScrollPane.setScrollbarsAlwaysVisible(false);
		mySVGScrollPane.addComponentListener(new ComponentAdapter(){
			public void componentResized(ComponentEvent e)
			{									
				mySVGScrollPane.reset();
			}
		});
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
        frame.addWindowListener(new WindowAdapter() {
                public void windowOpened(WindowEvent e) {
                    // The canvas is ready to load the base document
                    // now, from the AWT thread.
                   
canvas.setURI("file://localhost/C:/13060ESPE07/docWithoutScript.svg");
                }
            });

        frame.getContentPane().add(mySVGScrollPane);
        frame.setSize(800, 600);
        frame.show();
    }

    public void registerListeners() {
        // Gets an element from the loaded document.
        Element elt = document.getElementById("MyBox");
        EventTarget t = (EventTarget)elt;

        // Adds a 'onload' listener
        t.addEventListener("SVGLoad", new OnLoadAction(), false);

        // Adds a 'onclick' listener
        t.addEventListener("click", new OnClickAction(), false);
    }

    public class OnLoadAction implements EventListener {
        public void handleEvent(Event evt) {
            // Perform some actions here...
            
            // ...for example start an animation loop:
            window.setInterval(new Animation(), 50);
        }
    }

    public class OnClickAction implements EventListener {
        public void handleEvent(Event evt) {
            // Perform some actions here...
        	System.out.println("I am clicked");

            // ...for example schedule an action for later:
            window.setTimeout(new DelayedTask(), 500);
        }
    }

    public class Animation implements Runnable {
        public void run() {
            // Insert animation code here...
        }
    }

    public class DelayedTask implements Runnable {
        public void run() {
            // Perform some actions here...
        	
        	// Add the svg element which contain the ecmascript script
    		try {
    			
    			ImageIcon aIcon = new
ImageIcon("C:\\13060ESPE07\\docWithScript.svg");
    			
    			ViewLocation location = new ViewLocation();
    			location.setLocation(10,10);
    			Element e = aIcon.moveToLocation( location );
    			
    			Node myN = document.importNode( e, true );
    			
    			SVGOMElement svgRoot =
(SVGOMElement)((SVGDocument)document).getRootElement();
    			svgRoot.appendChild(myN);
    			
    			
    		} catch (URISyntaxException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}

            // ...for example displays an alert dialog:
            window.alert("Delayed Action invoked!");
            
            SVGDataWriter writer = new SVGDataWriter();
            try {
            	// write the new svg doc in the Output.svg file
				writer.writeSVGDoc((SVGDocument)document,
"C:\\13060ESPE07\\Output.svg");
			} catch (EMException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
    }
}



And the docWithoutScript.svg is :

<?xml version="1.0" encoding="UTF-8"?>
<svg width="584" xmlns="http://www.w3.org/2000/svg" height="299"
    viewBox="0 0 584.0 299.0" xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs/> 
			<g>
				<rect id="MyBox" x="5.0" y="80.0" fill-opacity="1.0" fill="green"
	                width="89.0"
	                height="18.0" stroke="black" stroke-opacity="1.0"
	                stroke-width="2.0"/>
			</g>
</svg>


And the docWithScript.svg.svg is :

<?xml version="1.0" encoding="UTF-8"?>
<svg width="584" xmlns="http://www.w3.org/2000/svg" height="299"
    viewBox="0 0 584.0 299.0" xmlns:xlink="http://www.w3.org/1999/xlink">
    <defs/>   
		
			<g onclick="Init( evt )">
			<script type="text/ecmascript">
				//<![CDATA[
				
				function Init()
				{
					rect = document.getElementById("MyBox");
					rect.setAttribute("stroke", "red");
				}
			
			// ]]></script>
			<rect id="MyBox1" x="5.0" y="10.0" fill-opacity="1.0" fill="yellow"
                width="89.0"
                height="18.0" stroke="black" stroke-opacity="1.0"
                stroke-width="2.0"/>
			</g>
</svg>


But if I create a Document from a SVG file which contain the green box,
yellow box and ecmascript, the script is running fine.  

Following is the code for that:

public class SVGApplication {

    public static void main(String[] args) {
        new SVGApplication();
    }

    JFrame frame;
    JSVGScrollPane mySVGScrollPane;
    SVGCanvas canvas;
    Document document;
    Window window;

    public SVGApplication() {
    	
        final Semaphore docUpdateSemaphore =
        	new Semaphore(1);
        
        frame = new JFrame();
        canvas = new SVGCanvas();
        // Forces the canvas to always be dynamic even if the current
        // document does not contain scripting or animation.
        canvas.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
        canvas.addSVGLoadEventDispatcherListener
            (new SVGLoadEventDispatcherAdapter() {
                    public void svgLoadEventDispatchStarted
                        (SVGLoadEventDispatcherEvent e) {
                        // At this time the document is available...
                        document = canvas.getSVGDocument();
                        // ...and the window object too.
                        UpdateManager um = canvas.getUpdateManager();
                        
                        window = um.
                            getScriptingEnvironment().createWindow();
                        // Registers the listeners on the document
                        // just before the SVGLoad event is
                        // dispatched.
//                        registerListeners();
                        // It is time to pack the frame.
                        frame.pack();
                    }
                });
        
        mySVGScrollPane = new JSVGScrollPane(canvas);
        mySVGScrollPane.setScrollbarsAlwaysVisible(false);
		mySVGScrollPane.addComponentListener(new ComponentAdapter(){
			public void componentResized(ComponentEvent e)
			{									
				mySVGScrollPane.reset();
			}
		});
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
        frame.addWindowListener(new WindowAdapter() {
                public void windowOpened(WindowEvent e) {
                    // The canvas is ready to load the base document
                    // now, from the AWT thread.
                   
canvas.setURI("file://localhost/C:/13060ESPE07/Output.svg");
                }
            });

        frame.getContentPane().add(mySVGScrollPane);
        frame.setSize(800, 600);
        frame.show();
    }
}



And the Output.svg is :

<?xml version="1.0" encoding="UTF-8"?>
<svg width="584" xmlns:xlink="http://www.w3.org/1999/xlink"
    viewBox="0 0 584.0 299.0" height="299"
xmlns="http://www.w3.org/2000/svg">
    <defs/>
    <g>
        <rect x="5.0" y="80.0" fill-opacity="1.0" fill="green"
            width="89.0" height="18.0" id="MyBox" stroke="black"
            stroke-width="2.0" stroke-opacity="1.0"/>
    </g>
    <g transform="translate(10.0,10.0)">
        <g onclick="Init( evt )">
            <script type="text/ecmascript">
				//<![CDATA[
				
				function Init()
				{
					rect = document.getElementById("MyBox");
					rect.setAttribute("stroke", "red");
				}
			
			// ]]></script>
            <rect x="5.0" y="10.0" fill-opacity="1.0" fill="yellow"
                width="89.0" id="MyBox1" height="18.0" stroke="black"
                stroke-opacity="1.0" stroke-width="2.0"/>
        </g>
    </g>
</svg>


Why batik doesn’t runs the ecmascript if ecmascript is added in the document
in the runtime?

-- 
View this message in context: http://old.nabble.com/Batik-can-not-run-the-ecmascript-tp26284849p26284849.html
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Mime
View raw message