1
1
package tfm .exec ;
2
2
3
3
import com .github .javaparser .ast .Node ;
4
- import guru .nidi .graphviz .engine .Format ;
5
- import guru .nidi .graphviz .engine .Graphviz ;
6
4
import tfm .graphs .Graph ;
7
5
import tfm .utils .FileUtil ;
8
6
import tfm .utils .Logger ;
9
7
10
- import java .io .File ;
11
- import java .io .IOException ;
8
+ import java .io .*;
12
9
13
10
public abstract class GraphLog <G extends Graph > {
11
+ public enum Format {
12
+ PNG ("png" ),
13
+ PDF ("pdf" );
14
+
15
+ private String ext ;
16
+ Format (String ext ) {
17
+ this .ext = ext ;
18
+ }
19
+
20
+ public String getExt () {
21
+ return ext ;
22
+ }
23
+ }
14
24
15
25
static final String CFG = "cfg" ;
16
26
static final String PDG = "pdg" ;
@@ -61,9 +71,22 @@ public void generateImages(String imageName, Format format) throws IOException {
61
71
this .imageName = imageName ;
62
72
this .format = format ;
63
73
generated = true ;
64
- Graphviz .fromString (graph .toGraphvizRepresentation ())
65
- .render (format )
66
- .toFile (getImageFile ());
74
+ File tmpDot = File .createTempFile ("graph-source-" , ".dot" );
75
+ tmpDot .deleteOnExit ();
76
+ try (Writer w = new FileWriter (tmpDot )) {
77
+ w .write (graph .toGraphvizRepresentation ());
78
+ }
79
+ ProcessBuilder pb = new ProcessBuilder ("dot" ,
80
+ tmpDot .getAbsolutePath (), "-T" + format .getExt (),
81
+ "-o" , getImageFile ().getAbsolutePath ());
82
+ try {
83
+ int result = pb .start ().waitFor ();
84
+ if (result != 0 ) {
85
+ Logger .log ("Image generation failed" );
86
+ }
87
+ } catch (InterruptedException e ) {
88
+ Logger .log ("Image generation failed\n " + e .getMessage ());
89
+ }
67
90
}
68
91
69
92
public void openVisualRepresentation () throws IOException {
@@ -72,6 +95,6 @@ public void openVisualRepresentation() throws IOException {
72
95
}
73
96
74
97
protected File getImageFile () {
75
- return new File ("./out/" + imageName + "." + format .name ());
98
+ return new File ("./out/" + imageName + "." + format .getExt ());
76
99
}
77
100
}
0 commit comments