--- /dev/null
+package org.simantics.graphviz.internal.process;\r
+\r
+import java.io.ByteArrayInputStream;\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.PrintStream;\r
+\r
+import org.simantics.graphviz.Activator;\r
+import org.simantics.graphviz.Graph;\r
+import org.simantics.graphviz.Graphs;\r
+import org.simantics.graphviz.continuation.ComputationThread;\r
+\r
+public class CreateXDot extends ComputationThread<Graph> {\r
+\r
+ Graph graph;\r
+ String algorithm;\r
+ \r
+ Process process;\r
+ \r
+ public CreateXDot(Graph graph, String algorithm) {\r
+ this.graph = graph;\r
+ this.algorithm = algorithm;\r
+ }\r
+\r
+ @Override\r
+ protected void failWith(Exception exception) {\r
+ process.destroy();\r
+ super.failWith(exception);\r
+ }\r
+ \r
+ @Override\r
+ public void run() {\r
+ try {\r
+ File DOT_EXE = Activator.getDotExe();\r
+ process = new ProcessBuilder(\r
+ DOT_EXE.toString(),\r
+ "-Txdot", "-K" + algorithm)\r
+ .directory(DOT_EXE.getParentFile())\r
+ .start();\r
+ \r
+ // Writes output to the process\r
+ new Thread() {\r
+ public void run() {\r
+ PrintStream stream = new PrintStream(process.getOutputStream());\r
+ graph.write(stream);\r
+ stream.close();\r
+ }\r
+ }.start();\r
+ \r
+ // Prints errors to stderr\r
+ new Thread() {\r
+ public void run() {\r
+ try {\r
+ InputStream errorStream = process.getErrorStream();\r
+ while(true) {\r
+ int c = errorStream.read();\r
+ if(c <= 0)\r
+ break;\r
+ System.err.print((char)c);\r
+ }\r
+ errorStream.close();\r
+ } catch(IOException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ }.start();\r
+ \r
+ InputStream inputStream = process.getInputStream();\r
+ byte[] buffer = Graphs.read(inputStream);\r
+ inputStream.close(); \r
+ \r
+ //System.out.println(new String(buffer));\r
+ if(!isDone())\r
+ doneWith(Graphs.parse(new ByteArrayInputStream(buffer)));\r
+ } catch(Exception e) {\r
+ failWith(e);\r
+ }\r
+ }\r
+\r
+}\r