]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.graphviz/src/org/simantics/graphviz/internal/process/CreateXDot.java b/bundles/org.simantics.graphviz/src/org/simantics/graphviz/internal/process/CreateXDot.java
new file mode 100644 (file)
index 0000000..6a14465
--- /dev/null
@@ -0,0 +1,81 @@
+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