1 18 package org.apache.batik.transcoder.wmf.tosvg; 19 20 import java.awt.Dimension ; 21 import java.io.BufferedInputStream ; 22 import java.io.DataInputStream ; 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.io.OutputStreamWriter ; 29 import java.io.Writer ; 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 import java.net.URLConnection ; 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 ; 42 import org.w3c.dom.Document ; 43 import org.w3c.dom.Element ; 44 import org.xml.sax.XMLFilter ; 45 46 47 62 public class WMFTranscoder extends AbstractTranscoder 63 implements SVGConstants{ 64 65 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 76 public WMFTranscoder(){ 77 } 78 79 85 public void transcode(TranscoderInput input, TranscoderOutput output) 86 throws TranscoderException { 87 DataInputStream is = getCompatibleInput(input); 91 92 WMFRecordStore currentStore = new WMFRecordStore(); 96 try{ 97 currentStore.read(is); 98 }catch(IOException e){ 99 handler.fatalError(new TranscoderException(e)); 100 return; 101 } 102 103 WMFPainter painter = new WMFPainter(currentStore); 107 108 DOMImplementation domImpl 112 = SVGDOMImplementation.getDOMImplementation(); 113 114 Document 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 int vpX = currentStore.getVpX(); 125 int vpY = currentStore.getVpY(); 126 int vpW = currentStore.getVpW(); 127 int vpH = currentStore.getVpH(); 128 svgGenerator.setSVGCanvasSize(new Dimension (vpW, vpH)); 129 130 Element svgRoot = svgGenerator.getRoot(); 131 svgRoot.setAttributeNS(null, SVG_VIEW_BOX_ATTRIBUTE, 132 "" + vpX + " " + vpY + " " + 133 vpW + " " + vpH ); 134 135 writeSVGToOutput(svgGenerator, svgRoot, output); 139 } 140 141 145 private void writeSVGToOutput(SVGGraphics2D svgGenerator, 146 Element svgRoot, 147 TranscoderOutput output) 148 throws TranscoderException { 149 XMLFilter xmlFilter = output.getXMLFilter(); 151 if(xmlFilter != null){ 152 handler.fatalError(new TranscoderException("" + ERROR_INCOMPATIBLE_OUTPUT_TYPE)); 153 } 154 155 Document doc = output.getDocument(); 157 if(doc != null){ 158 handler.fatalError(new TranscoderException("" + ERROR_INCOMPATIBLE_OUTPUT_TYPE)); 159 } 160 161 try{ 162 OutputStream os = output.getOutputStream(); 164 if( os != null ){ 165 svgGenerator.stream(svgRoot, new OutputStreamWriter (os)); 166 return; 167 } 168 169 Writer wr = output.getWriter(); 171 if( wr != null ){ 172 svgGenerator.stream(svgRoot, wr); 173 return; 174 } 175 176 String uri = output.getURI(); 178 if( uri != null ){ 179 try{ 180 URL url = new URL (uri); 181 URLConnection urlCnx = url.openConnection(); 182 os = urlCnx.getOutputStream(); 183 svgGenerator.stream(svgRoot, new OutputStreamWriter (os)); 184 return; 185 }catch(MalformedURLException e){ 186 handler.fatalError(new TranscoderException(e)); 187 }catch(IOException e){ 188 handler.fatalError(new TranscoderException(e)); 189 } 190 } 191 }catch(IOException e){ 192 throw new TranscoderException(e); 193 } 194 195 throw new TranscoderException("" + ERROR_INCOMPATIBLE_OUTPUT_TYPE); 196 197 } 198 199 203 private DataInputStream getCompatibleInput(TranscoderInput input) 204 throws TranscoderException { 205 if(input == null){ 207 handler.fatalError(new TranscoderException("" + ERROR_NULL_INPUT)); 208 } 209 210 InputStream in = input.getInputStream(); 212 if(in != null){ 213 return new DataInputStream (new BufferedInputStream (in)); 214 } 215 216 String uri = input.getURI(); 218 if(uri != null){ 219 try{ 220 URL url = new URL (uri); 221 in = url.openStream(); 222 return new DataInputStream (new BufferedInputStream (in)); 223 }catch(MalformedURLException e){ 224 handler.fatalError(new TranscoderException(e)); 225 }catch(IOException 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 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 WMF_EXTENSION = ".wmf"; 240 public static final String SVG_EXTENSION = ".svg"; 241 242 245 public static void main(String 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 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 outputFileName = fileName.substring(0, fileName.toLowerCase().indexOf(WMF_EXTENSION)) + SVG_EXTENSION; 262 File inputFile = new File (fileName); 263 File outputFile = new File (outputFileName); 264 try{ 265 TranscoderInput input = new TranscoderInput(inputFile.toURL().toString()); 266 TranscoderOutput output = new TranscoderOutput(new FileOutputStream (outputFile)); 267 transcoder.transcode(input, output); 268 }catch(MalformedURLException e){ 269 throw new TranscoderException(e); 270 }catch(IOException e){ 271 throw new TranscoderException(e); 272 } 273 System.out.println(".... Done"); 274 } 275 } 276 277 System.exit(0); 278 } 279 } 280 | Popular Tags |