1 package org.simantics.graphviz.internal.process;
\r
3 import java.io.ByteArrayInputStream;
\r
5 import java.io.IOException;
\r
6 import java.io.InputStream;
\r
7 import java.io.PrintStream;
\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
14 public class CreateXDot extends ComputationThread<Graph> {
\r
21 public CreateXDot(Graph graph, String algorithm) {
\r
23 this.algorithm = algorithm;
\r
27 protected void failWith(Exception exception) {
\r
29 super.failWith(exception);
\r
35 File DOT_EXE = Activator.getDotExe();
\r
36 process = new ProcessBuilder(
\r
38 "-Txdot", "-K" + algorithm)
\r
39 .directory(DOT_EXE.getParentFile())
\r
42 // Writes output to the process
\r
45 PrintStream stream = new PrintStream(process.getOutputStream());
\r
46 graph.write(stream);
\r
51 // Prints errors to stderr
\r
55 InputStream errorStream = process.getErrorStream();
\r
57 int c = errorStream.read();
\r
60 System.err.print((char)c);
\r
62 errorStream.close();
\r
63 } catch(IOException e) {
\r
64 e.printStackTrace();
\r
69 InputStream inputStream = process.getInputStream();
\r
70 byte[] buffer = Graphs.read(inputStream);
\r
71 inputStream.close();
\r
73 //System.out.println(new String(buffer));
\r
75 doneWith(Graphs.parse(new ByteArrayInputStream(buffer)));
\r
76 } catch(Exception e) {
\r