1 51 package org.apache.fop.image.analyser; 52 53 import java.io.BufferedInputStream ; 55 import java.io.IOException ; 56 57 import java.awt.geom.AffineTransform ; 58 import java.awt.geom.Dimension2D ; 59 import java.awt.Dimension ; 60 61 import org.w3c.dom.Element ; 63 import org.w3c.dom.svg.SVGDocument; 64 65 import org.apache.fop.messaging.MessageHandler; 67 import org.apache.fop.image.SVGImage; 68 69 import org.apache.batik.dom.svg.SVGOMDocument; 71 import org.apache.batik.dom.svg.SAXSVGDocumentFactory; 72 import org.apache.batik.bridge.UserAgentAdapter; 73 import org.apache.batik.bridge.UserAgent; 74 import org.apache.batik.bridge.BridgeContext; 75 import org.apache.batik.bridge.UnitProcessor; 76 77 80 public class SVGReader extends AbstractImageReader { 81 82 public boolean verifySignature(String uri, 83 BufferedInputStream fis) throws IOException { 84 this.imageStream = fis; 85 return loadImage(uri); 86 } 87 88 public String getMimeType() { 89 return "image/svg+xml"; 90 } 91 92 96 protected boolean loadImage(String uri) { 97 try { 99 SAXSVGDocumentFactory factory = 100 new SAXSVGDocumentFactory(SVGImage.getParserName()); 101 SVGDocument doc = (SVGDocument)factory.createDocument(uri, imageStream); 102 103 UserAgent userAgent = new MUserAgent(new AffineTransform ()); 104 BridgeContext ctx = new BridgeContext(userAgent); 105 106 Element e = ((SVGDocument)doc).getRootElement(); 107 UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e); 108 109 String s; 110 s = e.getAttributeNS(null, SVGOMDocument.SVG_WIDTH_ATTRIBUTE); 112 if (s.length() == 0) { 113 s = SVGOMDocument.SVG_SVG_WIDTH_DEFAULT_VALUE; 114 } 115 width = (int)UnitProcessor.svgHorizontalLengthToUserSpace 116 (s, SVGOMDocument.SVG_WIDTH_ATTRIBUTE, uctx); 117 118 s = e.getAttributeNS(null, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE); 120 if (s.length() == 0) { 121 s = SVGOMDocument.SVG_SVG_HEIGHT_DEFAULT_VALUE; 122 } 123 height = (int)UnitProcessor.svgVerticalLengthToUserSpace 124 (s, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE, uctx); 125 126 return true; 127 } catch (NoClassDefFoundError ncdfe) { 128 MessageHandler.errorln("Batik not in class path"); 129 return false; 130 } catch (Exception e) { 131 MessageHandler.errorln("Could not load external SVG: " + 132 e.getMessage()); 133 return false; 136 } 137 } 138 139 protected class MUserAgent extends UserAgentAdapter { 140 AffineTransform currentTransform = null; 141 142 145 protected MUserAgent(AffineTransform at) { 146 currentTransform = at; 147 } 148 149 152 public void displayError(String message) { 153 MessageHandler.error(message); 154 } 155 156 159 public void displayError(Exception ex) { 160 MessageHandler.error(org.apache.avalon.framework.ExceptionUtil.printStackTrace(ex)); 161 } 162 163 167 public void displayMessage(String message) { 168 MessageHandler.log(message); 169 } 170 171 174 public float getPixelToMM() { 175 return 0.35277777777777777778f; } 179 180 public float getPixelUnitToMillimeter() { 181 return 0.35277777777777777778f; } 185 186 189 public String getLanguages() { 190 return "en"; } 192 193 public String getMedia() { 194 return "print"; 195 } 196 197 public boolean isXMLParserValidating() { 198 return true; 199 } 200 201 205 public String getUserStyleSheetURI() { 206 return null; } 208 209 212 public String getXMLParserClassName() { 213 return org.apache.fop.apps.Driver.getParserClassName(); 214 } 215 216 public AffineTransform getTransform() { 217 return currentTransform; 218 } 219 220 public Dimension2D getViewportSize() { 221 return new Dimension (100, 100); 222 } 223 224 } 225 226 } 227 228 | Popular Tags |