Source Code Cross Referenced for BaseScriptingEnvironment.java in  » Graphic-Library » batik » org » apache » batik » bridge » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Graphic Library » batik » org.apache.batik.bridge 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:           Licensed to the Apache Software Foundation (ASF) under one or more
004:           contributor license agreements.  See the NOTICE file distributed with
005:           this work for additional information regarding copyright ownership.
006:           The ASF licenses this file to You under the Apache License, Version 2.0
007:           (the "License"); you may not use this file except in compliance with
008:           the License.  You may obtain a copy of the License at
009:
010:               http://www.apache.org/licenses/LICENSE-2.0
011:
012:           Unless required by applicable law or agreed to in writing, software
013:           distributed under the License is distributed on an "AS IS" BASIS,
014:           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015:           See the License for the specific language governing permissions and
016:           limitations under the License.
017:
018:         */
019:        package org.apache.batik.bridge;
020:
021:        import java.io.IOException;
022:        import java.io.InputStream;
023:        import java.io.InputStreamReader;
024:        import java.io.PushbackInputStream;
025:        import java.io.Reader;
026:        import java.io.StringReader;
027:        import java.io.UnsupportedEncodingException;
028:        import java.net.MalformedURLException;
029:        import java.net.URL;
030:        import java.util.HashSet;
031:        import java.util.Iterator;
032:        import java.util.List;
033:        import java.util.Set;
034:        import java.util.jar.Manifest;
035:
036:        import org.apache.batik.dom.AbstractElement;
037:        import org.apache.batik.dom.events.AbstractEvent;
038:        import org.apache.batik.dom.events.NodeEventTarget;
039:        import org.apache.batik.dom.util.XLinkSupport;
040:        import org.apache.batik.script.Interpreter;
041:        import org.apache.batik.script.InterpreterException;
042:        import org.apache.batik.script.ScriptEventWrapper;
043:        import org.apache.batik.script.ScriptHandler;
044:        import org.apache.batik.util.ParsedURL;
045:        import org.apache.batik.util.SVGConstants;
046:        import org.apache.batik.util.XMLConstants;
047:
048:        import org.w3c.dom.Document;
049:        import org.w3c.dom.Element;
050:        import org.w3c.dom.Node;
051:        import org.w3c.dom.NodeList;
052:        import org.w3c.dom.events.DocumentEvent;
053:        import org.w3c.dom.events.Event;
054:        import org.w3c.dom.events.EventListener;
055:        import org.w3c.dom.events.EventTarget;
056:        import org.w3c.dom.svg.SVGDocument;
057:        import org.w3c.dom.svg.SVGSVGElement;
058:        import org.w3c.dom.svg.EventListenerInitializer;
059:
060:        /**
061:         * This class is the base class for SVG scripting.
062:         *
063:         * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
064:         * @version $Id: BaseScriptingEnvironment.java 494029 2007-01-08 11:18:11Z deweese $
065:         */
066:        public class BaseScriptingEnvironment {
067:            /**
068:             * Constant used to describe inline scripts.
069:             * <pre>
070:             * {0} - URL of document containing script.
071:             * {1} - Element tag
072:             * {2} - line number of element.
073:             * </pre>
074:             */
075:            public static final String INLINE_SCRIPT_DESCRIPTION = "BaseScriptingEnvironment.constant.inline.script.description";
076:
077:            /**
078:             * Constant used to describe inline scripts.
079:             * <pre>
080:             * {0} - URL of document containing script.
081:             * {1} - Event attribute name
082:             * {2} - line number of element.
083:             * </pre>
084:             */
085:            public static final String EVENT_SCRIPT_DESCRIPTION = "BaseScriptingEnvironment.constant.event.script.description";
086:
087:            /**
088:             * Tells whether the given SVG document is dynamic.
089:             */
090:            public static boolean isDynamicDocument(BridgeContext ctx,
091:                    Document doc) {
092:                Element elt = doc.getDocumentElement();
093:                if ((elt != null)
094:                        && SVGConstants.SVG_NAMESPACE_URI.equals(elt
095:                                .getNamespaceURI())) {
096:                    if (elt.getAttributeNS(null,
097:                            SVGConstants.SVG_ONABORT_ATTRIBUTE).length() > 0) {
098:                        return true;
099:                    }
100:                    if (elt.getAttributeNS(null,
101:                            SVGConstants.SVG_ONERROR_ATTRIBUTE).length() > 0) {
102:                        return true;
103:                    }
104:                    if (elt.getAttributeNS(null,
105:                            SVGConstants.SVG_ONRESIZE_ATTRIBUTE).length() > 0) {
106:                        return true;
107:                    }
108:                    if (elt.getAttributeNS(null,
109:                            SVGConstants.SVG_ONUNLOAD_ATTRIBUTE).length() > 0) {
110:                        return true;
111:                    }
112:                    if (elt.getAttributeNS(null,
113:                            SVGConstants.SVG_ONSCROLL_ATTRIBUTE).length() > 0) {
114:                        return true;
115:                    }
116:                    if (elt.getAttributeNS(null,
117:                            SVGConstants.SVG_ONZOOM_ATTRIBUTE).length() > 0) {
118:                        return true;
119:                    }
120:                    return isDynamicElement(ctx, doc.getDocumentElement());
121:                }
122:                return false;
123:            }
124:
125:            public static boolean isDynamicElement(BridgeContext ctx,
126:                    Element elt) {
127:                List bridgeExtensions = ctx.getBridgeExtensions(elt
128:                        .getOwnerDocument());
129:                return isDynamicElement(elt, ctx, bridgeExtensions);
130:            }
131:
132:            /**
133:             * Tells whether the given SVG element is dynamic.
134:             */
135:            public static boolean isDynamicElement(Element elt,
136:                    BridgeContext ctx, List bridgeExtensions) {
137:                Iterator i = bridgeExtensions.iterator();
138:                while (i.hasNext()) {
139:                    BridgeExtension bridgeExtension = (BridgeExtension) i
140:                            .next();
141:                    if (bridgeExtension.isDynamicElement(elt)) {
142:                        return true;
143:                    }
144:                }
145:                if (SVGConstants.SVG_NAMESPACE_URI
146:                        .equals(elt.getNamespaceURI())) {
147:                    if (elt.getAttributeNS(null,
148:                            SVGConstants.SVG_ONKEYUP_ATTRIBUTE).length() > 0) {
149:                        return true;
150:                    }
151:                    if (elt.getAttributeNS(null,
152:                            SVGConstants.SVG_ONKEYDOWN_ATTRIBUTE).length() > 0) {
153:                        return true;
154:                    }
155:                    if (elt.getAttributeNS(null,
156:                            SVGConstants.SVG_ONKEYPRESS_ATTRIBUTE).length() > 0) {
157:                        return true;
158:                    }
159:                    if (elt.getAttributeNS(null,
160:                            SVGConstants.SVG_ONLOAD_ATTRIBUTE).length() > 0) {
161:                        return true;
162:                    }
163:                    if (elt.getAttributeNS(null,
164:                            SVGConstants.SVG_ONERROR_ATTRIBUTE).length() > 0) {
165:                        return true;
166:                    }
167:                    if (elt.getAttributeNS(null,
168:                            SVGConstants.SVG_ONACTIVATE_ATTRIBUTE).length() > 0) {
169:                        return true;
170:                    }
171:                    if (elt.getAttributeNS(null,
172:                            SVGConstants.SVG_ONCLICK_ATTRIBUTE).length() > 0) {
173:                        return true;
174:                    }
175:                    if (elt.getAttributeNS(null,
176:                            SVGConstants.SVG_ONFOCUSIN_ATTRIBUTE).length() > 0) {
177:                        return true;
178:                    }
179:                    if (elt.getAttributeNS(null,
180:                            SVGConstants.SVG_ONFOCUSOUT_ATTRIBUTE).length() > 0) {
181:                        return true;
182:                    }
183:                    if (elt.getAttributeNS(null,
184:                            SVGConstants.SVG_ONMOUSEDOWN_ATTRIBUTE).length() > 0) {
185:                        return true;
186:                    }
187:                    if (elt.getAttributeNS(null,
188:                            SVGConstants.SVG_ONMOUSEMOVE_ATTRIBUTE).length() > 0) {
189:                        return true;
190:                    }
191:                    if (elt.getAttributeNS(null,
192:                            SVGConstants.SVG_ONMOUSEOUT_ATTRIBUTE).length() > 0) {
193:                        return true;
194:                    }
195:                    if (elt.getAttributeNS(null,
196:                            SVGConstants.SVG_ONMOUSEOVER_ATTRIBUTE).length() > 0) {
197:                        return true;
198:                    }
199:                    if (elt.getAttributeNS(null,
200:                            SVGConstants.SVG_ONMOUSEUP_ATTRIBUTE).length() > 0) {
201:                        return true;
202:                    }
203:                }
204:
205:                for (Node n = elt.getFirstChild(); n != null; n = n
206:                        .getNextSibling()) {
207:                    if (n.getNodeType() == Node.ELEMENT_NODE) {
208:                        if (isDynamicElement(ctx, (Element) n)) {
209:                            return true;
210:                        }
211:                    }
212:                }
213:                return false;
214:            }
215:
216:            protected static final String EVENT_NAME = "event";
217:            protected static final String ALTERNATE_EVENT_NAME = "evt";
218:
219:            /**
220:             * The 'application/ecmascript' MIME type.
221:             */
222:            protected static final String APPLICATION_ECMASCRIPT = "application/ecmascript";
223:
224:            /**
225:             * The bridge context.
226:             */
227:            protected BridgeContext bridgeContext;
228:
229:            /**
230:             * The user-agent.
231:             */
232:            protected UserAgent userAgent;
233:
234:            /**
235:             * The document to manage.
236:             */
237:            protected Document document;
238:
239:            /**
240:             * The URL of the document ot manage
241:             */
242:            protected ParsedURL docPURL;
243:
244:            protected Set languages = new HashSet();
245:
246:            /**
247:             * The default Interpreter for the document
248:             */
249:            protected Interpreter interpreter;
250:
251:            /**
252:             * Creates a new BaseScriptingEnvironment.
253:             * @param ctx the bridge context
254:             */
255:            public BaseScriptingEnvironment(BridgeContext ctx) {
256:                bridgeContext = ctx;
257:                document = ctx.getDocument();
258:                docPURL = new ParsedURL(((SVGDocument) document).getURL());
259:                userAgent = bridgeContext.getUserAgent();
260:            }
261:
262:            /**
263:             * Creates a new Window object.
264:             */
265:            public org.apache.batik.script.Window createWindow(
266:                    Interpreter interp, String lang) {
267:                return new Window(interp, lang);
268:            }
269:
270:            /**
271:             * Creates a new Window object.
272:             */
273:            public org.apache.batik.script.Window createWindow() {
274:                return createWindow(null, null);
275:            }
276:
277:            /**
278:             * Returns the default Interpreter for this document.
279:             */
280:            public Interpreter getInterpreter() {
281:                if (interpreter != null)
282:                    return interpreter;
283:
284:                SVGSVGElement root = (SVGSVGElement) document
285:                        .getDocumentElement();
286:                String lang = root.getContentScriptType();
287:                return getInterpreter(lang);
288:            }
289:
290:            public Interpreter getInterpreter(String lang) {
291:                interpreter = bridgeContext.getInterpreter(lang);
292:                if (interpreter == null) {
293:                    if (languages.contains(lang)) {
294:                        // Already issued warning so just return null;
295:                        return null;
296:                    }
297:
298:                    // So we know we have processed this interpreter.
299:                    languages.add(lang);
300:                    return null;
301:                }
302:
303:                if (!languages.contains(lang)) {
304:                    languages.add(lang);
305:                    initializeEnvironment(interpreter, lang);
306:                }
307:                return interpreter;
308:            }
309:
310:            /**
311:             * Initializes the environment of the given interpreter.
312:             */
313:            public void initializeEnvironment(Interpreter interp, String lang) {
314:                interp.bindObject("window", createWindow(interp, lang));
315:            }
316:
317:            /**
318:             * Loads the scripts contained in the <script> elements.
319:             */
320:            public void loadScripts() {
321:                org.apache.batik.script.Window window = null;
322:
323:                NodeList scripts = document.getElementsByTagNameNS(
324:                        SVGConstants.SVG_NAMESPACE_URI,
325:                        SVGConstants.SVG_SCRIPT_TAG);
326:                int len = scripts.getLength();
327:
328:                if (len == 0) {
329:                    return;
330:                }
331:
332:                for (int i = 0; i < len; i++) {
333:                    AbstractElement script = (AbstractElement) scripts.item(i);
334:                    String type = script.getAttributeNS(null,
335:                            SVGConstants.SVG_TYPE_ATTRIBUTE);
336:
337:                    if (type.length() == 0) {
338:                        type = SVGConstants.SVG_SCRIPT_TYPE_DEFAULT_VALUE;
339:                    }
340:
341:                    //
342:                    // Java code invocation.
343:                    //
344:                    if (type.equals(SVGConstants.SVG_SCRIPT_TYPE_JAVA)) {
345:                        try {
346:                            String href = XLinkSupport.getXLinkHref(script);
347:                            ParsedURL purl = new ParsedURL(script.getBaseURI(),
348:                                    href);
349:
350:                            checkCompatibleScriptURL(type, purl);
351:
352:                            DocumentJarClassLoader cll;
353:                            URL docURL = null;
354:                            try {
355:                                docURL = new URL(docPURL.toString());
356:                            } catch (MalformedURLException mue) {
357:                                /* nothing just let docURL be null */
358:                            }
359:                            cll = new DocumentJarClassLoader(new URL(purl
360:                                    .toString()), docURL);
361:
362:                            // Get the 'Script-Handler' entry in the manifest.
363:                            URL url = cll.findResource("META-INF/MANIFEST.MF");
364:                            if (url == null) {
365:                                continue;
366:                            }
367:                            Manifest man = new Manifest(url.openStream());
368:
369:                            String sh;
370:
371:                            sh = man.getMainAttributes().getValue(
372:                                    "Script-Handler");
373:                            if (sh != null) {
374:                                // Run the script handler.
375:                                ScriptHandler h;
376:                                h = (ScriptHandler) cll.loadClass(sh)
377:                                        .newInstance();
378:
379:                                if (window == null) {
380:                                    window = createWindow();
381:                                }
382:
383:                                h.run(document, window);
384:                            }
385:
386:                            sh = man.getMainAttributes().getValue(
387:                                    "SVG-Handler-Class");
388:                            if (sh != null) {
389:                                // Run the initializer
390:                                EventListenerInitializer initializer;
391:                                initializer = (EventListenerInitializer) cll
392:                                        .loadClass(sh).newInstance();
393:
394:                                if (window == null) {
395:                                    window = createWindow();
396:                                }
397:
398:                                initializer
399:                                        .initializeEventListeners((SVGDocument) document);
400:                            }
401:                        } catch (Exception e) {
402:                            if (userAgent != null) {
403:                                userAgent.displayError(e);
404:                            }
405:                        }
406:                        continue;
407:                    }
408:
409:                    //
410:                    // Scripting language invocation.
411:                    //
412:                    Interpreter interpreter = getInterpreter(type);
413:                    if (interpreter == null)
414:                        // Can't find interpreter so just skip this script block.
415:                        continue;
416:
417:                    try {
418:                        String href = XLinkSupport.getXLinkHref(script);
419:                        String desc = null;
420:                        Reader reader = null;
421:
422:                        if (href.length() > 0) {
423:                            desc = href;
424:
425:                            // External script.
426:                            ParsedURL purl = new ParsedURL(script.getBaseURI(),
427:                                    href);
428:
429:                            checkCompatibleScriptURL(type, purl);
430:                            InputStream is = purl.openStream();
431:                            String mediaType = purl.getContentTypeMediaType();
432:                            String enc = purl.getContentTypeCharset();
433:                            if (enc != null) {
434:                                try {
435:                                    reader = new InputStreamReader(is, enc);
436:                                } catch (UnsupportedEncodingException uee) {
437:                                    enc = null;
438:                                }
439:                            }
440:                            if (reader == null) {
441:                                if (APPLICATION_ECMASCRIPT.equals(mediaType)) {
442:                                    // No encoding was specified in the MIME type, so
443:                                    // infer it according to RFC 4329.
444:                                    if (purl.hasContentTypeParameter("version")) {
445:                                        // Future versions of application/ecmascript 
446:                                        // are not supported, so skip this script 
447:                                        // element if the version parameter is present.
448:                                        continue;
449:                                    }
450:
451:                                    PushbackInputStream pbis = new PushbackInputStream(
452:                                            is, 8);
453:                                    byte[] buf = new byte[4];
454:                                    int read = pbis.read(buf);
455:                                    if (read > 0) {
456:                                        pbis.unread(buf, 0, read);
457:                                        if (read >= 2) {
458:                                            if (buf[0] == (byte) 0xff
459:                                                    && buf[1] == (byte) 0xfe) {
460:                                                if (read >= 4 && buf[2] == 0
461:                                                        && buf[3] == 0) {
462:                                                    enc = "UTF32-LE";
463:                                                    pbis.skip(4);
464:                                                } else {
465:                                                    enc = "UTF-16LE";
466:                                                    pbis.skip(2);
467:                                                }
468:                                            } else if (buf[0] == (byte) 0xfe
469:                                                    && buf[1] == (byte) 0xff) {
470:                                                enc = "UTF-16BE";
471:                                                pbis.skip(2);
472:                                            } else if (read >= 3
473:                                                    && buf[0] == (byte) 0xef
474:                                                    && buf[1] == (byte) 0xbb
475:                                                    && buf[2] == (byte) 0xbf) {
476:                                                enc = "UTF-8";
477:                                                pbis.skip(3);
478:                                            } else if (read >= 4 && buf[0] == 0
479:                                                    && buf[1] == 0
480:                                                    && buf[2] == (byte) 0xfe
481:                                                    && buf[3] == (byte) 0xff) {
482:                                                enc = "UTF-32BE";
483:                                                pbis.skip(4);
484:                                            }
485:                                        }
486:                                        if (enc == null) {
487:                                            enc = "UTF-8";
488:                                        }
489:                                    }
490:                                    reader = new InputStreamReader(pbis, enc);
491:                                } else {
492:                                    reader = new InputStreamReader(is);
493:                                }
494:                            }
495:                        } else {
496:                            checkCompatibleScriptURL(type, docPURL);
497:                            DocumentLoader dl = bridgeContext
498:                                    .getDocumentLoader();
499:                            Element e = script;
500:                            SVGDocument d = (SVGDocument) e.getOwnerDocument();
501:                            int line = dl.getLineNumber(script);
502:                            desc = Messages.formatMessage(
503:                                    INLINE_SCRIPT_DESCRIPTION, new Object[] {
504:                                            d.getURL(),
505:                                            "<" + script.getNodeName() + ">",
506:                                            new Integer(line) });
507:                            // Inline script.
508:                            Node n = script.getFirstChild();
509:                            if (n != null) {
510:                                StringBuffer sb = new StringBuffer();
511:                                while (n != null) {
512:                                    if (n.getNodeType() == Node.CDATA_SECTION_NODE
513:                                            || n.getNodeType() == Node.TEXT_NODE)
514:                                        sb.append(n.getNodeValue());
515:                                    n = n.getNextSibling();
516:                                }
517:                                reader = new StringReader(sb.toString());
518:                            } else {
519:                                continue;
520:                            }
521:                        }
522:
523:                        interpreter.evaluate(reader, desc);
524:
525:                    } catch (IOException e) {
526:                        if (userAgent != null) {
527:                            userAgent.displayError(e);
528:                        }
529:                        return;
530:                    } catch (InterpreterException e) {
531:                        System.err.println("InterpExcept: " + e);
532:                        handleInterpreterException(e);
533:                        return;
534:                    } catch (SecurityException e) {
535:                        if (userAgent != null) {
536:                            userAgent.displayError(e);
537:                        }
538:                    }
539:                }
540:            }
541:
542:            /**
543:             * Checks that the script URLs and the document url are
544:             * compatible. A SecurityException is thrown if loading
545:             * the script is not allowed.
546:             */
547:            protected void checkCompatibleScriptURL(String scriptType,
548:                    ParsedURL scriptPURL) {
549:                userAgent.checkLoadScript(scriptType, scriptPURL, docPURL);
550:            }
551:
552:            /**
553:             * Recursively dispatch the SVG 'onload' event.
554:             */
555:            public void dispatchSVGLoadEvent() {
556:                SVGSVGElement root = (SVGSVGElement) document
557:                        .getDocumentElement();
558:                String lang = root.getContentScriptType();
559:                long documentStartTime = System.currentTimeMillis();
560:                dispatchSVGLoad(root, true, lang);
561:                bridgeContext.getAnimationEngine().start(documentStartTime);
562:            }
563:
564:            /**
565:             * Auxiliary method for dispatchSVGLoad.
566:             */
567:            protected void dispatchSVGLoad(Element elt, boolean checkCanRun,
568:                    String lang) {
569:                for (Node n = elt.getFirstChild(); n != null; n = n
570:                        .getNextSibling()) {
571:                    if (n.getNodeType() == Node.ELEMENT_NODE) {
572:                        dispatchSVGLoad((Element) n, checkCanRun, lang);
573:                    }
574:                }
575:
576:                DocumentEvent de = (DocumentEvent) elt.getOwnerDocument();
577:                AbstractEvent ev = (AbstractEvent) de.createEvent("SVGEvents");
578:                String type;
579:                if (bridgeContext.isSVG12()) {
580:                    type = "load";
581:                } else {
582:                    type = "SVGLoad";
583:                }
584:                ev.initEventNS(XMLConstants.XML_EVENTS_NAMESPACE_URI, type,
585:                        false, false);
586:                NodeEventTarget t = (NodeEventTarget) elt;
587:
588:                final String s = elt.getAttributeNS(null,
589:                        SVGConstants.SVG_ONLOAD_ATTRIBUTE);
590:                if (s.length() == 0) {
591:                    // No script to run so just dispatch the event to DOM
592:                    // (For java presumably).
593:                    t.dispatchEvent(ev);
594:                    return;
595:                }
596:
597:                final Interpreter interp = getInterpreter();
598:                if (interp == null) {
599:                    // Can't load interpreter so just dispatch normal event
600:                    // to the DOM (for java presumably).
601:                    t.dispatchEvent(ev);
602:                    return;
603:                }
604:
605:                if (checkCanRun) {
606:                    // Check that it is ok to run embeded scripts
607:                    checkCompatibleScriptURL(lang, docPURL);
608:                    checkCanRun = false; // we only check once for onload handlers
609:                }
610:
611:                DocumentLoader dl = bridgeContext.getDocumentLoader();
612:                SVGDocument d = (SVGDocument) elt.getOwnerDocument();
613:                int line = dl.getLineNumber(elt);
614:                final String desc = Messages.formatMessage(
615:                        EVENT_SCRIPT_DESCRIPTION, new Object[] { d.getURL(),
616:                                SVGConstants.SVG_ONLOAD_ATTRIBUTE,
617:                                new Integer(line) });
618:
619:                EventListener l = new EventListener() {
620:                    public void handleEvent(Event evt) {
621:                        try {
622:                            Object event;
623:                            if (evt instanceof  ScriptEventWrapper) {
624:                                event = ((ScriptEventWrapper) evt)
625:                                        .getEventObject();
626:                            } else {
627:                                event = evt;
628:                            }
629:                            interp.bindObject(EVENT_NAME, event);
630:                            interp.bindObject(ALTERNATE_EVENT_NAME, event);
631:                            interp.evaluate(new StringReader(s), desc);
632:                        } catch (IOException io) {
633:                        } catch (InterpreterException e) {
634:                            handleInterpreterException(e);
635:                        }
636:                    }
637:                };
638:                t.addEventListenerNS(XMLConstants.XML_EVENTS_NAMESPACE_URI,
639:                        type, l, false, null);
640:                t.dispatchEvent(ev);
641:                t.removeEventListenerNS(XMLConstants.XML_EVENTS_NAMESPACE_URI,
642:                        type, l, false);
643:            }
644:
645:            /**
646:             * Method to dispatch SVG Zoom event.
647:             */
648:            protected void dispatchSVGZoomEvent() {
649:                if (bridgeContext.isSVG12()) {
650:                    dispatchSVGDocEvent("zoom");
651:                } else {
652:                    dispatchSVGDocEvent("SVGZoom");
653:                }
654:            }
655:
656:            /**
657:             * Method to dispatch SVG Scroll event.
658:             */
659:            protected void dispatchSVGScrollEvent() {
660:                if (bridgeContext.isSVG12()) {
661:                    dispatchSVGDocEvent("scroll");
662:                } else {
663:                    dispatchSVGDocEvent("SVGScroll");
664:                }
665:            }
666:
667:            /**
668:             * Method to dispatch SVG Resize event.
669:             */
670:            protected void dispatchSVGResizeEvent() {
671:                if (bridgeContext.isSVG12()) {
672:                    dispatchSVGDocEvent("resize");
673:                } else {
674:                    dispatchSVGDocEvent("SVGResize");
675:                }
676:            }
677:
678:            protected void dispatchSVGDocEvent(String eventType) {
679:                SVGSVGElement root = (SVGSVGElement) document
680:                        .getDocumentElement();
681:                // Event is dispatched on outermost SVG element.
682:                EventTarget t = root;
683:
684:                DocumentEvent de = (DocumentEvent) document;
685:                AbstractEvent ev = (AbstractEvent) de.createEvent("SVGEvents");
686:                ev.initEventNS(XMLConstants.XML_EVENTS_NAMESPACE_URI,
687:                        eventType, false, false);
688:                t.dispatchEvent(ev);
689:            }
690:
691:            /**
692:             * Handles the given exception.
693:             */
694:            protected void handleInterpreterException(InterpreterException ie) {
695:                if (userAgent != null) {
696:                    Exception ex = ie.getException();
697:                    userAgent.displayError((ex == null) ? ie : ex);
698:                }
699:            }
700:
701:            /**
702:             * Handles the given exception.
703:             */
704:            protected void handleSecurityException(SecurityException se) {
705:                if (userAgent != null) {
706:                    userAgent.displayError(se);
707:                }
708:            }
709:
710:            /**
711:             * Represents the window object of this environment.
712:             */
713:            protected class Window implements  org.apache.batik.script.Window {
714:
715:                /**
716:                 * The associated interpreter.
717:                 */
718:                protected Interpreter interpreter;
719:
720:                /**
721:                 * The associated language.
722:                 */
723:                protected String language;
724:
725:                /**
726:                 * Creates a new Window.
727:                 */
728:                public Window(Interpreter interp, String lang) {
729:                    interpreter = interp;
730:                    language = lang;
731:                }
732:
733:                /**
734:                 * Implements {@link
735:                 * org.apache.batik.script.Window#setInterval(String,long)}.
736:                 */
737:                public Object setInterval(final String script, long interval) {
738:                    return null;
739:                }
740:
741:                /**
742:                 * Implements {@link
743:                 * org.apache.batik.script.Window#setInterval(Runnable,long)}.
744:                 */
745:                public Object setInterval(final Runnable r, long interval) {
746:                    return null;
747:                }
748:
749:                /**
750:                 * Implements {@link
751:                 * org.apache.batik.script.Window#clearInterval(Object)}.
752:                 */
753:                public void clearInterval(Object interval) {
754:                }
755:
756:                /**
757:                 * Implements {@link
758:                 * org.apache.batik.script.Window#setTimeout(String,long)}.
759:                 */
760:                public Object setTimeout(final String script, long timeout) {
761:                    return null;
762:                }
763:
764:                /**
765:                 * Implements {@link
766:                 * org.apache.batik.script.Window#setTimeout(Runnable,long)}.
767:                 */
768:                public Object setTimeout(final Runnable r, long timeout) {
769:                    return null;
770:                }
771:
772:                /**
773:                 * Implements {@link
774:                 * org.apache.batik.script.Window#clearTimeout(Object)}.
775:                 */
776:                public void clearTimeout(Object timeout) {
777:                }
778:
779:                /**
780:                 * Parses the given XML string into a DocumentFragment of the
781:                 * given document or a new document if 'doc' is null.
782:                 * The implementation in this class always returns 'null'
783:                 * @return The document/document fragment or null on error.
784:                 */
785:                public Node parseXML(String text, Document doc) {
786:                    return null;
787:                }
788:
789:                /**
790:                 * Gets data from the given URI.
791:                 * @param uri The URI where the data is located.
792:                 * @param h A handler called when the data is available.
793:                 */
794:                public void getURL(String uri,
795:                        org.apache.batik.script.Window.URLResponseHandler h) {
796:                    getURL(uri, h, "UTF8");
797:                }
798:
799:                /**
800:                 * Gets data from the given URI.
801:                 * @param uri The URI where the data is located.
802:                 * @param h A handler called when the data is available.
803:                 * @param enc The character encoding of the data.
804:                 */
805:                public void getURL(String uri,
806:                        org.apache.batik.script.Window.URLResponseHandler h,
807:                        String enc) {
808:                }
809:
810:                public void postURL(String uri, String content,
811:                        org.apache.batik.script.Window.URLResponseHandler h) {
812:                    postURL(uri, content, h, "text/plain", null);
813:                }
814:
815:                public void postURL(String uri, String content,
816:                        org.apache.batik.script.Window.URLResponseHandler h,
817:                        String mimeType) {
818:                    postURL(uri, content, h, mimeType, null);
819:                }
820:
821:                public void postURL(String uri, String content,
822:                        org.apache.batik.script.Window.URLResponseHandler h,
823:                        String mimeType, String fEnc) {
824:                }
825:
826:                /**
827:                 * Displays an alert dialog box.
828:                 */
829:                public void alert(String message) {
830:                }
831:
832:                /**
833:                 * Displays a confirm dialog box.
834:                 */
835:                public boolean confirm(String message) {
836:                    return false;
837:                }
838:
839:                /**
840:                 * Displays an input dialog box.
841:                 */
842:                public String prompt(String message) {
843:                    return null;
844:                }
845:
846:                /**
847:                 * Displays an input dialog box, given the default value.
848:                 */
849:                public String prompt(String message, String defVal) {
850:                    return null;
851:                }
852:
853:                /**
854:                 * Returns the current BridgeContext.
855:                 */
856:                public BridgeContext getBridgeContext() {
857:                    return bridgeContext;
858:                }
859:
860:                /**
861:                 * Returns the associated interpreter.
862:                 */
863:                public Interpreter getInterpreter() {
864:                    return interpreter;
865:                }
866:
867:            }
868:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.