001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2002 Sable Research Group
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: /*
021: * Modified by the Sable Research Group and others 1997-1999.
022: * See the 'credits' file distributed with Soot for the complete list of
023: * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
024: */
025:
026: package soot.util.dot;
027:
028: import java.io.*;
029: import java.util.*;
030:
031: /* Graph edges are the major elements of a graph
032: * @author Feng Qian
033: */
034: public class DotGraphEdge implements Renderable {
035: private boolean isDirected;
036: private DotGraphNode start, end;
037: private List<DotGraphAttribute> attributes;
038:
039: /**
040: * Draws a directed edge.
041: * @param src, the source node
042: * @param dst, the end node
043: */
044: public DotGraphEdge(DotGraphNode src, DotGraphNode dst) {
045: this .start = src;
046: this .end = dst;
047: this .isDirected = true;
048: }
049:
050: /**
051: * Draws a graph edge by specifying directed or undirected.
052: * @param src, the source node
053: * @param dst, the end node
054: * @param directed, the edge is directed or not
055: */
056: public DotGraphEdge(DotGraphNode src, DotGraphNode dst,
057: boolean directed) {
058: this .start = src;
059: this .end = dst;
060: this .isDirected = directed;
061: }
062:
063: /**
064: * Sets the label for the edge.
065: * @param label, a label string
066: */
067: public void setLabel(String label) {
068: label = DotGraphUtility.replaceQuotes(label);
069: label = DotGraphUtility.replaceReturns(label);
070: this .setAttribute("label", "\"" + label + "\"");
071: }
072:
073: /**
074: * Sets the edge style.
075: * @param style, a style of edge
076: * @see DotGraphConstants
077: */
078: public void setStyle(String style) {
079: this .setAttribute("style", style);
080: }
081:
082: /**
083: * Sets an edge attribute.
084: * @param id, the attribute id to be set
085: * @param value, the attribute value
086: */
087: public void setAttribute(String id, String value) {
088: this .setAttribute(new DotGraphAttribute(id, value));
089: }
090:
091: /**
092: * Sets an edge attribute.
093: * @param attr, a {@link DotGraphAttribute} specifying the
094: * attribute name and value.
095: */
096: public void setAttribute(DotGraphAttribute attr) {
097: if (this .attributes == null) {
098: this .attributes = new LinkedList<DotGraphAttribute>();
099: }
100:
101: this .attributes.add(attr);
102: }
103:
104: public void render(OutputStream out, int indent) throws IOException {
105: StringBuffer line = new StringBuffer(start.getName());
106: line.append((this .isDirected) ? "->" : "--");
107: line.append(end.getName());
108:
109: if (this .attributes != null) {
110:
111: line.append(" [");
112: Iterator<DotGraphAttribute> attrIt = this .attributes
113: .iterator();
114: while (attrIt.hasNext()) {
115: DotGraphAttribute attr = 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(line), indent);
125: }
126: }
|