KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > util > dot > DotGraphEdge


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2002 Sable Research Group
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28 package soot.util.dot;
29
30 import java.io.*;
31 import java.util.*;
32
33 /* Graph edges are the major elements of a graph
34  * @author Feng Qian
35  */

36 public class DotGraphEdge implements Renderable {
37   private boolean isDirected;
38   private DotGraphNode start, end;
39   private List attributes;
40
41   /**
42    * Draws a directed edge.
43    * @param src, the source node
44    * @param dst, the end node
45    */

46   public DotGraphEdge(DotGraphNode src, DotGraphNode dst){
47     this.start = src;
48     this.end = dst;
49     this.isDirected = true;
50   }
51
52   /**
53    * Draws a graph edge by specifying directed or undirected.
54    * @param src, the source node
55    * @param dst, the end node
56    * @param directed, the edge is directed or not
57    */

58   public DotGraphEdge(DotGraphNode src, DotGraphNode dst, boolean directed){
59     this.start = src;
60     this.end = dst;
61     this.isDirected = directed;
62   }
63
64   /**
65    * Sets the label for the edge.
66    * @param label, a label string
67    */

68   public void setLabel(String JavaDoc label){
69     label = DotGraphUtility.replaceQuotes(label);
70     label = DotGraphUtility.replaceReturns(label);
71     this.setAttribute("label", "\""+label+"\"");
72   }
73
74   /**
75    * Sets the edge style.
76    * @param style, a style of edge
77    * @see DotGraphConstants
78    */

79   public void setStyle(String JavaDoc style){
80     this.setAttribute("style", style);
81   }
82
83   /**
84    * Sets an edge attribute.
85    * @param id, the attribute id to be set
86    * @param value, the attribute value
87    */

88   public void setAttribute(String JavaDoc id, String JavaDoc value) {
89     this.setAttribute(new DotGraphAttribute(id, value));
90   }
91
92   /**
93    * Sets an edge attribute.
94    * @param attr, a {@link DotGraphAttribute} specifying the
95    * attribute name and value.
96    */

97   public void setAttribute(DotGraphAttribute attr) {
98     if (this.attributes == null) {
99       this.attributes = new LinkedList();
100     }
101     
102     this.attributes.add(attr);
103   }
104
105   public void render(OutputStream out, int indent) throws IOException {
106     StringBuffer JavaDoc line = new StringBuffer JavaDoc(start.getName());
107     line.append((this.isDirected)?"->":"--");
108     line.append(end.getName());
109
110     if (this.attributes != null) {
111       
112       line.append(" [");
113       Iterator attrIt = this.attributes.iterator();
114       while (attrIt.hasNext()) {
115     DotGraphAttribute attr = (DotGraphAttribute)attrIt.next();
116     line.append(attr.toString());
117     line.append(",");
118       }
119       line.append("]");
120     }
121
122     line.append(";");
123
124     DotGraphUtility.renderLine(out, new String JavaDoc(line), indent);
125   }
126 }
127
128
Popular Tags