xmlgraphics-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From build...@apache.org
Subject svn commit: r840429 [2/5] - in /websites/staging/xmlgraphics/trunk/content: ./ batik/dev/ batik/tools/ batik/using/ batik/using/scripting/
Date Mon, 03 Dec 2012 03:49:38 GMT
Modified: websites/staging/xmlgraphics/trunk/content/batik/using/scripting/ecmascript.html
==============================================================================
--- websites/staging/xmlgraphics/trunk/content/batik/using/scripting/ecmascript.html (original)
+++ websites/staging/xmlgraphics/trunk/content/batik/using/scripting/ecmascript.html Mon Dec  3 03:49:37 2012
@@ -168,23 +168,30 @@ $(document).ready(function () {
       	<!-- <div id="breadcrumb"><a href="/">Home</a>&nbsp;&raquo&nbsp;<a href="/batik/">Batik</a>&nbsp;&raquo&nbsp;<a href="/batik/using/">Using</a>&nbsp;&raquo&nbsp;<a href="/batik/using/scripting/">Scripting</a></div> -->
       	<div class="section-content"><h1 id="scripting-with-ecmascript">Scripting with ECMAScript</h1>
 <p>This page is a brief introduction to scripting SVG documents with ECMAScript, and how Batik’s ECMAScript environment can be extended.</p>
-<h2 id="scripting-basics-wzxhzdk3wzxhzdk4">Scripting basics <a id="scriptingBasics"></a></h2>
+<h2 id="scripting-basics-wzxhzdk6wzxhzdk7">Scripting basics <a id="scriptingBasics"></a></h2>
 <p>As the ECMAScript language (the standardised version of JavaScript) is one of the most popular scripting languages, and as the SVG specification states that an SVG conforming implementation must support it, SVG documents processed by Batik support scripting with ECMAScript using Mozilla’s ECMAScript interpreter, <a href="http://www.mozilla.org/rhino/">Rhino</a> .</p>
 <p>There are two places in an SVG file where you can put scripts.</p>
-<p>The first one is in the <code>script</code> element, where you can place any code, including function definitions, to be executed just before the document <code>SVGLoad</code> event is fired.
-<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
-  <script type="text/ecmascript">
+<p>The first one is in the <code>script</code> element, where you can place any code, including function definitions, to be executed just before the document <code>SVGLoad</code> event is fired.</p>
+<div class="codehilite"><pre><span class="nt">&lt;svg</span> <span class="na">xmlns=</span><span class="s">&quot;http://www.w3.org/2000/svg&quot;</span> <span class="na">width=</span><span class="s">&quot;100&quot;</span> <span class="na">height=</span><span class="s">&quot;100&quot;</span><span class="nt">&gt;</span>
+  <span class="nt">&lt;script</span> <span class="na">type=</span><span class="s">&quot;text/ecmascript&quot;</span><span class="nt">&gt;</span>
     // ECMAScript code to be executed 
-  </script></p>
-<p><!-- Remainder of the document... -->
-</svg>
-You can also attach script to respond to user or document events using attributes on SVG elements. As shown in the previous example, the scripting language must be set on the <code>script</code> element. However, for event handling the default language type <code>text/ecmascript</code> is assumed. If you want to change it you can use the <code>contentScriptType</code> attribute on the <code>svg</code> element.</p>
-<p>The event attribute can contain any script code to execute when the event reaches the element (as described by the <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow">DOM event flow</a> ) in either the bubbling or at-target phases. The following example will change the <code>rect</code> to be filled in blue when it is clicked.
-<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
-  <rect x="0" y="0" width="10" height="10"
-        onclick="evt.target.setAttribute('fill', 'blue')"/>
-</svg>
-Note that inside the event attribute script, there is a variable called <code>evt</code> that is a reference to the <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Event">Event</a> object that represents the event that is being handled.</p>
+  <span class="nt">&lt;/script&gt;</span>
+
+  <span class="c">&lt;!-- Remainder of the document... --&gt;</span>
+<span class="nt">&lt;/svg&gt;</span>
+</pre></div>
+
+
+<p>You can also attach script to respond to user or document events using attributes on SVG elements. As shown in the previous example, the scripting language must be set on the <code>script</code> element. However, for event handling the default language type <code>text/ecmascript</code> is assumed. If you want to change it you can use the <code>contentScriptType</code> attribute on the <code>svg</code> element.</p>
+<p>The event attribute can contain any script code to execute when the event reaches the element (as described by the <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow">DOM event flow</a> ) in either the bubbling or at-target phases. The following example will change the <code>rect</code> to be filled in blue when it is clicked.</p>
+<div class="codehilite"><pre><span class="nt">&lt;svg</span> <span class="na">xmlns=</span><span class="s">&quot;http://www.w3.org/2000/svg&quot;</span> <span class="na">width=</span><span class="s">&quot;100&quot;</span> <span class="na">height=</span><span class="s">&quot;100&quot;</span><span class="nt">&gt;</span>
+  <span class="nt">&lt;rect</span> <span class="na">x=</span><span class="s">&quot;0&quot;</span> <span class="na">y=</span><span class="s">&quot;0&quot;</span> <span class="na">width=</span><span class="s">&quot;10&quot;</span> <span class="na">height=</span><span class="s">&quot;10&quot;</span>
+        <span class="na">onclick=</span><span class="s">&quot;evt.target.setAttribute(&#39;fill&#39;, &#39;blue&#39;)&quot;</span><span class="nt">/&gt;</span>
+<span class="nt">&lt;/svg&gt;</span>
+</pre></div>
+
+
+<p>Note that inside the event attribute script, there is a variable called <code>evt</code> that is a reference to the <a href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Event">Event</a> object that represents the event that is being handled.</p>
 <p>For more information on using scripting in SVG you can have a look at:</p>
 <ul>
 <li>
@@ -194,100 +201,111 @@ Note that inside the event attribute scr
 <p>the <a href="http://www.ecma.ch/ecma1/stand/ecma-262.htm">ECMAScript specification</a> , for advanced information on the ECMAScript language.</p>
 </li>
 </ul>
-<h2 id="using-rhino-features-wzxhzdk13wzxhzdk14">Using Rhino features <a id="rhinoFeatures"></a></h2>
+<h2 id="using-rhino-features-wzxhzdk8wzxhzdk9">Using Rhino features <a id="rhinoFeatures"></a></h2>
 <p>Rhino has a number of features beyond those supported by standard ECMAScript interpreters, and these can be used with Batik. One useful feature is that ECMAScript code can use Java classes and objects, and not just the standard ECMAScript primitive types and host objects exposed by Batik.</p>
 <p>To create an instance of a Java class from ECMAScript, you first need to import the package in which it resides. This is done using the <code>importPackage</code> global function that Rhino provides. For example, to import the <code>javax.swing.JFrame</code> class, you use:
 importPackage(Packages.javax.swing);
 This then exposes a global property for each class in the <code>javax.swing</code> package that you can use to create a new object of this class, similar to a <code>import javax.swing.*;</code> statement in Java. We can use the exposed <code>JFrame</code> property to create a new instance of this class:
 var frame = new JFrame("My test frame");
 Note how an ECMAScript string value is passed as the parameter to <code>JFrame</code> ’s constructor. Rhino will attempt to convert ECMAScript values into appropriate Java primitive types or objects to make underlying constructor or method calls. In this instance, the ECMAScript string value is converted into a <code>java.lang.String</code> object to be passed to the constructor.</p>
-<p>Now that we have a reference to this Java object, we can call any method on it as we usually would from Java code. The following complete example demonstrates this, where clicking the green circle will pop up a frame:
-<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
-  <circle cx="50" cy="50" r="50" fill="green" onclick="showFrame()"/>
-  <script type="text/ecmascript">
-    importPackage(Packages.javax.swing);</p>
-<div class="codehilite"><pre><span class="n">function</span> <span class="n">showFrame</span><span class="p">()</span> <span class="p">{</span>
-  <span class="n">var</span> <span class="n">frame</span> <span class="o">=</span> <span class="k">new</span> <span class="n">JFrame</span><span class="p">(</span><span class="s">&quot;My test frame&quot;</span><span class="p">);</span>
-  <span class="n">var</span> <span class="n">label</span> <span class="o">=</span> <span class="k">new</span> <span class="n">JLabel</span><span class="p">(</span><span class="s">&quot;Hello from Java objects created in ECMAScript!&quot;</span><span class="p">);</span>
-  <span class="n">label</span><span class="o">.</span><span class="n">setHorizontalAlignment</span><span class="p">(</span><span class="n">SwingConstants</span><span class="o">.</span><span class="n">CENTER</span><span class="p">);</span>
-  <span class="n">frame</span><span class="o">.</span><span class="n">getContentPane</span><span class="p">()</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">label</span><span class="p">);</span>
-  <span class="n">frame</span><span class="o">.</span><span class="n">setSize</span><span class="p">(</span><span class="mi">400</span><span class="p">,</span> <span class="mi">100</span><span class="p">);</span>
-  <span class="n">frame</span><span class="o">.</span><span class="n">setVisible</span><span class="p">(</span><span class="n">true</span><span class="p">);</span>
-  <span class="n">frame</span><span class="o">.</span><span class="n">setDefaultCloseOperation</span><span class="p">(</span><span class="n">WindowConstants</span><span class="o">.</span><span class="n">DISPOSE_ON_CLOSE</span><span class="p">);</span>
-<span class="p">}</span>
+<p>Now that we have a reference to this Java object, we can call any method on it as we usually would from Java code. The following complete example demonstrates this, where clicking the green circle will pop up a frame:</p>
+<div class="codehilite"><pre><span class="nt">&lt;svg</span> <span class="na">xmlns=</span><span class="s">&quot;http://www.w3.org/2000/svg&quot;</span> <span class="na">width=</span><span class="s">&quot;100&quot;</span> <span class="na">height=</span><span class="s">&quot;100&quot;</span><span class="nt">&gt;</span>
+  <span class="nt">&lt;circle</span> <span class="na">cx=</span><span class="s">&quot;50&quot;</span> <span class="na">cy=</span><span class="s">&quot;50&quot;</span> <span class="na">r=</span><span class="s">&quot;50&quot;</span> <span class="na">fill=</span><span class="s">&quot;green&quot;</span> <span class="na">onclick=</span><span class="s">&quot;showFrame()&quot;</span><span class="nt">/&gt;</span>
+  <span class="nt">&lt;script</span> <span class="na">type=</span><span class="s">&quot;text/ecmascript&quot;</span><span class="nt">&gt;</span>
+    importPackage(Packages.javax.swing);
+
+    function showFrame() {
+      var frame = new JFrame(&quot;My test frame&quot;);
+      var label = new JLabel(&quot;Hello from Java objects created in ECMAScript!&quot;);
+      label.setHorizontalAlignment(SwingConstants.CENTER);
+      frame.getContentPane().add(label);
+      frame.setSize(400, 100);
+      frame.setVisible(true);
+      frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
+    }
+  <span class="nt">&lt;/script&gt;</span>
+<span class="nt">&lt;/svg&gt;</span>
 </pre></div>
 
 
-<p></script>
-</svg>
-For more information on scripting Java classes from ECMAScript code, see Rhino's <a href="http://www.mozilla.org/rhino/ScriptingJava.html">Scripting Java</a> document.</p>
-<h2 id="customizing-the-rhino-interpreter-wzxhzdk20wzxhzdk21">Customizing the Rhino interpreter <a id="customizingRhino"></a></h2>
+<p>For more information on scripting Java classes from ECMAScript code, see Rhino's <a href="http://www.mozilla.org/rhino/ScriptingJava.html">Scripting Java</a> document.</p>
+<h2 id="customizing-the-rhino-interpreter-wzxhzdk10wzxhzdk11">Customizing the Rhino interpreter <a id="customizingRhino"></a></h2>
 <p>A useful example of customization of the Rhino interpreter comes from the fact that the ECMAScript specification doesn’t provide any predefined I/O facilities to interact with the console. However, it is very common for ECMAScript compatible languages to provide a function named <code>print</code> to output messages to the console. We will describe here an example of cutomization of the Batik Rhino interpreter to add such functionality to it.</p>
-<p>You should first subclass the default Batik ECMAScript interpreter to add the functionality to it as below.
-import org.apache.batik.script.rhino.RhinoInterpreter;</p>
-<p>import java.net.URL;</p>
-<p>import org.mozilla.javascript.Context;
-import org.mozilla.javascript.Function;
-import org.mozilla.javascript.Scriptable;
-import org.mozilla.javascript.ScriptableObject;
-import org.mozilla.javascript.PropertyException;</p>
-<p>public class ExtendedRhinoInterpreter extends RhinoInterpreter {</p>
-<div class="codehilite"><pre><span class="n">public</span> <span class="n">ExtendedRhinoInterpreter</span><span class="p">(</span><span class="n">URL</span> <span class="n">documentURL</span><span class="p">)</span> <span class="p">{</span>
-    <span class="n">super</span><span class="p">(</span><span class="n">documentURL</span><span class="p">);</span>
-
-    <span class="sr">//</span> <span class="n">Array</span> <span class="n">of</span> <span class="n">functions</span> <span class="n">to</span> <span class="n">put</span> <span class="n">in</span> <span class="n">the</span> <span class="n">global</span> <span class="n">object</span><span class="o">.</span>
-    <span class="n">final</span> <span class="n">String</span><span class="o">[]</span> <span class="n">names</span> <span class="o">=</span> <span class="p">{</span> <span class="s">&quot;print&quot;</span> <span class="p">}</span>
-    <span class="n">try</span> <span class="p">{</span>
-        <span class="sr">//</span> <span class="n">Add</span> <span class="n">the</span> <span class="n">functions</span> <span class="n">to</span> <span class="n">the</span> <span class="n">global</span> <span class="n">object</span><span class="o">.</span>
-        <span class="n">getGlobalObject</span><span class="p">()</span><span class="o">.</span><span class="n">defineFunctionProperties</span>
-            <span class="p">(</span><span class="n">names</span><span class="p">,</span> <span class="n">ExtendedRhinoInterpreter</span><span class="o">.</span><span class="n">class</span><span class="p">,</span>
-             <span class="n">ScriptableObject</span><span class="o">.</span><span class="n">DONTENUM</span><span class="p">);</span>
-    <span class="p">}</span> <span class="n">catch</span> <span class="p">(</span><span class="n">PropertyException</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span>
-        <span class="n">throw</span> <span class="k">new</span> <span class="n">Error</span><span class="p">(</span><span class="n">e</span><span class="p">);</span>
-    <span class="p">}</span>
-<span class="p">}</span>
+<p>You should first subclass the default Batik ECMAScript interpreter to add the functionality to it as below.</p>
+<div class="codehilite"><pre><span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">apache</span><span class="o">.</span><span class="n">batik</span><span class="o">.</span><span class="n">script</span><span class="o">.</span><span class="n">rhino</span><span class="o">.</span><span class="n">RhinoInterpreter</span><span class="p">;</span>
 
-<span class="n">public</span> <span class="n">static</span> <span class="n">void</span> <span class="k">print</span><span class="p">(</span><span class="n">Context</span> <span class="n">cx</span><span class="p">,</span> <span class="n">Scriptable</span> <span class="n">thisObj</span><span class="p">,</span>
-                         <span class="n">Object</span><span class="o">[]</span> <span class="n">args</span><span class="p">,</span> <span class="n">Function</span> <span class="n">funObj</span><span class="p">)</span> <span class="p">{</span>
-    <span class="k">for</span> <span class="p">(</span><span class="nb">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">args</span><span class="o">.</span><span class="nb">length</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
-        <span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
-            <span class="n">System</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="k">print</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="p">);</span>
+<span class="nb">import</span> <span class="n">java</span><span class="o">.</span><span class="n">net</span><span class="o">.</span><span class="n">URL</span><span class="p">;</span>
+
+<span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">mozilla</span><span class="o">.</span><span class="n">javascript</span><span class="o">.</span><span class="n">Context</span><span class="p">;</span>
+<span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">mozilla</span><span class="o">.</span><span class="n">javascript</span><span class="o">.</span><span class="n">Function</span><span class="p">;</span>
+<span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">mozilla</span><span class="o">.</span><span class="n">javascript</span><span class="o">.</span><span class="n">Scriptable</span><span class="p">;</span>
+<span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">mozilla</span><span class="o">.</span><span class="n">javascript</span><span class="o">.</span><span class="n">ScriptableObject</span><span class="p">;</span>
+<span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">mozilla</span><span class="o">.</span><span class="n">javascript</span><span class="o">.</span><span class="n">PropertyException</span><span class="p">;</span>
+
+<span class="n">public</span> <span class="n">class</span> <span class="n">ExtendedRhinoInterpreter</span> <span class="n">extends</span> <span class="n">RhinoInterpreter</span> <span class="p">{</span>
+
+    <span class="n">public</span> <span class="n">ExtendedRhinoInterpreter</span><span class="p">(</span><span class="n">URL</span> <span class="n">documentURL</span><span class="p">)</span> <span class="p">{</span>
+        <span class="n">super</span><span class="p">(</span><span class="n">documentURL</span><span class="p">);</span>
+
+        <span class="sr">//</span> <span class="n">Array</span> <span class="n">of</span> <span class="n">functions</span> <span class="n">to</span> <span class="n">put</span> <span class="n">in</span> <span class="n">the</span> <span class="n">global</span> <span class="n">object</span><span class="o">.</span>
+        <span class="n">final</span> <span class="n">String</span><span class="o">[]</span> <span class="n">names</span> <span class="o">=</span> <span class="p">{</span> <span class="s">&quot;print&quot;</span> <span class="p">}</span>
+        <span class="n">try</span> <span class="p">{</span>
+            <span class="sr">//</span> <span class="n">Add</span> <span class="n">the</span> <span class="n">functions</span> <span class="n">to</span> <span class="n">the</span> <span class="n">global</span> <span class="n">object</span><span class="o">.</span>
+            <span class="n">getGlobalObject</span><span class="p">()</span><span class="o">.</span><span class="n">defineFunctionProperties</span>
+                <span class="p">(</span><span class="n">names</span><span class="p">,</span> <span class="n">ExtendedRhinoInterpreter</span><span class="o">.</span><span class="n">class</span><span class="p">,</span>
+                 <span class="n">ScriptableObject</span><span class="o">.</span><span class="n">DONTENUM</span><span class="p">);</span>
+        <span class="p">}</span> <span class="n">catch</span> <span class="p">(</span><span class="n">PropertyException</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span>
+            <span class="n">throw</span> <span class="k">new</span> <span class="n">Error</span><span class="p">(</span><span class="n">e</span><span class="p">);</span>
         <span class="p">}</span>
+    <span class="p">}</span>
 
-        <span class="sr">//</span> <span class="n">Convert</span> <span class="n">the</span> <span class="n">ECMAScript</span> <span class="n">value</span> <span class="n">into</span> <span class="n">a</span> <span class="n">string</span> <span class="n">form</span><span class="o">.</span>
-        <span class="n">String</span> <span class="n">s</span> <span class="o">=</span> <span class="n">Context</span><span class="o">.</span><span class="n">toString</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
-        <span class="n">System</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="k">print</span><span class="p">(</span><span class="n">s</span><span class="p">);</span>
+    <span class="n">public</span> <span class="n">static</span> <span class="n">void</span> <span class="k">print</span><span class="p">(</span><span class="n">Context</span> <span class="n">cx</span><span class="p">,</span> <span class="n">Scriptable</span> <span class="n">thisObj</span><span class="p">,</span>
+                             <span class="n">Object</span><span class="o">[]</span> <span class="n">args</span><span class="p">,</span> <span class="n">Function</span> <span class="n">funObj</span><span class="p">)</span> <span class="p">{</span>
+        <span class="k">for</span> <span class="p">(</span><span class="nb">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">args</span><span class="o">.</span><span class="nb">length</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
+            <span class="k">if</span> <span class="p">(</span><span class="n">i</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
+                <span class="n">System</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="k">print</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="p">);</span>
+            <span class="p">}</span>
+
+            <span class="sr">//</span> <span class="n">Convert</span> <span class="n">the</span> <span class="n">ECMAScript</span> <span class="n">value</span> <span class="n">into</span> <span class="n">a</span> <span class="n">string</span> <span class="n">form</span><span class="o">.</span>
+            <span class="n">String</span> <span class="n">s</span> <span class="o">=</span> <span class="n">Context</span><span class="o">.</span><span class="n">toString</span><span class="p">(</span><span class="n">args</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
+            <span class="n">System</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="k">print</span><span class="p">(</span><span class="n">s</span><span class="p">);</span>
+        <span class="p">}</span>
+        <span class="n">System</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">println</span><span class="p">();</span>
     <span class="p">}</span>
-    <span class="n">System</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">println</span><span class="p">();</span>
 <span class="p">}</span>
 </pre></div>
 
 
-<p>}
-Now, you need to tell to Batik to use this interpreter instead of the default one. For that, you must first define a factory to create instances of your interpreter.
-import org.apache.batik.script.Interpreter;
-import org.apache.batik.script.rhino.RhinoInterpreterFactory;</p>
-<p>public class ExtendedRhinoInterpreterFactory extends RhinoInterpreterFactory {</p>
-<div class="codehilite"><pre><span class="n">public</span> <span class="n">Interpreter</span> <span class="n">createInterpreter</span><span class="p">(</span><span class="n">URL</span> <span class="n">documentURL</span><span class="p">,</span> <span class="n">boolean</span> <span class="n">isSVG12</span><span class="p">)</span> <span class="p">{</span>
-    <span class="k">return</span> <span class="k">new</span> <span class="n">ExtendedRhinoInterpreter</span><span class="p">(</span><span class="n">documentURL</span><span class="p">);</span>
+<p>Now, you need to tell to Batik to use this interpreter instead of the default one. For that, you must first define a factory to create instances of your interpreter.</p>
+<div class="codehilite"><pre><span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">apache</span><span class="o">.</span><span class="n">batik</span><span class="o">.</span><span class="n">script</span><span class="o">.</span><span class="n">Interpreter</span><span class="p">;</span>
+<span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">apache</span><span class="o">.</span><span class="n">batik</span><span class="o">.</span><span class="n">script</span><span class="o">.</span><span class="n">rhino</span><span class="o">.</span><span class="n">RhinoInterpreterFactory</span><span class="p">;</span>
+
+<span class="n">public</span> <span class="n">class</span> <span class="n">ExtendedRhinoInterpreterFactory</span> <span class="n">extends</span> <span class="n">RhinoInterpreterFactory</span> <span class="p">{</span>
+
+    <span class="n">public</span> <span class="n">Interpreter</span> <span class="n">createInterpreter</span><span class="p">(</span><span class="n">URL</span> <span class="n">documentURL</span><span class="p">,</span> <span class="n">boolean</span> <span class="n">isSVG12</span><span class="p">)</span> <span class="p">{</span>
+        <span class="k">return</span> <span class="k">new</span> <span class="n">ExtendedRhinoInterpreter</span><span class="p">(</span><span class="n">documentURL</span><span class="p">);</span>
+    <span class="p">}</span>
 <span class="p">}</span>
 </pre></div>
 
 
-<p>}
-Then, you must build an <a href="../../javadoc/org/apache/batik/script/InterpreterPool.html">IntepreterPool</a> that will use this factory, and then set the pool on the <a href="../../javadoc/org/apache/batik/bridge/BridgeContext.html">BridgeContext</a> of your application.
-org.apache.batik.bridge.BridgeContext ctx = ...;
-org.apache.batik.script.InterpreterPool pool =
-    new org.apache.batik.script.InterpreterPool();
-InterpreterFactory f = new ExtendedRhinoInterpreterFactory();</p>
-<p>// Register the interpreter factory for all four MIME types that
-// Batik normally supports for ECMAScript.
-pool.putInterpreterFactory("text/ecmascript", f);
-pool.putInterpreterFactory("text/javascript", f);
-pool.putInterpreterFactory("application/ecmascript", f);
-pool.putInterpreterFactory("application/javascript", f);
-ctx.setIntepreterPool(pool);
-For example if you are using the Batik SVG browser application you should be able to use the previous piece of code on a subclass of the <a href="../../javadoc/org/apache/batik/swing/JSVGCanvas.html">JSVGCanvas</a> class in the <code>createBridgeContext()</code> method.</p>
+<p>Then, you must build an <a href="../../javadoc/org/apache/batik/script/InterpreterPool.html">IntepreterPool</a> that will use this factory, and then set the pool on the <a href="../../javadoc/org/apache/batik/bridge/BridgeContext.html">BridgeContext</a> of your application.</p>
+<div class="codehilite"><pre><span class="n">org</span><span class="o">.</span><span class="n">apache</span><span class="o">.</span><span class="n">batik</span><span class="o">.</span><span class="n">bridge</span><span class="o">.</span><span class="n">BridgeContext</span> <span class="n">ctx</span> <span class="o">=</span> <span class="o">...</span><span class="p">;</span>
+<span class="n">org</span><span class="o">.</span><span class="n">apache</span><span class="o">.</span><span class="n">batik</span><span class="o">.</span><span class="n">script</span><span class="o">.</span><span class="n">InterpreterPool</span> <span class="n">pool</span> <span class="o">=</span>
+    <span class="k">new</span> <span class="n">org</span><span class="o">.</span><span class="n">apache</span><span class="o">.</span><span class="n">batik</span><span class="o">.</span><span class="n">script</span><span class="o">.</span><span class="n">InterpreterPool</span><span class="p">();</span>
+<span class="n">InterpreterFactory</span> <span class="n">f</span> <span class="o">=</span> <span class="k">new</span> <span class="n">ExtendedRhinoInterpreterFactory</span><span class="p">();</span>
+
+<span class="sr">//</span> <span class="n">Register</span> <span class="n">the</span> <span class="n">interpreter</span> <span class="n">factory</span> <span class="k">for</span> <span class="n">all</span> <span class="n">four</span> <span class="n">MIME</span> <span class="n">types</span> <span class="n">that</span>
+<span class="sr">//</span> <span class="n">Batik</span> <span class="n">normally</span> <span class="n">supports</span> <span class="k">for</span> <span class="n">ECMAScript</span><span class="o">.</span>
+<span class="n">pool</span><span class="o">.</span><span class="n">putInterpreterFactory</span><span class="p">(</span><span class="s">&quot;text/ecmascript&quot;</span><span class="p">,</span> <span class="n">f</span><span class="p">);</span>
+<span class="n">pool</span><span class="o">.</span><span class="n">putInterpreterFactory</span><span class="p">(</span><span class="s">&quot;text/javascript&quot;</span><span class="p">,</span> <span class="n">f</span><span class="p">);</span>
+<span class="n">pool</span><span class="o">.</span><span class="n">putInterpreterFactory</span><span class="p">(</span><span class="s">&quot;application/ecmascript&quot;</span><span class="p">,</span> <span class="n">f</span><span class="p">);</span>
+<span class="n">pool</span><span class="o">.</span><span class="n">putInterpreterFactory</span><span class="p">(</span><span class="s">&quot;application/javascript&quot;</span><span class="p">,</span> <span class="n">f</span><span class="p">);</span>
+<span class="n">ctx</span><span class="o">.</span><span class="n">setIntepreterPool</span><span class="p">(</span><span class="n">pool</span><span class="p">);</span>
+</pre></div>
+
+
+<p>For example if you are using the Batik SVG browser application you should be able to use the previous piece of code on a subclass of the <a href="../../javadoc/org/apache/batik/swing/JSVGCanvas.html">JSVGCanvas</a> class in the <code>createBridgeContext()</code> method.</p>
 <p>For further information on working with Rhino, consult the <a href="http://www.mozilla.org/rhino/">Rhino website</a> .</p></div>
       </div>
       

Modified: websites/staging/xmlgraphics/trunk/content/batik/using/scripting/java.html
==============================================================================
--- websites/staging/xmlgraphics/trunk/content/batik/using/scripting/java.html (original)
+++ websites/staging/xmlgraphics/trunk/content/batik/using/scripting/java.html Mon Dec  3 03:49:37 2012
@@ -168,164 +168,185 @@ $(document).ready(function () {
       	<!-- <div id="breadcrumb"><a href="/">Home</a>&nbsp;&raquo&nbsp;<a href="/batik/">Batik</a>&nbsp;&raquo&nbsp;<a href="/batik/using/">Using</a>&nbsp;&raquo&nbsp;<a href="/batik/using/scripting/">Scripting</a></div> -->
       	<div class="section-content"><h1 id="scripting-with-java">Scripting With Java</h1>
 <p>This page explains how to manipulate the DOM tree of an SVG document from a Java program.</p>
-<h2 id="how-to-manipulate-a-document-in-a-jsvgcanvas-wzxhzdk1wzxhzdk2">How to manipulate a document in a JSVGCanvas <a id="Swing"></a></h2>
+<h2 id="how-to-manipulate-a-document-in-a-jsvgcanvas-wzxhzdk6wzxhzdk7">How to manipulate a document in a JSVGCanvas <a id="Swing"></a></h2>
 <p>The follow code template demonstrates how to manipulate an SVG document displayed in a <a href="../../javadoc/org/apache/batik/swing/JSVGCanvas.html">JSVGCanvas</a> directly from a Java program.
-You don’t have to worry about graphics updates; after each event listener invocation the canvas is updated if needed.import java.awt.event.WindowAdapter;
-import java.awt.event.WindowEvent;</p>
-<p>import javax.swing.JFrame;</p>
-<p>import org.apache.batik.swing.JSVGCanvas;
-import org.apache.batik.swing.svg.SVGLoadEventDispatcherAdapter;
-import org.apache.batik.swing.svg.SVGLoadEventDispatcherEvent;
-import org.apache.batik.script.Window;</p>
-<p>import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.events.Event;
-import org.w3c.dom.events.EventListener;
-import org.w3c.dom.events.EventTarget;</p>
-<p>public class SVGApplication {</p>
-<div class="codehilite"><pre><span class="n">public</span> <span class="n">static</span> <span class="n">void</span> <span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="p">)</span> <span class="p">{</span>
-    <span class="k">new</span> <span class="n">SVGApplication</span><span class="p">();</span>
-<span class="p">}</span>
+You don’t have to worry about graphics updates; after each event listener invocation the canvas is updated if needed.</p>
+<div class="codehilite"><pre><span class="nb">import</span> <span class="n">java</span><span class="o">.</span><span class="n">awt</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">WindowAdapter</span><span class="p">;</span>
+<span class="nb">import</span> <span class="n">java</span><span class="o">.</span><span class="n">awt</span><span class="o">.</span><span class="n">event</span><span class="o">.</span><span class="n">WindowEvent</span><span class="p">;</span>
+
+<span class="nb">import</span> <span class="n">javax</span><span class="o">.</span><span class="n">swing</span><span class="o">.</span><span class="n">JFrame</span><span class="p">;</span>
+
+<span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">apache</span><span class="o">.</span><span class="n">batik</span><span class="o">.</span><span class="n">swing</span><span class="o">.</span><span class="n">JSVGCanvas</span><span class="p">;</span>
+<span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">apache</span><span class="o">.</span><span class="n">batik</span><span class="o">.</span><span class="n">swing</span><span class="o">.</span><span class="n">svg</span><span class="o">.</span><span class="n">SVGLoadEventDispatcherAdapter</span><span class="p">;</span>
+<span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">apache</span><span class="o">.</span><span class="n">batik</span><span class="o">.</span><span class="n">swing</span><span class="o">.</span><span class="n">svg</span><span class="o">.</span><span class="n">SVGLoadEventDispatcherEvent</span><span class="p">;</span>
+<span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">apache</span><span class="o">.</span><span class="n">batik</span><span class="o">.</span><span class="n">script</span><span class="o">.</span><span class="n">Window</span><span class="p">;</span>
+
+<span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">w3c</span><span class="o">.</span><span class="n">dom</span><span class="o">.</span><span class="n">Document</span><span class="p">;</span>
+<span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">w3c</span><span class="o">.</span><span class="n">dom</span><span class="o">.</span><span class="n">Element</span><span class="p">;</span>
+<span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">w3c</span><span class="o">.</span><span class="n">dom</span><span class="o">.</span><span class="n">events</span><span class="o">.</span><span class="n">Event</span><span class="p">;</span>
+<span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">w3c</span><span class="o">.</span><span class="n">dom</span><span class="o">.</span><span class="n">events</span><span class="o">.</span><span class="n">EventListener</span><span class="p">;</span>
+<span class="nb">import</span> <span class="n">org</span><span class="o">.</span><span class="n">w3c</span><span class="o">.</span><span class="n">dom</span><span class="o">.</span><span class="n">events</span><span class="o">.</span><span class="n">EventTarget</span><span class="p">;</span>
+
+<span class="n">public</span> <span class="n">class</span> <span class="n">SVGApplication</span> <span class="p">{</span>
+
+    <span class="n">public</span> <span class="n">static</span> <span class="n">void</span> <span class="n">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="p">)</span> <span class="p">{</span>
+        <span class="k">new</span> <span class="n">SVGApplication</span><span class="p">();</span>
+    <span class="p">}</span>
 
-<span class="n">JFrame</span> <span class="n">frame</span><span class="p">;</span>
-<span class="n">JSVGCanvas</span> <span class="n">canvas</span><span class="p">;</span>
-<span class="n">Document</span> <span class="n">document</span><span class="p">;</span>
-<span class="n">Window</span> <span class="n">window</span><span class="p">;</span>
-
-<span class="n">public</span> <span class="n">SVGApplication</span><span class="p">()</span> <span class="p">{</span>
-    <span class="n">frame</span> <span class="o">=</span> <span class="k">new</span> <span class="n">JFrame</span><span class="p">();</span>
-    <span class="n">canvas</span> <span class="o">=</span> <span class="k">new</span> <span class="n">JSVGCanvas</span><span class="p">();</span>
-    <span class="sr">//</span> <span class="n">Forces</span> <span class="n">the</span> <span class="n">canvas</span> <span class="n">to</span> <span class="n">always</span> <span class="n">be</span> <span class="n">dynamic</span> <span class="n">even</span> <span class="k">if</span> <span class="n">the</span> <span class="n">current</span>
-    <span class="sr">//</span> <span class="n">document</span> <span class="n">does</span> <span class="ow">not</span> <span class="n">contain</span> <span class="n">scripting</span> <span class="ow">or</span> <span class="n">animation</span><span class="o">.</span>
-    <span class="n">canvas</span><span class="o">.</span><span class="n">setDocumentState</span><span class="p">(</span><span class="n">JSVGCanvas</span><span class="o">.</span><span class="n">ALWAYS_DYNAMIC</span><span class="p">);</span>
-    <span class="n">canvas</span><span class="o">.</span><span class="n">addSVGLoadEventDispatcherListener</span>
-        <span class="p">(</span><span class="k">new</span> <span class="n">SVGLoadEventDispatcherAdapter</span><span class="p">()</span> <span class="p">{</span>
-                <span class="n">public</span> <span class="n">void</span> <span class="n">svgLoadEventDispatchStarted</span>
-                    <span class="p">(</span><span class="n">SVGLoadEventDispatcherEvent</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span>
-                    <span class="sr">//</span> <span class="n">At</span> <span class="n">this</span> <span class="nb">time</span> <span class="n">the</span> <span class="n">document</span> <span class="n">is</span> <span class="n">available</span><span class="o">...</span>
-                    <span class="n">document</span> <span class="o">=</span> <span class="n">canvas</span><span class="o">.</span><span class="n">getSVGDocument</span><span class="p">();</span>
-                    <span class="sr">//</span> <span class="o">...</span><span class="ow">and</span> <span class="n">the</span> <span class="n">window</span> <span class="n">object</span> <span class="n">too</span><span class="o">.</span>
-                    <span class="n">window</span> <span class="o">=</span> <span class="n">canvas</span><span class="o">.</span><span class="n">getUpdateManager</span><span class="p">()</span><span class="o">.</span>
-                        <span class="n">getScriptingEnvironment</span><span class="p">()</span><span class="o">.</span><span class="n">createWindow</span><span class="p">();</span>
-                    <span class="sr">//</span> <span class="n">Registers</span> <span class="n">the</span> <span class="n">listeners</span> <span class="n">on</span> <span class="n">the</span> <span class="n">document</span>
-                    <span class="sr">//</span> <span class="n">just</span> <span class="n">before</span> <span class="n">the</span> <span class="n">SVGLoad</span> <span class="n">event</span> <span class="n">is</span>
-                    <span class="sr">//</span> <span class="n">dispatched</span><span class="o">.</span>
-                    <span class="n">registerListeners</span><span class="p">();</span>
-                    <span class="sr">//</span> <span class="n">It</span> <span class="n">is</span> <span class="nb">time</span> <span class="n">to</span> <span class="nb">pack</span> <span class="n">the</span> <span class="n">frame</span><span class="o">.</span>
-                    <span class="n">frame</span><span class="o">.</span><span class="nb">pack</span><span class="p">();</span>
+    <span class="n">JFrame</span> <span class="n">frame</span><span class="p">;</span>
+    <span class="n">JSVGCanvas</span> <span class="n">canvas</span><span class="p">;</span>
+    <span class="n">Document</span> <span class="n">document</span><span class="p">;</span>
+    <span class="n">Window</span> <span class="n">window</span><span class="p">;</span>
+
+    <span class="n">public</span> <span class="n">SVGApplication</span><span class="p">()</span> <span class="p">{</span>
+        <span class="n">frame</span> <span class="o">=</span> <span class="k">new</span> <span class="n">JFrame</span><span class="p">();</span>
+        <span class="n">canvas</span> <span class="o">=</span> <span class="k">new</span> <span class="n">JSVGCanvas</span><span class="p">();</span>
+        <span class="sr">//</span> <span class="n">Forces</span> <span class="n">the</span> <span class="n">canvas</span> <span class="n">to</span> <span class="n">always</span> <span class="n">be</span> <span class="n">dynamic</span> <span class="n">even</span> <span class="k">if</span> <span class="n">the</span> <span class="n">current</span>
+        <span class="sr">//</span> <span class="n">document</span> <span class="n">does</span> <span class="ow">not</span> <span class="n">contain</span> <span class="n">scripting</span> <span class="ow">or</span> <span class="n">animation</span><span class="o">.</span>
+        <span class="n">canvas</span><span class="o">.</span><span class="n">setDocumentState</span><span class="p">(</span><span class="n">JSVGCanvas</span><span class="o">.</span><span class="n">ALWAYS_DYNAMIC</span><span class="p">);</span>
+        <span class="n">canvas</span><span class="o">.</span><span class="n">addSVGLoadEventDispatcherListener</span>
+            <span class="p">(</span><span class="k">new</span> <span class="n">SVGLoadEventDispatcherAdapter</span><span class="p">()</span> <span class="p">{</span>
+                    <span class="n">public</span> <span class="n">void</span> <span class="n">svgLoadEventDispatchStarted</span>
+                        <span class="p">(</span><span class="n">SVGLoadEventDispatcherEvent</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span>
+                        <span class="sr">//</span> <span class="n">At</span> <span class="n">this</span> <span class="nb">time</span> <span class="n">the</span> <span class="n">document</span> <span class="n">is</span> <span class="n">available</span><span class="o">...</span>
+                        <span class="n">document</span> <span class="o">=</span> <span class="n">canvas</span><span class="o">.</span><span class="n">getSVGDocument</span><span class="p">();</span>
+                        <span class="sr">//</span> <span class="o">...</span><span class="ow">and</span> <span class="n">the</span> <span class="n">window</span> <span class="n">object</span> <span class="n">too</span><span class="o">.</span>
+                        <span class="n">window</span> <span class="o">=</span> <span class="n">canvas</span><span class="o">.</span><span class="n">getUpdateManager</span><span class="p">()</span><span class="o">.</span>
+                            <span class="n">getScriptingEnvironment</span><span class="p">()</span><span class="o">.</span><span class="n">createWindow</span><span class="p">();</span>
+                        <span class="sr">//</span> <span class="n">Registers</span> <span class="n">the</span> <span class="n">listeners</span> <span class="n">on</span> <span class="n">the</span> <span class="n">document</span>
+                        <span class="sr">//</span> <span class="n">just</span> <span class="n">before</span> <span class="n">the</span> <span class="n">SVGLoad</span> <span class="n">event</span> <span class="n">is</span>
+                        <span class="sr">//</span> <span class="n">dispatched</span><span class="o">.</span>
+                        <span class="n">registerListeners</span><span class="p">();</span>
+                        <span class="sr">//</span> <span class="n">It</span> <span class="n">is</span> <span class="nb">time</span> <span class="n">to</span> <span class="nb">pack</span> <span class="n">the</span> <span class="n">frame</span><span class="o">.</span>
+                        <span class="n">frame</span><span class="o">.</span><span class="nb">pack</span><span class="p">();</span>
+                    <span class="p">}</span>
+                <span class="p">});</span>
+
+        <span class="n">frame</span><span class="o">.</span><span class="n">addWindowListener</span><span class="p">(</span><span class="k">new</span> <span class="n">WindowAdapter</span><span class="p">()</span> <span class="p">{</span>
+                <span class="n">public</span> <span class="n">void</span> <span class="n">windowOpened</span><span class="p">(</span><span class="n">WindowEvent</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span>
+                    <span class="sr">//</span> <span class="n">The</span> <span class="n">canvas</span> <span class="n">is</span> <span class="n">ready</span> <span class="n">to</span> <span class="n">load</span> <span class="n">the</span> <span class="n">base</span> <span class="n">document</span>
+                    <span class="sr">//</span> <span class="n">now</span><span class="p">,</span> <span class="n">from</span> <span class="n">the</span> <span class="n">AWT</span> <span class="n">thread</span><span class="o">.</span>
+                    <span class="n">canvas</span><span class="o">.</span><span class="n">setURI</span><span class="p">(</span><span class="s">&quot;doc.svg&quot;</span><span class="p">);</span>
                 <span class="p">}</span>
             <span class="p">});</span>
 
-    <span class="n">frame</span><span class="o">.</span><span class="n">addWindowListener</span><span class="p">(</span><span class="k">new</span> <span class="n">WindowAdapter</span><span class="p">()</span> <span class="p">{</span>
-            <span class="n">public</span> <span class="n">void</span> <span class="n">windowOpened</span><span class="p">(</span><span class="n">WindowEvent</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span>
-                <span class="sr">//</span> <span class="n">The</span> <span class="n">canvas</span> <span class="n">is</span> <span class="n">ready</span> <span class="n">to</span> <span class="n">load</span> <span class="n">the</span> <span class="n">base</span> <span class="n">document</span>
-                <span class="sr">//</span> <span class="n">now</span><span class="p">,</span> <span class="n">from</span> <span class="n">the</span> <span class="n">AWT</span> <span class="n">thread</span><span class="o">.</span>
-                <span class="n">canvas</span><span class="o">.</span><span class="n">setURI</span><span class="p">(</span><span class="s">&quot;doc.svg&quot;</span><span class="p">);</span>
-            <span class="p">}</span>
-        <span class="p">});</span>
-
-    <span class="n">frame</span><span class="o">.</span><span class="n">getContentPane</span><span class="p">()</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">canvas</span><span class="p">);</span>
-    <span class="n">frame</span><span class="o">.</span><span class="n">setSize</span><span class="p">(</span><span class="mi">800</span><span class="p">,</span> <span class="mi">600</span><span class="p">);</span>
-    <span class="n">frame</span><span class="o">.</span><span class="n">show</span><span class="p">();</span>
-<span class="p">}</span>
-
-<span class="n">public</span> <span class="n">void</span> <span class="n">registerListeners</span><span class="p">()</span> <span class="p">{</span>
-    <span class="sr">//</span> <span class="n">Gets</span> <span class="n">an</span> <span class="n">element</span> <span class="n">from</span> <span class="n">the</span> <span class="n">loaded</span> <span class="n">document</span><span class="o">.</span>
-    <span class="n">Element</span> <span class="n">elt</span> <span class="o">=</span> <span class="n">document</span><span class="o">.</span><span class="n">getElementById</span><span class="p">(</span><span class="s">&quot;elt-id&quot;</span><span class="p">);</span>
-    <span class="n">EventTarget</span> <span class="n">t</span> <span class="o">=</span> <span class="p">(</span><span class="n">EventTarget</span><span class="p">)</span><span class="n">elt</span><span class="p">;</span>
+        <span class="n">frame</span><span class="o">.</span><span class="n">getContentPane</span><span class="p">()</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">canvas</span><span class="p">);</span>
+        <span class="n">frame</span><span class="o">.</span><span class="n">setSize</span><span class="p">(</span><span class="mi">800</span><span class="p">,</span> <span class="mi">600</span><span class="p">);</span>
+        <span class="n">frame</span><span class="o">.</span><span class="n">show</span><span class="p">();</span>
+    <span class="p">}</span>
 
-    <span class="sr">//</span> <span class="n">Adds</span> <span class="n">a</span> <span class="s">&#39;onload&#39;</span> <span class="n">listener</span>
-    <span class="n">t</span><span class="o">.</span><span class="n">addEventListener</span><span class="p">(</span><span class="s">&quot;SVGLoad&quot;</span><span class="p">,</span> <span class="k">new</span> <span class="n">OnLoadAction</span><span class="p">(),</span> <span class="n">false</span><span class="p">);</span>
+    <span class="n">public</span> <span class="n">void</span> <span class="n">registerListeners</span><span class="p">()</span> <span class="p">{</span>
+        <span class="sr">//</span> <span class="n">Gets</span> <span class="n">an</span> <span class="n">element</span> <span class="n">from</span> <span class="n">the</span> <span class="n">loaded</span> <span class="n">document</span><span class="o">.</span>
+        <span class="n">Element</span> <span class="n">elt</span> <span class="o">=</span> <span class="n">document</span><span class="o">.</span><span class="n">getElementById</span><span class="p">(</span><span class="s">&quot;elt-id&quot;</span><span class="p">);</span>
+        <span class="n">EventTarget</span> <span class="n">t</span> <span class="o">=</span> <span class="p">(</span><span class="n">EventTarget</span><span class="p">)</span><span class="n">elt</span><span class="p">;</span>
 
-    <span class="sr">//</span> <span class="n">Adds</span> <span class="n">a</span> <span class="s">&#39;onclick&#39;</span> <span class="n">listener</span>
-    <span class="n">t</span><span class="o">.</span><span class="n">addEventListener</span><span class="p">(</span><span class="s">&quot;click&quot;</span><span class="p">,</span> <span class="k">new</span> <span class="n">OnClickAction</span><span class="p">(),</span> <span class="n">false</span><span class="p">);</span>
-<span class="p">}</span>
+        <span class="sr">//</span> <span class="n">Adds</span> <span class="n">a</span> <span class="s">&#39;onload&#39;</span> <span class="n">listener</span>
+        <span class="n">t</span><span class="o">.</span><span class="n">addEventListener</span><span class="p">(</span><span class="s">&quot;SVGLoad&quot;</span><span class="p">,</span> <span class="k">new</span> <span class="n">OnLoadAction</span><span class="p">(),</span> <span class="n">false</span><span class="p">);</span>
 
-<span class="n">public</span> <span class="n">class</span> <span class="n">OnLoadAction</span> <span class="n">implements</span> <span class="n">EventListener</span> <span class="p">{</span>
-    <span class="n">public</span> <span class="n">void</span> <span class="n">handleEvent</span><span class="p">(</span><span class="n">Event</span> <span class="n">evt</span><span class="p">)</span> <span class="p">{</span>
-        <span class="sr">//</span> <span class="n">Perform</span> <span class="n">some</span> <span class="n">actions</span> <span class="n">here</span><span class="o">...</span>
-
-        <span class="sr">//</span> <span class="o">...</span><span class="k">for</span> <span class="n">example</span> <span class="n">start</span> <span class="n">an</span> <span class="n">animation</span> <span class="n">loop:</span>
-        <span class="n">window</span><span class="o">.</span><span class="n">setInterval</span><span class="p">(</span><span class="k">new</span> <span class="n">Animation</span><span class="p">(),</span> <span class="mi">50</span><span class="p">);</span>
+        <span class="sr">//</span> <span class="n">Adds</span> <span class="n">a</span> <span class="s">&#39;onclick&#39;</span> <span class="n">listener</span>
+        <span class="n">t</span><span class="o">.</span><span class="n">addEventListener</span><span class="p">(</span><span class="s">&quot;click&quot;</span><span class="p">,</span> <span class="k">new</span> <span class="n">OnClickAction</span><span class="p">(),</span> <span class="n">false</span><span class="p">);</span>
     <span class="p">}</span>
-<span class="p">}</span>
 
-<span class="n">public</span> <span class="n">class</span> <span class="n">OnClickAction</span> <span class="n">implements</span> <span class="n">EventListener</span> <span class="p">{</span>
-    <span class="n">public</span> <span class="n">void</span> <span class="n">handleEvent</span><span class="p">(</span><span class="n">Event</span> <span class="n">evt</span><span class="p">)</span> <span class="p">{</span>
-        <span class="sr">//</span> <span class="n">Perform</span> <span class="n">some</span> <span class="n">actions</span> <span class="n">here</span><span class="o">...</span>
-
-        <span class="sr">//</span> <span class="o">...</span><span class="k">for</span> <span class="n">example</span> <span class="n">schedule</span> <span class="n">an</span> <span class="n">action</span> <span class="k">for</span> <span class="n">later:</span>
-        <span class="n">window</span><span class="o">.</span><span class="n">setTimeout</span><span class="p">(</span><span class="k">new</span> <span class="n">DelayedTask</span><span class="p">(),</span> <span class="mi">500</span><span class="p">);</span>
+    <span class="n">public</span> <span class="n">class</span> <span class="n">OnLoadAction</span> <span class="n">implements</span> <span class="n">EventListener</span> <span class="p">{</span>
+        <span class="n">public</span> <span class="n">void</span> <span class="n">handleEvent</span><span class="p">(</span><span class="n">Event</span> <span class="n">evt</span><span class="p">)</span> <span class="p">{</span>
+            <span class="sr">//</span> <span class="n">Perform</span> <span class="n">some</span> <span class="n">actions</span> <span class="n">here</span><span class="o">...</span>
+
+            <span class="sr">//</span> <span class="o">...</span><span class="k">for</span> <span class="n">example</span> <span class="n">start</span> <span class="n">an</span> <span class="n">animation</span> <span class="n">loop:</span>
+            <span class="n">window</span><span class="o">.</span><span class="n">setInterval</span><span class="p">(</span><span class="k">new</span> <span class="n">Animation</span><span class="p">(),</span> <span class="mi">50</span><span class="p">);</span>
+        <span class="p">}</span>
     <span class="p">}</span>
-<span class="p">}</span>
 
-<span class="n">public</span> <span class="n">class</span> <span class="n">Animation</span> <span class="n">implements</span> <span class="n">Runnable</span> <span class="p">{</span>
-    <span class="n">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span> <span class="p">{</span>
-        <span class="sr">//</span> <span class="n">Insert</span> <span class="n">animation</span> <span class="n">code</span> <span class="n">here</span><span class="o">...</span>
+    <span class="n">public</span> <span class="n">class</span> <span class="n">OnClickAction</span> <span class="n">implements</span> <span class="n">EventListener</span> <span class="p">{</span>
+        <span class="n">public</span> <span class="n">void</span> <span class="n">handleEvent</span><span class="p">(</span><span class="n">Event</span> <span class="n">evt</span><span class="p">)</span> <span class="p">{</span>
+            <span class="sr">//</span> <span class="n">Perform</span> <span class="n">some</span> <span class="n">actions</span> <span class="n">here</span><span class="o">...</span>
+
+            <span class="sr">//</span> <span class="o">...</span><span class="k">for</span> <span class="n">example</span> <span class="n">schedule</span> <span class="n">an</span> <span class="n">action</span> <span class="k">for</span> <span class="n">later:</span>
+            <span class="n">window</span><span class="o">.</span><span class="n">setTimeout</span><span class="p">(</span><span class="k">new</span> <span class="n">DelayedTask</span><span class="p">(),</span> <span class="mi">500</span><span class="p">);</span>
+        <span class="p">}</span>
     <span class="p">}</span>
-<span class="p">}</span>
 
-<span class="n">public</span> <span class="n">class</span> <span class="n">DelayedTask</span> <span class="n">implements</span> <span class="n">Runnable</span> <span class="p">{</span>
-    <span class="n">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span> <span class="p">{</span>
-        <span class="sr">//</span> <span class="n">Perform</span> <span class="n">some</span> <span class="n">actions</span> <span class="n">here</span><span class="o">...</span>
+    <span class="n">public</span> <span class="n">class</span> <span class="n">Animation</span> <span class="n">implements</span> <span class="n">Runnable</span> <span class="p">{</span>
+        <span class="n">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span> <span class="p">{</span>
+            <span class="sr">//</span> <span class="n">Insert</span> <span class="n">animation</span> <span class="n">code</span> <span class="n">here</span><span class="o">...</span>
+        <span class="p">}</span>
+    <span class="p">}</span>
 
-        <span class="sr">//</span> <span class="o">...</span><span class="k">for</span> <span class="n">example</span> <span class="n">displays</span> <span class="n">an</span> <span class="n">alert</span> <span class="n">dialog:</span>
-        <span class="n">window</span><span class="o">.</span><span class="n">alert</span><span class="p">(</span><span class="s">&quot;Delayed Action invoked!&quot;</span><span class="p">);</span>
+    <span class="n">public</span> <span class="n">class</span> <span class="n">DelayedTask</span> <span class="n">implements</span> <span class="n">Runnable</span> <span class="p">{</span>
+        <span class="n">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span> <span class="p">{</span>
+            <span class="sr">//</span> <span class="n">Perform</span> <span class="n">some</span> <span class="n">actions</span> <span class="n">here</span><span class="o">...</span>
+
+            <span class="sr">//</span> <span class="o">...</span><span class="k">for</span> <span class="n">example</span> <span class="n">displays</span> <span class="n">an</span> <span class="n">alert</span> <span class="n">dialog:</span>
+            <span class="n">window</span><span class="o">.</span><span class="n">alert</span><span class="p">(</span><span class="s">&quot;Delayed Action invoked!&quot;</span><span class="p">);</span>
+        <span class="p">}</span>
     <span class="p">}</span>
 <span class="p">}</span>
 </pre></div>
 
 
-<p>}</p>
-<h2 id="writing-thread-safe-code-wzxhzdk3wzxhzdk4">Writing thread-safe code <a id="Threads"></a></h2>
+<h2 id="writing-thread-safe-code-wzxhzdk8wzxhzdk9">Writing thread-safe code <a id="Threads"></a></h2>
 <p>The DOM listeners registered on the SVG document are called from the canvas update thread. To avoid race conditions, do not manipulate the DOM tree from another thread.</p>
-<p>The way to switch from an external thread to the canvas update thread is to use the following code:
-// Returns immediately
-canvas.getUpdateManager().getUpdateRunnableQueue().
-    invokeLater(new Runnable() {
-        public void run() {
-            // Insert some actions on the DOM here
-        }
-    });
-or:
-// Waits until the Runnable is invoked
-canvas.getUpdateManager().getUpdateRunnableQueue().
-    invokeAndWait(new Runnable() {
-        public void run() {
-            // Insert some actions on the DOM here
-        }
-    });
-Like with event listeners, when a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runnable.html">Runnable</a> is invoked from the update thread, the graphics are updated.
+<p>The way to switch from an external thread to the canvas update thread is to use the following code:</p>
+<div class="codehilite"><pre><span class="sr">//</span> <span class="n">Returns</span> <span class="n">immediately</span>
+<span class="n">canvas</span><span class="o">.</span><span class="n">getUpdateManager</span><span class="p">()</span><span class="o">.</span><span class="n">getUpdateRunnableQueue</span><span class="p">()</span><span class="o">.</span>
+    <span class="n">invokeLater</span><span class="p">(</span><span class="k">new</span> <span class="n">Runnable</span><span class="p">()</span> <span class="p">{</span>
+        <span class="n">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span> <span class="p">{</span>
+            <span class="sr">//</span> <span class="n">Insert</span> <span class="n">some</span> <span class="n">actions</span> <span class="n">on</span> <span class="n">the</span> <span class="n">DOM</span> <span class="n">here</span>
+        <span class="p">}</span>
+    <span class="p">});</span>
+<span class="ow">or</span><span class="p">:</span>
+<span class="sr">//</span> <span class="n">Waits</span> <span class="k">until</span> <span class="n">the</span> <span class="n">Runnable</span> <span class="n">is</span> <span class="n">invoked</span>
+<span class="n">canvas</span><span class="o">.</span><span class="n">getUpdateManager</span><span class="p">()</span><span class="o">.</span><span class="n">getUpdateRunnableQueue</span><span class="p">()</span><span class="o">.</span>
+    <span class="n">invokeAndWait</span><span class="p">(</span><span class="k">new</span> <span class="n">Runnable</span><span class="p">()</span> <span class="p">{</span>
+        <span class="n">public</span> <span class="n">void</span> <span class="n">run</span><span class="p">()</span> <span class="p">{</span>
+            <span class="sr">//</span> <span class="n">Insert</span> <span class="n">some</span> <span class="n">actions</span> <span class="n">on</span> <span class="n">the</span> <span class="n">DOM</span> <span class="n">here</span>
+        <span class="p">}</span>
+    <span class="p">});</span>
+</pre></div>
+
+
+<p>Like with event listeners, when a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Runnable.html">Runnable</a> is invoked from the update thread, the graphics are updated.
 It is very dangerous to call <code>invokeAndWait</code> from the Swing event thread. This is the only thread that can be used to interact with Swing components. In some cases DOM calls may need to query the canvas for information (such as actual Swing component size, etc). If this happens you will get a thread deadlock. You should only make invokeAndWait calls from “third party” threads.</p>
-<h1 id="using-the-svg-dom-wzxhzdk5wzxhzdk6">Using the SVG DOM <a id="SVGDOM"></a></h1>
-<p>Batik provides a fairly complete implementation of the SVG DOM. The SVG DOM provides all the functionality most applications require. In particular, the DOM implements DOM Events, including mutation and UI Events. As an example, DOM events allow you to get notified when the cursor moves over elements of interest:
-// Element of Interest.
-Element theElem = ...;
-((EventTarget) theElem).addEventListener("mouseover", mouseOverListener, true);
-where <code>mouseOverListener</code> implements the <a href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/events/EventListener.html">org.w3c.dom.events.EventListener</a> interface. This interface consists of the method:
-void handleEvent(Event evt);
-This is called whenever the event occurs with the element it is registered on. It is worth reviewing the DOM Events specification as there are many useful features to this interface that are not immediately obvious.</p>
-<p>It is also worth looking at the DOM interfaces defined by the SVG specification as they provide a large number of useful methods, in particular those of <a href="../../javadoc/org/w3c/dom/svg/SVGLocatable.html">SVGLocatable</a> :
-// Returns Bounding box of SVG Element.
-public SVGRect   getBBox (  );
-// Returns the transformation matrix to the screen.
-public SVGMatrix getScreenCTM (  );
-// Returns the transformation matrix to the given element.
-public SVGMatrix getTransformToElement ( SVGElement element )
-                throws SVGException;
-In particular, <code>getScreenCTM</code> is very useful for taking the <code>clientX</code> and <code>clientY</code> attributes of the DOM <a href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/events/UIEvent.html">UIEvent</a> and mapping them to the coordinate system of an element in the SVG document:
-SVGMatrix mat  = elem.getScreenCTM();
-SVGMatrix imat = mat.inverse();
-SVGPoint  cPt  = document.getRootElement().createSVGPoint();
-cPt.setX(uiEvt.getClientX());
-cPt.setY(uiEvt.getClientY());
-cPt = cPt.matrixTransform(imat);</p>
-<h1 id="referencing-java-code-from-a-document-wzxhzdk7wzxhzdk8">Referencing Java code from a document <a id="javaInDocument"></a></h1>
+<h1 id="using-the-svg-dom-wzxhzdk10wzxhzdk11">Using the SVG DOM <a id="SVGDOM"></a></h1>
+<p>Batik provides a fairly complete implementation of the SVG DOM. The SVG DOM provides all the functionality most applications require. In particular, the DOM implements DOM Events, including mutation and UI Events. As an example, DOM events allow you to get notified when the cursor moves over elements of interest:</p>
+<div class="codehilite"><pre><span class="sr">//</span> <span class="n">Element</span> <span class="n">of</span> <span class="n">Interest</span><span class="o">.</span>
+<span class="n">Element</span> <span class="n">theElem</span> <span class="o">=</span> <span class="o">...</span><span class="p">;</span>
+<span class="p">((</span><span class="n">EventTarget</span><span class="p">)</span> <span class="n">theElem</span><span class="p">)</span><span class="o">.</span><span class="n">addEventListener</span><span class="p">(</span><span class="s">&quot;mouseover&quot;</span><span class="p">,</span> <span class="n">mouseOverListener</span><span class="p">,</span> <span class="n">true</span><span class="p">);</span>
+</pre></div>
+
+
+<p>where <code>mouseOverListener</code> implements the <a href="http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/events/EventListener.html">org.w3c.dom.events.EventListener</a> interface. This interface consists of the method:</p>
+<div class="codehilite"><pre><span class="n">void</span> <span class="n">handleEvent</span><span class="p">(</span><span class="n">Event</span> <span class="n">evt</span><span class="p">);</span>
+</pre></div>
+
+
+<p>This is called whenever the event occurs with the element it is registered on. It is worth reviewing the DOM Events specification as there are many useful features to this interface that are not immediately obvious.</p>
+<p>It is also worth looking at the DOM interfaces defined by the SVG specification as they provide a large number of useful methods, in particular those of <a href="../../javadoc/org/w3c/dom/svg/SVGLocatable.html">SVGLocatable</a> :</p>
+<div class="codehilite"><pre><span class="sr">//</span> <span class="n">Returns</span> <span class="n">Bounding</span> <span class="n">box</span> <span class="n">of</span> <span class="n">SVG</span> <span class="n">Element</span><span class="o">.</span>
+<span class="n">public</span> <span class="n">SVGRect</span>   <span class="n">getBBox</span> <span class="p">(</span>  <span class="p">);</span>
+<span class="sr">//</span> <span class="n">Returns</span> <span class="n">the</span> <span class="n">transformation</span> <span class="n">matrix</span> <span class="n">to</span> <span class="n">the</span> <span class="n">screen</span><span class="o">.</span>
+<span class="n">public</span> <span class="n">SVGMatrix</span> <span class="n">getScreenCTM</span> <span class="p">(</span>  <span class="p">);</span>
+<span class="sr">//</span> <span class="n">Returns</span> <span class="n">the</span> <span class="n">transformation</span> <span class="n">matrix</span> <span class="n">to</span> <span class="n">the</span> <span class="n">given</span> <span class="n">element</span><span class="o">.</span>
+<span class="n">public</span> <span class="n">SVGMatrix</span> <span class="n">getTransformToElement</span> <span class="p">(</span> <span class="n">SVGElement</span> <span class="n">element</span> <span class="p">)</span>
+                <span class="n">throws</span> <span class="n">SVGException</span><span class="p">;</span>
+<span class="n">In</span> <span class="n">particular</span><span class="p">,</span> <span class="sb">`getScreenCTM`</span> <span class="n">is</span> <span class="n">very</span> <span class="n">useful</span> <span class="k">for</span> <span class="n">taking</span> <span class="n">the</span> <span class="sb">`clientX`</span> <span class="ow">and</span> <span class="sb">`clientY`</span> <span class="n">attributes</span> <span class="n">of</span> <span class="n">the</span> <span class="n">DOM</span> <span class="p">[</span><span class="n">UIEvent</span><span class="p">](</span><span class="n">http:</span><span class="sr">//</span><span class="n">java</span><span class="o">.</span><span class="n">sun</span><span class="o">.</span><span class="n">com</span><span class="sr">/j2se/</span><span class="mf">1.5.0</span><span class="sr">/docs/</span><span class="n">api</span><span class="sr">/org/</span><span class="n">w3c</span><span class="sr">/dom/</span><span class="n">events</span>
 <span class="o">/</span><span class="n">UIEvent</span><span class="o">.</span><span class="n">html</span><span class="p">)</span> <span class="ow">and</span> <span class="n">mapping</span> <span class="n">them</span> <span class="n">to</span> <span class="n">the</span> <span class="n">coordinate</span> <span class="nb">system</span> <span class="n">of</span> <span class="n">an</span> <span class="n">element</span> <span class="n">in</span> <span class="n">the</span> <span class="n">SVG</span> <span class="n">document:</span>
+<span class="n">SVGMatrix</span> <span class="n">mat</span>  <span class="o">=</span> <span class="n">elem</span><span class="o">.</span><span class="n">getScreenCTM</span><span class="p">();</span>
+<span class="n">SVGMatrix</span> <span class="n">imat</span> <span class="o">=</span> <span class="n">mat</span><span class="o">.</span><span class="n">inverse</span><span class="p">();</span>
+<span class="n">SVGPoint</span>  <span class="n">cPt</span>  <span class="o">=</span> <span class="n">document</span><span class="o">.</span><span class="n">getRootElement</span><span class="p">()</span><span class="o">.</span><span class="n">createSVGPoint</span><span class="p">();</span>
+<span class="n">cPt</span><span class="o">.</span><span class="n">setX</span><span class="p">(</span><span class="n">uiEvt</span><span class="o">.</span><span class="n">getClientX</span><span class="p">());</span>
+<span class="n">cPt</span><span class="o">.</span><span class="n">setY</span><span class="p">(</span><span class="n">uiEvt</span><span class="o">.</span><span class="n">getClientY</span><span class="p">());</span>
+<span class="n">cPt</span> <span class="o">=</span> <span class="n">cPt</span><span class="o">.</span><span class="n">matrixTransform</span><span class="p">(</span><span class="n">imat</span><span class="p">);</span>
+</pre></div>
+
+
+<h1 id="referencing-java-code-from-a-document-wzxhzdk12wzxhzdk13">Referencing Java code from a document <a id="javaInDocument"></a></h1>
 <p>Batik implements the Java bindings for SVG, and thus allows Java code to be referenced from <code>script</code> elements. This feature is available to any application of Batik that uses the bridge module (for example the Swing component and the transcoders).</p>
 <p>In order to use this extension, the <code>type</code> attribute of a <code>script</code> element must be set to <code>application/java-archive</code> . In addition, the <code>xlink:href</code> attribute must be the URI of a jar file that contains the code to run.</p>
-<p>The manifest of this jar file must contains an entry of the form:
-SVG-Handler-Class: classname
-where <em>classname</em> must be the name of a class that implements the <a href="../../javadoc/org/w3c/dom/svg/EventListenerInitializer.html">org.w3c.dom.svg.EventListenerInitializer</a> interface. Just before the document <code>SVGLoad</code> event is fired, an instance of this class is created, and this instance has its <code>initializeEventListeners</code> method invoked. Note that there is no way to specify Java handlers in event attributes on SVG elements, so having the <code>initializeEventListeners</code> call <code>addEventListener</code> on a node is the only way to attach a Java listener from within the document.</p>
+<p>The manifest of this jar file must contains an entry of the form:</p>
+<div class="codehilite"><pre><span class="n">SVG</span><span class="o">-</span><span class="n">Handler</span><span class="o">-</span><span class="n">Class:</span> <span class="n">classname</span>
+</pre></div>
+
+
+<p>where <em>classname</em> must be the name of a class that implements the <a href="../../javadoc/org/w3c/dom/svg/EventListenerInitializer.html">org.w3c.dom.svg.EventListenerInitializer</a> interface. Just before the document <code>SVGLoad</code> event is fired, an instance of this class is created, and this instance has its <code>initializeEventListeners</code> method invoked. Note that there is no way to specify Java handlers in event attributes on SVG elements, so having the <code>initializeEventListeners</code> call <code>addEventListener</code> on a node is the only way to attach a Java listener from within the document.</p>
 <p>The class specified by <code>SVG-Handler-Class</code> can be contained directly in the jar file, but it is also possible for it to be contained in a jar file added to the classpath using the <code>Class-Path</code> entry of the manifest.</p></div>
       </div>
       



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


Mime
View raw message