package graph.dot; import graph.*; import java.io.*; import java.util.*; /** * Output a graph into a .DOT file format * * @author Michael Shilman (michaels@eecs.berkeley.edu) * @version $Id$ */ public class DotWriter { public static int s_dotIndex = AttributeManager.NO_INDEX; /** * Output a graph into a .DOT file format */ public static void write(Graph g, OutputStream s) throws IOException { DataOutputStream os = new DataOutputStream(s); os.writeChars("graph \"" + concat(g.name) + "\" {\n"); Node n; for(Enumeration e = g.nodes.elements(); e.hasMoreElements(); ) { n = (Node)e.nextElement(); writeNode(n, os); } for(Enumeration e = g.nodes.elements(); e.hasMoreElements(); ) { n = (Node)e.nextElement(); writeEdges(n, os); } os.writeChars("}\n"); } /** * Write out the properties of this node */ protected static void writeNode(Node n, DataOutputStream os) throws IOException { os.writeChars("\t" + concat(n.name) + " [ "); os.writeChars("pos=\"" + n.x + "," + n.y + "\""); //XXX actually get the properties os.writeChars(" ];\n"); } /** * Write out the outgoing edges of this node */ protected static void writeEdges(Node n, DataOutputStream os) throws IOException { Edge e; for(int i = 0; i < n.out.size(); i++) { e = (Edge)n.out.elementAt(i); os.writeChars("\t" + concat(e.tail.name) + (e.directed ? " -> " : " -- ") + concat(e.head.name) + "\n"); } } public static String concat(String s) { return ((s == null) ? s : s.replace(' ', '_')); } }