KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > webFlow > WebFlow


1 /*
2  * Created on Aug 12, 2004 by mgreer
3  */

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 /**
13  * // START SNIPPET: javadocs-intro
14  * WebFlow is a tool that renders out GraphViz-generated images depicting your
15  * WebWork-powered web application's flow. WebFlow requires GraphViz be installed
16  * and that the "dot" executable be in your command path. You can find GraphViz
17  * at http://www.graphviz.org.
18  * // END SNIPPET: javadocs-intro
19  * <p/>
20  * // START SNIPPET: javadocs-api
21  * If you wish to use WebFlow through its API rather than through the command line,
22  * you can do that as well. All you need to do is create a new WebFlow instance,
23  * optionally specify a {@link Writer} to output the dot content to, and then call
24  * {@link #prepare()}.
25  * // END SNIPPET: javadocs-api
26  */

27 public class WebFlow {
28
29     private static final Log LOG = LogFactory.getLog(WebFlow.class);
30
31     private String JavaDoc configDir;
32     private String JavaDoc views;
33     private String JavaDoc output;
34     private String JavaDoc namespace;
35     private Writer writer;
36
37     public WebFlow(String JavaDoc configDir, String JavaDoc views, String JavaDoc output, String JavaDoc 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 JavaDoc[] 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 JavaDoc usage = baos.toString();
59             System.out.println(usage.replaceAll("//.*", ""));
60             return;
61         }
62
63         String JavaDoc configDir = getArg(args, "config");
64         String JavaDoc views = getArg(args, "views");
65         String JavaDoc output = getArg(args, "output");
66         String JavaDoc namespace = getArg(args, "ns");
67
68         // START SNIPPET: example-api
69
WebFlow webFlow = new WebFlow(configDir, views, output, namespace);
70         webFlow.prepare();
71         webFlow.render();
72         // END SNIPPET: example-api
73
}
74
75     private static String JavaDoc getArg(String JavaDoc[] args, String JavaDoc 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     /**
86      * Prepares the dot generated content and writes out to the provided writer
87      * object. If no writer has been given, that a {@link FileWriter} pointing to "out.dot"
88      * in the specified output directly shall be used.
89      */

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 JavaDoc(e);
96             }
97         }
98
99         XWorkConfigRetriever.setConfiguration(configDir, views.split("[, ]+"));
100         DOTRenderer renderer = new DOTRenderer(writer);
101         renderer.render(namespace);
102     }
103
104     /**
105      * Invokes the dot command, cause GraphViz to render out.dot in the form of out.gif,
106      * located in the specified output directory. If an error occurs during this process,
107      * the error is logged and the method completes without throwing an exception.
108      */

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