KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > transcoder > image > DOMTest


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.transcoder.image;
19
20 import java.awt.Color JavaDoc;
21 import java.awt.Graphics2D JavaDoc;
22 import java.awt.image.BufferedImage JavaDoc;
23
24 import java.io.ByteArrayOutputStream JavaDoc;
25
26 import org.apache.batik.transcoder.TranscoderInput;
27 import org.apache.batik.transcoder.TranscoderOutput;
28 import org.apache.batik.dom.svg.SVGDOMImplementation;
29
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32 import org.w3c.dom.DOMImplementation JavaDoc;
33
34 /**
35  * Test the ImageTranscoder input with a DOM tree.
36  *
37  * @author <a HREF="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
38  * @version $Id: DOMTest.java,v 1.5 2004/08/18 07:17:12 vhardy Exp $
39  */

40 public class DOMTest extends AbstractImageTranscoderTest {
41
42     /**
43      * Constructs a new <tt>DOMTest</tt>.
44      */

45     public DOMTest() {
46     }
47
48     /**
49      * Creates the <tt>TranscoderInput</tt>.
50      */

51     protected TranscoderInput createTranscoderInput() {
52     DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
53     String JavaDoc svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
54     Document JavaDoc doc = impl.createDocument(svgNS, "svg", null);
55
56     Element JavaDoc root = doc.getDocumentElement();
57
58     root.setAttributeNS(null, "width", "400");
59     root.setAttributeNS(null, "height", "400");
60
61     Element JavaDoc r = doc.createElementNS(svgNS, "rect");
62     r.setAttributeNS(null, "x", "0");
63     r.setAttributeNS(null, "y", "0");
64     r.setAttributeNS(null, "width", "400");
65     r.setAttributeNS(null, "height", "400");
66     r.setAttributeNS(null, "style", "fill:black");
67     root.appendChild(r);
68
69     r = doc.createElementNS(svgNS, "rect");
70     r.setAttributeNS(null, "x", "100");
71     r.setAttributeNS(null, "y", "50");
72     r.setAttributeNS(null, "width", "100");
73     r.setAttributeNS(null, "height", "50");
74     r.setAttributeNS(null, "style", "stroke:red; fill:none");
75     root.appendChild(r);
76
77     return new TranscoderInput(doc);
78     }
79
80     /**
81      * Returns the reference image for this test.
82      */

83     protected byte [] getReferenceImageData() {
84         try {
85             BufferedImage JavaDoc img = new BufferedImage JavaDoc
86                 (400, 400, BufferedImage.TYPE_INT_ARGB);
87             Graphics2D JavaDoc g2d = img.createGraphics();
88             g2d.setColor(Color.black);
89             g2d.fillRect(0, 0, 400, 400);
90             g2d.setColor(Color.red);
91             g2d.drawRect(100, 50, 100, 50);
92             ByteArrayOutputStream JavaDoc ostream = new ByteArrayOutputStream JavaDoc();
93             PNGTranscoder t = new PNGTranscoder();
94             TranscoderOutput output = new TranscoderOutput(ostream);
95             t.writeImage(img, output);
96             return ostream.toByteArray();
97         } catch (Exception JavaDoc ex) {
98             throw new RuntimeException JavaDoc("DOMTest error");
99         }
100     }
101 }
102
Popular Tags