]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphviz/src/org/simantics/graphviz/internal/process/CreateXDot.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / internal / process / CreateXDot.java
1 package org.simantics.graphviz.internal.process;\r
2 \r
3 import java.io.ByteArrayInputStream;\r
4 import java.io.File;\r
5 import java.io.IOException;\r
6 import java.io.InputStream;\r
7 import java.io.PrintStream;\r
8 \r
9 import org.simantics.graphviz.Activator;\r
10 import org.simantics.graphviz.Graph;\r
11 import org.simantics.graphviz.Graphs;\r
12 import org.simantics.graphviz.continuation.ComputationThread;\r
13 \r
14 public class CreateXDot extends ComputationThread<Graph> {\r
15 \r
16     Graph graph;\r
17     String algorithm;\r
18     \r
19     Process process;\r
20     \r
21     public CreateXDot(Graph graph, String algorithm) {\r
22         this.graph = graph;\r
23         this.algorithm = algorithm;\r
24     }\r
25 \r
26     @Override\r
27     protected void failWith(Exception exception) {\r
28         process.destroy();\r
29         super.failWith(exception);\r
30     }\r
31     \r
32     @Override\r
33     public void run() {\r
34         try {\r
35             File DOT_EXE = Activator.getDotExe();\r
36             process = new ProcessBuilder(\r
37                     DOT_EXE.toString(),\r
38                     "-Txdot", "-K" + algorithm)\r
39                     .directory(DOT_EXE.getParentFile())\r
40                     .start();\r
41             \r
42             // Writes output to the process\r
43             new Thread() {\r
44                 public void run() {\r
45                     PrintStream stream = new PrintStream(process.getOutputStream());\r
46                     graph.write(stream);\r
47                     stream.close();\r
48                 }\r
49             }.start();\r
50             \r
51             // Prints errors to stderr\r
52             new Thread() {\r
53                 public void run() {\r
54                     try {\r
55                         InputStream errorStream = process.getErrorStream();\r
56                         while(true) {\r
57                             int c = errorStream.read();\r
58                             if(c <= 0)\r
59                                 break;\r
60                             System.err.print((char)c);\r
61                         }\r
62                         errorStream.close();\r
63                     } catch(IOException e) {\r
64                         e.printStackTrace();\r
65                     }\r
66                 }\r
67             }.start();\r
68             \r
69             InputStream inputStream = process.getInputStream();\r
70             byte[] buffer = Graphs.read(inputStream);\r
71             inputStream.close();       \r
72             \r
73             //System.out.println(new String(buffer));\r
74             if(!isDone())\r
75                 doneWith(Graphs.parse(new ByteArrayInputStream(buffer)));\r
76         } catch(Exception e) {\r
77             failWith(e);\r
78         }\r
79     }\r
80 \r
81 }\r