1 19 20 25 26 27 35 36 package soot.util.dot; 37 import soot.*; 38 39 import java.io.*; 40 import java.util.*; 41 42 public class DotGraph implements Renderable{ 43 44 50 private String graphname; 51 private boolean isSubGraph; 52 53 private HashMap nodes; 54 55 private List drawElements; 56 57 private List attributes; 58 59 63 public final static String DOT_EXTENSION = ".dot"; 64 65 69 public DotGraph(String graphname) { 70 this.graphname = graphname; 71 this.isSubGraph = false; 72 this.nodes = new HashMap(100); 73 this.drawElements = new LinkedList(); 74 this.attributes = new LinkedList(); 75 } 76 77 82 public void plot(String filename) { 83 try { 84 BufferedOutputStream out = 85 new BufferedOutputStream(new FileOutputStream(filename)); 86 87 render(out, 0); 88 out.close(); 89 } catch (IOException ioe) { 90 } 91 } 92 93 100 public DotGraphEdge drawEdge(String from, String to) { 101 102 DotGraphNode src = drawNode(from); 103 DotGraphNode dst = drawNode(to); 104 DotGraphEdge edge = new DotGraphEdge(src, dst); 105 106 this.drawElements.add(edge); 107 108 return edge; 109 } 110 111 117 public DotGraphNode drawNode(String name){ 118 DotGraphNode node = getNode(name); 119 120 if(node == null) 121 throw new RuntimeException ("Assertion failed."); 122 123 if(!this.drawElements.contains(node)) 124 this.drawElements.add(node); 125 126 return node; 127 } 128 129 135 public DotGraphNode getNode(String name){ 136 DotGraphNode node = (DotGraphNode)nodes.get(name); 137 if (node == null) { 138 node = new DotGraphNode(name); 139 nodes.put(name, node); 140 } 141 return node; 142 } 143 144 148 public void setNodeShape(String shape){ 149 StringBuffer command = new StringBuffer ("node [shape="); 150 command.append(shape); 151 command.append("];"); 152 this.drawElements.add(new DotGraphCommand(new String (command))); 153 } 154 155 159 public void setNodeStyle(String style){ 160 StringBuffer command = new StringBuffer ("node [style="); 161 command.append(style); 162 command.append("];"); 163 this.drawElements.add(new DotGraphCommand(new String (command))); 164 } 165 166 169 public void setGraphSize(double width, double height){ 170 String size = "\""+width+","+height+"\""; 171 this.setGraphAttribute("size", size); 172 } 173 174 178 public void setPageSize(double width, double height){ 179 String size = "\""+width+", "+height+"\""; 180 this.setGraphAttribute("page", size); 181 } 182 183 186 public void setOrientation(String orientation){ 187 this.setGraphAttribute("orientation", orientation); 188 } 189 190 193 public void setGraphLabel(String label){ 194 label = DotGraphUtility.replaceQuotes(label); 195 label = DotGraphUtility.replaceReturns(label); 196 this.setGraphAttribute("label", "\""+label+"\""); 197 } 198 199 204 public void setGraphAttribute(String id, String value){ 205 this.setGraphAttribute(new DotGraphAttribute(id, value)); 206 } 207 208 213 public void setGraphAttribute(DotGraphAttribute attr){ 214 this.attributes.add(attr); 215 } 216 217 221 public void drawUndirectedEdge(String label1, String label2) { 222 } 223 224 228 public DotGraph createSubGraph(String label){ 229 DotGraph subgraph = new DotGraph(label); 231 subgraph.isSubGraph = true; 232 233 this.drawElements.add(subgraph); 234 235 return subgraph; 236 } 237 238 239 public void render(OutputStream out, int indent) throws IOException{ 240 String graphname = this.graphname; 242 243 if (!isSubGraph) { 244 DotGraphUtility.renderLine(out, "digraph \""+graphname+"\" {", indent); 245 } else { 246 DotGraphUtility.renderLine(out, "subgraph \""+graphname+"\" {", indent); 247 } 248 249 250 Iterator attrIt = this.attributes.iterator(); 251 while (attrIt.hasNext()) { 252 DotGraphAttribute attr = (DotGraphAttribute)attrIt.next(); 253 DotGraphUtility.renderLine(out, attr.toString()+";", indent+4); 254 } 255 256 257 Iterator elmntsIt = this.drawElements.iterator(); 258 while (elmntsIt.hasNext()) { 259 Renderable element = (Renderable)elmntsIt.next(); 260 element.render(out, indent+4); 261 } 262 263 DotGraphUtility.renderLine(out, "}", indent); 265 } 266 } 267 | Popular Tags |