]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphviz/src/org/simantics/graphviz/Graphs.java
b0b82f9e8dc30f25a4db96eebbd6b10274b80bce
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / Graphs.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.graphviz;
13
14 import java.io.File;
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
22 import javax.swing.SwingUtilities;
23
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;
30
31 /**
32  * A class containing only static methods for operating
33  * on graphs.
34  * 
35  * @author Hannu Niemist�
36  */
37 public class Graphs {
38
39     private Graphs() {}
40     
41     /**
42      * Parses a dot graph from an input stream.
43      */
44     public static Graph parse(InputStream stream) throws IOException {
45         try {
46             return new DotParser(stream).document();
47         } catch(ParseException e) {
48                 System.out.println(
49                                 e.currentToken.beginLine + ":" + e.currentToken.beginColumn + " - " +
50                                 e.currentToken.endLine + ":" + e.currentToken.endColumn);
51             throw new IOException(e);
52         }
53     }
54     
55     /**
56      * Parses a dot graph from a file.
57      */
58     public static Graph parse(File file) throws IOException {
59         InputStream stream = new FileInputStream(file);
60         Graph graph = parse(stream);
61         stream.close();
62         
63         return graph;
64     }
65     
66     /**
67      * Layouts a graph with a given algorithm and writes it to the file using given output format.
68      */
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));
73             graph.write(stream);
74             stream.close();
75         }
76         else {  
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())
81                   .start();
82             
83             PrintStream stream = new PrintStream(process.getOutputStream());
84             
85             graph.write(stream);
86             stream.close();
87            
88             InputStream errorStream = process.getErrorStream();
89             while(true) {
90                 int c = errorStream.read();
91                 if(c <= 0)
92                     break;
93                 System.err.print((char)c);
94             }
95             errorStream.close();
96             
97             try {
98                 process.waitFor();
99             } catch (InterruptedException e) {
100                 // TODO Auto-generated catch block
101                 e.printStackTrace();
102             }
103         }
104     }
105     
106     /**
107      * Layouts the graph.
108      */
109     public static Computation<Graph> createXDot(Graph graph, String algorithm) {
110         CreateXDot process = new CreateXDot(graph, algorithm);
111         process.start();
112         return process;
113     }
114     
115     public static byte[] read(InputStream stream) throws IOException {
116         byte[] buffer = new byte[1024];
117         int pos = 0;
118         while(true) {                   
119                 int len = stream.read(buffer, pos, buffer.length-pos);
120                 if(len < 0)
121                         return Arrays.copyOf(buffer, pos);
122                 pos += len;
123                 if(pos == buffer.length) {
124                         buffer = Arrays.copyOf(buffer, (int)(buffer.length * 1.5));
125                 }
126         }
127     }
128
129     /**
130      * Layouts the graph using given algorithm and visualizes it
131      * in a new viewer window.
132      */
133     public static void show(final Graph graph, final String algorithm) {
134         SwingUtilities.invokeLater(new Runnable() {
135             public void run() {
136                 new JViewer(new GraphDrawable(graph, algorithm));
137             }
138         });        
139     }   
140     
141     /**
142      * Layouts the graph using dot algorithm and visualizes it
143      * in a new viewer window.
144      */
145     public static void show(final Graph graph) {
146         show(graph, "dot");      
147     }   
148     
149 }