1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.mvdb.maven.plugins.svg;
19
20 import java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.OutputStream;
24 import java.net.MalformedURLException;
25
26 import org.apache.batik.transcoder.TranscoderException;
27 import org.apache.batik.transcoder.TranscoderInput;
28 import org.apache.batik.transcoder.TranscoderOutput;
29 import org.apache.batik.transcoder.image.ImageTranscoder;
30 import org.apache.batik.transcoder.image.JPEGTranscoder;
31 import org.apache.batik.transcoder.image.PNGTranscoder;
32
33 /***
34 * This class converts svg images to png, jpg and tiff.
35 *
36 * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
37 * @version $Id: SVGConverter.java,v 1.5 2005/12/09 20:41:15 mvdb Exp $
38 */
39 public class SVGConverter {
40
41 /***
42 * The type of target
43 */
44 private String type;
45 /***
46 * The file to convert
47 */
48 private String file;
49 /***
50 * The destination of the file
51 */
52 private String destination;
53
54 /***
55 *
56 */
57 public SVGConverter() {
58 super();
59 }
60
61 /***
62 * Converts the svg to the type specified in getType()
63 * It defaults to png.
64 */
65 public void convert() {
66 if ("jpg".equalsIgnoreCase(getType())) {
67 convertToJPG();
68 } else {
69 convertToPNG();
70 }
71 }
72
73 /***
74 * Convert the svg to PNG format
75 */
76 public void convertToPNG() {
77 setType("png");
78 PNGTranscoder transcoder = new PNGTranscoder();
79 convertTo(transcoder, ".png");
80 }
81
82 /***
83 * Convert the svg to JPEG format
84 */
85 public void convertToJPG() {
86 setType("jpg");
87 JPEGTranscoder transcoder = new JPEGTranscoder();
88 transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(.8));
89 convertTo(transcoder, ".jpg");
90 }
91
92 /***
93 * Converts the svg file to the target file
94 *
95 * @param transcoder the transcoder to use
96 * @param extension the filename extension to use
97 */
98 protected void convertTo(ImageTranscoder transcoder, String extension) {
99 if (getFile() == null) {
100 throw new IllegalArgumentException("File cannot be null");
101 }
102 TranscoderInput input = null;
103 try {
104 String svgFile = new File(getFile()).toURL().toString();
105 if (!new File(getFile()).exists()) {
106 throw new IllegalArgumentException("Cannot find file " + svgFile);
107 }
108 input = new TranscoderInput(svgFile);
109 } catch (MalformedURLException mfue) {
110 throw new IllegalArgumentException("Invalid filename : " + getFile());
111 }
112 String realFilename = new File(file).getName();
113 if (!destination.endsWith("/")) {
114 destination += "/";
115 }
116 String pngFileName = destination + realFilename.substring(0, realFilename.indexOf('.')) + extension;
117 OutputStream out = null;
118 try {
119 out = new FileOutputStream(pngFileName);
120 TranscoderOutput output = new TranscoderOutput(out);
121
122 transcoder.transcode(input, output);
123 } catch (Exception e) {
124 if (e instanceof FileNotFoundException) {
125 throw new IllegalArgumentException("Cannot find file " + pngFileName);
126 } else if (e instanceof TranscoderException) {
127 Exception te = ((TranscoderException)e).getException();
128 te.printStackTrace(System.err);
129 throw new IllegalArgumentException("Exception : \n" + te);
130 } else {
131 e.printStackTrace(System.err);
132 }
133 } finally {
134 if (out != null) {
135 try {
136 out.flush();
137 out.close();
138 } catch (Exception e) {
139 e.printStackTrace(System.err);
140 }
141 }
142 }
143 }
144
145 /***
146 * @return the destination
147 */
148 public String getDestination() {
149 return destination;
150 }
151
152 /***
153 * @return the file
154 */
155 public String getFile() {
156 return file;
157 }
158
159 /***
160 * @return the type
161 */
162 public String getType() {
163 return type;
164 }
165
166 /***
167 * @param destination the destination
168 */
169 public void setDestination(String destination) {
170 this.destination = destination;
171 }
172
173 /***
174 * @param file the file
175 */
176 public void setFile(String file) {
177 this.file = file;
178 }
179
180 /***
181 * @param type the type
182 */
183 public void setType(String type) {
184 this.type = type;
185 }
186
187 }