Search in sources :

Example 1 with ForwardingInputStream

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;
}
Also used : WrappedException(org.eclipse.elk.core.util.WrappedException) ForwardingInputStream(org.eclipse.elk.alg.graphviz.layouter.util.ForwardingInputStream) InputStream(java.io.InputStream) Diagnostic(org.eclipse.emf.ecore.resource.Resource.Diagnostic) XtextResource(org.eclipse.xtext.resource.XtextResource) IOException(java.io.IOException) ForwardingInputStream(org.eclipse.elk.alg.graphviz.layouter.util.ForwardingInputStream) WrappedException(org.eclipse.elk.core.util.WrappedException) IOException(java.io.IOException) GraphvizModel(org.eclipse.elk.alg.graphviz.dot.dot.GraphvizModel) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GraphvizModel (org.eclipse.elk.alg.graphviz.dot.dot.GraphvizModel)1 ForwardingInputStream (org.eclipse.elk.alg.graphviz.layouter.util.ForwardingInputStream)1 WrappedException (org.eclipse.elk.core.util.WrappedException)1 Diagnostic (org.eclipse.emf.ecore.resource.Resource.Diagnostic)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1