1 4 package com.opensymphony.webwork.webFlow; 5 6 import com.opensymphony.webwork.webFlow.renderers.DOTRenderer; 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 10 import java.io.*; 11 12 27 public class WebFlow { 28 29 private static final Log LOG = LogFactory.getLog(WebFlow.class); 30 31 private String configDir; 32 private String views; 33 private String output; 34 private String namespace; 35 private Writer writer; 36 37 public WebFlow(String configDir, String views, String output, String namespace) { 38 this.configDir = configDir; 39 this.views = views; 40 this.output = output; 41 this.namespace = namespace; 42 } 43 44 public static void main(String [] args) throws IOException { 45 LOG.info("WebFlow starting..."); 46 47 if (args.length != 8 && args.length != 6) { 48 InputStream is = WebFlow.class.getResourceAsStream("webflow-usage.txt"); 49 byte[] buffer = new byte[2048]; 50 int length = -1; 51 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 52 while ((length = is.read(buffer)) != -1) { 53 baos.write(buffer, 0, length); 54 } 55 is.close(); 56 baos.close(); 57 58 String usage = baos.toString(); 59 System.out.println(usage.replaceAll("//.*", "")); 60 return; 61 } 62 63 String configDir = getArg(args, "config"); 64 String views = getArg(args, "views"); 65 String output = getArg(args, "output"); 66 String namespace = getArg(args, "ns"); 67 68 WebFlow webFlow = new WebFlow(configDir, views, output, namespace); 70 webFlow.prepare(); 71 webFlow.render(); 72 } 74 75 private static String getArg(String [] args, String arg) { 76 for (int i = 0; i < args.length; i++) { 77 if (("-" + arg).equals(args[i]) && ((i + 1) < args.length)) { 78 return args[i + 1]; 79 } 80 } 81 82 return ""; 83 } 84 85 90 public void prepare() { 91 if (writer == null) { 92 try { 93 writer = new FileWriter(output + "/out.dot"); 94 } catch (IOException e) { 95 throw new RuntimeException (e); 96 } 97 } 98 99 XWorkConfigRetriever.setConfiguration(configDir, views.split("[, ]+")); 100 DOTRenderer renderer = new DOTRenderer(writer); 101 renderer.render(namespace); 102 } 103 104 109 public void render() { 110 try { 111 Runtime.getRuntime().exec("dot -o" + output + "/out.gif -Tgif " + output + "/out.dot"); 112 } catch (IOException e) { 113 LOG.error("Could not invoke dot", e); 114 } 115 } 116 117 public void setWriter(Writer writer) { 118 this.writer = writer; 119 } 120 } | Popular Tags |