001: /*
002: * Created on Aug 12, 2004 by mgreer
003: */
004: package com.opensymphony.webwork.sitegraph;
005:
006: import com.opensymphony.webwork.sitegraph.renderers.DOTRenderer;
007: import com.opensymphony.webwork.WebWorkException;
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010:
011: import java.io.*;
012:
013: /**
014: * // START SNIPPET: javadocs-intro
015: * SiteGraph is a tool that renders out GraphViz-generated images depicting your
016: * WebWork-powered web application's flow. SiteGraph requires GraphViz be installed
017: * and that the "dot" executable be in your command path. You can find GraphViz
018: * at http://www.graphviz.org.
019: * // END SNIPPET: javadocs-intro
020: * <p/>
021: * // START SNIPPET: javadocs-api
022: * If you wish to use SiteGraph through its API rather than through the command line,
023: * you can do that as well. All you need to do is create a new SiteGraph instance,
024: * optionally specify a {@link Writer} to output the dot content to, and then call
025: * {@link #prepare()}.
026: * // END SNIPPET: javadocs-api
027: */
028: public class SiteGraph {
029:
030: private static final Log LOG = LogFactory.getLog(SiteGraph.class);
031:
032: private String configDir;
033: private String views;
034: private String output;
035: private String namespace;
036: private Writer writer;
037:
038: public SiteGraph(String configDir, String views, String output,
039: String namespace) {
040: this .configDir = configDir;
041: this .views = views;
042: this .output = output;
043: this .namespace = namespace;
044: }
045:
046: public static void main(String[] args) throws IOException {
047: LOG.info("SiteGraph starting...");
048:
049: if (args.length != 8 && args.length != 6) {
050: InputStream is = SiteGraph.class
051: .getResourceAsStream("sitegraph-usage.txt");
052: byte[] buffer = new byte[2048];
053: int length = -1;
054: ByteArrayOutputStream baos = new ByteArrayOutputStream();
055: while ((length = is.read(buffer)) != -1) {
056: baos.write(buffer, 0, length);
057: }
058: is.close();
059: baos.close();
060:
061: String usage = baos.toString();
062: System.out.println(usage.replaceAll("//.*", ""));
063: return;
064: }
065:
066: String configDir = getArg(args, "config");
067: String views = getArg(args, "views");
068: String output = getArg(args, "output");
069: String namespace = getArg(args, "ns");
070:
071: // START SNIPPET: example-api
072: SiteGraph siteGraph = new SiteGraph(configDir, views, output,
073: namespace);
074: siteGraph.prepare();
075: siteGraph.render();
076: // END SNIPPET: example-api
077: }
078:
079: private static String getArg(String[] args, String arg) {
080: for (int i = 0; i < args.length; i++) {
081: if (("-" + arg).equals(args[i]) && ((i + 1) < args.length)) {
082: return args[i + 1];
083: }
084: }
085:
086: return "";
087: }
088:
089: /**
090: * Prepares the dot generated content and writes out to the provided writer
091: * object. If no writer has been given, that a {@link FileWriter} pointing to "out.dot"
092: * in the specified output directly shall be used.
093: */
094: public void prepare() {
095: if (writer == null) {
096: try {
097: writer = new FileWriter(output + "/out.dot");
098: } catch (IOException e) {
099: throw new WebWorkException(e);
100: }
101: }
102:
103: XWorkConfigRetriever.setConfiguration(configDir, views
104: .split("[, ]+"));
105: DOTRenderer renderer = new DOTRenderer(writer);
106: renderer.render(namespace);
107: }
108:
109: /**
110: * Invokes the dot command, cause GraphViz to render out.dot in the form of out.gif,
111: * located in the specified output directory. If an error occurs during this process,
112: * the error is logged and the method completes without throwing an exception.
113: */
114: public void render() {
115: try {
116: Runtime.getRuntime().exec(
117: "dot -o" + output + "/out.gif -Tgif " + output
118: + "/out.dot");
119: } catch (IOException e) {
120: LOG.error("Could not invoke dot", e);
121: }
122: }
123:
124: public void setWriter(Writer writer) {
125: this.writer = writer;
126: }
127: }
|