1 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 ; 16 import java.io.Writer ; 17 import java.util.*; 18 19 22 public class DOTRenderer { 23 24 private Writer writer; 25 private List links = new ArrayList(); 26 27 public DOTRenderer(Writer writer) { 28 this.writer = writer; 29 } 30 31 public void render(String ns) { 32 Graph graph = new Graph(); 33 34 TreeMap viewMap = new TreeMap(new Comparator() { 35 public int compare(Object o1, Object 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 namespace = (String ) 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 actionName = (String ) 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 resultName = (String ) iterator2.next(); 65 ResultConfig resultConfig = ((ResultConfig) actionConfig.getResults().get(resultName)); 66 String 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 location = getViewLocation((String ) resultConfig.getParams().get("location"), namespace); 78 if (location.endsWith((String ) 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 String location = getViewLocation((String ) resultConfig.getParams().get("location"), namespace); 98 if (location.endsWith((String ) 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 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 viewTarget = target.getTarget(); 125 addTempLink(view, viewTarget, target.getType(), ""); 126 } 127 } 128 129 for (Iterator iterator = links.iterator(); iterator.hasNext();) { 131 TempLink temp = (TempLink) iterator.next(); 132 String location = temp.location; 133 if (location.endsWith((String ) Configuration.get("webwork.action.extension"))) { 134 location = location.substring(0, location.indexOf((String ) 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 graph.render(new IndentWriter(writer)); 150 writer.flush(); 151 writer.close(); 152 } catch (IOException e) { 153 e.printStackTrace(); 154 } 155 } 156 157 private void addTempLink(WebFlowNode node, String location, int type, String label) { 158 links.add(new TempLink(node, location, type, label)); 159 } 160 161 private String stripLocation(String location) { 162 return location.substring(location.lastIndexOf('/') + 1); 163 } 164 165 private View getView(String namespace, String actionName, String resultName, String 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 getViewLocation(String location, String namespace) { 176 String 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 location; 193 int typeResult; 194 String label; 195 196 public TempLink(WebFlowNode node, String location, int typeResult, String label) { 197 this.node = node; 198 this.location = location; 199 this.typeResult = typeResult; 200 this.label = label; 201 } 202 203 public boolean equals(Object 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 |