1 | package view; |
---|
2 | |
---|
3 | // GraphViz.java - a simple API to call dot from Java programs |
---|
4 | |
---|
5 | /*$Id$*/ |
---|
6 | /* |
---|
7 | ****************************************************************************** |
---|
8 | * * |
---|
9 | * (c) Copyright 2003 Laszlo Szathmary * |
---|
10 | * * |
---|
11 | * This program is free software; you can redistribute it and/or modify it * |
---|
12 | * under the terms of the GNU Lesser General Public License as published by * |
---|
13 | * the Free Software Foundation; either version 2.1 of the License, or * |
---|
14 | * (at your option) any later version. * |
---|
15 | * * |
---|
16 | * This program is distributed in the hope that it will be useful, but * |
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * |
---|
18 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public * |
---|
19 | * License for more details. * |
---|
20 | * * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License * |
---|
22 | * along with this program; if not, write to the Free Software Foundation, * |
---|
23 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * |
---|
24 | * * |
---|
25 | ****************************************************************************** |
---|
26 | */ |
---|
27 | |
---|
28 | import java.io.File; |
---|
29 | import java.io.FileInputStream; |
---|
30 | import java.io.FileOutputStream; |
---|
31 | import java.io.FileWriter; |
---|
32 | |
---|
33 | /** |
---|
34 | * <dl> |
---|
35 | * <dt>Purpose: GraphViz Java API |
---|
36 | * <dd> |
---|
37 | * |
---|
38 | * <dt>Description: |
---|
39 | * <dd> With this Java class you can simply call dot |
---|
40 | * from your Java programs |
---|
41 | * <dt>Example usage: |
---|
42 | * <dd> |
---|
43 | * <pre> |
---|
44 | * GraphViz gv = new GraphViz(); |
---|
45 | * gv.addln(gv.start_graph()); |
---|
46 | * gv.addln("A -> B;"); |
---|
47 | * gv.addln("A -> C;"); |
---|
48 | * gv.addln(gv.end_graph()); |
---|
49 | * System.out.println(gv.getDotSource()); |
---|
50 | * |
---|
51 | * File out = new File("out.gif"); |
---|
52 | * gv.writeGraphToFile(gv.getGraph(gv.getDotSource()), out); |
---|
53 | * </pre> |
---|
54 | * </dd> |
---|
55 | * |
---|
56 | * </dl> |
---|
57 | * |
---|
58 | * @version v0.2, 2010/07/22 (July) -- bug fix |
---|
59 | * @version v0.1, 2003/12/04 (Decembre) -- first release |
---|
60 | * @author Laszlo Szathmary (<a href="jabba.laci@gmail.com">jabba.laci@gmail.com</a>) |
---|
61 | */ |
---|
62 | public class GraphViz |
---|
63 | { |
---|
64 | /** |
---|
65 | * The dir. where temporary files will be created. |
---|
66 | */ |
---|
67 | private static String TEMP_DIR = "/tmp"; |
---|
68 | |
---|
69 | /** |
---|
70 | * Where is your dot program located? It will be called externally. |
---|
71 | */ |
---|
72 | private static String DOT = "/usr/bin/dot"; |
---|
73 | |
---|
74 | /** |
---|
75 | * The source of the graph written in dot language. |
---|
76 | */ |
---|
77 | private StringBuffer graph = new StringBuffer(); |
---|
78 | |
---|
79 | /** |
---|
80 | * Constructor: creates a new GraphViz object that will contain |
---|
81 | * a graph. |
---|
82 | */ |
---|
83 | public GraphViz() { |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Returns the graph's source description in dot language. |
---|
88 | * @return Source of the graph in dot language. |
---|
89 | */ |
---|
90 | public String getDotSource() { |
---|
91 | return graph.toString(); |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * Adds a string to the graph's source (without newline). |
---|
96 | */ |
---|
97 | public void add(String line) { |
---|
98 | graph.append(line); |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * Adds a string to the graph's source (with newline). |
---|
103 | */ |
---|
104 | public void addln(String line) { |
---|
105 | graph.append(line + "\n"); |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * Adds a newline to the graph's source. |
---|
110 | */ |
---|
111 | public void addln() { |
---|
112 | graph.append('\n'); |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Returns the graph as an image in binary format. |
---|
117 | * @param dot_source Source of the graph to be drawn. |
---|
118 | * @return A byte array containing the image of the graph. |
---|
119 | */ |
---|
120 | public byte[] getGraph(String dot_source) |
---|
121 | { |
---|
122 | File dot; |
---|
123 | byte[] img_stream = null; |
---|
124 | |
---|
125 | try { |
---|
126 | dot = writeDotSourceToFile(dot_source); |
---|
127 | if (dot != null) |
---|
128 | { |
---|
129 | img_stream = get_img_stream(dot); |
---|
130 | if (dot.delete() == false) |
---|
131 | System.err.println("Warning: " + dot.getAbsolutePath() + " could not be deleted!"); |
---|
132 | return img_stream; |
---|
133 | } |
---|
134 | return null; |
---|
135 | } catch (java.io.IOException ioe) { return null; } |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * Writes the graph's image in a file. |
---|
140 | * @param img A byte array containing the image of the graph. |
---|
141 | * @param file Name of the file to where we want to write. |
---|
142 | * @return Success: 1, Failure: -1 |
---|
143 | */ |
---|
144 | public int writeGraphToFile(byte[] img, String file) |
---|
145 | { |
---|
146 | File to = new File(file); |
---|
147 | return writeGraphToFile(img, to); |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Writes the graph's image in a file. |
---|
152 | * @param img A byte array containing the image of the graph. |
---|
153 | * @param to A File object to where we want to write. |
---|
154 | * @return Success: 1, Failure: -1 |
---|
155 | */ |
---|
156 | public int writeGraphToFile(byte[] img, File to) |
---|
157 | { |
---|
158 | try { |
---|
159 | FileOutputStream fos = new FileOutputStream(to); |
---|
160 | fos.write(img); |
---|
161 | fos.close(); |
---|
162 | } catch (java.io.IOException ioe) { return -1; } |
---|
163 | return 1; |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * It will call the external dot program, and return the image in |
---|
168 | * binary format. |
---|
169 | * @param dot Source of the graph (in dot language). |
---|
170 | * @return The image of the graph in .gif format. |
---|
171 | */ |
---|
172 | private byte[] get_img_stream(File dot) |
---|
173 | { |
---|
174 | File img; |
---|
175 | byte[] img_stream = null; |
---|
176 | |
---|
177 | try { |
---|
178 | img = File.createTempFile("graph_", ".gif", new File(GraphViz.TEMP_DIR)); |
---|
179 | Runtime rt = Runtime.getRuntime(); |
---|
180 | |
---|
181 | // String cmd = DOT + " -Tgif "+dot.getAbsolutePath()+" -o"+img.getAbsolutePath(); |
---|
182 | // Process p = rt.exec(cmd); |
---|
183 | // patch by Mike Chenault |
---|
184 | String[] args = {DOT,"-Nshape=diamond","-Tgif", dot.getAbsolutePath(), "-o", img.getAbsolutePath()}; |
---|
185 | Process p = rt.exec(args); |
---|
186 | |
---|
187 | p.waitFor(); |
---|
188 | |
---|
189 | FileInputStream in = new FileInputStream(img.getAbsolutePath()); |
---|
190 | img_stream = new byte[in.available()]; |
---|
191 | in.read(img_stream); |
---|
192 | // Close it if we need to |
---|
193 | if( in != null ) in.close(); |
---|
194 | |
---|
195 | if (img.delete() == false) |
---|
196 | System.err.println("Warning: " + img.getAbsolutePath() + " could not be deleted!"); |
---|
197 | } |
---|
198 | catch (java.io.IOException ioe) { |
---|
199 | System.err.println("Error: in I/O processing of tempfile in dir " + GraphViz.TEMP_DIR+"\n"); |
---|
200 | System.err.println(" or in calling external command"); |
---|
201 | ioe.printStackTrace(); |
---|
202 | } |
---|
203 | catch (java.lang.InterruptedException ie) { |
---|
204 | System.err.println("Error: the execution of the external program was interrupted"); |
---|
205 | ie.printStackTrace(); |
---|
206 | } |
---|
207 | |
---|
208 | return img_stream; |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * Writes the source of the graph in a file, and returns the written file |
---|
213 | * as a File object. |
---|
214 | * @param str Source of the graph (in dot language). |
---|
215 | * @return The file (as a File object) that contains the source of the graph. |
---|
216 | */ |
---|
217 | private File writeDotSourceToFile(String str) throws java.io.IOException |
---|
218 | { |
---|
219 | File temp; |
---|
220 | try { |
---|
221 | temp = File.createTempFile("graph_", ".dot.tmp", new File(GraphViz.TEMP_DIR)); |
---|
222 | FileWriter fout = new FileWriter(temp); |
---|
223 | fout.write(str); |
---|
224 | fout.close(); |
---|
225 | } |
---|
226 | catch (Exception e) { |
---|
227 | System.err.println("Error: I/O error while writing the dot source to temp file!"); |
---|
228 | return null; |
---|
229 | } |
---|
230 | return temp; |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * Returns a string that is used to start a graph. |
---|
235 | * @return A string to open a graph. |
---|
236 | */ |
---|
237 | public String start_graph() { |
---|
238 | return "digraph G {"; |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * Returns a string that is used to end a graph. |
---|
243 | * @return A string to close a graph. |
---|
244 | */ |
---|
245 | public String end_graph() { |
---|
246 | return "}"; |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|