View Javadoc

1   /*
2    * $Id: DOTtoMAP.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.tools.visualizer.util;
12  
13  import org.mule.tools.visualizer.config.GraphEnvironment;
14  
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.io.OutputStream;
18  
19  public class DOTtoMAP
20  {
21  
22      private static final int BUFFER_SIZE = 32768;
23      private GraphEnvironment env = null;
24  
25      DOTtoMAP(GraphEnvironment env)
26      {
27          this.env = env;
28      }
29  
30      public static void transform(String dotExeFileName,
31                                   String dotFileName,
32                                   String outFormat,
33                                   OutputStream out,
34                                   GraphEnvironment env) throws IOException
35      {
36          (new DOTtoMAP(env)).innerTransform(dotExeFileName, dotFileName, outFormat, out);
37      }
38  
39      public static void transform(String dotExeFileName,
40                                   String dotFileName,
41                                   String outFileName,
42                                   GraphEnvironment env) throws IOException
43      {
44          (new DOTtoMAP(env)).innerTransform(dotExeFileName, dotFileName, outFileName);
45      }
46  
47      private String getFormatForFile(String outFileName)
48      {
49          int idx = outFileName.lastIndexOf(".");
50          if (idx == -1 || idx == outFileName.length() - 1)
51          {
52              throw new IllegalArgumentException("Can't determine file name extention for file name "
53                              + outFileName);
54          }
55          else
56          {
57              return outFileName.substring(idx + 1);
58          }
59      }
60  
61      private void innerTransform(String dotExeFileName, String dotFileName, String outFormat, OutputStream out)
62          throws IOException
63      {
64          String exeCmd = dotExeFileName + " -T" + outFormat + " " + dotFileName;
65          Process p = Runtime.getRuntime().exec(exeCmd);
66          InputStream is = p.getInputStream();
67          byte buf[] = new byte[BUFFER_SIZE];
68          do
69          {
70              int len = is.read(buf);
71              if (len > 0)
72              {
73                  out.write(buf, 0, len);
74              }
75              else
76              {
77                  is.close();
78                  return;
79              }
80          }
81          while (true);
82      }
83  
84      private void innerTransform(String dotExeFileName, String dotFileName, String outFileName)
85          throws IOException
86      {
87          String exeCmd = dotExeFileName + " -T" + getFormatForFile(outFileName) + " " + dotFileName + " -o "
88                          + outFileName;
89          env.log(exeCmd);
90          Process p = Runtime.getRuntime().exec(exeCmd);
91          try
92          {
93              int i = p.waitFor();
94              env.log("result code from process is: " + i);
95          }
96          catch (Exception ie)
97          {
98              env.logError("Warning: failed to wait for native process to exit...", ie);
99          }
100     }
101 }