KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 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.Graphics2D JavaDoc;
21
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.net.URL JavaDoc;
27
28 import java.util.List JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import org.apache.batik.test.DefaultTestReport;
32 import org.apache.batik.test.TestReport;
33
34 import org.apache.batik.swing.JSVGCanvasHandler;
35
36 import org.apache.batik.swing.JSVGCanvas;
37 import org.apache.batik.swing.gvt.Overlay;
38
39 import java.awt.image.BufferedImage JavaDoc;
40
41 /**
42  * One line Class Desc
43  *
44  * Complete Class Desc
45  *
46  * @author <a HREF="mailto:deweese@apache.org">l449433</a>
47  * @version $Id: JSVGRenderingAccuracyTest.java,v 1.9 2005/03/27 08:58:37 cam Exp $
48  */

49 public class JSVGRenderingAccuracyTest extends SamplesRenderingTest
50        implements JSVGCanvasHandler.Delegate {
51
52     /**
53      * Error when canvas can't peform render update SVG file.
54      * {0} The file/url that could not be updated..
55      */

56     public static final String JavaDoc ERROR_SAVE_FAILED =
57         "JSVGRenderingAccuracyTest.message.error.save.failed";
58
59     public static String JavaDoc fmt(String JavaDoc key, Object JavaDoc []args) {
60         return Messages.formatMessage(key, args);
61     }
62
63     /**
64      * For subclasses
65      */

66     public JSVGRenderingAccuracyTest(){
67     }
68
69     protected URL JavaDoc srcURL;
70     protected FileOutputStream JavaDoc fos;
71     protected TestReport failReport = null;
72     protected boolean done;
73     protected JSVGCanvasHandler handler = null;
74
75     public JSVGCanvasHandler createCanvasHandler() {
76         return new JSVGCanvasHandler(this, this);
77     }
78
79     public TestReport encode(URL JavaDoc srcURL, FileOutputStream JavaDoc fos) {
80         this.srcURL = srcURL;
81         this.fos = fos;
82
83         handler = createCanvasHandler();
84         done = false;
85         handler.runCanvas(srcURL.toString());
86
87         handler = null;
88
89         if (failReport != null) return failReport;
90         
91         DefaultTestReport report = new DefaultTestReport(this);
92         report.setPassed(true);
93         return report;
94     }
95
96     public void scriptDone() {
97         synchronized (this) {
98             done = true;
99             handler.scriptDone();
100         }
101     }
102
103     /* JSVGCanvasHandler.Delegate Interface */
104     public boolean canvasInit(JSVGCanvas canvas) {
105         canvas.setURI(srcURL.toString());
106         return true;
107     }
108
109     public void canvasLoaded(JSVGCanvas canvas) {
110     }
111
112     public void canvasRendered(JSVGCanvas canvas) {
113     }
114
115     public boolean canvasUpdated(JSVGCanvas canvas) {
116         synchronized (this) {
117             return done;
118         }
119     }
120
121     public void canvasDone(JSVGCanvas canvas) {
122         synchronized (this) {
123             done = true;
124             if (failReport != null)
125                 return;
126
127             try {
128                 // Get the base image
129
BufferedImage JavaDoc theImage = copyImage(canvas.getOffScreen());
130
131                 // Capture the overlays
132
List JavaDoc overlays = canvas.getOverlays();
133
134                 // Paint the overlays
135
Graphics2D JavaDoc g = theImage.createGraphics();
136                 Iterator JavaDoc it = overlays.iterator();
137                 while (it.hasNext()) {
138                     ((Overlay)it.next()).paint(g);
139                 }
140
141                 saveImage(theImage, fos);
142             } catch (IOException JavaDoc ioe) {
143                 StringWriter JavaDoc trace = new StringWriter JavaDoc();
144                 ioe.printStackTrace(new PrintWriter JavaDoc(trace));
145                 DefaultTestReport report = new DefaultTestReport(this);
146                 report.setErrorCode(ERROR_SAVE_FAILED);
147                 report.setDescription(new TestReport.Entry[] {
148                     new TestReport.Entry
149                     (fmt(ENTRY_KEY_ERROR_DESCRIPTION, null),
150                      fmt(ERROR_SAVE_FAILED,
151                          new Object JavaDoc[]{ srcURL.toString(),
152                                        trace.toString()}))
153                 });
154                 report.setPassed(false);
155                 failReport = report;
156             }
157         }
158     }
159
160     public void failure(TestReport report) {
161         synchronized (this) {
162             done = true;
163             failReport = report;
164         }
165     }
166
167     public static BufferedImage JavaDoc copyImage(BufferedImage JavaDoc bi) {
168         // Copy off the image just rendered.
169
BufferedImage JavaDoc ret;
170         ret = new BufferedImage JavaDoc(bi.getWidth(), bi.getHeight(), bi.getType());
171         bi.copyData(ret.getRaster());
172         return ret;
173     }
174 }
175
176
Popular Tags