1 package net.sf.mvnflexreports; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileOutputStream; 6 import java.io.InputStreamReader; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import org.apache.maven.doxia.sink.Sink; 11 import org.apache.maven.plugin.logging.Log; 12 13 /*** 14 * @author RĂ©mi Flament 15 * 16 */ 17 public abstract class AbstractGraphvizMavenReportGenerator { 18 19 protected Sink sink; 20 21 protected String fontName; 22 23 protected String fontSize; 24 25 /*** 26 * The program name for dot (can be dot, circo, neato, or any program that 27 * understand dot files) 28 */ 29 protected String programName; 30 31 protected String layout; 32 33 public String getLayout() { 34 return layout; 35 } 36 37 public void setLayout(String layout) { 38 this.layout = layout; 39 } 40 41 public Sink getSink() { 42 return sink; 43 } 44 45 public void setSink(Sink sink) { 46 this.sink = sink; 47 } 48 49 public String getFontName() { 50 return fontName; 51 } 52 53 public void setFontName(String fontName) { 54 this.fontName = fontName; 55 } 56 57 public String getFontSize() { 58 return fontSize; 59 } 60 61 public void setFontSize(String fontSize) { 62 this.fontSize = fontSize; 63 } 64 65 public String getProgramName() { 66 return programName; 67 } 68 69 public void setProgramName(String programName) { 70 this.programName = programName; 71 } 72 73 protected File invokeDot(String dot, String name, String outputDirectory, Log log) throws Exception { 74 75 File imagesDir = new File(outputDirectory + File.separator + "images"); 76 File output = new File(outputDirectory + File.separator + "images" + File.separator + name + ".png"); 77 imagesDir.mkdirs(); 78 79 File dotFile = File.createTempFile(name, ".dot"); 80 log.debug("Generating dot file in : " + dotFile.getAbsolutePath()); 81 FileOutputStream stream = new FileOutputStream(dotFile); 82 stream.write(dot.getBytes()); 83 stream.close(); 84 85 log.debug("Generating image file in : " + output.getAbsolutePath()); 86 87 List<String> commands = new ArrayList<String>(); 88 commands.add(programName); 89 commands.add("-Tpng"); 90 commands.add("-o"); 91 commands.add(output.getAbsolutePath()); 92 commands.add(dotFile.getAbsolutePath()); 93 94 ProcessBuilder builder = new ProcessBuilder(commands); 95 log.debug("Running command " + builder.command().toString()); 96 97 Process process = builder.redirectErrorStream(true).directory(null).start(); 98 99 int status = process.waitFor(); 100 101 if (status != 0) { 102 log.error("Command [" + builder.command().toString() + "] exited with status : " + status); 103 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); 104 String line = null; 105 while ((line = bufferedReader.readLine()) != null) { 106 log.error(line); 107 } 108 109 } 110 111 return output; 112 } 113 114 }