KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > bridge > ScriptingEnvironment


1 /*
2
3    Copyright 2002-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.bridge;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.io.StringReader JavaDoc;
28 import java.io.UnsupportedEncodingException JavaDoc;
29 import java.io.Writer JavaDoc;
30
31 import java.net.URL JavaDoc;
32 import java.net.URLConnection JavaDoc;
33
34 import java.util.HashMap JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Timer JavaDoc;
37 import java.util.TimerTask JavaDoc;
38 import java.util.zip.GZIPOutputStream JavaDoc;
39 import java.util.zip.DeflaterOutputStream JavaDoc;
40
41 import org.apache.batik.dom.GenericDOMImplementation;
42 import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
43 import org.apache.batik.dom.svg.SVGOMDocument;
44 import org.apache.batik.dom.util.SAXDocumentFactory;
45 import org.apache.batik.dom.util.XLinkSupport;
46 import org.apache.batik.script.Interpreter;
47 import org.apache.batik.script.InterpreterException;
48 import org.apache.batik.util.EncodingUtilities;
49 import org.apache.batik.util.ParsedURL;
50 import org.apache.batik.util.RunnableQueue;
51 import org.apache.batik.util.SVGConstants;
52 import org.apache.batik.util.XMLResourceDescriptor;
53
54 import org.w3c.dom.Document JavaDoc;
55 import org.w3c.dom.Element JavaDoc;
56 import org.w3c.dom.Node JavaDoc;
57 import org.w3c.dom.events.Event JavaDoc;
58 import org.w3c.dom.events.EventListener JavaDoc;
59 import org.w3c.dom.events.EventTarget JavaDoc;
60 import org.w3c.dom.events.MutationEvent JavaDoc;
61 import org.w3c.dom.svg.SVGDocument;
62
63 /**
64  * This class contains the informations needed by the SVG scripting.
65  *
66  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
67  * @version $Id: ScriptingEnvironment.java,v 1.49 2005/04/02 14:26:09 deweese Exp $
68  */

69 public class ScriptingEnvironment extends BaseScriptingEnvironment {
70
71     /**
72      * Used in 'parseXML()'.
73      */

74     protected final static String JavaDoc FRAGMENT_PREFIX =
75         "<svg xmlns='" +
76         SVGConstants.SVG_NAMESPACE_URI +
77         "' xmlns:xlink='" +
78         XLinkSupport.XLINK_NAMESPACE_URI +
79         "'>";
80
81     protected final static String JavaDoc FRAGMENT_SUFFIX =
82         "</svg>";
83
84     public final static String JavaDoc [] SVG_EVENT_ATTRS = {
85         "onabort", // SVG element
86
"onerror", // SVG element
87
"onresize", // SVG element
88
"onscroll", // SVG element
89
"onunload", // SVG element
90
"onzoom", // SVG element
91

92         "onbegin", // SMIL
93
"onend", // SMIL
94
"onrepeat", // SMIL
95

96         "onfocusin", // UI Events
97
"onfocusout", // UI Events
98
"onactivate", // UI Events
99
"onclick", // UI Events
100

101         "onmousedown", // UI Events
102
"onmouseup", // UI Events
103
"onmouseover", // UI Events
104
"onmouseout", // UI Events
105
"onmousemove", // UI Events
106

107         "onkeypress", // UI Events
108
"onkeydown", // UI Events
109
"onkeyup" // UI Events
110
};
111
112     public final static String JavaDoc [] SVG_DOM_EVENT = {
113         "SVGAbort", // SVG element
114
"SVGError", // SVG element
115
"SVGResize", // SVG element
116
"SVGScroll", // SVG element
117
"SVGUnload", // SVG element
118
"SVGZoom", // SVG element
119

120         "beginEvent", // SMIL
121
"endEvent", // SMIL
122
"repeatEvent", // SMIL
123

124         "DOMFocusIn", // UI Events
125
"DOMFocusOut", // UI Events
126
"DOMActivate", // UI Events
127
"click", // UI Events
128
"mousedown", // UI Events
129
"mouseup", // UI Events
130
"mouseover", // UI Events
131
"mouseout", // UI Events
132
"mousemove", // UI Events
133
"keypress", // UI Events
134
"keydown", // UI Events
135
"keyup" // UI Events
136
};
137
138     /**
139      * The timer for periodic or delayed tasks.
140      */

141     protected Timer JavaDoc timer = new Timer JavaDoc(true);
142
143     /**
144      * The update manager.
145      */

146     protected UpdateManager updateManager;
147
148     /**
149      * The update runnable queue.
150      */

151     protected RunnableQueue updateRunnableQueue;
152
153     /**
154      * The DOMNodeInserted event listener.
155      */

156     protected EventListener JavaDoc domNodeInsertedListener
157         = new DOMNodeInsertedListener();
158
159     /**
160      * The DOMNodeRemoved event listener.
161      */

162     protected EventListener JavaDoc domNodeRemovedListener
163         = new DOMNodeRemovedListener();
164
165     /**
166      * The DOMAttrModified event listener.
167      */

168     protected EventListener JavaDoc domAttrModifiedListener
169         = new DOMAttrModifiedListener();
170
171     /**
172      * The SVGAbort event listener.
173      */

174     protected EventListener JavaDoc svgAbortListener =
175         new ScriptingEventListener("onabort");
176
177     /**
178      * The SVGError event listener.
179      */

180     protected EventListener JavaDoc svgErrorListener =
181         new ScriptingEventListener("onerror");
182
183     /**
184      * The SVGResize event listener.
185      */

186     protected EventListener JavaDoc svgResizeListener =
187         new ScriptingEventListener("onresize");
188
189     /**
190      * The SVGScroll event listener.
191      */

192     protected EventListener JavaDoc svgScrollListener =
193         new ScriptingEventListener("onscroll");
194
195     /**
196      * The SVGUnload event listener.
197      */

198     protected EventListener JavaDoc svgUnloadListener =
199         new ScriptingEventListener("onunload");
200
201     /**
202      * The SVGZoom event listener.
203      */

204     protected EventListener JavaDoc svgZoomListener =
205         new ScriptingEventListener("onzoom");
206
207     /**
208      * The begin event listener.
209      */

210     protected EventListener JavaDoc beginListener =
211         new ScriptingEventListener("onbegin");
212
213     /**
214      * The end event listener.
215      */

216     protected EventListener JavaDoc endListener =
217         new ScriptingEventListener("onend");
218
219     /**
220      * The repeat event listener.
221      */

222     protected EventListener JavaDoc repeatListener =
223         new ScriptingEventListener("onrepeat");
224
225     /**
226      * The focusin event listener.
227      */

228     protected EventListener JavaDoc focusinListener =
229         new ScriptingEventListener("onfocusin");
230
231     /**
232      * The focusout event listener.
233      */

234     protected EventListener JavaDoc focusoutListener =
235         new ScriptingEventListener("onfocusout");
236
237     /**
238      * The activate event listener.
239      */

240     protected EventListener JavaDoc activateListener =
241         new ScriptingEventListener("onactivate");
242
243     /**
244      * The click event listener.
245      */

246     protected EventListener JavaDoc clickListener =
247         new ScriptingEventListener("onclick");
248
249     /**
250      * The mousedown event listener.
251      */

252     protected EventListener JavaDoc mousedownListener =
253         new ScriptingEventListener("onmousedown");
254
255     /**
256      * The mouseup event listener.
257      */

258     protected EventListener JavaDoc mouseupListener =
259         new ScriptingEventListener("onmouseup");
260
261     /**
262      * The mouseover event listener.
263      */

264     protected EventListener JavaDoc mouseoverListener =
265         new ScriptingEventListener("onmouseover");
266
267     /**
268      * The mouseout event listener.
269      */

270     protected EventListener JavaDoc mouseoutListener =
271         new ScriptingEventListener("onmouseout");
272
273     /**
274      * The mousemove event listener.
275      */

276     protected EventListener JavaDoc mousemoveListener =
277         new ScriptingEventListener("onmousemove");
278
279     /**
280      * The keypress event listener.
281      */

282     protected EventListener JavaDoc keypressListener =
283         new ScriptingEventListener("onkeypress");
284
285     /**
286      * The keydown event listener.
287      */

288     protected EventListener JavaDoc keydownListener =
289         new ScriptingEventListener("onkeydown");
290
291     /**
292      * The keyup event listener.
293      */

294     protected EventListener JavaDoc keyupListener =
295         new ScriptingEventListener("onkeyup");
296
297     
298     protected EventListener JavaDoc [] listeners = {
299         svgAbortListener,
300         svgErrorListener,
301         svgResizeListener,
302         svgScrollListener,
303         svgUnloadListener,
304         svgZoomListener,
305
306         beginListener,
307         endListener,
308         repeatListener,
309
310         focusinListener,
311         focusoutListener,
312         activateListener,
313         clickListener,
314
315         mousedownListener,
316         mouseupListener,
317         mouseoverListener,
318         mouseoutListener,
319         mousemoveListener,
320
321         keypressListener,
322         keydownListener,
323         keyupListener
324     };
325
326     Map JavaDoc attrToDOMEvent = new HashMap JavaDoc(SVG_EVENT_ATTRS.length);
327     Map JavaDoc attrToListener = new HashMap JavaDoc(SVG_EVENT_ATTRS.length);
328     {
329         for (int i=0; i<SVG_EVENT_ATTRS.length; i++) {
330             attrToDOMEvent.put(SVG_EVENT_ATTRS[i], SVG_DOM_EVENT[i]);
331             attrToListener.put(SVG_EVENT_ATTRS[i], listeners[i]);
332         }
333     }
334
335     /**
336      * Creates a new ScriptingEnvironment.
337      * @param ctx the bridge context
338      */

339     public ScriptingEnvironment(BridgeContext ctx) {
340         super(ctx);
341         updateManager = ctx.getUpdateManager();
342         updateRunnableQueue = updateManager.getUpdateRunnableQueue();
343         
344         // Add the scripting listeners.
345
addScriptingListeners(document.getDocumentElement());
346
347         // Add the listeners responsible of updating the event attributes
348
EventTarget JavaDoc et = (EventTarget JavaDoc)document;
349         et.addEventListener("DOMNodeInserted",
350                             domNodeInsertedListener,
351                             false);
352         et.addEventListener("DOMNodeRemoved",
353                             domNodeRemovedListener,
354                             false);
355         et.addEventListener("DOMAttrModified",
356                             domAttrModifiedListener,
357                             false);
358     }
359
360     /**
361      * Creates a new Window object.
362      */

363     public org.apache.batik.script.Window createWindow(Interpreter interp,
364                                                        String JavaDoc lang) {
365         return new Window(interp, lang);
366     }
367
368     /**
369      * Runs an event handler.
370      */

371     public void runEventHandler(String JavaDoc script, Event JavaDoc evt,
372                                 String JavaDoc lang, String JavaDoc desc) {
373         Interpreter interpreter = getInterpreter(lang);
374         if (interpreter == null)
375             return;
376
377         try {
378             checkCompatibleScriptURL(lang, docPURL);
379
380             interpreter.bindObject(EVENT_NAME, evt);
381             interpreter.bindObject(ALTERNATE_EVENT_NAME, evt);
382             interpreter.evaluate(new StringReader JavaDoc(script), desc);
383         } catch (IOException JavaDoc ioe) {
384             // Do nothing, can't really happen with StringReader
385
} catch (InterpreterException ie) {
386             handleInterpreterException(ie);
387         } catch (SecurityException JavaDoc se) {
388             handleSecurityException(se);
389         }
390     }
391
392     /**
393      * Interrupts the periodic tasks and dispose this ScriptingEnvironment.
394      */

395     public void interrupt() {
396         timer.cancel();
397         // Remove the scripting listeners.
398
removeScriptingListeners(document.getDocumentElement());
399
400         // Remove the listeners responsible of updating the event attributes
401
EventTarget JavaDoc et = (EventTarget JavaDoc)document;
402         et.removeEventListener("DOMNodeInserted",
403                                domNodeInsertedListener,
404                                false);
405         et.removeEventListener("DOMNodeRemoved",
406                                domNodeRemovedListener,
407                                false);
408         et.removeEventListener("DOMAttrModified",
409                                domAttrModifiedListener,
410                                false);
411     }
412
413     /**
414      * Adds the scripting listeners to the given element.
415      */

416     protected void addScriptingListeners(Node JavaDoc node) {
417         if (node.getNodeType() == Node.ELEMENT_NODE) {
418             // Attach the listeners
419
Element JavaDoc elt = (Element JavaDoc)node;
420             EventTarget JavaDoc target = (EventTarget JavaDoc)elt;
421             if (SVGConstants.SVG_NAMESPACE_URI.equals(elt.getNamespaceURI())) {
422                 if (SVGConstants.SVG_SVG_TAG.equals(elt.getLocalName())) {
423                     // <svg> listeners
424
if (elt.hasAttributeNS(null, "onabort")) {
425                         target.addEventListener("SVGAbort",
426                                                 svgAbortListener, false);
427                     }
428                     if (elt.hasAttributeNS(null, "onerror")) {
429                         target.addEventListener("SVGError",
430                                                 svgErrorListener, false);
431                     }
432                     if (elt.hasAttributeNS(null, "onresize")) {
433                         target.addEventListener("SVGResize",
434                                                 svgResizeListener, false);
435                     }
436                     if (elt.hasAttributeNS(null, "onscroll")) {
437                         target.addEventListener("SVGScroll",
438                                             svgScrollListener, false);
439                     }
440                     if (elt.hasAttributeNS(null, "onunload")) {
441                         target.addEventListener("SVGUnload",
442                                                 svgUnloadListener, false);
443                     }
444                     if (elt.hasAttributeNS(null, "onzoom")) {
445                         target.addEventListener("SVGZoom",
446                                                 svgZoomListener, false);
447                     }
448                 } else {
449                     String JavaDoc name = elt.getLocalName();
450                     if (name.equals(SVGConstants.SVG_SET_TAG) ||
451                         name.startsWith("animate")) {
452                         // animation listeners
453
if (elt.hasAttributeNS(null, "onbegin")) {
454                             target.addEventListener("beginEvent",
455                                                     beginListener ,
456                                                     false);
457                         }
458                         if (elt.hasAttributeNS(null, "onend")) {
459                             target.addEventListener("endEvent",
460                                                     endListener,
461                                                     false);
462                         }
463                         if (elt.hasAttributeNS(null, "onrepeat")) {
464                             target.addEventListener("repeatEvent",
465                                                     repeatListener ,
466                                                     false);
467                         }
468                         return;
469                     }
470                 }
471             }
472
473             // UI listeners
474
if (elt.hasAttributeNS(null, "onfocusin")) {
475                 target.addEventListener("DOMFocusIn", focusinListener, false);
476             }
477             if (elt.hasAttributeNS(null, "onfocusout")) {
478                 target.addEventListener("DOMFocusOut", focusoutListener,
479                                         false);
480             }
481             if (elt.hasAttributeNS(null, "onactivate")) {
482                 target.addEventListener("DOMActivate", activateListener,
483                                         false);
484             }
485             if (elt.hasAttributeNS(null, "onclick")) {
486                 target.addEventListener("click", clickListener, false);
487             }
488             if (elt.hasAttributeNS(null, "onmousedown")) {
489                 target.addEventListener("mousedown", mousedownListener, false);
490             }
491             if (elt.hasAttributeNS(null, "onmouseup")) {
492                 target.addEventListener("mouseup", mouseupListener, false);
493             }
494             if (elt.hasAttributeNS(null, "onmouseover")) {
495                 target.addEventListener("mouseover", mouseoverListener, false);
496             }
497             if (elt.hasAttributeNS(null, "onmouseout")) {
498                 target.addEventListener("mouseout", mouseoutListener, false);
499             }
500             if (elt.hasAttributeNS(null, "onmousemove")) {
501                 target.addEventListener("mousemove", mousemoveListener, false);
502             }
503             if (elt.hasAttributeNS(null, "onkeypress")) {
504                 target.addEventListener("keypress", keypressListener, false);
505             }
506             if (elt.hasAttributeNS(null, "onkeydown")) {
507                 target.addEventListener("keydown", keydownListener, false);
508             }
509             if (elt.hasAttributeNS(null, "onkeyup")) {
510                 target.addEventListener("keyup", keyupListener, false);
511             }
512         }
513
514         // Adds the listeners to the children
515
for (Node JavaDoc n = node.getFirstChild();
516              n != null;
517              n = n.getNextSibling()) {
518             addScriptingListeners(n);
519         }
520     }
521
522     /**
523      * Removes the scripting listeners from the given element.
524      */

525     protected void removeScriptingListeners(Node JavaDoc node) {
526         if (node.getNodeType() == Node.ELEMENT_NODE) {
527             // Detach the listeners
528
Element JavaDoc elt = (Element JavaDoc)node;
529             EventTarget JavaDoc target = (EventTarget JavaDoc)elt;
530             if (SVGConstants.SVG_NAMESPACE_URI.equals(elt.getNamespaceURI())) {
531                 if (SVGConstants.SVG_SVG_TAG.equals(elt.getLocalName())) {
532                     // <svg> listeners
533
target.removeEventListener("SVGAbort",
534                                                svgAbortListener, false);
535                     target.removeEventListener("SVGError",
536                                                svgErrorListener, false);
537                     target.removeEventListener("SVGResize",
538                                                svgResizeListener, false);
539                     target.removeEventListener("SVGScroll",
540                                                svgScrollListener, false);
541                     target.removeEventListener("SVGUnload",
542                                                svgUnloadListener, false);
543                     target.removeEventListener("SVGZoom",
544                                                svgZoomListener, false);
545                 } else {
546                     String JavaDoc name = elt.getLocalName();
547                     if (name.equals(SVGConstants.SVG_SET_TAG) ||
548                         name.startsWith("animate")) {
549                         // animation listeners
550
target.removeEventListener("beginEvent",
551                                                    beginListener ,
552                                                    false);
553                         target.removeEventListener("endEvent",
554                                                    endListener,
555                                                    false);
556                         target.removeEventListener("repeatEvent",
557                                                    repeatListener ,
558                                                    false);
559                         return;
560                     }
561                 }
562             }
563
564             // UI listeners
565
target.removeEventListener("DOMFocusIn", focusinListener, false);
566             target.removeEventListener("DOMFocusOut", focusoutListener, false);
567             target.removeEventListener("DOMActivate", activateListener, false);
568             target.removeEventListener("click", clickListener, false);
569             target.removeEventListener("mousedown", mousedownListener, false);
570             target.removeEventListener("mouseup", mouseupListener, false);
571             target.removeEventListener("mouseover", mouseoverListener, false);
572             target.removeEventListener("mouseout", mouseoutListener, false);
573             target.removeEventListener("mousemove", mousemoveListener, false);
574             target.removeEventListener("keypress", keypressListener, false);
575             target.removeEventListener("keydown", keydownListener, false);
576             target.removeEventListener("keyup", keyupListener, false);
577         }
578
579         // Removes the listeners from the children
580
for (Node JavaDoc n = node.getFirstChild();
581              n != null;
582              n = n.getNextSibling()) {
583             removeScriptingListeners(n);
584         }
585     }
586
587     /**
588      * Updates the registration of a listener on the given element.
589      */

590     protected void updateScriptingListeners(Element JavaDoc elt, String JavaDoc attr) {
591         String JavaDoc domEvt = (String JavaDoc) attrToDOMEvent.get(attr);
592         if (domEvt == null) return; // Not an event attr.
593
EventListener JavaDoc listener = (EventListener JavaDoc)attrToListener.get(attr);
594         EventTarget JavaDoc target = (EventTarget JavaDoc) elt;
595         if (elt.hasAttributeNS(null, attr))
596             target.addEventListener(domEvt, listener, false);
597         else
598             target.removeEventListener(domEvt, listener, false);
599     }
600     
601
602     /**
603      * To interpret a script.
604      */

605     protected class EvaluateRunnable implements Runnable JavaDoc {
606         protected Interpreter interpreter;
607         protected String JavaDoc script;
608         public EvaluateRunnable(String JavaDoc s, Interpreter interp) {
609             interpreter = interp;
610             script = s;
611         }
612         public void run() {
613             try {
614                 interpreter.evaluate(script);
615             } catch (InterpreterException ie) {
616                 handleInterpreterException(ie);
617             }
618         }
619     }
620
621     /**
622      * To interpret a script.
623      */

624     protected class EvaluateIntervalRunnable implements Runnable JavaDoc {
625         /**
626          * Incremented each time this runnable is added to the queue.
627          */

628         public int count;
629         public boolean error;
630
631         protected Interpreter interpreter;
632         protected String JavaDoc script;
633
634         public EvaluateIntervalRunnable(String JavaDoc s, Interpreter interp) {
635             interpreter = interp;
636             script = s;
637         }
638         public void run() {
639             synchronized (this) {
640                 if (error)
641                     return;
642                 count--;
643             }
644             try {
645                 interpreter.evaluate(script);
646             } catch (InterpreterException ie) {
647                 handleInterpreterException(ie);
648                 synchronized (this) {
649                     error = true;
650                 }
651             } catch (Exception JavaDoc e) {
652                 if (userAgent != null) {
653                     userAgent.displayError(e);
654                 } else {
655                     e.printStackTrace(); // No UA so just output...
656
}
657                 synchronized (this) {
658                     error = true;
659                 }
660             }
661         }
662     }
663
664     /**
665      * To call a Runnable.
666      */

667     protected class EvaluateRunnableRunnable implements Runnable JavaDoc {
668         /**
669          * Incremented each time this runnable is put in the queue.
670          */

671         public int count;
672         public boolean error;
673
674         protected Runnable JavaDoc runnable;
675
676         public EvaluateRunnableRunnable(Runnable JavaDoc r) {
677             runnable = r;
678         }
679         public void run() {
680             synchronized (this) {
681                 if (error)
682                     return;
683                 count--;
684             }
685             try {
686                 runnable.run();
687             } catch (Exception JavaDoc e) {
688                 if (userAgent != null) {
689                     userAgent.displayError(e);
690                 } else {
691                     e.printStackTrace(); // No UA so just output...
692
}
693                 synchronized (this) {
694                     error = true;
695                 }
696             }
697         }
698     }
699
700     /**
701      * Represents the window object of this environment.
702      */

703     protected class Window implements org.apache.batik.script.Window {
704
705         /**
706          * The associated interpreter.
707          */

708         protected Interpreter interpreter;
709
710         /**
711          * The associated language.
712          */

713         protected String JavaDoc language;
714
715         /**
716          * Creates a new Window for the given language.
717          */

718         public Window(Interpreter interp, String JavaDoc lang) {
719             interpreter = interp;
720             language = lang;
721         }
722
723         /**
724          * Implements {@link
725          * org.apache.batik.script.Window#setInterval(String,long)}.
726          */

727         public Object JavaDoc setInterval(final String JavaDoc script, long interval) {
728             TimerTask JavaDoc tt = new TimerTask JavaDoc() {
729                     EvaluateIntervalRunnable eir =
730                         new EvaluateIntervalRunnable(script, interpreter);
731                     public void run() {
732                         synchronized (eir) {
733                             if (eir.count > 1)
734                                 return;
735                             eir.count++;
736                         }
737                         synchronized (updateRunnableQueue.getIteratorLock()) {
738                             if (updateRunnableQueue.getThread() == null) {
739                                 cancel();
740                                 return;
741                             }
742                             updateRunnableQueue.invokeLater(eir);
743                         }
744                         synchronized (eir) {
745                             if (eir.error)
746                                 cancel();
747                         }
748                     }
749                 };
750
751             timer.schedule(tt, interval, interval);
752             return tt;
753         }
754
755         /**
756          * Implements {@link
757          * org.apache.batik.script.Window#setInterval(Runnable,long)}.
758          */

759         public Object JavaDoc setInterval(final Runnable JavaDoc r, long interval) {
760             TimerTask JavaDoc tt = new TimerTask JavaDoc() {
761                     EvaluateRunnableRunnable eihr =
762                         new EvaluateRunnableRunnable(r);
763                     public void run() {
764                         synchronized (eihr) {
765                             if (eihr.count > 1)
766                                 return;
767                             eihr.count++;
768                         }
769                         updateRunnableQueue.invokeLater(eihr);
770                         synchronized (eihr) {
771                             if (eihr.error)
772                                 cancel();
773                         }
774                     }
775                 };
776             
777             timer.schedule(tt, interval, interval);
778             return tt;
779         }
780
781         /**
782          * Implements {@link
783          * org.apache.batik.script.Window#clearInterval(Object)}.
784          */

785         public void clearInterval(Object JavaDoc interval) {
786             if (interval == null) return;
787             ((TimerTask JavaDoc)interval).cancel();
788         }
789
790         /**
791          * Implements {@link
792          * org.apache.batik.script.Window#setTimeout(String,long)}.
793          */

794         public Object JavaDoc setTimeout(final String JavaDoc script, long timeout) {
795             TimerTask JavaDoc tt = new TimerTask JavaDoc() {
796                     public void run() {
797                         updateRunnableQueue.invokeLater
798                             (new EvaluateRunnable(script, interpreter));
799                     }
800                 };
801
802             timer.schedule(tt, timeout);
803             return tt;
804         }
805
806         /**
807          * Implements {@link
808          * org.apache.batik.script.Window#setTimeout(Runnable,long)}.
809          */

810         public Object JavaDoc setTimeout(final Runnable JavaDoc r, long timeout) {
811             TimerTask JavaDoc tt = new TimerTask JavaDoc() {
812                     public void run() {
813                         updateRunnableQueue.invokeLater(new Runnable JavaDoc() {
814                                 public void run() {
815                                     try {
816                                         r.run();
817                                     } catch (Exception JavaDoc e) {
818                                         if (userAgent != null) {
819                                             userAgent.displayError(e);
820                                         }
821                                     }
822                                 }
823                             });
824                     }
825                 };
826
827             timer.schedule(tt, timeout);
828             return tt;
829         }
830
831         /**
832          * Implements {@link
833          * org.apache.batik.script.Window#clearTimeout(Object)}.
834          */

835         public void clearTimeout(Object JavaDoc timeout) {
836             if (timeout == null) return;
837             ((TimerTask JavaDoc)timeout).cancel();
838         }
839
840         /**
841          * Implements {@link
842          * org.apache.batik.script.Window#parseXML(String,Document)}.
843          */

844         public Node JavaDoc parseXML(String JavaDoc text, Document doc) {
845             // System.err.println("Text: " + text);
846
// Try and parse it as an SVGDocument
847
SAXSVGDocumentFactory df = new SAXSVGDocumentFactory
848                 (XMLResourceDescriptor.getXMLParserClassName());
849             URL JavaDoc urlObj = null;
850             if ((doc != null) && (doc instanceof SVGOMDocument))
851                 urlObj = ((SVGOMDocument)doc).getURLObject();
852             if (urlObj == null) {
853                 urlObj = ((SVGOMDocument)bridgeContext.getDocument()).
854                     getURLObject();
855             }
856             String JavaDoc uri = (urlObj==null)?"":urlObj.toString();
857             try {
858                 Document d = df.createDocument(uri, new StringReader JavaDoc(text));
859                 if (doc == null)
860                     return d;
861
862                 Node JavaDoc result = doc.createDocumentFragment();
863                 result.appendChild(doc.importNode(d.getDocumentElement(),
864                                                   true));
865                 return result;
866             } catch (Exception JavaDoc ex) {
867                 /* nothing */
868             }
869             
870             if ((doc != null) && (doc instanceof SVGOMDocument)) {
871                 // Try and parse with an 'svg' element wrapper - for
872
// things like '<rect ../>' - ensure that rect ends up
873
// in SVG namespace - xlink namespace is declared etc...
874

875                 // Only do this when generating a doc fragment, since
876
// a 'rect' element can not be root of SVG Document
877
// (only an svg element can be).
878
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(FRAGMENT_PREFIX.length() +
879                                                    text.length() +
880                                                    FRAGMENT_SUFFIX.length());
881                 sb.append(FRAGMENT_PREFIX);
882                 sb.append(text);
883                 sb.append(FRAGMENT_SUFFIX);
884                 String JavaDoc newText = sb.toString();
885                 try {
886                     Document d = df.createDocument
887                         (uri, new StringReader JavaDoc(newText));
888                     // No document given so make doc fragment from our
889
// new Document.
890
if (doc == null) doc = d;
891                     for (Node JavaDoc n = d.getDocumentElement().getFirstChild();
892                          n != null;
893                          n = n.getNextSibling()) {
894                         if (n.getNodeType() == Node.ELEMENT_NODE) {
895                             n = doc.importNode(n, true);
896                             Node JavaDoc result = doc.createDocumentFragment();
897                             result.appendChild(n);
898                             return result;
899                         }
900                     }
901                 } catch (Exception JavaDoc exc) {
902                     /* nothing - try something else*/
903                 }
904             }
905
906             // Parse as a generic XML document.
907
SAXDocumentFactory sdf;
908             if (doc != null) {
909                 sdf = new SAXDocumentFactory
910                     (doc.getImplementation(),
911                      XMLResourceDescriptor.getXMLParserClassName());
912             } else {
913                 sdf = new SAXDocumentFactory
914                     (new GenericDOMImplementation(),
915                      XMLResourceDescriptor.getXMLParserClassName());
916             }
917             try {
918                 Document d = sdf.createDocument(uri, new StringReader JavaDoc(text));
919                 if (doc == null)
920                     return d;
921
922                 Node JavaDoc result = doc.createDocumentFragment();
923                 result.appendChild(doc.importNode(d.getDocumentElement(),
924                                                   true));
925                 return result;
926             } catch (Exception JavaDoc ext) {
927                 if (userAgent != null)
928                     userAgent.displayError(ext);
929             }
930             
931             return null;
932         }
933
934         /**
935          * Implements {@link
936          * org.apache.batik.script.Window#getURL(String,org.apache.batik.script.Window.URLResponseHandler)}.
937          */

938         public void getURL(String JavaDoc uri, org.apache.batik.script.Window.URLResponseHandler h) {
939             getURL(uri, h, null);
940         }
941
942         final static String JavaDoc DEFLATE="deflate";
943         final static String JavaDoc GZIP ="gzip";
944         final static String JavaDoc UTF_8 ="UTF-8";
945         /**
946          * Implements {@link
947          * org.apache.batik.script.Window#getURL(String,org.apache.batik.script.Window.URLResponseHandler,String)}.
948          */

949         public void getURL(final String JavaDoc uri,
950                            final org.apache.batik.script.Window.URLResponseHandler h,
951                            final String JavaDoc enc) {
952             Thread JavaDoc t = new Thread JavaDoc() {
953                     public void run() {
954                         try {
955                             URL JavaDoc burl;
956                             burl = ((SVGOMDocument)document).getURLObject();
957                             final ParsedURL purl = new ParsedURL(burl, uri);
958                             String JavaDoc e = null;
959                             if (enc != null) {
960                                 e = EncodingUtilities.javaEncoding(enc);
961                                 e = ((e == null) ? enc : e);
962                             }
963
964                             InputStream JavaDoc is = purl.openStream();
965                             Reader JavaDoc r;
966                             if (e == null) {
967                                 // Not really a char encoding.
968
r = new InputStreamReader JavaDoc(is);
969                             } else {
970                                 try {
971                                     r = new InputStreamReader JavaDoc(is, e);
972                                 } catch (UnsupportedEncodingException JavaDoc uee) {
973                                     // Try with no encoding.
974
r = new InputStreamReader JavaDoc(is);
975                                 }
976                             }
977                             r = new BufferedReader JavaDoc(r);
978                             final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
979                             int read;
980                             char[] buf = new char[4096];
981                             while ((read = r.read(buf, 0, buf.length)) != -1) {
982                                 sb.append(buf, 0, read);
983                             }
984                             r.close();
985
986                             updateRunnableQueue.invokeLater(new Runnable JavaDoc() {
987                                     public void run() {
988                                         try {
989                                             h.getURLDone(true,
990                                                          purl.getContentType(),
991                                                          sb.toString());
992                                         } catch (Exception JavaDoc e){
993                                             if (userAgent != null) {
994                                                 userAgent.displayError(e);
995                                             }
996                                         }
997                                     }
998                                 });
999                         } catch (Exception JavaDoc e) {
1000                            if (e instanceof SecurityException JavaDoc) {
1001                                userAgent.displayError(e);
1002                            }
1003                            updateRunnableQueue.invokeLater(new Runnable JavaDoc() {
1004                                    public void run() {
1005                                        try {
1006                                            h.getURLDone(false, null, null);
1007                                        } catch (Exception JavaDoc e){
1008                                            if (userAgent != null) {
1009                                                userAgent.displayError(e);
1010                                            }
1011                                        }
1012                                    }
1013                                });
1014                        }
1015                    }
1016
1017                };
1018            t.setPriority(Thread.MIN_PRIORITY);
1019            t.start();
1020        }
1021
1022
1023        public void postURL(String JavaDoc uri, String JavaDoc content,
1024                            org.apache.batik.script.Window.URLResponseHandler h) {
1025            postURL(uri, content, h, "text/plain", null);
1026        }
1027
1028        public void postURL(String JavaDoc uri, String JavaDoc content,
1029                            org.apache.batik.script.Window.URLResponseHandler h,
1030                     String JavaDoc mimeType) {
1031            postURL(uri, content, h, mimeType, null);
1032        }
1033
1034        public void postURL(final String JavaDoc uri,
1035                            final String JavaDoc content,
1036                            final org.apache.batik.script.Window.URLResponseHandler h,
1037                            final String JavaDoc mimeType,
1038                            final String JavaDoc fEnc) {
1039            Thread JavaDoc t = new Thread JavaDoc() {
1040                    public void run() {
1041                        try {
1042                            URL JavaDoc burl;
1043                            burl = ((SVGOMDocument)document).getURLObject();
1044                            URL JavaDoc url;
1045                            if (burl != null)
1046                                url = new URL JavaDoc(burl, uri);
1047                            else url = new URL JavaDoc(uri);
1048                            final URLConnection JavaDoc conn = url.openConnection();
1049                            conn.setDoOutput(true);
1050                            conn.setDoInput(true);
1051                            conn.setUseCaches(false);
1052                            conn.setRequestProperty("Content-Type", mimeType);
1053                            
1054                            OutputStream JavaDoc os = conn.getOutputStream();
1055                            String JavaDoc e=null, enc = fEnc;
1056                            if (enc != null) {
1057                                if (enc.startsWith(DEFLATE)) {
1058                                    os = new DeflaterOutputStream JavaDoc(os);
1059
1060                                    if (enc.length() > DEFLATE.length())
1061                                        enc = enc.substring(DEFLATE.length()+1);
1062                                    else
1063                                        enc = "";
1064                                    conn.setRequestProperty("Content-Encoding",
1065                                                            DEFLATE);
1066                                }
1067                                if (enc.startsWith(GZIP)) {
1068                                    os = new GZIPOutputStream JavaDoc(os);
1069                                    if (enc.length() > GZIP.length())
1070                                        enc = enc.substring(GZIP.length()+1);
1071                                    else
1072                                        enc ="";
1073                                    conn.setRequestProperty("Content-Encoding",
1074                                                            DEFLATE);
1075                                }
1076                                if (enc.length() != 0) {
1077                                    e = EncodingUtilities.javaEncoding(enc);
1078                                    if (e == null) e = UTF_8;
1079                                } else {
1080                                    e = UTF_8;
1081                                }
1082                            }
1083                            Writer JavaDoc w;
1084                            if (e == null)
1085                                w = new OutputStreamWriter JavaDoc(os);
1086                            else
1087                                w = new OutputStreamWriter JavaDoc(os, e);
1088                            w.write(content);
1089                            w.flush();
1090                            w.close();
1091                            os.close();
1092
1093                            InputStream JavaDoc is = conn.getInputStream();
1094                            Reader JavaDoc r;
1095                            e = UTF_8;
1096                            if (e == null)
1097                                r = new InputStreamReader JavaDoc(is);
1098                            else
1099                                r = new InputStreamReader JavaDoc(is, e);
1100                            r = new BufferedReader JavaDoc(r);
1101
1102                            final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
1103                            int read;
1104                            char[] buf = new char[4096];
1105                            while ((read = r.read(buf, 0, buf.length)) != -1) {
1106                                sb.append(buf, 0, read);
1107                            }
1108                            r.close();
1109
1110                            updateRunnableQueue.invokeLater(new Runnable JavaDoc() {
1111                                    public void run() {
1112                                        try {
1113                                            h.getURLDone(true,
1114                                                         conn.getContentType(),
1115                                                         sb.toString());
1116                                        } catch (Exception JavaDoc e){
1117                                            if (userAgent != null) {
1118                                                userAgent.displayError(e);
1119                                            }
1120                                        }
1121                                    }
1122                                });
1123                        } catch (Exception JavaDoc e) {
1124                            if (e instanceof SecurityException JavaDoc) {
1125                                userAgent.displayError(e);
1126                            }
1127                            updateRunnableQueue.invokeLater(new Runnable JavaDoc() {
1128                                    public void run() {
1129                                        try {
1130                                            h.getURLDone(false, null, null);
1131                                        } catch (Exception JavaDoc e){
1132                                            if (userAgent != null) {
1133                                                userAgent.displayError(e);
1134                                            }
1135                                        }
1136                                    }
1137                                });
1138                        }
1139                    }
1140
1141                };
1142            t.setPriority(Thread.MIN_PRIORITY);
1143            t.start();
1144        }
1145
1146        /**
1147         * Displays an alert dialog box.
1148         */

1149        public void alert(String JavaDoc message) {
1150            if (userAgent != null) {
1151                userAgent.showAlert(message);
1152            }
1153        }
1154
1155        /**
1156         * Displays a confirm dialog box.
1157         */

1158        public boolean confirm(String JavaDoc message) {
1159            if (userAgent != null) {
1160                return userAgent.showConfirm(message);
1161            }
1162            return false;
1163        }
1164
1165        /**
1166         * Displays an input dialog box.
1167         */

1168        public String JavaDoc prompt(String JavaDoc message) {
1169            if (userAgent != null) {
1170                return userAgent.showPrompt(message);
1171            }
1172            return null;
1173        }
1174
1175        /**
1176         * Displays an input dialog box, given the default value.
1177         */

1178        public String JavaDoc prompt(String JavaDoc message, String JavaDoc defVal) {
1179            if (userAgent != null) {
1180                return userAgent.showPrompt(message, defVal);
1181            }
1182            return null;
1183        }
1184
1185        /**
1186         * Returns the current BridgeContext.
1187         */

1188        public BridgeContext getBridgeContext() {
1189            return bridgeContext;
1190        }
1191
1192        /**
1193         * Returns the associated interpreter.
1194         */

1195        public Interpreter getInterpreter() {
1196            return interpreter;
1197        }
1198    }
1199
1200    /**
1201     * The listener class for 'DOMNodeInserted' event.
1202     */

1203    protected class DOMNodeInsertedListener implements EventListener JavaDoc {
1204        public void handleEvent(Event JavaDoc evt) {
1205            addScriptingListeners((Node JavaDoc)evt.getTarget());
1206        }
1207    }
1208
1209    /**
1210     * The listener class for 'DOMNodeRemoved' event.
1211     */

1212    protected class DOMNodeRemovedListener implements EventListener JavaDoc {
1213        public void handleEvent(Event JavaDoc evt) {
1214            removeScriptingListeners((Node JavaDoc)evt.getTarget());
1215        }
1216    }
1217
1218    protected class DOMAttrModifiedListener implements EventListener JavaDoc {
1219        public void handleEvent (Event JavaDoc evt) {
1220            MutationEvent JavaDoc me = (MutationEvent JavaDoc)evt;
1221            if (me.getAttrChange() != MutationEvent.MODIFICATION)
1222                updateScriptingListeners((Element JavaDoc)me.getTarget(),
1223                                         me.getAttrName());
1224        }
1225    }
1226
1227    /**
1228     * To handle a scripting event.
1229     */

1230    protected class ScriptingEventListener implements EventListener JavaDoc {
1231
1232        /**
1233         * The script attribute.
1234         */

1235        protected String JavaDoc attribute;
1236        
1237        /**
1238         * Creates a new ScriptingEventListener.
1239         */

1240        public ScriptingEventListener(String JavaDoc attr) {
1241            attribute = attr;
1242        }
1243
1244        /**
1245         * Runs the script.
1246         */

1247        public void handleEvent(Event JavaDoc evt) {
1248            Element JavaDoc elt = (Element JavaDoc)evt.getCurrentTarget();
1249            // Evaluate the script
1250
String JavaDoc script = elt.getAttributeNS(null, attribute);
1251            if (script.length() == 0)
1252                return;
1253
1254            DocumentLoader dl = bridgeContext.getDocumentLoader();
1255            SVGDocument d = (SVGDocument)elt.getOwnerDocument();
1256            int line = dl.getLineNumber(elt);
1257            final String JavaDoc desc = Messages.formatMessage
1258                (EVENT_SCRIPT_DESCRIPTION,
1259                 new Object JavaDoc [] {d.getURL(), attribute, new Integer JavaDoc(line)});
1260
1261            // Find the scripting language
1262
Element JavaDoc e = elt;
1263            while (e != null &&
1264                   (!SVGConstants.SVG_NAMESPACE_URI.equals
1265                    (e.getNamespaceURI()) ||
1266                    !SVGConstants.SVG_SVG_TAG.equals(e.getLocalName()))) {
1267                e = SVGUtilities.getParentElement(e);
1268            }
1269            if (e == null)
1270                return;
1271
1272            String JavaDoc lang = e.getAttributeNS
1273                (null, SVGConstants.SVG_CONTENT_SCRIPT_TYPE_ATTRIBUTE);
1274
1275            runEventHandler(script, evt, lang, desc);
1276        }
1277    }
1278}
1279
Popular Tags