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;
22 import javax.swing.SwingUtilities;
24 import org.simantics.graphviz.continuation.Computation;
25 import org.simantics.graphviz.drawable.GraphDrawable;
26 import org.simantics.graphviz.drawable.JViewer;
27 import org.simantics.graphviz.internal.parser.DotParser;
28 import org.simantics.graphviz.internal.parser.ParseException;
29 import org.simantics.graphviz.internal.process.CreateXDot;
32 * A class containing only static methods for operating
35 * @author Hannu Niemist�
42 * Parses a dot graph from an input stream.
44 public static Graph parse(InputStream stream) throws IOException {
46 return new DotParser(stream).document();
47 } catch(ParseException e) {
49 e.currentToken.beginLine + ":" + e.currentToken.beginColumn + " - " +
50 e.currentToken.endLine + ":" + e.currentToken.endColumn);
51 throw new IOException(e);
56 * Parses a dot graph from a file.
58 public static Graph parse(File file) throws IOException {
59 InputStream stream = new FileInputStream(file);
60 Graph graph = parse(stream);
67 * Layouts a graph with a given algorithm and writes it to the file using given output format.
69 public static void createImage(Graph graph, String algorithm, String type, File file) throws IOException {
70 System.out.println("Create image");
71 if(type.equals("dot")) {
72 PrintStream stream = new PrintStream(new FileOutputStream(file));
77 File DOT_EXE = Activator.getDotExe();
78 Process process = new ProcessBuilder(
79 DOT_EXE.toString(), "-T" + type, "-K" + algorithm, "-o"+ file.getAbsolutePath())
80 .directory(DOT_EXE.getParentFile())
83 PrintStream stream = new PrintStream(process.getOutputStream());
88 InputStream errorStream = process.getErrorStream();
90 int c = errorStream.read();
93 System.err.print((char)c);
99 } catch (InterruptedException e) {
100 // TODO Auto-generated catch block
109 public static Computation<Graph> createXDot(Graph graph, String algorithm) {
110 CreateXDot process = new CreateXDot(graph, algorithm);
115 public static byte[] read(InputStream stream) throws IOException {
116 byte[] buffer = new byte[1024];
119 int len = stream.read(buffer, pos, buffer.length-pos);
121 return Arrays.copyOf(buffer, pos);
123 if(pos == buffer.length) {
124 buffer = Arrays.copyOf(buffer, (int)(buffer.length * 1.5));
130 * Layouts the graph using given algorithm and visualizes it
131 * in a new viewer window.
133 public static void show(final Graph graph, final String algorithm) {
134 SwingUtilities.invokeLater(new Runnable() {
136 new JViewer(new GraphDrawable(graph, algorithm));
142 * Layouts the graph using dot algorithm and visualizes it
143 * in a new viewer window.
145 public static void show(final Graph graph) {