1 17 18 19 20 package org.apache.fop.svg; 21 22 import org.xml.sax.EntityResolver ; 23 24 import org.apache.commons.logging.impl.SimpleLog; 25 import org.apache.commons.logging.Log; 26 import org.apache.batik.bridge.UserAgent; 27 import org.apache.batik.dom.svg.SVGDOMImplementation; 28 import org.apache.batik.dom.util.DocumentFactory; 29 import org.apache.batik.transcoder.ErrorHandler; 30 import org.apache.batik.transcoder.TranscoderException; 31 import org.apache.batik.transcoder.TranscodingHints; 32 import org.apache.batik.transcoder.SVGAbstractTranscoder; 33 import org.apache.batik.transcoder.image.ImageTranscoder; 34 import org.apache.batik.transcoder.keys.BooleanKey; 35 import org.apache.batik.util.SVGConstants; 36 import org.w3c.dom.DOMImplementation ; 37 38 41 public abstract class AbstractFOPTranscoder extends SVGAbstractTranscoder 42 { 43 44 48 public static final TranscodingHints.Key KEY_STROKE_TEXT = new BooleanKey(); 49 50 51 public static final Boolean VALUE_FORMAT_ON = Boolean.TRUE; 52 53 54 public static final Boolean VALUE_FORMAT_OFF = Boolean.FALSE; 55 56 59 protected UserAgent userAgent = createUserAgent(); 60 61 private Log logger; 62 private EntityResolver resolver; 63 64 67 public AbstractFOPTranscoder() { 68 hints.put(KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, 69 SVGConstants.SVG_NAMESPACE_URI); 70 hints.put(KEY_DOCUMENT_ELEMENT, SVGConstants.SVG_SVG_TAG); 71 hints.put(KEY_DOM_IMPLEMENTATION, 72 SVGDOMImplementation.getDOMImplementation()); 73 } 74 75 80 protected UserAgent createUserAgent() { 81 return new FOPTranscoderUserAgent(); 82 } 83 84 public void setLogger(Log logger) { 85 this.logger = logger; 86 } 87 88 92 public void setEntityResolver(EntityResolver resolver) { 93 this.resolver = resolver; 94 } 95 96 101 protected final Log getLogger() { 102 if (this.logger == null) { 103 this.logger = new SimpleLog("FOP/Transcoder"); 104 ((SimpleLog) logger).setLevel(SimpleLog.LOG_LEVEL_INFO); 105 } 106 return this.logger; 107 } 108 109 118 protected DocumentFactory createDocumentFactory(DOMImplementation domImpl, 119 String parserClassname) { 120 final FOPSAXSVGDocumentFactory factory 121 = new FOPSAXSVGDocumentFactory(parserClassname); 122 if (this.resolver != null) { 123 factory.setAdditionalEntityResolver(this.resolver); 124 } 125 return factory; 126 } 127 128 132 137 protected class FOPErrorHandler implements ErrorHandler { 138 139 142 public void error(TranscoderException te) 143 throws TranscoderException { 144 getLogger().error(te.getMessage()); 145 } 146 147 150 public void fatalError(TranscoderException te) 151 throws TranscoderException { 152 throw te; 153 } 154 155 158 public void warning(TranscoderException te) 159 throws TranscoderException { 160 getLogger().warn(te.getMessage()); 161 } 162 163 } 164 165 169 172 protected class FOPTranscoderUserAgent extends SVGAbstractTranscoderUserAgent { 173 174 178 public void displayError(String message) { 179 try { 180 getErrorHandler().error(new TranscoderException(message)); 181 } catch (TranscoderException ex) { 182 throw new RuntimeException (); 183 } 184 } 185 186 190 public void displayError(Exception e) { 191 try { 192 getErrorHandler().error(new TranscoderException(e)); 193 } catch (TranscoderException ex) { 194 throw new RuntimeException (); 195 } 196 } 197 198 202 public void displayMessage(String message) { 203 getLogger().info(message); 204 } 205 206 211 public float getPixelUnitToMillimeter() { 212 Object key = ImageTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER; 213 if (getTranscodingHints().containsKey(key)) { 214 return ((Float )getTranscodingHints().get(key)).floatValue(); 215 } else { 216 return 25.4f / 96; } 219 } 220 221 225 public String getMedia() { 226 return "print"; 227 } 228 229 } 230 231 } 232 | Popular Tags |