Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* Soot - a J*va Optimization Framework
   * Copyright (C) 2002 Sable Research Group
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the
  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
 
 /*
  * Modified by the Sable Research Group and others 1997-1999.  
  * See the 'credits' file distributed with Soot for the complete list of
  * contributors.  (Soot is distributed at http://www.sable.mcgill.ca/soot)
  */


DotGraph provides an interface to SOOT for generating DOT language for graphviz from ATT research lab. Intended usage: virtualize CFG, graphes, etc...

Author(s):
Feng Qian
 
 
 package soot.util.dot;
 import java.io.*;
 import java.util.*;
 
 public class DotGraph implements Renderable{
   
   /* allow a serialized drawing, following steps:
    * 1. new DotGraph
    * 2. draw(Directed)Edge / drawUndirectedEdge
    *    attachAttributes, addNode
    * 3. plot
    */
   private String  graphname;
   private boolean isSubGraph;
 
   private HashMap<StringDotGraphNodenodes;
   /* draw elements are sub graphs, edges, commands */
   private List<Renderable>    drawElements;
  
   private List<DotGraphAttribute>    attributes;

  
The extension added to output files, exported so that clients can search for the filenames.
 
   public final static String DOT_EXTENSION = ".dot";

  
Creates a new graph for drawing.

Parameters:
graphname, the name used to identify the graph in the dot source.
 
   public DotGraph(String graphname) {
     this.   = graphname;
     this. = false;
     this.      = new HashMap<StringDotGraphNode>(100);
     this. = new LinkedList<Renderable>();
     this.   = new LinkedList<DotGraphAttribute>();
   }
  
  
Generates the drawing on canvas to the dot file.

Parameters:
filename the name for the output file. By convention, it should end with DOT_EXTENSION, but this is not enforced.
 
   public void plot(String filename) {
     try {
       BufferedOutputStream out = 
 	new BufferedOutputStream(new FileOutputStream(filename));
 							  
       render(out, 0);
       out.close();
     } catch (IOException ioe) {
     }
   }

  
Draws a directed edge (including the source and end nodes, if they have not already been drawn).

Parameters:
from, the source node
to, the end node
Returns:
a graph edge
 
   public DotGraphEdge drawEdge(String fromString to) {
 
    DotGraphNode src = drawNode(from);
    DotGraphNode dst = drawNode(to);
    DotGraphEdge edge = new DotGraphEdge(srcdst);
    
    this..add(edge);
    
    return edge;
  }

  
Draws a node.

Parameters:
name, the node to draw.
Returns:
the DotGraphNode corresponding to the specified name.
  public DotGraphNode drawNode(String name){
      DotGraphNode node = getNode(name);
      if(node == null)
          throw new RuntimeException("Assertion failed.");
      if(!this..contains(node))
          this..add(node);
      return node;
  }

  
Gets the graph node by name.

Parameters:
name, unique name of the node.
Returns:
the node with the specified name, or null if there is no such node.
  public DotGraphNode getNode(String name){
      DotGraphNode node = .get(name);
      if (node == null) {
          node = new DotGraphNode(name);
          .put(namenode);
      }
      return node;
  }

  
Sets all node shapes, see the list of node shapes in DotGraphConstants.

Parameters:
shape, the node shape
  public void setNodeShape(String shape){
    StringBuffer command = new StringBuffer("node [shape=");
    command.append(shape);
    command.append("];");
    this..add(new DotGraphCommand(new String(command)));
  }

  
Sets all node styles

Parameters:
style, the node style
  public void setNodeStyle(String style){
    StringBuffer command = new StringBuffer("node [style=");
    command.append(style);
    command.append("];");
    this..add(new DotGraphCommand(new String(command)));
  }

  
sets the size of drawing area, in inches
  public void setGraphSize(double widthdouble height){
    String size = "\""+width+","+height+"\"";
    this.setGraphAttribute("size"size);
  }

  
sets the pages size, once this is set, the generated graph will be broken into several pages.
  public void setPageSize(double widthdouble height){
    String size = "\""+width+", "+height+"\"";
    this.setGraphAttribute("page"size);
  }

  
sets the graph rotation angles
  public void setOrientation(String orientation){
    this.setGraphAttribute("orientation"orientation);
  }

  
sets the graph label
  public void setGraphLabel(String label){
    label = DotGraphUtility.replaceQuotes(label);
    label = DotGraphUtility.replaceReturns(label);
    this.setGraphAttribute("label""\""+label+"\"");
  }

  
sets any general attributes

Parameters:
id is the attribute name.
value is the attribute value.
  public void setGraphAttribute(String idString value){
    this.setGraphAttribute(new DotGraphAttribute(idvalue));    
  }

  
sets any general attributes

Parameters:
attr a DotGraphAttribute specifying the attribute name and value.
  public void setGraphAttribute(DotGraphAttribute attr){
    this..add(attr);    
  }

  
draws an undirected edge

Parameters:
label1, label2
  public void drawUndirectedEdge(String label1String label2) {
  }

  
creates a sub graph.

Returns:
the newly created sub graph.
  public DotGraph createSubGraph(String label){
    // file name is used as label of sub graph.
    DotGraph subgraph = new DotGraph(label);
    subgraph.isSubGraph = true;
    this..add(subgraph);
    return subgraph;
  }
  /* implements renderable interface. */
  public void render(OutputStream outint indentthrows IOException{
    // header
    String graphname = this.;
    if (!) {
      DotGraphUtility.renderLine(out"digraph \""+graphname+"\" {"indent);
    } else {
      DotGraphUtility.renderLine(out"subgraph \""+graphname+"\" {"indent);
    }
    /* render graph attributes */
    Iterator<DotGraphAttributeattrIt = this..iterator();
    while (attrIt.hasNext()) {
      DotGraphAttribute attr = attrIt.next();
      DotGraphUtility.renderLine(outattr.toString()+";"indent+4);
    }
    /* render elements */
    Iterator<RenderableelmntsIt = this..iterator();
    while (elmntsIt.hasNext()) {
      Renderable element = elmntsIt.next();
      element.render(outindent+4);
    }
    // close the description
    DotGraphUtility.renderLine(out"}"indent);
  }
New to GrepCode? Check out our FAQ X