KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > DefaultImageHandler


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.svggen;
19
20 import java.awt.Image JavaDoc;
21 import java.awt.image.RenderedImage JavaDoc;
22 import java.awt.image.renderable.RenderableImage JavaDoc;
23
24 import org.w3c.dom.Element JavaDoc;
25
26 /**
27  * This class provides a default implementation of the ImageHandler
28  * interface simply puts a place holder in the xlink:href
29  * attribute and sets the width and height of the element.
30  *
31  * @author <a HREF="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
32  * @version $Id: DefaultImageHandler.java,v 1.13 2004/08/18 07:14:59 vhardy Exp $
33  * @see org.apache.batik.svggen.SVGGraphics2D
34  */

35 public class DefaultImageHandler implements ImageHandler, ErrorConstants {
36     // duplicate the string here to remove dependencies on
37
// org.apache.batik.dom.util.XLinkSupport
38
static final String JavaDoc XLINK_NAMESPACE_URI =
39         "http://www.w3.org/1999/xlink";
40
41     /**
42      * Build a <code>DefaultImageHandler</code>.
43      */

44     public DefaultImageHandler() {
45     }
46
47     /**
48      * The handler should set the xlink:href tag and the width and
49      * height attributes.
50      */

51     public void handleImage(Image JavaDoc image, Element JavaDoc imageElement,
52                             SVGGeneratorContext generatorContext) {
53         //
54
// First, set the image width and height
55
//
56
imageElement.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE,
57                                     "" + image.getWidth(null));
58         imageElement.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE,
59                                     "" + image.getHeight(null));
60
61         //
62
// Now, set the href
63
//
64
try {
65             handleHREF(image, imageElement, generatorContext);
66         } catch (SVGGraphics2DIOException e) {
67             try {
68                 generatorContext.errorHandler.handleError(e);
69             } catch (SVGGraphics2DIOException io) {
70                 // we need a runtime exception because
71
// java.awt.Graphics2D method doesn't throw exceptions..
72
throw new SVGGraphics2DRuntimeException(io);
73             }
74         }
75     }
76
77     /**
78      * The handler should set the xlink:href tag and the width and
79      * height attributes.
80      */

81     public void handleImage(RenderedImage JavaDoc image, Element JavaDoc imageElement,
82                             SVGGeneratorContext generatorContext) {
83         //
84
// First, set the image width and height
85
//
86
imageElement.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE,
87                                     "" + image.getWidth());
88         imageElement.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE,
89                                     "" + image.getHeight());
90
91         //
92
// Now, set the href
93
//
94
try {
95             handleHREF(image, imageElement, generatorContext);
96         } catch (SVGGraphics2DIOException e) {
97             try {
98                 generatorContext.errorHandler.handleError(e);
99             } catch (SVGGraphics2DIOException io) {
100                 // we need a runtime exception because
101
// java.awt.Graphics2D method doesn't throw exceptions..
102
throw new SVGGraphics2DRuntimeException(io);
103             }
104         }
105     }
106
107     /**
108      * The handler should set the xlink:href tag and the width and
109      * height attributes.
110      */

111     public void handleImage(RenderableImage JavaDoc image, Element JavaDoc imageElement,
112                             SVGGeneratorContext generatorContext) {
113         //
114
// First, set the image width and height
115
//
116
imageElement.setAttributeNS(null, SVG_WIDTH_ATTRIBUTE,
117                                     "" + image.getWidth());
118         imageElement.setAttributeNS(null, SVG_HEIGHT_ATTRIBUTE,
119                                     "" + image.getHeight());
120
121         //
122
// Now, set the href
123
//
124
try {
125             handleHREF(image, imageElement, generatorContext);
126         } catch (SVGGraphics2DIOException e) {
127             try {
128                 generatorContext.errorHandler.handleError(e);
129             } catch (SVGGraphics2DIOException io) {
130                 // we need a runtime exception because
131
// java.awt.Graphics2D method doesn't throw exceptions..
132
throw new SVGGraphics2DRuntimeException(io);
133             }
134         }
135     }
136
137     /**
138      * This template method should set the xlink:href attribute on the input
139      * Element parameter
140      */

141     protected void handleHREF(Image JavaDoc image, Element JavaDoc imageElement,
142                               SVGGeneratorContext generatorContext)
143         throws SVGGraphics2DIOException {
144         // Simply write a placeholder
145
imageElement.setAttributeNS(XLINK_NAMESPACE_URI,
146                                     ATTR_XLINK_HREF, image.toString());
147     }
148
149     /**
150      * This template method should set the xlink:href attribute on the input
151      * Element parameter
152      */

153     protected void handleHREF(RenderedImage JavaDoc image, Element JavaDoc imageElement,
154                               SVGGeneratorContext generatorContext)
155         throws SVGGraphics2DIOException {
156         // Simply write a placeholder
157
imageElement.setAttributeNS(XLINK_NAMESPACE_URI,
158                                     ATTR_XLINK_HREF, image.toString());
159     }
160
161     /**
162      * This template method should set the xlink:href attribute on the input
163      * Element parameter
164      */

165     protected void handleHREF(RenderableImage JavaDoc image, Element JavaDoc imageElement,
166                               SVGGeneratorContext generatorContext)
167         throws SVGGraphics2DIOException {
168         // Simply write a placeholder
169
imageElement.setAttributeNS(XLINK_NAMESPACE_URI,
170                                     ATTR_XLINK_HREF, image.toString());
171     }
172 }
173
174
Popular Tags