KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > image > analyser > SVGReader


1 /*
2  * $Id: SVGReader.java,v 1.12.2.5 2003/02/25 13:38:24 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.image.analyser;
52
53 // Java
54
import java.io.BufferedInputStream JavaDoc;
55 import java.io.IOException JavaDoc;
56
57 import java.awt.geom.AffineTransform JavaDoc;
58 import java.awt.geom.Dimension2D JavaDoc;
59 import java.awt.Dimension JavaDoc;
60
61 // DOM
62
import org.w3c.dom.Element JavaDoc;
63 import org.w3c.dom.svg.SVGDocument;
64
65 // FOP
66
import org.apache.fop.messaging.MessageHandler;
67 import org.apache.fop.image.SVGImage;
68
69 //Batik
70
import org.apache.batik.dom.svg.SVGOMDocument;
71 import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
72 import org.apache.batik.bridge.UserAgentAdapter;
73 import org.apache.batik.bridge.UserAgent;
74 import org.apache.batik.bridge.BridgeContext;
75 import org.apache.batik.bridge.UnitProcessor;
76
77 /**
78  * ImageReader object for SVG document image type.
79  */

80 public class SVGReader extends AbstractImageReader {
81
82     public boolean verifySignature(String JavaDoc uri,
83                                    BufferedInputStream JavaDoc fis) throws IOException JavaDoc {
84         this.imageStream = fis;
85         return loadImage(uri);
86     }
87
88     public String JavaDoc getMimeType() {
89         return "image/svg+xml";
90     }
91
92     /**
93      * This means the external svg document will be loaded twice.
94      * Possibly need a slightly different design for the image stuff.
95      */

96     protected boolean loadImage(String JavaDoc uri) {
97         // parse document and get the size attributes of the svg element
98
try {
99             SAXSVGDocumentFactory factory =
100               new SAXSVGDocumentFactory(SVGImage.getParserName());
101             SVGDocument doc = (SVGDocument)factory.createDocument(uri, imageStream);
102
103             UserAgent userAgent = new MUserAgent(new AffineTransform JavaDoc());
104             BridgeContext ctx = new BridgeContext(userAgent);
105
106             Element JavaDoc e = ((SVGDocument)doc).getRootElement();
107             UnitProcessor.Context uctx = UnitProcessor.createContext(ctx, e);
108
109             String JavaDoc s;
110             // 'width' attribute - default is 100%
111
s = e.getAttributeNS(null, SVGOMDocument.SVG_WIDTH_ATTRIBUTE);
112             if (s.length() == 0) {
113                 s = SVGOMDocument.SVG_SVG_WIDTH_DEFAULT_VALUE;
114             }
115             width = (int)UnitProcessor.svgHorizontalLengthToUserSpace
116                          (s, SVGOMDocument.SVG_WIDTH_ATTRIBUTE, uctx);
117
118             // 'height' attribute - default is 100%
119
s = e.getAttributeNS(null, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE);
120             if (s.length() == 0) {
121                 s = SVGOMDocument.SVG_SVG_HEIGHT_DEFAULT_VALUE;
122             }
123             height = (int)UnitProcessor.svgVerticalLengthToUserSpace
124                          (s, SVGOMDocument.SVG_HEIGHT_ATTRIBUTE, uctx);
125
126             return true;
127         } catch (NoClassDefFoundError JavaDoc ncdfe) {
128             MessageHandler.errorln("Batik not in class path");
129             return false;
130         } catch (Exception JavaDoc e) {
131             MessageHandler.errorln("Could not load external SVG: " +
132                                    e.getMessage());
133             // assuming any exception means this document is not svg
134
// or could not be loaded for some reason
135
return false;
136         }
137     }
138
139     protected class MUserAgent extends UserAgentAdapter {
140         AffineTransform JavaDoc currentTransform = null;
141
142         /**
143          * Creates a new SVGUserAgent.
144          */

145         protected MUserAgent(AffineTransform JavaDoc at) {
146             currentTransform = at;
147         }
148
149         /**
150          * Displays an error message.
151          */

152         public void displayError(String JavaDoc message) {
153             MessageHandler.error(message);
154         }
155
156         /**
157          * Displays an error resulting from the specified Exception.
158          */

159         public void displayError(Exception JavaDoc ex) {
160             MessageHandler.error(org.apache.avalon.framework.ExceptionUtil.printStackTrace(ex));
161         }
162
163         /**
164          * Displays a message in the User Agent interface.
165          * The given message is typically displayed in a status bar.
166          */

167         public void displayMessage(String JavaDoc message) {
168             MessageHandler.log(message);
169         }
170
171         /**
172          * Returns a customized the pixel to mm factor.
173          */

174         public float getPixelToMM() {
175             // this is set to 72dpi as the values in fo are 72dpi
176
return 0.35277777777777777778f; // 72 dpi
177
// return 0.26458333333333333333333333333333f; // 96dpi
178
}
179
180         public float getPixelUnitToMillimeter() {
181             // this is set to 72dpi as the values in fo are 72dpi
182
return 0.35277777777777777778f; // 72 dpi
183
// return 0.26458333333333333333333333333333f; // 96dpi
184
}
185
186         /**
187          * Returns the language settings.
188          */

189         public String JavaDoc getLanguages() {
190             return "en"; // userLanguages;
191
}
192
193         public String JavaDoc getMedia() {
194             return "print";
195         }
196
197         public boolean isXMLParserValidating() {
198             return true;
199         }
200
201         /**
202          * Returns the user stylesheet uri.
203          * @return null if no user style sheet was specified.
204          */

205         public String JavaDoc getUserStyleSheetURI() {
206             return null; // userStyleSheetURI;
207
}
208
209         /**
210          * Returns the class name of the XML parser.
211          */

212         public String JavaDoc getXMLParserClassName() {
213             return org.apache.fop.apps.Driver.getParserClassName();
214         }
215
216         public AffineTransform JavaDoc getTransform() {
217             return currentTransform;
218         }
219
220         public Dimension2D JavaDoc getViewportSize() {
221             return new Dimension JavaDoc(100, 100);
222         }
223
224     }
225
226 }
227
228
Popular Tags