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