├── .classpath ├── .gitignore ├── .project ├── .settings ├── org.eclipse.jdt.core.prefs └── org.eclipse.m2e.core.prefs ├── README.md ├── SConstruct ├── config └── config.properties ├── pom.xml ├── sample └── simple.dot ├── src └── main │ └── java │ └── com │ └── github │ └── jabbalaci │ └── graphviz │ ├── GraphViz.java │ └── Proba.java └── tmp └── .gitignore /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bin/ 2 | target/ 3 | .sconsign.dblite 4 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | graphviz-java-api 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.m2e.core.maven2Nature 21 | org.eclipse.jdt.core.javanature 22 | 23 | 24 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.6 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 12 | org.eclipse.jdt.core.compiler.source=1.6 13 | -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **New (Jan 9, 2017):** This project is not maintained anymore. If you want 2 | to add something to it, make a fork. Thanks. 3 | 4 | GraphViz Java API 5 | ================= 6 | 7 | * Author: Laszlo Szathmary (jabba.laci@gmail.com) 8 | * Date: 2003--2016 9 | * License: GPL 2 or Apache 2 10 | 11 | GitHub: https://github.com/jabbalaci/graphviz-java-api 12 | 13 | 14 | With this Java class you can simply call dot from your Java programs. 15 | It is made to facilitate the usage of [GraphViz](http://www.graphviz.org/) 16 | in your Java programs. 17 | 18 | Example 19 | ------- 20 | Check out the file `Proba.java` to see how to use this simple API. 21 | 22 | How to compile the sources in command-line 23 | ------------------------------------------ 24 | You can use maven. The output will appear in the `target/` folder. 25 | 26 | $ mvn package 27 | 28 | Or, you can use SCons. The output will appear in the `bin/` folder. 29 | 30 | $ scons 31 | 32 | How to launch the example from command-line 33 | ------------------------------------------- 34 | If you used SCons for instance, then 35 | 36 | $ java -cp bin/ com/github/jabbalaci/graphviz/Proba 37 | 38 | It prints a dot source to the stdout, and creates its corresponding GIF 39 | representation in your temp folder. 40 | 41 | 42 | History 43 | ------- 44 | small changes are not listed... 45 | 46 | 0.6.1 Apr. 10, 2016 The eclipse project configuration was extended with the maven nature. 47 | (patch of Markus Keunecke) 48 | 0.6 Nov. 28, 2013 Representation type can be specified (dot, neato, fdp, sfdp, twopi, circo) 49 | (patch of Olivier Duplouy) 50 | 0.5.1 Mar. 18, 2013 Mac support (patch of Juan Hoyos) 51 | 0.5 Apr. 24, 2012 OS detection, start subgraph, read config file 52 | (patch of Abdur Rahman) 53 | 0.4.5 Apr. 16, 2011 Image dpi can be changed (patch of Peter Mueller). 54 | Apr. 10, 2011 Moving to GitHub. 55 | 0.4 Feb. 5, 2011 Patch of Keheliya Gallaba is added. Now you can 56 | specify the type of the output file: 57 | gif, dot, fig, pdf, ps, svg, png, etc. 58 | 0.3 Nov. 29, 2010 Windows support (only took 7 years :)) 59 | + ability to read DOT source from a file 60 | 0.2 July 22, 2010 simple bug fix 61 | 0.1 Dec. 4, 2003 first release 62 | 63 | 64 | Contributors 65 | ------------ 66 | Thanks to these people for their contributions: 67 | 68 | * Daniel Khashabi 69 | * Mike Chenault 70 | * Keheliya Gallaba 71 | * Peter Mueller 72 | * Abdur Rahman 73 | * Juan Hoyos 74 | * Olivier Duplouy 75 | * Markus Keunecke 76 | -------------------------------------------------------------------------------- /SConstruct: -------------------------------------------------------------------------------- 1 | Java("bin", "src") 2 | -------------------------------------------------------------------------------- /config/config.properties: -------------------------------------------------------------------------------- 1 | ############################################################## 2 | # Linux Configurations # 3 | ############################################################## 4 | # The dir. where temporary files will be created. 5 | tempDirForLinux = /tmp 6 | # Where is your dot program located? It will be called externally. 7 | dotForLinux = /usr/bin/dot 8 | 9 | ############################################################## 10 | # Windows Configurations # 11 | ############################################################## 12 | # The dir. where temporary files will be created. 13 | tempDirForWindows = c:/temp 14 | # Where is your dot program located? It will be called externally. 15 | dotForWindows = "c:/Program Files (x86)/Graphviz 2.28/bin/dot.exe" 16 | 17 | ############################################################## 18 | # Mac Configurations # 19 | ############################################################## 20 | # The dir. where temporary files will be created. 21 | tempDirForMacOSX = /tmp 22 | # Where is your dot program located? It will be called externally. 23 | dotForMacOSX = /usr/local/bin/dot -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.github.jabbalaci 8 | graphviz-java-api 9 | 1.0-SNAPSHOT 10 | 11 | 12 | https://github.com/jabbalaci/graphviz-java-api.git 13 | 14 | 15 | -------------------------------------------------------------------------------- /sample/simple.dot: -------------------------------------------------------------------------------- 1 | digraph G { 2 | A -> B; 3 | A -> C; 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/graphviz/GraphViz.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.graphviz; 2 | 3 | // GraphViz.java - a simple API to call dot from Java programs 4 | 5 | /*$Id$*/ 6 | /* 7 | ****************************************************************************** 8 | * * 9 | * (c) Copyright 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.BufferedReader; 29 | import java.io.DataInputStream; 30 | import java.io.File; 31 | import java.io.FileInputStream; 32 | import java.io.FileOutputStream; 33 | import java.io.FileWriter; 34 | import java.io.InputStreamReader; 35 | 36 | /** 37 | *
38 | *
Purpose: GraphViz Java API 39 | *
40 | * 41 | *
Description: 42 | *
With this Java class you can simply call dot 43 | * from your Java programs. 44 | *
Example usage: 45 | *
46 | *
 47 |  *    GraphViz gv = new GraphViz();
 48 |  *    gv.addln(gv.start_graph());
 49 |  *    gv.addln("A -> B;");
 50 |  *    gv.addln("A -> C;");
 51 |  *    gv.addln(gv.end_graph());
 52 |  *    System.out.println(gv.getDotSource());
 53 |  *
 54 |  *    String type = "gif";
 55 |  *    String representationType="dot";
 56 |  *    File out = new File("out." + type);   // out.gif in this example
 57 |  *    gv.writeGraphToFile( gv.getGraph(gv.getDotSource(), type, representationType), out );
 58 |  * 
59 | *
60 | * 61 | *
62 | * 63 | * @version v0.6.1, 2016/04/10 (April) -- Patch of Markus Keunecke is added. 64 | * The eclipse project configuration was extended with the maven nature. 65 | * @version v0.6, 2013/11/28 (November) -- Patch of Olivier Duplouy is added. Now you 66 | * can specify the representation type of your graph: dot, neato, fdp, sfdp, twopi, circo 67 | * @version v0.5.1, 2013/03/18 (March) -- Patch of Juan Hoyos (Mac support) 68 | * @version v0.5, 2012/04/24 (April) -- Patch of Abdur Rahman (OS detection + start subgraph + 69 | * read config file) 70 | * @version v0.4, 2011/02/05 (February) -- Patch of Keheliya Gallaba is added. Now you 71 | * can specify the type of the output file: gif, dot, fig, pdf, ps, svg, png, etc. 72 | * @version v0.3, 2010/11/29 (November) -- Windows support + ability to read the graph from a text file 73 | * @version v0.2, 2010/07/22 (July) -- bug fix 74 | * @version v0.1, 2003/12/04 (December) -- first release 75 | * @author Laszlo Szathmary (jabba.laci@gmail.com) 76 | */ 77 | public class GraphViz 78 | { 79 | /** 80 | * Detects the client's operating system. 81 | */ 82 | private final static String osName = System.getProperty("os.name").replaceAll("\\s",""); 83 | 84 | /** 85 | * The image size in dpi. 96 dpi is normal size. Higher values are 10% higher each. 86 | * Lower values 10% lower each. 87 | * 88 | * dpi patch by Peter Mueller 89 | */ 90 | private final int[] dpiSizes = {46, 51, 57, 63, 70, 78, 86, 96, 106, 116, 128, 141, 155, 170, 187, 206, 226, 249}; 91 | 92 | /** 93 | * Define the index in the image size array. 94 | */ 95 | private int currentDpiPos = 7; 96 | 97 | /** 98 | * Increase the image size (dpi). 99 | */ 100 | public void increaseDpi() { 101 | if ( this.currentDpiPos < (this.dpiSizes.length - 1) ) { 102 | ++this.currentDpiPos; 103 | } 104 | } 105 | 106 | /** 107 | * Decrease the image size (dpi). 108 | */ 109 | public void decreaseDpi() { 110 | if (this.currentDpiPos > 0) { 111 | --this.currentDpiPos; 112 | } 113 | } 114 | 115 | public int getImageDpi() { 116 | return this.dpiSizes[this.currentDpiPos]; 117 | } 118 | 119 | /** 120 | * The source of the graph written in dot language. 121 | */ 122 | private StringBuilder graph = new StringBuilder(); 123 | 124 | private String tempDir; 125 | 126 | private String executable; 127 | 128 | /** 129 | * Convenience Constructor with default OS specific pathes 130 | * creates a new GraphViz object that will contain a graph. 131 | * Windows: 132 | * executable = c:/Program Files (x86)/Graphviz 2.28/bin/dot.exe 133 | * tempDir = c:/temp 134 | * MacOs: 135 | * executable = /usr/local/bin/dot 136 | * tempDir = /tmp 137 | * Linux: 138 | * executable = /usr/bin/dot 139 | * tempDir = /tmp 140 | */ 141 | public GraphViz() { 142 | if (GraphViz.osName.contains("Windows")) { 143 | this.tempDir = "c:/temp"; 144 | this.executable = "c:/Program Files (x86)/Graphviz 2.28/bin/dot.exe"; 145 | } else if (GraphViz.osName.equals("MacOSX")) { 146 | this.tempDir = "/tmp"; 147 | this.executable = "/usr/local/bin/dot"; 148 | } else if (GraphViz.osName.equals("Linux")) { 149 | this.tempDir = "/tmp"; 150 | this.executable = "/usr/bin/dot"; 151 | } 152 | } 153 | 154 | /** 155 | * Configurable Constructor with path to executable dot and a temp dir 156 | * 157 | * @param executable absolute path to dot executable 158 | * @param tempDir absolute path to temp directory 159 | */ 160 | public GraphViz(String executable, String tempDir) { 161 | this.executable = executable; 162 | this.tempDir = tempDir; 163 | } 164 | 165 | /** 166 | * Returns the graph's source description in dot language. 167 | * @return Source of the graph in dot language. 168 | */ 169 | public String getDotSource() { 170 | return this.graph.toString(); 171 | } 172 | 173 | /** 174 | * Adds a string to the graph's source (without newline). 175 | */ 176 | public void add(String line) { 177 | this.graph.append(line); 178 | } 179 | 180 | /** 181 | * Adds a string to the graph's source (with newline). 182 | */ 183 | public void addln(String line) { 184 | this.graph.append(line + "\n"); 185 | } 186 | 187 | /** 188 | * Adds a newline to the graph's source. 189 | */ 190 | public void addln() { 191 | this.graph.append('\n'); 192 | } 193 | 194 | public void clearGraph(){ 195 | this.graph = new StringBuilder(); 196 | } 197 | 198 | /** 199 | * Returns the graph as an image in binary format. 200 | * @param dot_source Source of the graph to be drawn. 201 | * @param type Type of the output image to be produced, e.g.: gif, dot, fig, pdf, ps, svg, png. 202 | * @param representationType Type of how you want to represent the graph: 203 | * 211 | * @see http://www.graphviz.org under the Roadmap title 212 | * @return A byte array containing the image of the graph. 213 | */ 214 | public byte[] getGraph(String dot_source, String type, String representationType) 215 | { 216 | File dot; 217 | byte[] img_stream = null; 218 | 219 | try { 220 | dot = writeDotSourceToFile(dot_source); 221 | if (dot != null) 222 | { 223 | img_stream = get_img_stream(dot, type, representationType); 224 | if (dot.delete() == false) { 225 | System.err.println("Warning: " + dot.getAbsolutePath() + " could not be deleted!"); 226 | } 227 | return img_stream; 228 | } 229 | return null; 230 | } catch (java.io.IOException ioe) { return null; } 231 | } 232 | 233 | /** 234 | * Writes the graph's image in a file. 235 | * @param img A byte array containing the image of the graph. 236 | * @param file Name of the file to where we want to write. 237 | * @return Success: 1, Failure: -1 238 | */ 239 | public int writeGraphToFile(byte[] img, String file) 240 | { 241 | File to = new File(file); 242 | return writeGraphToFile(img, to); 243 | } 244 | 245 | /** 246 | * Writes the graph's image in a file. 247 | * @param img A byte array containing the image of the graph. 248 | * @param to A File object to where we want to write. 249 | * @return Success: 1, Failure: -1 250 | */ 251 | public int writeGraphToFile(byte[] img, File to) 252 | { 253 | try { 254 | FileOutputStream fos = new FileOutputStream(to); 255 | fos.write(img); 256 | fos.close(); 257 | } catch (java.io.IOException ioe) { return -1; } 258 | return 1; 259 | } 260 | 261 | /** 262 | * It will call the external dot program, and return the image in 263 | * binary format. 264 | * @param dot Source of the graph (in dot language). 265 | * @param type Type of the output image to be produced, e.g.: gif, dot, fig, pdf, ps, svg, png. 266 | * @param representationType Type of how you want to represent the graph: 267 | * 275 | * @see http://www.graphviz.org under the Roadmap title 276 | * @return The image of the graph in .gif format. 277 | */ 278 | private byte[] get_img_stream(File dot, String type, String representationType) 279 | { 280 | File img; 281 | byte[] img_stream = null; 282 | 283 | try { 284 | img = File.createTempFile("graph_", "." + type, new File(this.tempDir)); 285 | Runtime rt = Runtime.getRuntime(); 286 | 287 | // patch by Mike Chenault 288 | // representation type with -K argument by Olivier Duplouy 289 | String[] args = { executable, "-T" + type, "-K" + representationType, "-Gdpi=" + dpiSizes[this.currentDpiPos], dot.getAbsolutePath(), "-o", img.getAbsolutePath() }; 290 | Process p = rt.exec(args); 291 | p.waitFor(); 292 | 293 | FileInputStream in = new FileInputStream(img.getAbsolutePath()); 294 | img_stream = new byte[in.available()]; 295 | in.read(img_stream); 296 | // Close it if we need to 297 | if( in != null ) { 298 | in.close(); 299 | } 300 | 301 | if (img.delete() == false) { 302 | System.err.println("Warning: " + img.getAbsolutePath() + " could not be deleted!"); 303 | } 304 | } 305 | catch (java.io.IOException ioe) { 306 | System.err.println("Error: in I/O processing of tempfile in dir " + tempDir + "\n"); 307 | System.err.println(" or in calling external command"); 308 | ioe.printStackTrace(); 309 | } 310 | catch (java.lang.InterruptedException ie) { 311 | System.err.println("Error: the execution of the external program was interrupted"); 312 | ie.printStackTrace(); 313 | } 314 | 315 | return img_stream; 316 | } 317 | 318 | /** 319 | * Writes the source of the graph in a file, and returns the written file 320 | * as a File object. 321 | * @param str Source of the graph (in dot language). 322 | * @return The file (as a File object) that contains the source of the graph. 323 | */ 324 | private File writeDotSourceToFile(String str) throws java.io.IOException 325 | { 326 | File temp; 327 | try { 328 | temp = File.createTempFile("graph_", ".dot.tmp", new File(tempDir)); 329 | FileWriter fout = new FileWriter(temp); 330 | fout.write(str); 331 | fout.close(); 332 | } 333 | catch (Exception e) { 334 | System.err.println("Error: I/O error while writing the dot source to temp file!"); 335 | return null; 336 | } 337 | return temp; 338 | } 339 | 340 | /** 341 | * Returns a string that is used to start a graph. 342 | * @return A string to open a graph. 343 | */ 344 | public String start_graph() { 345 | return "digraph G {"; 346 | } 347 | 348 | /** 349 | * Returns a string that is used to end a graph. 350 | * @return A string to close a graph. 351 | */ 352 | public String end_graph() { 353 | return "}"; 354 | } 355 | 356 | /** 357 | * Takes the cluster or subgraph id as input parameter and returns a string 358 | * that is used to start a subgraph. 359 | * @return A string to open a subgraph. 360 | */ 361 | public String start_subgraph(int clusterid) { 362 | return "subgraph cluster_" + clusterid + " {"; 363 | } 364 | 365 | /** 366 | * Returns a string that is used to end a graph. 367 | * @return A string to close a graph. 368 | */ 369 | public String end_subgraph() { 370 | return "}"; 371 | } 372 | 373 | /** 374 | * Read a DOT graph from a text file. 375 | * 376 | * @param input Input text file containing the DOT graph 377 | * source. 378 | */ 379 | public void readSource(String input) 380 | { 381 | StringBuilder sb = new StringBuilder(); 382 | 383 | try 384 | { 385 | FileInputStream fis = new FileInputStream(input); 386 | DataInputStream dis = new DataInputStream(fis); 387 | BufferedReader br = new BufferedReader(new InputStreamReader(dis)); 388 | String line; 389 | while ((line = br.readLine()) != null) { 390 | sb.append(line); 391 | } 392 | dis.close(); 393 | } 394 | catch (Exception e) { 395 | System.err.println("Error: " + e.getMessage()); 396 | } 397 | 398 | this.graph = sb; 399 | } 400 | 401 | } // end of class GraphViz 402 | -------------------------------------------------------------------------------- /src/main/java/com/github/jabbalaci/graphviz/Proba.java: -------------------------------------------------------------------------------- 1 | package com.github.jabbalaci.graphviz; 2 | 3 | import java.io.File; 4 | 5 | public class Proba 6 | { 7 | public static void main(String[] args) 8 | { 9 | Proba p = new Proba(); 10 | p.start(); 11 | // p.start2(); 12 | } 13 | 14 | /** 15 | * Construct a DOT graph in memory, convert it 16 | * to image and store the image in the file system. 17 | */ 18 | private void start() 19 | { 20 | GraphViz gv = new GraphViz(); 21 | gv.addln(gv.start_graph()); 22 | gv.addln("A -> B;"); 23 | gv.addln("A -> C;"); 24 | gv.addln(gv.end_graph()); 25 | System.out.println(gv.getDotSource()); 26 | 27 | gv.increaseDpi(); // 106 dpi 28 | 29 | String type = "gif"; 30 | // String type = "dot"; 31 | // String type = "fig"; // open with xfig 32 | // String type = "pdf"; 33 | // String type = "ps"; 34 | // String type = "svg"; // open with inkscape 35 | // String type = "png"; 36 | // String type = "plain"; 37 | 38 | String repesentationType= "dot"; 39 | // String repesentationType= "neato"; 40 | // String repesentationType= "fdp"; 41 | // String repesentationType= "sfdp"; 42 | // String repesentationType= "twopi"; 43 | // String repesentationType= "circo"; 44 | 45 | File out = new File("/tmp/out"+gv.getImageDpi()+"."+ type); // Linux 46 | // File out = new File("c:/eclipse.ws/graphviz-java-api/out." + type); // Windows 47 | gv.writeGraphToFile( gv.getGraph(gv.getDotSource(), type, repesentationType), out ); 48 | } 49 | 50 | /** 51 | * Read the DOT source from a file, 52 | * convert to image and store the image in the file system. 53 | */ 54 | private void start2() 55 | { 56 | String dir = "/home/jabba/Dropbox/git.projects/laszlo.own/graphviz-java-api"; // Linux 57 | String input = dir + "/sample/simple.dot"; 58 | // String input = "c:/eclipse.ws/graphviz-java-api/sample/simple.dot"; // Windows 59 | 60 | GraphViz gv = new GraphViz(); 61 | gv.readSource(input); 62 | System.out.println(gv.getDotSource()); 63 | 64 | String type = "gif"; 65 | // String type = "dot"; 66 | // String type = "fig"; // open with xfig 67 | // String type = "pdf"; 68 | // String type = "ps"; 69 | // String type = "svg"; // open with inkscape 70 | // String type = "png"; 71 | // String type = "plain"; 72 | 73 | 74 | String repesentationType= "dot"; 75 | // String repesentationType= "neato"; 76 | // String repesentationType= "fdp"; 77 | // String repesentationType= "sfdp"; 78 | // String repesentationType= "twopi"; 79 | // String repesentationType= "circo"; 80 | 81 | File out = new File("/tmp/simple." + type); // Linux 82 | // File out = new File("c:/eclipse.ws/graphviz-java-api/tmp/simple." + type); // Windows 83 | gv.writeGraphToFile( gv.getGraph(gv.getDotSource(), type, repesentationType), out ); 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /tmp/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | --------------------------------------------------------------------------------