KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > transcoder > wmf > tosvg > WMFTranscoder


1 /*
2
3    Copyright 1999-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.wmf.tosvg;
19
20 import java.awt.Dimension JavaDoc;
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.DataInputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.io.Writer JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.net.URLConnection JavaDoc;
33
34 import org.apache.batik.dom.svg.SVGDOMImplementation;
35 import org.apache.batik.svggen.SVGGraphics2D;
36 import org.apache.batik.transcoder.AbstractTranscoder;
37 import org.apache.batik.transcoder.TranscoderException;
38 import org.apache.batik.transcoder.TranscoderInput;
39 import org.apache.batik.transcoder.TranscoderOutput;
40 import org.apache.batik.util.SVGConstants;
41 import org.w3c.dom.DOMImplementation JavaDoc;
42 import org.w3c.dom.Document JavaDoc;
43 import org.w3c.dom.Element JavaDoc;
44 import org.xml.sax.XMLFilter JavaDoc;
45
46
47 /**
48  * This class implements the <tt>Transcoder</tt> interface and
49  * can convert a WMF input document into an SVG document.
50  *
51  * It can use <tt>TranscoderInput</tt> that are either a URI
52  * or a <tt>InputStream</tt> or a <tt>Reader</tt>. The
53  * <tt>XMLReader</tt> and <tt>Document</tt> <tt>TranscoderInput</tt>
54  * types are not supported.
55  *
56  * This transcoder can use <tt>TranscoderOutputs</tt> that are
57  * of any type except the <tt>XMLFilter</tt> type.
58  *
59  * @version $Id: WMFTranscoder.java,v 1.7 2004/11/18 01:47:02 deweese Exp $
60  * @author <a HREF="mailto:luano@asd.ie">Luan O'Carroll</a>
61  */

62 public class WMFTranscoder extends AbstractTranscoder
63     implements SVGConstants{
64
65     /**
66      * Error codes for the WMFTranscoder
67      */

68     public static final int WMF_TRANSCODER_ERROR_BASE = 0xff00;
69     public static final int ERROR_NULL_INPUT = WMF_TRANSCODER_ERROR_BASE + 0;
70     public static final int ERROR_INCOMPATIBLE_INPUT_TYPE = WMF_TRANSCODER_ERROR_BASE + 1;
71     public static final int ERROR_INCOMPATIBLE_OUTPUT_TYPE = WMF_TRANSCODER_ERROR_BASE + 2;
72
73     /**
74      * Default constructor
75      */

76     public WMFTranscoder(){
77     }
78
79     /**
80      * Transcodes the specified input in the specified output.
81      * @param input the input to transcode
82      * @param output the ouput where to transcode
83      * @exception TranscoderException if an error occured while transcoding
84      */

85     public void transcode(TranscoderInput input, TranscoderOutput output)
86         throws TranscoderException {
87         //
88
// Extract the input
89
//
90
DataInputStream JavaDoc is = getCompatibleInput(input);
91
92         //
93
// Build a RecordStore from the input
94
//
95
WMFRecordStore currentStore = new WMFRecordStore();
96         try{
97             currentStore.read(is);
98         }catch(IOException JavaDoc e){
99             handler.fatalError(new TranscoderException(e));
100             return;
101         }
102
103         //
104
// Build a painter for the RecordStore
105
//
106
WMFPainter painter = new WMFPainter(currentStore);
107
108         //
109
// Use SVGGraphics2D to generate SVG content
110
//
111
DOMImplementation domImpl
112             = SVGDOMImplementation.getDOMImplementation();
113
114         Document JavaDoc doc = domImpl.createDocument(SVG_NAMESPACE_URI,
115                                               SVG_SVG_TAG, null);
116
117         SVGGraphics2D svgGenerator = new SVGGraphics2D(doc);
118
119         painter.paint(svgGenerator);
120
121         //
122
// Set the size and viewBox on the output document
123
//
124
int vpX = currentStore.getVpX();
125         int vpY = currentStore.getVpY();
126         int vpW = currentStore.getVpW();
127         int vpH = currentStore.getVpH();
128         svgGenerator.setSVGCanvasSize(new Dimension JavaDoc(vpW, vpH));
129
130         Element JavaDoc svgRoot = svgGenerator.getRoot();
131         svgRoot.setAttributeNS(null, SVG_VIEW_BOX_ATTRIBUTE,
132                                "" + vpX + " " + vpY + " " +
133                                vpW + " " + vpH );
134
135         //
136
// Now, write the SVG content to the output
137
//
138
writeSVGToOutput(svgGenerator, svgRoot, output);
139     }
140
141     /**
142      * Writes the SVG content held by the svgGenerator to the
143      * <tt>TranscoderOutput</tt>.
144      */

145     private void writeSVGToOutput(SVGGraphics2D svgGenerator,
146                                   Element JavaDoc svgRoot,
147                                   TranscoderOutput output)
148         throws TranscoderException {
149         // XMLFilter
150
XMLFilter JavaDoc xmlFilter = output.getXMLFilter();
151         if(xmlFilter != null){
152             handler.fatalError(new TranscoderException("" + ERROR_INCOMPATIBLE_OUTPUT_TYPE));
153         }
154
155         // <!> FIX ME: SHOULD HANDLE DOCUMENT INPUT
156
Document JavaDoc doc = output.getDocument();
157         if(doc != null){
158             handler.fatalError(new TranscoderException("" + ERROR_INCOMPATIBLE_OUTPUT_TYPE));
159         }
160
161         try{
162             // Output stream
163
OutputStream JavaDoc os = output.getOutputStream();
164             if( os != null ){
165                 svgGenerator.stream(svgRoot, new OutputStreamWriter JavaDoc(os));
166                 return;
167             }
168
169             // Writer
170
Writer JavaDoc wr = output.getWriter();
171             if( wr != null ){
172                 svgGenerator.stream(svgRoot, wr);
173                 return;
174             }
175
176             // URI
177
String JavaDoc uri = output.getURI();
178             if( uri != null ){
179                 try{
180                     URL JavaDoc url = new URL JavaDoc(uri);
181                     URLConnection JavaDoc urlCnx = url.openConnection();
182                     os = urlCnx.getOutputStream();
183                     svgGenerator.stream(svgRoot, new OutputStreamWriter JavaDoc(os));
184                     return;
185                 }catch(MalformedURLException JavaDoc e){
186                     handler.fatalError(new TranscoderException(e));
187                 }catch(IOException JavaDoc e){
188                     handler.fatalError(new TranscoderException(e));
189                 }
190             }
191         }catch(IOException JavaDoc e){
192             throw new TranscoderException(e);
193         }
194
195         throw new TranscoderException("" + ERROR_INCOMPATIBLE_OUTPUT_TYPE);
196
197     }
198
199     /**
200      * Checks that the input is one of URI or an <tt>InputStream</tt>
201      * returns it as a DataInputStream
202      */

203     private DataInputStream JavaDoc getCompatibleInput(TranscoderInput input)
204         throws TranscoderException {
205         // Cannot deal with null input
206
if(input == null){
207             handler.fatalError(new TranscoderException("" + ERROR_NULL_INPUT));
208         }
209
210         // Can deal with InputStream
211
InputStream JavaDoc in = input.getInputStream();
212         if(in != null){
213             return new DataInputStream JavaDoc(new BufferedInputStream JavaDoc(in));
214         }
215
216         // Can deal with URI
217
String JavaDoc uri = input.getURI();
218         if(uri != null){
219             try{
220                 URL JavaDoc url = new URL JavaDoc(uri);
221                 in = url.openStream();
222                 return new DataInputStream JavaDoc(new BufferedInputStream JavaDoc(in));
223             }catch(MalformedURLException JavaDoc e){
224                 handler.fatalError(new TranscoderException(e));
225             }catch(IOException JavaDoc e){
226                 handler.fatalError(new TranscoderException(e));
227             }
228         }
229
230         handler.fatalError(new TranscoderException("" + ERROR_INCOMPATIBLE_INPUT_TYPE));
231         return null;
232     }
233
234     public static final String JavaDoc USAGE = "The WMFTranscoder converts a WMF document into an SVG document. \n" +
235         "This simple application generates SVG documents that have the same name, but a where the .wmf extension \n" +
236         "is replaced with .svg. To run the application, type the following at the command line: \n" +
237         "java org.apache.batik.transcoder.wmf.tosvg.WMFTranscoder fileName [fileName]+";
238
239     public static final String JavaDoc WMF_EXTENSION = ".wmf";
240     public static final String JavaDoc SVG_EXTENSION = ".svg";
241
242     /**
243      * Unit testing : Illustrates how the transcoder might be used.
244      */

245     public static void main(String JavaDoc args[]) throws TranscoderException {
246         if(args.length < 1){
247             System.err.println(USAGE);
248             System.exit(1);
249         }
250
251         WMFTranscoder transcoder = new WMFTranscoder();
252         int nFiles = args.length;
253
254         for(int i=0; i<nFiles; i++){
255             String JavaDoc fileName = args[i];
256             if(!fileName.toLowerCase().endsWith(WMF_EXTENSION)){
257                 System.err.println(args[i] + " does not have the " + WMF_EXTENSION + " extension. It is ignored");
258             }
259             else{
260                 System.out.print("Processing : " + args[i] + "...");
261                 String JavaDoc outputFileName = fileName.substring(0, fileName.toLowerCase().indexOf(WMF_EXTENSION)) + SVG_EXTENSION;
262                 File JavaDoc inputFile = new File JavaDoc(fileName);
263                 File JavaDoc outputFile = new File JavaDoc(outputFileName);
264                 try{
265                     TranscoderInput input = new TranscoderInput(inputFile.toURL().toString());
266                     TranscoderOutput output = new TranscoderOutput(new FileOutputStream JavaDoc(outputFile));
267                     transcoder.transcode(input, output);
268                 }catch(MalformedURLException JavaDoc e){
269                     throw new TranscoderException(e);
270                 }catch(IOException JavaDoc e){
271                     throw new TranscoderException(e);
272                 }
273                 System.out.println(".... Done");
274             }
275         }
276
277         System.exit(0);
278     }
279 }
280
Popular Tags