1 18 package org.apache.batik.bridge; 19 20 import java.awt.Cursor ; 21 import java.awt.geom.Dimension2D ; 22 import java.io.IOException ; 23 import java.io.InterruptedIOException ; 24 import java.lang.ref.SoftReference ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.ListIterator ; 33 import java.util.Map ; 34 import java.util.Set ; 35 import java.util.WeakHashMap ; 36 37 import org.apache.batik.bridge.svg12.SVG12BridgeExtension; 38 import org.apache.batik.css.engine.CSSContext; 39 import org.apache.batik.css.engine.CSSEngine; 40 import org.apache.batik.css.engine.CSSEngineEvent; 41 import org.apache.batik.css.engine.CSSEngineListener; 42 import org.apache.batik.css.engine.CSSEngineUserAgent; 43 import org.apache.batik.css.engine.SVGCSSEngine; 44 import org.apache.batik.css.engine.SystemColorSupport; 45 import org.apache.batik.css.engine.value.Value; 46 import org.apache.batik.dom.svg.SVGContext; 47 import org.apache.batik.dom.svg.SVGDOMImplementation; 48 import org.apache.batik.dom.svg.SVGOMDocument; 49 import org.apache.batik.dom.svg.SVGOMElement; 50 import org.apache.batik.dom.svg.SVGStylableElement; 51 import org.apache.batik.gvt.CompositeGraphicsNode; 52 import org.apache.batik.gvt.GraphicsNode; 53 import org.apache.batik.gvt.TextPainter; 54 import org.apache.batik.script.Interpreter; 55 import org.apache.batik.script.InterpreterPool; 56 import org.apache.batik.util.CSSConstants; 57 import org.apache.batik.util.CleanerThread; 58 import org.apache.batik.util.ParsedURL; 59 import org.apache.batik.util.SVGConstants; 60 import org.apache.batik.util.Service; 61 62 import org.w3c.dom.Document ; 63 import org.w3c.dom.Element ; 64 import org.w3c.dom.Node ; 65 import org.w3c.dom.events.Event ; 66 import org.w3c.dom.events.EventListener ; 67 import org.w3c.dom.events.EventTarget ; 68 import org.w3c.dom.events.MouseEvent ; 69 import org.w3c.dom.events.MutationEvent ; 70 import org.w3c.dom.svg.SVGDocument; 71 72 85 public class BridgeContext implements ErrorConstants, CSSContext { 86 87 90 protected Document document; 91 92 95 protected GVTBuilder gvtBuilder; 96 97 102 protected Map interpreterMap = new HashMap (7); 103 104 109 private Map fontFamilyMap; 110 111 116 protected Map viewportMap = new WeakHashMap (); 117 118 121 protected List viewportStack = new LinkedList (); 122 123 126 protected UserAgent userAgent; 127 128 133 protected Map elementNodeMap; 134 135 140 protected Map nodeElementMap; 141 142 147 protected Map namespaceURIMap; 148 149 159 protected Map elementDataMap; 160 161 162 165 protected InterpreterPool interpreterPool; 166 167 170 protected DocumentLoader documentLoader; 171 172 175 protected Dimension2D documentSize; 176 177 181 protected TextPainter textPainter; 182 183 189 public final static int STATIC = 0; 190 195 public final static int INTERACTIVE = 1; 196 197 202 public final static int DYNAMIC = 2; 203 204 207 protected int dynamicStatus = STATIC; 208 209 212 protected UpdateManager updateManager; 213 214 217 protected BridgeContext() {} 218 219 222 private static InterpreterPool sharedPool = new InterpreterPool(); 223 224 228 public BridgeContext(UserAgent userAgent) { 229 this(userAgent, 230 sharedPool, 231 new DocumentLoader(userAgent)); 232 } 233 234 239 public BridgeContext(UserAgent userAgent, 240 DocumentLoader loader) { 241 this(userAgent, sharedPool, loader); 242 } 243 244 250 public BridgeContext(UserAgent userAgent, 251 InterpreterPool interpreterPool, 252 DocumentLoader documentLoader) { 253 this.userAgent = userAgent; 254 this.viewportMap.put(userAgent, new UserAgentViewport(userAgent)); 255 this.interpreterPool = interpreterPool; 256 this.documentLoader = documentLoader; 257 } 258 259 262 protected void initializeDocument(Document document) { 263 SVGOMDocument doc = (SVGOMDocument)document; 264 CSSEngine eng = doc.getCSSEngine(); 265 if (eng == null) { 266 SVGDOMImplementation impl; 267 impl = (SVGDOMImplementation)doc.getImplementation(); 268 eng = impl.createCSSEngine(doc, this); 269 eng.setCSSEngineUserAgent(new CSSEngineUserAgentWrapper(userAgent)); 270 doc.setCSSEngine(eng); 271 eng.setMedia(userAgent.getMedia()); 272 String uri = userAgent.getUserStyleSheetURI(); 273 if (uri != null) { 274 try { 275 URL url = new URL (uri); 276 eng.setUserAgentStyleSheet 277 (eng.parseStyleSheet(url, "all")); 278 } catch (MalformedURLException e) { 279 userAgent.displayError(e); 280 } 281 } 282 eng.setAlternateStyleSheet(userAgent.getAlternateStyleSheet()); 283 } 284 } 285 286 289 public CSSEngine getCSSEngineForElement(Element e) { 290 SVGOMDocument doc = (SVGOMDocument)e.getOwnerDocument(); 291 return doc.getCSSEngine(); 292 } 293 294 296 303 public void setTextPainter(TextPainter textPainter) { 304 this.textPainter = textPainter; 305 } 306 307 310 public TextPainter getTextPainter() { 311 return textPainter; 312 } 313 314 317 public Document getDocument() { 318 return document; 319 } 320 321 326 protected void setDocument(Document document) { 327 if (this.document != document){ 328 fontFamilyMap = null; 329 } 330 this.document = document; 331 registerSVGBridges(); 332 } 333 334 337 public Map getFontFamilyMap(){ 338 if (fontFamilyMap == null){ 339 fontFamilyMap = new HashMap (); 340 } 341 return fontFamilyMap; 342 } 343 344 349 protected void setFontFamilyMap(Map fontFamilyMap) { 350 this.fontFamilyMap = fontFamilyMap; 351 } 352 353 358 public void setElementData(Node n, Object data) { 359 if (elementDataMap == null) 360 elementDataMap = new WeakHashMap (); 361 elementDataMap.put(n, new SoftReference (data)); 362 } 363 364 369 public Object getElementData(Node n) { 370 if (elementDataMap == null) 371 return null; 372 Object o = elementDataMap.get(n); 373 if (o == null) return null; 374 SoftReference sr = (SoftReference )o; 375 o = sr.get(); 376 if (o == null) { 377 elementDataMap.remove(n); 378 } 379 return o; 380 } 381 382 385 public UserAgent getUserAgent() { 386 return userAgent; 387 } 388 389 393 protected void setUserAgent(UserAgent userAgent) { 394 this.userAgent = userAgent; 395 } 396 397 400 public GVTBuilder getGVTBuilder() { 401 return gvtBuilder; 402 } 403 404 407 protected void setGVTBuilder(GVTBuilder gvtBuilder) { 408 this.gvtBuilder = gvtBuilder; 409 } 410 411 414 public InterpreterPool getInterpreterPool() { 415 return interpreterPool; 416 } 417 418 421 public FocusManager getFocusManager() { 422 return focusManager; 423 } 424 425 428 public CursorManager getCursorManager() { 429 return cursorManager; 430 } 431 432 437 protected void setInterpreterPool(InterpreterPool interpreterPool) { 438 this.interpreterPool = interpreterPool; 439 } 440 441 446 public Interpreter getInterpreter(String language) { 447 if (document == null) { 448 throw new RuntimeException ("Unknown document"); 449 } 450 Interpreter interpreter = (Interpreter)interpreterMap.get(language); 451 if (interpreter == null) { 452 try { 453 interpreter = interpreterPool.createInterpreter(document, language); 454 interpreterMap.put(language, interpreter); 455 } catch (Exception e) { 456 if (userAgent != null) { 457 userAgent.displayError(e); 458 return null; 459 } 460 } 461 } 462 463 if (interpreter == null) { 464 if (userAgent != null) { 465 userAgent.displayError(new Exception ("Unknown language: " + language)); 466 } 467 } 468 469 return interpreter; 470 } 471 472 475 public DocumentLoader getDocumentLoader() { 476 return documentLoader; 477 } 478 479 483 protected void setDocumentLoader(DocumentLoader newDocumentLoader) { 484 this.documentLoader = newDocumentLoader; 485 } 486 487 491 public Dimension2D getDocumentSize() { 492 return documentSize; 493 } 494 495 500 protected void setDocumentSize(Dimension2D d) { 501 this.documentSize = d; 502 } 503 504 507 public boolean isDynamic() { 508 return (dynamicStatus == DYNAMIC); 509 } 510 511 514 public boolean isInteractive() { 515 return (dynamicStatus != STATIC); 516 } 517 518 526 public void setDynamicState(int status) { 527 dynamicStatus = status; 528 } 529 530 534 public void setDynamic(boolean dynamic) { 535 if (dynamic) 536 setDynamicState(DYNAMIC); 537 else 538 setDynamicState(STATIC); 539 } 540 541 545 public void setInteractive(boolean interactive) { 546 if (interactive) 547 setDynamicState(INTERACTIVE); 548 else 549 setDynamicState(STATIC); 550 } 551 552 553 556 public UpdateManager getUpdateManager() { 557 return updateManager; 558 } 559 560 563 protected void setUpdateManager(UpdateManager um) { 564 updateManager = um; 565 } 566 567 569 576 public Element getReferencedElement(Element e, String uri) { 577 try { 578 SVGDocument document = (SVGDocument)e.getOwnerDocument(); 579 URIResolver ur = new URIResolver(document, documentLoader); 580 Element ref = ur.getElement(uri, e); 581 if (ref == null) { 582 throw new BridgeException(e, ERR_URI_BAD_TARGET, 583 new Object [] {uri}); 584 } else { 585 SVGOMDocument refDoc = (SVGOMDocument)ref.getOwnerDocument(); 586 if (refDoc != document) { 594 CSSEngine eng = refDoc.getCSSEngine(); 595 if (eng == null) { 596 BridgeContext subCtx; 597 subCtx = new BridgeContext(getUserAgent(), 598 getDocumentLoader()); 599 subCtx.setGVTBuilder(getGVTBuilder()); 600 subCtx.setDocument(refDoc); 601 subCtx.initializeDocument(refDoc); 602 } 603 } 604 return ref; 605 } 606 } catch (MalformedURLException ex) { 607 throw new BridgeException(e, ERR_URI_MALFORMED, 608 new Object [] {uri}); 609 } catch (InterruptedIOException ex) { 610 throw new InterruptedBridgeException(); 611 } catch (IOException ex) { 612 throw new BridgeException(e, ERR_URI_IO, 613 new Object [] {uri}); 614 } catch (IllegalArgumentException ex) { 615 throw new BridgeException(e, ERR_URI_REFERENCE_A_DOCUMENT, 616 new Object [] {uri}); 617 } catch (SecurityException ex) { 618 throw new BridgeException(e, ERR_URI_UNSECURE, 619 new Object [] {uri}); 620 } 621 } 622 623 625 630 public Viewport getViewport(Element e) { 631 if (viewportStack != null) { 632 if (viewportStack.size() == 0) { 634 return (Viewport)viewportMap.get(userAgent); 636 } else { 637 return (Viewport)viewportStack.get(0); 639 } 640 } else { 641 e = SVGUtilities.getParentElement(e); 643 while (e != null) { 644 Viewport viewport = (Viewport)viewportMap.get(e); 645 if (viewport != null) { 646 return viewport; 647 } 648 e = SVGUtilities.getParentElement(e); 649 } 650 return (Viewport)viewportMap.get(userAgent); 651 } 652 } 653 654 660 public void openViewport(Element e, Viewport viewport) { 661 viewportMap.put(e, viewport); 662 if (viewportStack == null) { 663 viewportStack = new LinkedList (); 664 } 665 viewportStack.add(0, viewport); 666 } 667 668 public void removeViewport(Element e) { 669 viewportMap.remove(e); 670 } 671 672 676 public void closeViewport(Element e) { 677 viewportStack.remove(0); 679 if (viewportStack.size() == 0) { 680 viewportStack = null; 681 } 682 } 683 684 686 694 public void bind(Element element, GraphicsNode node) { 695 if (elementNodeMap == null) { 696 elementNodeMap = new WeakHashMap (); 697 nodeElementMap = new WeakHashMap (); 698 } 699 elementNodeMap.put(element, new SoftReference (node)); 700 nodeElementMap.put(node, new SoftReference (element)); 701 } 702 703 708 public void unbind(Element element) { 709 if (elementNodeMap == null) { 710 return; 711 } 712 GraphicsNode node = null; 713 SoftReference sr = (SoftReference )elementNodeMap.get(element); 714 if (sr != null) 715 node = (GraphicsNode)sr.get(); 716 elementNodeMap.remove(element); 717 if (node != null) 718 nodeElementMap.remove(node); 719 } 720 721 727 public GraphicsNode getGraphicsNode(Element element) { 728 if (elementNodeMap != null) { 729 SoftReference sr = (SoftReference )elementNodeMap.get(element); 730 if (sr != null) 731 return (GraphicsNode)sr.get(); 732 } 733 return null; 734 } 735 736 742 public Element getElement(GraphicsNode node) { 743 if (nodeElementMap != null) { 744 SoftReference sr = (SoftReference )nodeElementMap.get(node); 745 if (sr != null) 746 return (Element)sr.get(); 747 } 748 return null; 749 } 750 751 753 759 public boolean hasGraphicsNodeBridge(Element element) { 760 if (namespaceURIMap == null || element == null) { 761 return false; 762 } 763 String localName = element.getLocalName(); 764 String namespaceURI = element.getNamespaceURI(); 765 namespaceURI = ((namespaceURI == null)? "" : namespaceURI); 766 HashMap localNameMap = (HashMap ) namespaceURIMap.get(namespaceURI); 767 if (localNameMap == null) { 768 return false; 769 } 770 return (localNameMap.get(localName) instanceof GraphicsNodeBridge); 771 } 772 773 778 public Bridge getBridge(Element element) { 779 if (namespaceURIMap == null || element == null) { 780 return null; 781 } 782 String localName = element.getLocalName(); 783 String namespaceURI = element.getNamespaceURI(); 784 namespaceURI = ((namespaceURI == null)? "" : namespaceURI); 785 return getBridge(namespaceURI, localName); 786 } 787 788 795 public Bridge getBridge(String namespaceURI, String localName) { 796 HashMap localNameMap = (HashMap ) namespaceURIMap.get(namespaceURI); 797 if (localNameMap == null) { 798 return null; 799 } 800 Bridge bridge = (Bridge)localNameMap.get(localName); 801 if (isDynamic()) { 802 return bridge == null ? null : bridge.getInstance(); 803 } else { 804 return bridge; 805 } 806 } 807 808 815 public void putBridge(String namespaceURI, String localName, Bridge bridge) { 816 if (!(namespaceURI.equals(bridge.getNamespaceURI()) 818 && localName.equals(bridge.getLocalName()))) { 819 throw new Error ("Invalid Bridge: "+ 820 namespaceURI+"/"+bridge.getNamespaceURI()+" "+ 821 localName+"/"+bridge.getLocalName()+" "+ 822 bridge.getClass()); 823 } 824 if (namespaceURIMap == null) { 826 namespaceURIMap = new HashMap (); 827 } 828 namespaceURI = ((namespaceURI == null)? "" : namespaceURI); 829 HashMap localNameMap = (HashMap ) namespaceURIMap.get(namespaceURI); 830 if (localNameMap == null) { 831 localNameMap = new HashMap (); 832 namespaceURIMap.put(namespaceURI, localNameMap); 833 } 834 localNameMap.put(localName, bridge); 835 } 836 837 843 public void putBridge(Bridge bridge) { 844 putBridge(bridge.getNamespaceURI(), bridge.getLocalName(), bridge); 845 } 846 847 854 public void removeBridge(String namespaceURI, String localName) { 855 if (namespaceURIMap == null) { 856 return; 857 } 858 namespaceURI = ((namespaceURI == null)? "" : namespaceURI); 859 HashMap localNameMap = (HashMap ) namespaceURIMap.get(namespaceURI); 860 if (localNameMap != null) { 861 localNameMap.remove(localName); 862 if (localNameMap.isEmpty()) { 863 namespaceURIMap.remove(namespaceURI); 864 if (namespaceURIMap.isEmpty()) { 865 namespaceURIMap = null; 866 } 867 } 868 } 869 } 870 871 873 877 protected Set eventListenerSet = new HashSet (); 878 879 882 protected EventListener domCharacterDataModifiedListener; 883 884 887 protected EventListener domAttrModifiedEventListener; 888 889 892 protected EventListener domNodeInsertedEventListener; 893 894 897 protected EventListener domNodeRemovedEventListener; 898 899 902 protected CSSEngineListener cssPropertiesChangedListener; 903 904 907 protected FocusManager focusManager; 908 909 912 protected CursorManager cursorManager = new CursorManager(this); 913 914 926 public void addUIEventListeners(Document doc) { 927 EventTarget evtTarget = (EventTarget )doc.getDocumentElement(); 928 929 DOMMouseOverEventListener domMouseOverListener = 930 new DOMMouseOverEventListener(); 931 evtTarget.addEventListener(SVGConstants.SVG_EVENT_MOUSEOVER, 932 domMouseOverListener, 933 true); 934 storeEventListener(evtTarget, SVGConstants.SVG_EVENT_MOUSEOVER, 935 domMouseOverListener, true); 936 937 DOMMouseOutEventListener domMouseOutListener = 938 new DOMMouseOutEventListener(); 939 evtTarget.addEventListener(SVGConstants.SVG_EVENT_MOUSEOUT, 940 domMouseOutListener, 941 true); 942 storeEventListener(evtTarget, SVGConstants.SVG_EVENT_MOUSEOUT, 943 domMouseOutListener, true); 944 945 } 946 947 948 public void removeUIEventListeners(Document doc) { 949 EventTarget evtTarget = (EventTarget )doc.getDocumentElement(); 950 synchronized (eventListenerSet) { 951 Iterator i = eventListenerSet.iterator(); 952 while (i.hasNext()) { 953 EventListenerMememto elm = (EventListenerMememto)i.next(); 954 EventTarget et = elm.getTarget(); 955 if (et == evtTarget) { 956 EventListener el = elm.getListener(); 957 boolean uc = elm.getUseCapture(); 958 String t = elm.getEventType(); 959 if ((et == null) || (el == null) || (t == null)) 960 continue; 961 et.removeEventListener(t, el, uc); 962 } 963 } 964 } 965 966 } 967 968 973 public void addDOMListeners() { 974 EventTarget evtTarget = (EventTarget )document; 975 976 domAttrModifiedEventListener = new DOMAttrModifiedEventListener(); 977 evtTarget.addEventListener("DOMAttrModified", 978 domAttrModifiedEventListener, 979 true); 980 981 domNodeInsertedEventListener = new DOMNodeInsertedEventListener(); 982 evtTarget.addEventListener("DOMNodeInserted", 983 domNodeInsertedEventListener, 984 true); 985 986 domNodeRemovedEventListener = new DOMNodeRemovedEventListener(); 987 evtTarget.addEventListener("DOMNodeRemoved", 988 domNodeRemovedEventListener, 989 true); 990 991 domCharacterDataModifiedListener = 992 new DOMCharacterDataModifiedListener(); 993 evtTarget.addEventListener("DOMCharacterDataModified", 994 domCharacterDataModifiedListener, 995 true); 996 997 998 focusManager = new FocusManager(document); 999 1000 SVGOMDocument svgDocument = (SVGOMDocument)document; 1001 CSSEngine cssEngine = svgDocument.getCSSEngine(); 1002 cssPropertiesChangedListener = new CSSPropertiesChangedListener(); 1003 cssEngine.addCSSEngineListener(cssPropertiesChangedListener); 1004 } 1005 1006 1010 protected void storeEventListener(EventTarget t, 1011 String s, 1012 EventListener l, 1013 boolean b) { 1014 synchronized (eventListenerSet) { 1015 eventListenerSet.add(new EventListenerMememto(t, s, l, b, this)); 1016 } 1017 } 1018 1019 public static class SoftReferenceMememto 1020 extends CleanerThread.SoftReferenceCleared { 1021 Object mememto; 1022 Set set; 1023 SoftReferenceMememto(Object ref, Object mememto, Set set) { 1025 super(ref); 1026 this.mememto = mememto; 1028 this.set = set; 1029 } 1030 1031 public void cleared() { 1032 synchronized (set) { 1033 set.remove(mememto); 1035 mememto = null; 1036 set = null; 1037 } 1038 } 1039 } 1040 1041 1044 protected static class EventListenerMememto { 1045 1046 public SoftReference target; public SoftReference listener; public boolean useCapture; 1049 public String eventType; 1050 1051 public EventListenerMememto(EventTarget t, 1052 String s, 1053 EventListener l, 1054 boolean b, 1055 BridgeContext ctx) { 1056 Set set = ctx.eventListenerSet; 1057 target = new SoftReferenceMememto(t, this, set); 1058 listener = new SoftReferenceMememto(l, this, set); 1059 eventType = s; 1060 useCapture = b; 1061 } 1062 1063 public EventListener getListener() { 1064 return (EventListener )listener.get(); 1065 } 1066 public EventTarget getTarget() { 1067 return (EventTarget )target.get(); 1068 } 1069 public boolean getUseCapture() { 1070 return useCapture; 1071 } 1072 public String getEventType() { 1073 return eventType; 1074 } 1075 } 1076 1077 1078 1081 public void dispose() { 1082 1083 synchronized (eventListenerSet) { 1084 Iterator iter = eventListenerSet.iterator(); 1086 while (iter.hasNext()) { 1087 EventListenerMememto m = (EventListenerMememto)iter.next(); 1088 EventTarget et = m.getTarget(); 1089 EventListener el = m.getListener(); 1090 boolean uc = m.getUseCapture(); 1091 String t = m.getEventType(); 1092 if ((et == null) || (el == null) || (t == null)) 1093 continue; 1094 et.removeEventListener(t, el, uc); 1095 } 1096 } 1097 1098 if (document != null) { 1099 EventTarget evtTarget = (EventTarget )document; 1100 1101 evtTarget.removeEventListener("DOMAttrModified", 1102 domAttrModifiedEventListener, 1103 true); 1104 evtTarget.removeEventListener("DOMNodeInserted", 1105 domNodeInsertedEventListener, 1106 true); 1107 evtTarget.removeEventListener("DOMNodeRemoved", 1108 domNodeRemovedEventListener, 1109 true); 1110 evtTarget.removeEventListener("DOMCharacterDataModified", 1111 domCharacterDataModifiedListener, 1112 true); 1113 1114 SVGOMDocument svgDocument = (SVGOMDocument)document; 1115 CSSEngine cssEngine = svgDocument.getCSSEngine(); 1116 if (cssEngine != null) { 1117 cssEngine.removeCSSEngineListener 1118 (cssPropertiesChangedListener); 1119 cssEngine.dispose(); 1120 svgDocument.setCSSEngine(null); 1121 } 1122 } 1123 Iterator iter = interpreterMap.values().iterator(); 1124 while (iter.hasNext()) { 1125 Interpreter interpreter = (Interpreter)iter.next(); 1126 if (interpreter != null) 1127 interpreter.dispose(); 1128 } 1129 interpreterMap.clear(); 1130 1131 if (focusManager != null) { 1132 focusManager.dispose(); 1133 } 1134 } 1135 1136 1139 protected static SVGContext getSVGContext(Node node) { 1140 if (node instanceof SVGOMElement) { 1141 return ((SVGOMElement)node).getSVGContext(); 1142 } else { 1143 return null; 1144 } 1145 } 1146 1147 1150 protected static BridgeUpdateHandler getBridgeUpdateHandler(Node node) { 1151 SVGContext ctx = getSVGContext(node); 1152 return (ctx == null) ? null : (BridgeUpdateHandler)ctx; 1153 } 1154 1155 1158 protected class DOMAttrModifiedEventListener implements EventListener { 1159 1160 1163 public void handleEvent(Event evt) { 1164 Node node = (Node)evt.getTarget(); 1165 BridgeUpdateHandler h = getBridgeUpdateHandler(node); 1166 if (h != null) { 1167 try { 1168 h.handleDOMAttrModifiedEvent((MutationEvent )evt); 1169 } catch (Exception e) { 1170 userAgent.displayError(e); 1171 } 1172 } 1173 } 1174 } 1175 1176 1179 protected class DOMMouseOutEventListener implements EventListener { 1180 public void handleEvent(Event evt) { 1181 MouseEvent me = (MouseEvent )evt; 1182 Element newTarget = (Element)me.getRelatedTarget(); 1183 Cursor cursor = CursorManager.DEFAULT_CURSOR; 1184 if (newTarget != null) 1185 cursor = CSSUtilities.convertCursor 1186 (newTarget, BridgeContext.this); 1187 if (cursor == null) 1188 cursor = CursorManager.DEFAULT_CURSOR; 1189 1190 userAgent.setSVGCursor(cursor); 1191 } 1192 } 1193 1194 1195 1202 protected class DOMMouseOverEventListener implements EventListener { 1203 1206 public void handleEvent(Event evt) { 1207 Element target = (Element)evt.getTarget(); 1208 Cursor cursor = CSSUtilities.convertCursor(target, BridgeContext.this); 1209 1210 if (cursor != null) { 1211 userAgent.setSVGCursor(cursor); 1212 } 1213 } 1214 } 1215 1216 1219 protected class DOMNodeInsertedEventListener implements EventListener { 1220 1221 1224 public void handleEvent(Event evt) { 1225 MutationEvent me = (MutationEvent )evt; 1226 BridgeUpdateHandler h = 1227 getBridgeUpdateHandler(me.getRelatedNode()); 1228 if (h != null) { 1229 try { 1230 h.handleDOMNodeInsertedEvent(me); 1231 } catch (InterruptedBridgeException ibe) { 1232 1233 } catch (Exception e) { 1234 userAgent.displayError(e); 1235 } 1236 } 1237 } 1238 } 1239 1240 1243 protected class DOMNodeRemovedEventListener implements EventListener { 1244 1245 1248 public void handleEvent(Event evt) { 1249 Node node = (Node)evt.getTarget(); 1250 BridgeUpdateHandler h = getBridgeUpdateHandler(node); 1251 if (h != null) { 1252 try { 1253 h.handleDOMNodeRemovedEvent((MutationEvent )evt); 1254 } catch (Exception e) { 1255 userAgent.displayError(e); 1256 } 1257 } 1258 } 1259 } 1260 1261 1264 protected class DOMCharacterDataModifiedListener implements EventListener { 1265 1266 1269 public void handleEvent(Event evt) { 1270 Node node = (Node)evt.getTarget(); 1271 while (node != null && !(node instanceof SVGOMElement)) { 1272 node = node.getParentNode(); 1273 } 1274 BridgeUpdateHandler h = getBridgeUpdateHandler(node); 1275 if (h != null) { 1276 try { 1277 h.handleDOMCharacterDataModified((MutationEvent )evt); 1278 } catch (Exception e) { 1279 userAgent.displayError(e); 1280 } 1281 } 1282 } 1283 } 1284 1285 1289 protected class CSSPropertiesChangedListener implements CSSEngineListener { 1290 1291 1295 public void propertiesChanged(CSSEngineEvent evt) { 1296 Element elem = evt.getElement(); 1297 SVGContext ctx = getSVGContext(elem); 1298 if (ctx == null) { 1299 GraphicsNode pgn = getGraphicsNode 1300 ((Element)elem.getParentNode()); 1301 if ((pgn == null) || !(pgn instanceof CompositeGraphicsNode)) { 1302 return; 1305 } 1306 CompositeGraphicsNode parent = (CompositeGraphicsNode)pgn; 1307 1309 int [] properties = evt.getProperties(); 1310 for (int i=0; i < properties.length; ++i) { 1311 if (properties[i] == SVGCSSEngine.DISPLAY_INDEX) { 1312 if (!CSSUtilities.convertDisplay(elem)) { 1313 break; 1315 } 1316 GVTBuilder builder = getGVTBuilder(); 1318 GraphicsNode childNode = builder.build 1319 (BridgeContext.this, elem); 1320 if (childNode == null) { 1321 break; 1323 } 1324 int idx = -1; 1325 for(Node ps = elem.getPreviousSibling(); ps != null; 1326 ps = ps.getPreviousSibling()) { 1327 if (ps.getNodeType() != Node.ELEMENT_NODE) 1328 continue; 1329 Element pse = (Element)ps; 1330 GraphicsNode gn = getGraphicsNode(pse); 1331 if (gn == null) 1332 continue; 1333 idx = parent.indexOf(gn); 1334 if (idx == -1) 1335 continue; 1336 break; 1337 } 1338 idx++; 1341 parent.add(idx, childNode); 1342 break; 1343 } 1344 } 1345 } if (ctx != null && (ctx instanceof BridgeUpdateHandler)) { 1346 ((BridgeUpdateHandler)ctx).handleCSSEngineEvent(evt); 1347 } 1348 } 1349 } 1350 1351 1353 1356 public Value getSystemColor(String ident) { 1357 return SystemColorSupport.getSystemColor(ident); 1358 } 1359 1360 1363 public Value getDefaultFontFamily() { 1364 SVGOMDocument doc = (SVGOMDocument)document; 1368 SVGStylableElement root = (SVGStylableElement)doc.getRootElement(); 1369 String str = userAgent.getDefaultFontFamily(); 1370 return doc.getCSSEngine().parsePropertyValue 1371 (root,SVGConstants.CSS_FONT_FAMILY_PROPERTY, str); 1372 } 1373 1374 1377 public float getLighterFontWeight(float f) { 1378 return userAgent.getLighterFontWeight(f); 1379 } 1380 1381 1384 public float getBolderFontWeight(float f) { 1385 return userAgent.getBolderFontWeight(f); 1386 } 1387 1388 1391 public float getPixelUnitToMillimeter() { 1392 return userAgent.getPixelUnitToMillimeter(); 1393 } 1394 1395 1400 public float getPixelToMillimeter() { 1401 return getPixelUnitToMillimeter(); 1402 1403 } 1404 1405 1408 public float getMediumFontSize() { 1409 return userAgent.getMediumFontSize(); 1410 } 1411 1412 1416 public float getBlockWidth(Element elt) { 1417 return getViewport(elt).getWidth(); 1418 } 1419 1420 1424 public float getBlockHeight(Element elt) { 1425 return getViewport(elt).getHeight(); 1426 } 1427 1428 1444 public void 1445 checkLoadExternalResource(ParsedURL resourceURL, 1446 ParsedURL docURL) throws SecurityException { 1447 userAgent.checkLoadExternalResource(resourceURL, 1448 docURL); 1449 } 1450 1451 1452 1455 public boolean isDynamicDocument(Document doc) { 1456 return BaseScriptingEnvironment.isDynamicDocument(this, doc); 1457 } 1458 1459 1464 public boolean isInteractiveDocument(Document doc) { 1465 1466 Element root = ((SVGDocument)doc).getRootElement(); 1467 if (!SVGConstants.SVG_NAMESPACE_URI.equals(root.getNamespaceURI())) 1468 return false; 1469 1470 return checkInteractiveElement(root); 1471 } 1472 1473 1477 public boolean checkInteractiveElement(Element e) { 1478 return checkInteractiveElement 1479 ((SVGDocument)e.getOwnerDocument(), e); 1480 } 1481 1482 1486 public boolean checkInteractiveElement(SVGDocument doc, 1487 Element e) { 1488 String tag = e.getLocalName(); 1489 1490 if (SVGConstants.SVG_A_TAG.equals(tag)) 1492 return true; 1493 1494 if (SVGConstants.SVG_TITLE_TAG.equals(tag)) { 1498 return (e.getParentNode() != doc.getRootElement()); 1499 } 1500 if (SVGConstants.SVG_DESC_TAG.equals(tag)) { 1501 return (e.getParentNode() != doc.getRootElement()); 1502 } 1503 if (SVGConstants.SVG_CURSOR_TAG.equals(tag)) 1504 return true; 1505 1506 if (e.getAttribute(CSSConstants.CSS_CURSOR_PROPERTY).length() >0) 1509 return true; 1510 1511 1523 1524 final String svg_ns = SVGConstants.SVG_NAMESPACE_URI; 1526 for (Node n = e.getFirstChild(); 1527 n != null; 1528 n = n.getNextSibling()) { 1529 if (n.getNodeType() == Node.ELEMENT_NODE) { 1530 Element child = (Element)n; 1531 if (svg_ns.equals(child.getNamespaceURI())) 1532 if (checkInteractiveElement(child)) 1533 return true; 1534 } 1535 } 1536 return false; 1537 } 1538 1539 1540 1542 protected List extensions = null; 1543 1544 1547 public void registerSVGBridges() { 1548 UserAgent ua = getUserAgent(); 1549 List ext = getBridgeExtensions(document); 1550 Iterator iter = ext.iterator(); 1551 1552 while(iter.hasNext()) { 1553 BridgeExtension be = (BridgeExtension)iter.next(); 1554 be.registerTags(this); 1555 ua.registerExtension(be); 1556 } 1557 } 1558 1559 public List getBridgeExtensions(Document doc) { 1560 Element root = ((SVGOMDocument)doc).getRootElement(); 1561 String ver = root.getAttributeNS 1562 (null, SVGConstants.SVG_VERSION_ATTRIBUTE); 1563 BridgeExtension svgBE; 1564 if ((ver.length()==0) || ver.equals("1.0") || ver.equals("1.1")) 1565 svgBE = new SVGBridgeExtension(); 1566 else 1567 svgBE = new SVG12BridgeExtension(); 1568 1569 float priority = svgBE.getPriority(); 1570 extensions = new LinkedList (getGlobalBridgeExtensions()); 1571 1572 ListIterator li = extensions.listIterator(); 1573 for (;;) { 1574 if (!li.hasNext()) { 1575 li.add(svgBE); 1576 break; 1577 } 1578 BridgeExtension lbe = (BridgeExtension)li.next(); 1579 if (lbe.getPriority() > priority) { 1580 li.previous(); 1581 li.add(svgBE); 1582 break; 1583 } 1584 } 1585 1586 return extensions; 1587 } 1588 1589 1592 protected static List globalExtensions = null; 1593 1594 public synchronized static List getGlobalBridgeExtensions() { 1595 if (globalExtensions != null) { 1596 return globalExtensions; 1597 } 1598 globalExtensions = new LinkedList (); 1599 1600 Iterator iter = Service.providers(BridgeExtension.class); 1601 1602 while (iter.hasNext()) { 1603 BridgeExtension be = (BridgeExtension)iter.next(); 1604 float priority = be.getPriority(); 1605 ListIterator li = globalExtensions.listIterator(); 1606 for (;;) { 1607 if (!li.hasNext()) { 1608 li.add(be); 1609 break; 1610 } 1611 BridgeExtension lbe = (BridgeExtension)li.next(); 1612 if (lbe.getPriority() > priority) { 1613 li.previous(); 1614 li.add(be); 1615 break; 1616 } 1617 } 1618 } 1619 return globalExtensions; 1620 } 1621 1622 public static class CSSEngineUserAgentWrapper implements CSSEngineUserAgent { 1623 UserAgent ua; 1624 CSSEngineUserAgentWrapper(UserAgent ua) { 1625 this.ua = ua; 1626 } 1627 1628 1631 public void displayError(Exception ex) { ua.displayError(ex); } 1632 1633 1636 public void displayMessage(String message) { ua.displayMessage(message); } 1637 } 1638 1639} 1640 1641 | Popular Tags |