1 18 package org.apache.batik.test.svg; 19 20 import java.awt.Color ; 21 import java.io.FileOutputStream ; 22 import java.io.StringWriter ; 23 import java.io.PrintWriter ; 24 import java.net.URL ; 25 26 import org.apache.batik.transcoder.TranscoderException; 27 import org.apache.batik.transcoder.TranscoderInput; 28 import org.apache.batik.transcoder.TranscoderOutput; 29 30 import org.apache.batik.transcoder.image.ImageTranscoder; 31 import org.apache.batik.transcoder.image.PNGTranscoder; 32 33 import org.apache.batik.test.DefaultTestReport; 34 import org.apache.batik.test.TestReport; 35 36 import org.w3c.dom.Document ; 37 38 39 49 public class SVGRenderingAccuracyTest extends AbstractRenderingAccuracyTest { 50 57 public static final String ERROR_CANNOT_TRANSCODE_SVG 58 = "SVGRenderingAccuracyTest.error.cannot.transcode.svg"; 59 60 63 public static final String VALIDATING_PARSER 64 = configuration.getString("validating.parser"); 65 66 70 protected boolean validate = false; 71 72 75 protected String userLanguage; 76 77 82 public SVGRenderingAccuracyTest(String svgURL, 83 String refImgURL){ 84 super(svgURL, refImgURL); 85 } 86 87 90 protected SVGRenderingAccuracyTest(){ 91 } 92 93 96 public void setValidating(Boolean validate){ 97 if (validate == null){ 98 throw new IllegalArgumentException (); 99 } 100 this.validate = validate.booleanValue(); 101 } 102 103 public boolean getValidating(){ 104 return validate; 105 } 106 107 110 public void setUserLanguage(String userLanguage){ 111 this.userLanguage = userLanguage; 112 } 113 114 public String getUserLanguage(){ 115 return this.userLanguage; 116 } 117 118 124 protected Document manipulateSVGDocument(Document doc) { 125 return doc; 126 } 127 128 129 public TestReport encode(URL srcURL, FileOutputStream fos) { 130 DefaultTestReport report = new DefaultTestReport(this); 131 try{ 132 ImageTranscoder transcoder = getTestImageTranscoder(); 133 TranscoderInput src = new TranscoderInput(svgURL.toString()); 134 TranscoderOutput dst = new TranscoderOutput(fos); 135 transcoder.transcode(src, dst); 136 return null; 137 }catch(TranscoderException e){ 138 StringWriter trace = new StringWriter (); 139 e.printStackTrace(new PrintWriter (trace)); 140 141 report.setErrorCode(ERROR_CANNOT_TRANSCODE_SVG); 142 report.setDescription(new TestReport.Entry[]{ 143 new TestReport.Entry 144 (Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null), 145 Messages.formatMessage(ERROR_CANNOT_TRANSCODE_SVG, 146 new String []{svgURL.toString(), 147 e.getClass().getName(), 148 e.getMessage(), 149 trace.toString() 150 })) }); 151 }catch(Exception e){ 152 StringWriter trace = new StringWriter (); 153 e.printStackTrace(new PrintWriter (trace)); 154 155 report.setErrorCode(ERROR_CANNOT_TRANSCODE_SVG); 156 report.setDescription(new TestReport.Entry[]{ 157 new TestReport.Entry 158 (Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null), 159 Messages.formatMessage(ERROR_CANNOT_TRANSCODE_SVG, 160 new String []{svgURL.toString(), 161 e.getClass().getName(), 162 e.getMessage(), 163 trace.toString() 164 })) }); 165 } 166 report.setPassed(false); 167 return report; 168 } 169 170 174 public ImageTranscoder getTestImageTranscoder(){ 175 ImageTranscoder t = new InternalPNGTranscoder(); 176 t.addTranscodingHint(PNGTranscoder.KEY_FORCE_TRANSPARENT_WHITE, 177 Boolean.FALSE); 178 t.addTranscodingHint(PNGTranscoder.KEY_BACKGROUND_COLOR, 179 new Color (0,0,0,0)); 180 t.addTranscodingHint(PNGTranscoder.KEY_EXECUTE_ONLOAD, 181 Boolean.TRUE); 182 183 if (validate){ 184 t.addTranscodingHint(PNGTranscoder.KEY_XML_PARSER_VALIDATING, 185 Boolean.TRUE); 186 t.addTranscodingHint(PNGTranscoder.KEY_XML_PARSER_CLASSNAME, 187 VALIDATING_PARSER); 188 } 189 190 if (userLanguage != null){ 191 t.addTranscodingHint(PNGTranscoder.KEY_LANGUAGE, 192 userLanguage); 193 } 194 return t; 195 } 196 197 201 protected class InternalPNGTranscoder extends PNGTranscoder{ 202 210 protected void transcode(Document document, 211 String uri, 212 TranscoderOutput output) 213 throws TranscoderException { 214 SVGRenderingAccuracyTest.this.manipulateSVGDocument(document); 215 super.transcode(document, uri, output); 216 } 217 } 218 } 219 | Popular Tags |