use of org.eclipse.elk.alg.graphviz.layouter.util.ForwardingInputStream in project elk by eclipse.
the class GraphvizLayoutProvider method readDotGraph.
/**
* Reads and parses a serialized Graphviz model.
*
* @param monitor
* a monitor to which progress is reported
* @param debugMode
* whether debug mode is active
* @param resourceSet
* the resoure set for parsing
* @return an instance of the parsed graphviz model
*/
private GraphvizModel readDotGraph(final IElkProgressMonitor monitor, final boolean debugMode, final XtextResourceSet resourceSet) {
monitor.begin("Parse output", 1);
InputStream inputStream = graphvizTool.output();
// enable debug output if needed
FileOutputStream debugStream = null;
if (debugMode) {
try {
String path = System.getProperty("user.home");
if (path.endsWith(File.separator)) {
path += "tmp" + File.separator + "graphviz";
} else {
path += File.separator + "tmp" + File.separator + "graphviz";
}
new File(path).mkdirs();
debugStream = new FileOutputStream(new File(path + File.separator + debugFileBase() + "-out.dot"));
inputStream = new ForwardingInputStream(inputStream, debugStream);
} catch (Exception exception) {
System.out.println("GraphvizLayouter: Could not initialize debug output: " + exception.getMessage());
}
}
// parse the output stream of the dot process
XtextResource resource = (XtextResource) resourceSet.createResource(URI.createURI("input.graphviz_dot"));
try {
resource.load(inputStream, null);
} catch (IOException exception) {
graphvizTool.cleanup(Cleanup.ERROR);
throw new WrappedException("Failed to read Graphviz output.", exception);
} finally {
if (debugStream != null) {
try {
debugStream.close();
} catch (IOException exception) {
// ignore exception
}
}
}
// analyze errors and retrieve parse result
if (!resource.getErrors().isEmpty()) {
StringBuilder errorString = new StringBuilder("Errors in Graphviz output:");
for (Diagnostic diagnostic : resource.getErrors()) {
errorString.append("\n" + diagnostic.getLine() + ": " + diagnostic.getMessage());
}
graphvizTool.cleanup(Cleanup.ERROR);
throw new GraphvizException(errorString.toString());
}
GraphvizModel graphvizModel = (GraphvizModel) resource.getParseResult().getRootASTElement();
if (graphvizModel == null || graphvizModel.getGraphs().isEmpty()) {
graphvizTool.cleanup(Cleanup.ERROR);
throw new GraphvizException("No output from the Graphviz process." + " Try increasing the timeout value in the Eclipse Diagram Layout preferences.");
}
monitor.done();
return graphvizModel;
}
Aggregations