1 18 package org.apache.batik.bridge; 19 20 import java.io.BufferedReader ; 21 import java.io.InputStream ; 22 import java.io.InputStreamReader ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.io.OutputStreamWriter ; 26 import java.io.Reader ; 27 import java.io.StringReader ; 28 import java.io.UnsupportedEncodingException ; 29 import java.io.Writer ; 30 31 import java.net.URL ; 32 import java.net.URLConnection ; 33 34 import java.util.HashMap ; 35 import java.util.Map ; 36 import java.util.Timer ; 37 import java.util.TimerTask ; 38 import java.util.zip.GZIPOutputStream ; 39 import java.util.zip.DeflaterOutputStream ; 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 ; 55 import org.w3c.dom.Element ; 56 import org.w3c.dom.Node ; 57 import org.w3c.dom.events.Event ; 58 import org.w3c.dom.events.EventListener ; 59 import org.w3c.dom.events.EventTarget ; 60 import org.w3c.dom.events.MutationEvent ; 61 import org.w3c.dom.svg.SVGDocument; 62 63 69 public class ScriptingEnvironment extends BaseScriptingEnvironment { 70 71 74 protected final static String 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 FRAGMENT_SUFFIX = 82 "</svg>"; 83 84 public final static String [] SVG_EVENT_ATTRS = { 85 "onabort", "onerror", "onresize", "onscroll", "onunload", "onzoom", 92 "onbegin", "onend", "onrepeat", 96 "onfocusin", "onfocusout", "onactivate", "onclick", 101 "onmousedown", "onmouseup", "onmouseover", "onmouseout", "onmousemove", 107 "onkeypress", "onkeydown", "onkeyup" }; 111 112 public final static String [] SVG_DOM_EVENT = { 113 "SVGAbort", "SVGError", "SVGResize", "SVGScroll", "SVGUnload", "SVGZoom", 120 "beginEvent", "endEvent", "repeatEvent", 124 "DOMFocusIn", "DOMFocusOut", "DOMActivate", "click", "mousedown", "mouseup", "mouseover", "mouseout", "mousemove", "keypress", "keydown", "keyup" }; 137 138 141 protected Timer timer = new Timer (true); 142 143 146 protected UpdateManager updateManager; 147 148 151 protected RunnableQueue updateRunnableQueue; 152 153 156 protected EventListener domNodeInsertedListener 157 = new DOMNodeInsertedListener(); 158 159 162 protected EventListener domNodeRemovedListener 163 = new DOMNodeRemovedListener(); 164 165 168 protected EventListener domAttrModifiedListener 169 = new DOMAttrModifiedListener(); 170 171 174 protected EventListener svgAbortListener = 175 new ScriptingEventListener("onabort"); 176 177 180 protected EventListener svgErrorListener = 181 new ScriptingEventListener("onerror"); 182 183 186 protected EventListener svgResizeListener = 187 new ScriptingEventListener("onresize"); 188 189 192 protected EventListener svgScrollListener = 193 new ScriptingEventListener("onscroll"); 194 195 198 protected EventListener svgUnloadListener = 199 new ScriptingEventListener("onunload"); 200 201 204 protected EventListener svgZoomListener = 205 new ScriptingEventListener("onzoom"); 206 207 210 protected EventListener beginListener = 211 new ScriptingEventListener("onbegin"); 212 213 216 protected EventListener endListener = 217 new ScriptingEventListener("onend"); 218 219 222 protected EventListener repeatListener = 223 new ScriptingEventListener("onrepeat"); 224 225 228 protected EventListener focusinListener = 229 new ScriptingEventListener("onfocusin"); 230 231 234 protected EventListener focusoutListener = 235 new ScriptingEventListener("onfocusout"); 236 237 240 protected EventListener activateListener = 241 new ScriptingEventListener("onactivate"); 242 243 246 protected EventListener clickListener = 247 new ScriptingEventListener("onclick"); 248 249 252 protected EventListener mousedownListener = 253 new ScriptingEventListener("onmousedown"); 254 255 258 protected EventListener mouseupListener = 259 new ScriptingEventListener("onmouseup"); 260 261 264 protected EventListener mouseoverListener = 265 new ScriptingEventListener("onmouseover"); 266 267 270 protected EventListener mouseoutListener = 271 new ScriptingEventListener("onmouseout"); 272 273 276 protected EventListener mousemoveListener = 277 new ScriptingEventListener("onmousemove"); 278 279 282 protected EventListener keypressListener = 283 new ScriptingEventListener("onkeypress"); 284 285 288 protected EventListener keydownListener = 289 new ScriptingEventListener("onkeydown"); 290 291 294 protected EventListener keyupListener = 295 new ScriptingEventListener("onkeyup"); 296 297 298 protected EventListener [] 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 attrToDOMEvent = new HashMap (SVG_EVENT_ATTRS.length); 327 Map attrToListener = new HashMap (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 339 public ScriptingEnvironment(BridgeContext ctx) { 340 super(ctx); 341 updateManager = ctx.getUpdateManager(); 342 updateRunnableQueue = updateManager.getUpdateRunnableQueue(); 343 344 addScriptingListeners(document.getDocumentElement()); 346 347 EventTarget et = (EventTarget )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 363 public org.apache.batik.script.Window createWindow(Interpreter interp, 364 String lang) { 365 return new Window(interp, lang); 366 } 367 368 371 public void runEventHandler(String script, Event evt, 372 String lang, String 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 (script), desc); 383 } catch (IOException ioe) { 384 } catch (InterpreterException ie) { 386 handleInterpreterException(ie); 387 } catch (SecurityException se) { 388 handleSecurityException(se); 389 } 390 } 391 392 395 public void interrupt() { 396 timer.cancel(); 397 removeScriptingListeners(document.getDocumentElement()); 399 400 EventTarget et = (EventTarget )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 416 protected void addScriptingListeners(Node node) { 417 if (node.getNodeType() == Node.ELEMENT_NODE) { 418 Element elt = (Element )node; 420 EventTarget target = (EventTarget )elt; 421 if (SVGConstants.SVG_NAMESPACE_URI.equals(elt.getNamespaceURI())) { 422 if (SVGConstants.SVG_SVG_TAG.equals(elt.getLocalName())) { 423 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 name = elt.getLocalName(); 450 if (name.equals(SVGConstants.SVG_SET_TAG) || 451 name.startsWith("animate")) { 452 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 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 for (Node n = node.getFirstChild(); 516 n != null; 517 n = n.getNextSibling()) { 518 addScriptingListeners(n); 519 } 520 } 521 522 525 protected void removeScriptingListeners(Node node) { 526 if (node.getNodeType() == Node.ELEMENT_NODE) { 527 Element elt = (Element )node; 529 EventTarget target = (EventTarget )elt; 530 if (SVGConstants.SVG_NAMESPACE_URI.equals(elt.getNamespaceURI())) { 531 if (SVGConstants.SVG_SVG_TAG.equals(elt.getLocalName())) { 532 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 name = elt.getLocalName(); 547 if (name.equals(SVGConstants.SVG_SET_TAG) || 548 name.startsWith("animate")) { 549 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 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 for (Node n = node.getFirstChild(); 581 n != null; 582 n = n.getNextSibling()) { 583 removeScriptingListeners(n); 584 } 585 } 586 587 590 protected void updateScriptingListeners(Element elt, String attr) { 591 String domEvt = (String ) attrToDOMEvent.get(attr); 592 if (domEvt == null) return; EventListener listener = (EventListener )attrToListener.get(attr); 594 EventTarget target = (EventTarget ) 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 605 protected class EvaluateRunnable implements Runnable { 606 protected Interpreter interpreter; 607 protected String script; 608 public EvaluateRunnable(String 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 624 protected class EvaluateIntervalRunnable implements Runnable { 625 628 public int count; 629 public boolean error; 630 631 protected Interpreter interpreter; 632 protected String script; 633 634 public EvaluateIntervalRunnable(String 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 e) { 652 if (userAgent != null) { 653 userAgent.displayError(e); 654 } else { 655 e.printStackTrace(); } 657 synchronized (this) { 658 error = true; 659 } 660 } 661 } 662 } 663 664 667 protected class EvaluateRunnableRunnable implements Runnable { 668 671 public int count; 672 public boolean error; 673 674 protected Runnable runnable; 675 676 public EvaluateRunnableRunnable(Runnable 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 e) { 688 if (userAgent != null) { 689 userAgent.displayError(e); 690 } else { 691 e.printStackTrace(); } 693 synchronized (this) { 694 error = true; 695 } 696 } 697 } 698 } 699 700 703 protected class Window implements org.apache.batik.script.Window { 704 705 708 protected Interpreter interpreter; 709 710 713 protected String language; 714 715 718 public Window(Interpreter interp, String lang) { 719 interpreter = interp; 720 language = lang; 721 } 722 723 727 public Object setInterval(final String script, long interval) { 728 TimerTask tt = new TimerTask () { 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 759 public Object setInterval(final Runnable r, long interval) { 760 TimerTask tt = new TimerTask () { 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 785 public void clearInterval(Object interval) { 786 if (interval == null) return; 787 ((TimerTask )interval).cancel(); 788 } 789 790 794 public Object setTimeout(final String script, long timeout) { 795 TimerTask tt = new TimerTask () { 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 810 public Object setTimeout(final Runnable r, long timeout) { 811 TimerTask tt = new TimerTask () { 812 public void run() { 813 updateRunnableQueue.invokeLater(new Runnable () { 814 public void run() { 815 try { 816 r.run(); 817 } catch (Exception 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 835 public void clearTimeout(Object timeout) { 836 if (timeout == null) return; 837 ((TimerTask )timeout).cancel(); 838 } 839 840 844 public Node parseXML(String text, Document doc) { 845 SAXSVGDocumentFactory df = new SAXSVGDocumentFactory 848 (XMLResourceDescriptor.getXMLParserClassName()); 849 URL 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 uri = (urlObj==null)?"":urlObj.toString(); 857 try { 858 Document d = df.createDocument(uri, new StringReader (text)); 859 if (doc == null) 860 return d; 861 862 Node result = doc.createDocumentFragment(); 863 result.appendChild(doc.importNode(d.getDocumentElement(), 864 true)); 865 return result; 866 } catch (Exception ex) { 867 868 } 869 870 if ((doc != null) && (doc instanceof SVGOMDocument)) { 871 875 StringBuffer sb = new StringBuffer (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 newText = sb.toString(); 885 try { 886 Document d = df.createDocument 887 (uri, new StringReader (newText)); 888 if (doc == null) doc = d; 891 for (Node 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 result = doc.createDocumentFragment(); 897 result.appendChild(n); 898 return result; 899 } 900 } 901 } catch (Exception exc) { 902 903 } 904 } 905 906 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 (text)); 919 if (doc == null) 920 return d; 921 922 Node result = doc.createDocumentFragment(); 923 result.appendChild(doc.importNode(d.getDocumentElement(), 924 true)); 925 return result; 926 } catch (Exception ext) { 927 if (userAgent != null) 928 userAgent.displayError(ext); 929 } 930 931 return null; 932 } 933 934 938 public void getURL(String uri, org.apache.batik.script.Window.URLResponseHandler h) { 939 getURL(uri, h, null); 940 } 941 942 final static String DEFLATE="deflate"; 943 final static String GZIP ="gzip"; 944 final static String UTF_8 ="UTF-8"; 945 949 public void getURL(final String uri, 950 final org.apache.batik.script.Window.URLResponseHandler h, 951 final String enc) { 952 Thread t = new Thread () { 953 public void run() { 954 try { 955 URL burl; 956 burl = ((SVGOMDocument)document).getURLObject(); 957 final ParsedURL purl = new ParsedURL(burl, uri); 958 String e = null; 959 if (enc != null) { 960 e = EncodingUtilities.javaEncoding(enc); 961 e = ((e == null) ? enc : e); 962 } 963 964 InputStream is = purl.openStream(); 965 Reader r; 966 if (e == null) { 967 r = new InputStreamReader (is); 969 } else { 970 try { 971 r = new InputStreamReader (is, e); 972 } catch (UnsupportedEncodingException uee) { 973 r = new InputStreamReader (is); 975 } 976 } 977 r = new BufferedReader (r); 978 final StringBuffer sb = new StringBuffer (); 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 () { 987 public void run() { 988 try { 989 h.getURLDone(true, 990 purl.getContentType(), 991 sb.toString()); 992 } catch (Exception e){ 993 if (userAgent != null) { 994 userAgent.displayError(e); 995 } 996 } 997 } 998 }); 999 } catch (Exception e) { 1000 if (e instanceof SecurityException ) { 1001 userAgent.displayError(e); 1002 } 1003 updateRunnableQueue.invokeLater(new Runnable () { 1004 public void run() { 1005 try { 1006 h.getURLDone(false, null, null); 1007 } catch (Exception 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 uri, String content, 1024 org.apache.batik.script.Window.URLResponseHandler h) { 1025 postURL(uri, content, h, "text/plain", null); 1026 } 1027 1028 public void postURL(String uri, String content, 1029 org.apache.batik.script.Window.URLResponseHandler h, 1030 String mimeType) { 1031 postURL(uri, content, h, mimeType, null); 1032 } 1033 1034 public void postURL(final String uri, 1035 final String content, 1036 final org.apache.batik.script.Window.URLResponseHandler h, 1037 final String mimeType, 1038 final String fEnc) { 1039 Thread t = new Thread () { 1040 public void run() { 1041 try { 1042 URL burl; 1043 burl = ((SVGOMDocument)document).getURLObject(); 1044 URL url; 1045 if (burl != null) 1046 url = new URL (burl, uri); 1047 else url = new URL (uri); 1048 final URLConnection conn = url.openConnection(); 1049 conn.setDoOutput(true); 1050 conn.setDoInput(true); 1051 conn.setUseCaches(false); 1052 conn.setRequestProperty("Content-Type", mimeType); 1053 1054 OutputStream os = conn.getOutputStream(); 1055 String e=null, enc = fEnc; 1056 if (enc != null) { 1057 if (enc.startsWith(DEFLATE)) { 1058 os = new DeflaterOutputStream (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 (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 w; 1084 if (e == null) 1085 w = new OutputStreamWriter (os); 1086 else 1087 w = new OutputStreamWriter (os, e); 1088 w.write(content); 1089 w.flush(); 1090 w.close(); 1091 os.close(); 1092 1093 InputStream is = conn.getInputStream(); 1094 Reader r; 1095 e = UTF_8; 1096 if (e == null) 1097 r = new InputStreamReader (is); 1098 else 1099 r = new InputStreamReader (is, e); 1100 r = new BufferedReader (r); 1101 1102 final StringBuffer sb = new StringBuffer (); 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 () { 1111 public void run() { 1112 try { 1113 h.getURLDone(true, 1114 conn.getContentType(), 1115 sb.toString()); 1116 } catch (Exception e){ 1117 if (userAgent != null) { 1118 userAgent.displayError(e); 1119 } 1120 } 1121 } 1122 }); 1123 } catch (Exception e) { 1124 if (e instanceof SecurityException ) { 1125 userAgent.displayError(e); 1126 } 1127 updateRunnableQueue.invokeLater(new Runnable () { 1128 public void run() { 1129 try { 1130 h.getURLDone(false, null, null); 1131 } catch (Exception 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 1149 public void alert(String message) { 1150 if (userAgent != null) { 1151 userAgent.showAlert(message); 1152 } 1153 } 1154 1155 1158 public boolean confirm(String message) { 1159 if (userAgent != null) { 1160 return userAgent.showConfirm(message); 1161 } 1162 return false; 1163 } 1164 1165 1168 public String prompt(String message) { 1169 if (userAgent != null) { 1170 return userAgent.showPrompt(message); 1171 } 1172 return null; 1173 } 1174 1175 1178 public String prompt(String message, String defVal) { 1179 if (userAgent != null) { 1180 return userAgent.showPrompt(message, defVal); 1181 } 1182 return null; 1183 } 1184 1185 1188 public BridgeContext getBridgeContext() { 1189 return bridgeContext; 1190 } 1191 1192 1195 public Interpreter getInterpreter() { 1196 return interpreter; 1197 } 1198 } 1199 1200 1203 protected class DOMNodeInsertedListener implements EventListener { 1204 public void handleEvent(Event evt) { 1205 addScriptingListeners((Node )evt.getTarget()); 1206 } 1207 } 1208 1209 1212 protected class DOMNodeRemovedListener implements EventListener { 1213 public void handleEvent(Event evt) { 1214 removeScriptingListeners((Node )evt.getTarget()); 1215 } 1216 } 1217 1218 protected class DOMAttrModifiedListener implements EventListener { 1219 public void handleEvent (Event evt) { 1220 MutationEvent me = (MutationEvent )evt; 1221 if (me.getAttrChange() != MutationEvent.MODIFICATION) 1222 updateScriptingListeners((Element )me.getTarget(), 1223 me.getAttrName()); 1224 } 1225 } 1226 1227 1230 protected class ScriptingEventListener implements EventListener { 1231 1232 1235 protected String attribute; 1236 1237 1240 public ScriptingEventListener(String attr) { 1241 attribute = attr; 1242 } 1243 1244 1247 public void handleEvent(Event evt) { 1248 Element elt = (Element )evt.getCurrentTarget(); 1249 String 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 desc = Messages.formatMessage 1258 (EVENT_SCRIPT_DESCRIPTION, 1259 new Object [] {d.getURL(), attribute, new Integer (line)}); 1260 1261 Element 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 lang = e.getAttributeNS 1273 (null, SVGConstants.SVG_CONTENT_SCRIPT_TYPE_ATTRIBUTE); 1274 1275 runEventHandler(script, evt, lang, desc); 1276 } 1277 } 1278} 1279 | Popular Tags |