KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > test > svg > SVGRenderingAccuracyTest


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.test.svg;
19
20 import java.awt.Color JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.StringWriter JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.net.URL JavaDoc;
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 JavaDoc;
37
38
39 /**
40  * Checks for regressions in rendering a specific SVG document.
41  * The <tt>Test</tt> will rasterize and SVG document and
42  * compare it to a reference image. The test passes if the
43  * rasterized SVG and the reference image match exactly (i.e.,
44  * all pixel values are the same).
45  *
46  * @author <a HREF="mailto:vhardy@apache.lorg">Vincent Hardy</a>
47  * @version $Id: SVGRenderingAccuracyTest.java,v 1.36 2004/08/18 07:17:03 vhardy Exp $
48  */

49 public class SVGRenderingAccuracyTest extends AbstractRenderingAccuracyTest {
50     /**
51      * Error when transcoding the SVG document generates an error
52      * {0} = URI of the transcoded SVG file
53      * {1} = Exception class
54      * {2} = Exception message
55      * {3} = Stack trace.
56      */

57     public static final String JavaDoc ERROR_CANNOT_TRANSCODE_SVG
58         = "SVGRenderingAccuracyTest.error.cannot.transcode.svg";
59
60     /**
61      * Validating parser class name
62      */

63     public static final String JavaDoc VALIDATING_PARSER
64     = configuration.getString("validating.parser");
65
66     /**
67      * Controls whether or not the SVG file should be
68      * validated. By default, no validation is used.
69      */

70     protected boolean validate = false;
71
72     /**
73      * The userLanguage for which the document should be tested.
74      */

75     protected String JavaDoc userLanguage;
76
77     /**
78      * Constructor.
79      * @param svgURL the URL String for the SVG document being tested.
80      * @param refImgURL the URL for the reference image.
81      */

82     public SVGRenderingAccuracyTest(String JavaDoc svgURL,
83                                     String JavaDoc refImgURL){
84         super(svgURL, refImgURL);
85     }
86
87     /**
88      * For subclasses
89      */

90     protected SVGRenderingAccuracyTest(){
91     }
92
93     /**
94      * If true, this test will use validation
95      */

96     public void setValidating(Boolean JavaDoc validate){
97     if (validate == null){
98         throw new IllegalArgumentException JavaDoc();
99     }
100         this.validate = validate.booleanValue();
101     }
102
103     public boolean getValidating(){
104         return validate;
105     }
106
107     /**
108      * Sets the userLanguage
109      */

110     public void setUserLanguage(String JavaDoc userLanguage){
111         this.userLanguage = userLanguage;
112     }
113
114     public String JavaDoc getUserLanguage(){
115         return this.userLanguage;
116     }
117
118     /**
119      * Template method which subclasses can override if they
120      * need to manipulate the DOM in some way before running
121      * the accuracy test. For example, this can be useful to
122      * test the alternate stylesheet support.
123      */

124     protected Document JavaDoc manipulateSVGDocument(Document JavaDoc doc) {
125         return doc;
126     }
127
128     
129     public TestReport encode(URL JavaDoc srcURL, FileOutputStream JavaDoc 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 JavaDoc trace = new StringWriter JavaDoc();
139             e.printStackTrace(new PrintWriter JavaDoc(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 JavaDoc[]{svgURL.toString(),
147                                                      e.getClass().getName(),
148                                                      e.getMessage(),
149                                                      trace.toString()
150                                         })) });
151         }catch(Exception JavaDoc e){
152             StringWriter JavaDoc trace = new StringWriter JavaDoc();
153             e.printStackTrace(new PrintWriter JavaDoc(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 JavaDoc[]{svgURL.toString(),
161                                                      e.getClass().getName(),
162                                                      e.getMessage(),
163                                                      trace.toString()
164                                         })) });
165         }
166         report.setPassed(false);
167         return report;
168     }
169
170     /**
171      * Returns the <tt>ImageTranscoder</tt> the Test should
172      * use
173      */

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 JavaDoc(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     /**
198      * Inner class which derives from the PNGTranscoder and calls the
199      * manipulateSVGDocument just before encoding happens.
200      */

201     protected class InternalPNGTranscoder extends PNGTranscoder{
202         /**
203          * Transcodes the specified Document as an image in the specified output.
204          *
205          * @param document the document to transcode
206          * @param uri the uri of the document or null if any
207          * @param output the ouput where to transcode
208          * @exception TranscoderException if an error occured while transcoding
209          */

210         protected void transcode(Document JavaDoc document,
211                                  String JavaDoc uri,
212                                  TranscoderOutput output)
213             throws TranscoderException {
214             SVGRenderingAccuracyTest.this.manipulateSVGDocument(document);
215             super.transcode(document, uri, output);
216         }
217     }
218 }
219
Popular Tags