1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.graphviz;
15 import java.io.FileInputStream;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.PrintStream;
20 import java.util.Arrays;
21 import java.util.HashMap;
23 import javax.swing.SwingUtilities;
25 import org.simantics.graphviz.continuation.Computation;
26 import org.simantics.graphviz.drawable.GraphDrawable;
27 import org.simantics.graphviz.drawable.JViewer;
28 import org.simantics.graphviz.internal.parser.DotParser;
29 import org.simantics.graphviz.internal.parser.ParseException;
30 import org.simantics.graphviz.internal.process.CreateXDot;
33 * A class containing only static methods for operating
36 * @author Hannu Niemist�
43 * Parses a dot graph from an input stream.
45 public static Graph parse(InputStream stream) throws IOException {
47 return new DotParser(stream).document();
48 } catch(ParseException e) {
50 e.currentToken.beginLine + ":" + e.currentToken.beginColumn + " - " +
51 e.currentToken.endLine + ":" + e.currentToken.endColumn);
52 throw new IOException(e);
57 * Parses a dot graph from a file.
59 public static Graph parse(File file) throws IOException {
60 InputStream stream = new FileInputStream(file);
61 Graph graph = parse(stream);
68 * Layouts a graph with a given algorithm and writes it to the file using given output format.
70 public static void createImage(Graph graph, String algorithm, String type, File file) throws IOException {
71 System.out.println("Create image");
72 if(type.equals("dot")) {
73 PrintStream stream = new PrintStream(new FileOutputStream(file));
78 File DOT_EXE = Activator.getDotExe();
79 Process process = new ProcessBuilder(
80 DOT_EXE.toString(), "-T" + type, "-K" + algorithm, "-o"+ file.getAbsolutePath())
81 .directory(DOT_EXE.getParentFile())
84 PrintStream stream = new PrintStream(process.getOutputStream());
89 InputStream errorStream = process.getErrorStream();
91 int c = errorStream.read();
94 System.err.print((char)c);
100 } catch (InterruptedException e) {
101 // TODO Auto-generated catch block
110 public static Computation<Graph> createXDot(Graph graph, String algorithm) {
111 CreateXDot process = new CreateXDot(graph, algorithm);
116 public static byte[] read(InputStream stream) throws IOException {
117 byte[] buffer = new byte[1024];
120 int len = stream.read(buffer, pos, buffer.length-pos);
122 return Arrays.copyOf(buffer, pos);
124 if(pos == buffer.length) {
125 buffer = Arrays.copyOf(buffer, (int)(buffer.length * 1.5));
131 * Layouts the graph using given algorithm and visualizes it
132 * in a new viewer window.
134 public static void show(final Graph graph, final String algorithm) {
135 SwingUtilities.invokeLater(new Runnable() {
137 new JViewer(new GraphDrawable(graph, algorithm));
143 * Layouts the graph using dot algorithm and visualizes it
144 * in a new viewer window.
146 public static void show(final Graph graph) {
150 public static void showWithNamedWindow(final String windowName, final Graph graph, final String algorithm) {
151 SwingUtilities.invokeLater(new Runnable() {
153 JViewer.getOrCreateViewer(windowName, new GraphDrawable(graph, algorithm));