KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > webFlow > renderers > DOTRenderer


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

4 package com.opensymphony.webwork.webFlow.renderers;
5
6 import com.opensymphony.webwork.config.Configuration;
7 import com.opensymphony.webwork.webFlow.XWorkConfigRetriever;
8 import com.opensymphony.webwork.webFlow.entities.Target;
9 import com.opensymphony.webwork.webFlow.entities.View;
10 import com.opensymphony.webwork.webFlow.model.*;
11 import com.opensymphony.xwork.ActionChainResult;
12 import com.opensymphony.xwork.config.entities.ActionConfig;
13 import com.opensymphony.xwork.config.entities.ResultConfig;
14
15 import java.io.IOException JavaDoc;
16 import java.io.Writer JavaDoc;
17 import java.util.*;
18
19 /**
20  * Renders flow diagram to the console at info level
21  */

22 public class DOTRenderer {
23
24     private Writer JavaDoc writer;
25     private List links = new ArrayList();
26
27     public DOTRenderer(Writer JavaDoc writer) {
28         this.writer = writer;
29     }
30
31     public void render(String JavaDoc ns) {
32         Graph graph = new Graph();
33
34         TreeMap viewMap = new TreeMap(new Comparator() {
35             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
36                 ViewNode v1 = (ViewNode) o1;
37                 ViewNode v2 = (ViewNode) o2;
38
39                 return v1.getFullName().compareTo(v2.getFullName());
40             }
41         });
42
43         Set namespaces = XWorkConfigRetriever.getNamespaces();
44         for (Iterator iter = namespaces.iterator(); iter.hasNext();) {
45             String JavaDoc namespace = (String JavaDoc) iter.next();
46
47             if (!namespace.startsWith(ns)) {
48                 continue;
49             }
50
51             SubGraph subGraph = graph.create(namespace);
52
53             Set actionNames = XWorkConfigRetriever.getActionNames(namespace);
54             for (Iterator iterator = actionNames.iterator(); iterator.hasNext();) {
55                 String JavaDoc actionName = (String JavaDoc) iterator.next();
56                 ActionConfig actionConfig = XWorkConfigRetriever.getActionConfig(namespace,
57                         actionName);
58
59                 ActionNode action = new ActionNode(actionName);
60                 subGraph.addNode(action);
61
62                 Set resultNames = actionConfig.getResults().keySet();
63                 for (Iterator iterator2 = resultNames.iterator(); iterator2.hasNext();) {
64                     String JavaDoc resultName = (String JavaDoc) iterator2.next();
65                     ResultConfig resultConfig = ((ResultConfig) actionConfig.getResults().get(resultName));
66                     String JavaDoc resultClassName = resultConfig.getClassName();
67
68                     if (resultClassName.equals(ActionChainResult.class.getName())) {
69
70                     } else if (resultClassName.indexOf("Dispatcher") != -1
71                             || resultClassName.indexOf("Velocity") != -1
72                             || resultClassName.indexOf("Freemarker") != -1) {
73                         if (resultConfig.getParams().get("location") == null) {
74                             continue;
75                         }
76
77                         String JavaDoc location = getViewLocation((String JavaDoc) resultConfig.getParams().get("location"), namespace);
78                         if (location.endsWith((String JavaDoc) Configuration.get("webwork.action.extension"))) {
79                             addTempLink(action, location, Link.TYPE_RESULT, resultConfig.getName());
80                         } else {
81                             ViewNode view = new ViewNode(stripLocation(location));
82                             subGraph.addNode(view);
83
84                             addTempLink(action, location, Link.TYPE_RESULT, resultConfig.getName());
85
86                             View viewFile = getView(namespace, actionName, resultName, location);
87                             if (viewFile != null) {
88                                 viewMap.put(view, viewFile);
89                             }
90                         }
91                     } else if (resultClassName.indexOf("Jasper") != -1) {
92
93                     } else if (resultClassName.indexOf("XSLT") != -1) {
94
95                     } else if (resultClassName.indexOf("Redirect") != -1) {
96                         // check if the redirect is to an action -- if so, link it
97
String JavaDoc location = getViewLocation((String JavaDoc) resultConfig.getParams().get("location"), namespace);
98                         if (location.endsWith((String JavaDoc) Configuration.get("webwork.action.extension"))) {
99                             addTempLink(action, location, Link.TYPE_REDIRECT, resultConfig.getName());
100                         } else {
101                             ViewNode view = new ViewNode(stripLocation(location));
102                             subGraph.addNode(view);
103
104                             addTempLink(action, location, Link.TYPE_REDIRECT, resultConfig.getName());
105
106                             View viewFile = getView(namespace, actionName, resultName, location);
107                             if (viewFile != null) {
108                                 viewMap.put(view, viewFile);
109                             }
110                         }
111                     }
112                 }
113             }
114         }
115
116         // now look for links in the view
117
for (Iterator iterator = viewMap.entrySet().iterator(); iterator.hasNext();) {
118             Map.Entry entry = (Map.Entry) iterator.next();
119             ViewNode view = (ViewNode) entry.getKey();
120             View viewFile = (View) entry.getValue();
121             Set targets = viewFile.getTargets();
122             for (Iterator iterator1 = targets.iterator(); iterator1.hasNext();) {
123                 Target target = (Target) iterator1.next();
124                 String JavaDoc viewTarget = target.getTarget();
125                 addTempLink(view, viewTarget, target.getType(), "");
126             }
127         }
128
129         // finally, let's match up these links as real Link objects
130
for (Iterator iterator = links.iterator(); iterator.hasNext();) {
131             TempLink temp = (TempLink) iterator.next();
132             String JavaDoc location = temp.location;
133             if (location.endsWith((String JavaDoc) Configuration.get("webwork.action.extension"))) {
134                 location = location.substring(0, location.indexOf((String JavaDoc) Configuration.get("webwork.action.extension")) - 1);
135
136                 if (location.indexOf('!') != -1) {
137                     temp.label = temp.label + "\\n(" + location.substring(location.indexOf('!')) + ")";
138                     location = location.substring(0, location.indexOf('!'));
139                 }
140             }
141             WebFlowNode to = graph.findNode(location, temp.node);
142             if (to != null) {
143                 graph.addLink(new Link(temp.node, to, temp.typeResult, temp.label));
144             }
145         }
146
147         try {
148             //writer.write(graph.to_s(true));
149
graph.render(new IndentWriter(writer));
150             writer.flush();
151             writer.close();
152         } catch (IOException JavaDoc e) {
153             e.printStackTrace();
154         }
155     }
156
157     private void addTempLink(WebFlowNode node, String JavaDoc location, int type, String JavaDoc label) {
158         links.add(new TempLink(node, location, type, label));
159     }
160
161     private String JavaDoc stripLocation(String JavaDoc location) {
162         return location.substring(location.lastIndexOf('/') + 1);
163     }
164
165     private View getView(String JavaDoc namespace, String JavaDoc actionName, String JavaDoc resultName, String JavaDoc location) {
166         int type = View.TYPE_JSP;
167         if (location.endsWith(".fm") || location.endsWith(".ftl")) {
168             type = View.TYPE_FTL;
169         } else if (location.endsWith(".vm")) {
170             type = View.TYPE_VM;
171         }
172         return XWorkConfigRetriever.getView(namespace, actionName, resultName, type);
173     }
174
175     private String JavaDoc getViewLocation(String JavaDoc location, String JavaDoc namespace) {
176         String JavaDoc view = null;
177         if (!location.startsWith("/")) {
178             view = namespace + "/" + location;
179         } else {
180             view = location;
181         }
182
183         if (view.indexOf('?') != -1) {
184             view = view.substring(0, view.indexOf('?'));
185         }
186
187         return view;
188     }
189
190     class TempLink {
191         WebFlowNode node;
192         String JavaDoc location;
193         int typeResult;
194         String JavaDoc label;
195
196         public TempLink(WebFlowNode node, String JavaDoc location, int typeResult, String JavaDoc label) {
197             this.node = node;
198             this.location = location;
199             this.typeResult = typeResult;
200             this.label = label;
201         }
202
203         public boolean equals(Object JavaDoc o) {
204             if (this == o) return true;
205             if (!(o instanceof TempLink)) return false;
206
207             final TempLink tempLink = (TempLink) o;
208
209             if (typeResult != tempLink.typeResult) return false;
210             if (label != null ? !label.equals(tempLink.label) : tempLink.label != null) return false;
211             if (location != null ? !location.equals(tempLink.location) : tempLink.location != null) return false;
212             if (node != null ? !node.equals(tempLink.node) : tempLink.node != null) return false;
213
214             return true;
215         }
216
217         public int hashCode() {
218             int result;
219             result = (node != null ? node.hashCode() : 0);
220             result = 29 * result + (location != null ? location.hashCode() : 0);
221             result = 29 * result + typeResult;
222             result = 29 * result + (label != null ? label.hashCode() : 0);
223             return result;
224         }
225     }
226 }
Popular Tags