1 18 package org.apache.batik.bridge; 19 20 import java.net.URL ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import org.apache.batik.dom.util.DOMUtilities; 25 import org.apache.batik.ext.awt.image.renderable.Filter; 26 import org.apache.batik.ext.awt.image.spi.DefaultBrokenLinkProvider; 27 import org.apache.batik.gvt.GraphicsNode; 28 import org.apache.batik.gvt.filter.GraphicsNodeRable8Bit; 29 import org.apache.batik.util.SVGConstants; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.svg.SVGDocument; 33 38 public class SVGBrokenLinkProvider 39 extends DefaultBrokenLinkProvider 40 implements ErrorConstants { 41 42 public final static String SVG_BROKEN_LINK_DOCUMENT_PROPERTY = 43 "org.apache.batik.bridge.BrokenLinkDocument"; 44 45 UserAgent userAgent; 46 DocumentLoader loader; 47 BridgeContext ctx; 48 GraphicsNode gvtRoot = null; 49 SVGDocument svgDoc; 50 51 public SVGBrokenLinkProvider() { 52 userAgent = new UserAgentAdapter(); 53 loader = new DocumentLoader(userAgent); 54 ctx = new BridgeContext(userAgent, loader); 55 56 Class cls = SVGBrokenLinkProvider.class; 57 URL blURL = cls.getResource("BrokenLink.svg"); 58 if (blURL == null) return; 59 60 GVTBuilder builder = new GVTBuilder(); 61 try { 62 svgDoc = (SVGDocument)loader.loadDocument(blURL.toString()); 63 gvtRoot = builder.build(ctx, svgDoc); 64 } catch (Exception ex) { 65 } 67 } 68 69 82 public Filter getBrokenLinkImage(Object base, String code, 83 Object [] params) { 84 if (gvtRoot == null) 85 return null; 86 87 String message = formatMessage(base, code, params); 88 Document doc = getBrokenLinkDocument(message); 89 Map props = new HashMap (); 90 props.put(BROKEN_LINK_PROPERTY, message); 91 props.put(SVG_BROKEN_LINK_DOCUMENT_PROPERTY, doc); 92 93 return new GraphicsNodeRable8Bit(gvtRoot, props); 94 } 95 96 public SVGDocument getBrokenLinkDocument(Object base, 97 String code, Object [] params) { 98 String message = formatMessage(base, code, params); 99 return getBrokenLinkDocument(message); 100 } 101 102 public SVGDocument getBrokenLinkDocument(String message) { 103 SVGDocument doc = (SVGDocument)DOMUtilities.deepCloneDocument 104 (svgDoc, svgDoc.getImplementation()); 105 Element infoE = doc.getElementById("__More_About"); 106 Element title = doc.createElementNS(SVGConstants.SVG_NAMESPACE_URI, 107 SVGConstants.SVG_TITLE_TAG); 108 title.appendChild(doc.createTextNode 109 (Messages.formatMessage 110 (MSG_BROKEN_LINK_TITLE, null))); 111 Element desc = doc.createElementNS(SVGConstants.SVG_NAMESPACE_URI, 112 SVGConstants.SVG_DESC_TAG); 113 desc.appendChild(doc.createTextNode(message)); 114 infoE.insertBefore(desc, infoE.getFirstChild()); 115 infoE.insertBefore(title, desc); 116 return doc; 117 } 118 } 119 | Popular Tags |