001 package org.kuali.maven.plugins.graph.dot; 002 003 import java.io.File; 004 import java.io.IOException; 005 import java.util.ArrayList; 006 import java.util.List; 007 008 import org.apache.commons.io.FileUtils; 009 import org.apache.commons.io.FilenameUtils; 010 import org.codehaus.plexus.util.cli.CommandLineException; 011 import org.codehaus.plexus.util.cli.CommandLineUtils; 012 import org.codehaus.plexus.util.cli.Commandline; 013 import org.codehaus.plexus.util.cli.DefaultConsumer; 014 import org.codehaus.plexus.util.cli.StreamConsumer; 015 import org.kuali.maven.plugins.graph.pojo.DotContext; 016 import org.slf4j.Logger; 017 import org.slf4j.LoggerFactory; 018 019 public class Dot { 020 private static final Logger logger = LoggerFactory.getLogger(Dot.class); 021 022 public DotContext getDotContext(File graph, String content, boolean keepDotFile) { 023 File dotFile = createDotFile(graph, content); 024 String type = getType(graph); 025 026 DotContext context = new DotContext(); 027 context.setGraph(graph); 028 context.setDotFile(dotFile); 029 context.setType(type); 030 context.setKeepDotFile(keepDotFile); 031 return context; 032 } 033 034 protected String[] getArgs(DotContext context) { 035 List<String> args = new ArrayList<String>(); 036 args.add("-T" + context.getType()); 037 args.add("-o" + context.getGraph().getAbsolutePath()); 038 args.add(context.getDotFile().getAbsolutePath()); 039 return args.toArray(new String[args.size()]); 040 } 041 042 protected Commandline getCommandLine(DotContext context) { 043 Commandline commandline = new Commandline(); 044 try { 045 commandline.addSystemEnvironment(); 046 } catch (Exception ignore) { 047 ; // ignore 048 } 049 commandline.setExecutable(context.getExecutable()); 050 commandline.addArguments(getArgs(context)); 051 return commandline; 052 } 053 054 protected int execute(Commandline commandLine, DotContext context) { 055 try { 056 StreamConsumer stdout = new DefaultConsumer(); 057 StreamConsumer stderr = new DefaultConsumer(); 058 int exitValue = CommandLineUtils.executeCommandLine(commandLine, stdout, stderr); 059 if (exitValue != 0) { 060 throw new GraphException(getErrorMessage(context, exitValue)); 061 } 062 return exitValue; 063 } catch (CommandLineException e) { 064 throw new GraphException(e); 065 } 066 } 067 068 protected String getErrorMessage(DotContext context, int exitValue) { 069 StringBuilder sb = new StringBuilder(); 070 sb.append("Execution of the '" + context.getExecutable() + "' command failed."); 071 sb.append(" Exit value: '" + exitValue + "'"); 072 sb.append(" Is it installed? See: http://www.graphviz.org"); 073 return sb.toString(); 074 } 075 076 public void execute(DotContext context) { 077 Commandline commandline = getCommandLine(context); 078 execute(commandline, context); 079 if (!context.isKeepDotFile()) { 080 context.getDotFile().delete(); 081 } else { 082 logger.info(context.getDotFile().getPath()); 083 } 084 logger.info(context.getGraph().getPath()); 085 } 086 087 protected File createDotFile(File graph, String content) { 088 File dotFile = getDotFile(graph); 089 try { 090 FileUtils.write(dotFile, content); 091 return dotFile; 092 } catch (IOException e) { 093 throw new GraphException(e); 094 } 095 } 096 097 protected String getType(File graph) { 098 return FilenameUtils.getExtension(graph.getName()); 099 } 100 101 protected File getDotFile(File graph) { 102 File dir = graph.getParentFile(); 103 String path = dir.getAbsolutePath(); 104 String basename = FilenameUtils.getBaseName(graph.getName()); 105 String extension = "dot"; 106 String filename = path + File.separator + basename + "." + extension; 107 return new File(filename); 108 } 109 110 }