1 18 package org.apache.batik.bridge; 19 20 import org.apache.batik.gvt.CompositeGraphicsNode; 21 import org.apache.batik.gvt.GraphicsNode; 22 import org.apache.batik.gvt.RootGraphicsNode; 23 import org.apache.batik.util.HaltingThread; 24 import org.apache.batik.util.SVGConstants; 25 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.Element ; 28 import org.w3c.dom.Node ; 29 30 36 public class GVTBuilder implements SVGConstants { 37 38 41 public GVTBuilder() { } 42 43 51 public GraphicsNode build(BridgeContext ctx, Document document) { 52 ctx.setDocument(document); 54 ctx.initializeDocument(document); 55 56 ctx.setGVTBuilder(this); 58 59 RootGraphicsNode rootNode = new RootGraphicsNode(); 61 Element svgElement = document.getDocumentElement(); 62 GraphicsNode topNode = null; 63 try { 64 Bridge bridge = ctx.getBridge(svgElement); 66 if (bridge == null || !(bridge instanceof GraphicsNodeBridge)) { 67 return null; 68 } 69 GraphicsNodeBridge gnBridge = (GraphicsNodeBridge)bridge; 71 topNode = gnBridge.createGraphicsNode(ctx, svgElement); 72 if (topNode == null) { 73 return null; 74 } 75 rootNode.getChildren().add(topNode); 76 77 buildComposite(ctx, svgElement, (CompositeGraphicsNode)topNode); 78 gnBridge.buildGraphicsNode(ctx, svgElement, topNode); 79 } catch (BridgeException ex) { 80 ex.setGraphicsNode(rootNode); 82 Element errElement = ex.getElement(); 83 ex.setLineNumber(ctx.getDocumentLoader().getLineNumber(errElement)); 84 throw ex; } 87 88 if (ctx.isInteractive()) { 90 ctx.addUIEventListeners(document); 91 92 BridgeEventSupport.addGVTListener(ctx, document); 94 } 95 96 if (ctx.isDynamic()) { 98 ctx.addDOMListeners(); 100 } 101 return rootNode; 102 } 103 104 112 public GraphicsNode build(BridgeContext ctx, Element e) { 113 Bridge bridge = ctx.getBridge(e); 115 if (bridge instanceof GenericBridge) { 116 ((GenericBridge) bridge).handleElement(ctx, e); 118 return null; 119 } else if (bridge == null || !(bridge instanceof GraphicsNodeBridge)) { 120 return null; 121 } 122 GraphicsNodeBridge gnBridge = (GraphicsNodeBridge)bridge; 124 if (!gnBridge.getDisplay(e)) { 126 return null; 127 } 128 GraphicsNode gn = gnBridge.createGraphicsNode(ctx, e); 129 if (gn != null) { 130 if (gnBridge.isComposite()) { 131 buildComposite(ctx, e, (CompositeGraphicsNode)gn); 132 } else { 133 handleGenericBridges(ctx, e); 134 } 135 gnBridge.buildGraphicsNode(ctx, e, gn); 136 } 137 if (ctx.isDynamic()) { 141 } 143 return gn; 144 } 145 146 156 protected void buildComposite(BridgeContext ctx, 157 Element e, 158 CompositeGraphicsNode parentNode) { 159 for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) { 160 if (n.getNodeType() == Node.ELEMENT_NODE) { 161 buildGraphicsNode(ctx, (Element )n, parentNode); 162 } 163 } 164 } 165 166 176 protected void buildGraphicsNode(BridgeContext ctx, 177 Element e, 178 CompositeGraphicsNode parentNode) { 179 if (HaltingThread.hasBeenHalted()) { 181 throw new InterruptedBridgeException(); 182 } 183 Bridge bridge = ctx.getBridge(e); 185 if (bridge instanceof GenericBridge) { 186 ((GenericBridge) bridge).handleElement(ctx, e); 188 return; 189 } else if (bridge == null || !(bridge instanceof GraphicsNodeBridge)) { 190 return; 191 } 192 if (!CSSUtilities.convertDisplay(e)) { 194 return; 195 } 196 GraphicsNodeBridge gnBridge = (GraphicsNodeBridge)bridge; 197 try { 198 GraphicsNode gn = gnBridge.createGraphicsNode(ctx, e); 200 if (gn != null) { 201 parentNode.getChildren().add(gn); 203 if (gnBridge.isComposite()) { 205 buildComposite(ctx, e, (CompositeGraphicsNode)gn); 206 } else { 207 handleGenericBridges(ctx, e); 209 } 210 gnBridge.buildGraphicsNode(ctx, e, gn); 211 } 212 } catch (BridgeException ex) { 213 GraphicsNode errNode = ex.getGraphicsNode(); 217 if (errNode != null) { 218 parentNode.getChildren().add(errNode); 219 gnBridge.buildGraphicsNode(ctx, e, errNode); 220 ex.setGraphicsNode(null); 221 } 222 throw ex; 224 } 225 } 226 227 233 protected void handleGenericBridges(BridgeContext ctx, Element e) { 234 for (Node n = e.getFirstChild(); n != null; n = n.getNextSibling()) { 235 if (n instanceof Element ) { 236 Element e2 = (Element ) n; 237 Bridge b = ctx.getBridge(e2); 238 if (b instanceof GenericBridge) { 239 ((GenericBridge) b).handleElement(ctx, e2); 240 } 241 } 242 } 243 } 244 } 245 | Popular Tags |