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