KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ca > mcgill > sable > soot > cfg > AnnotatedCFGSaver


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2005 Jennifer Lhotak
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 package ca.mcgill.sable.soot.cfg;
21 import soot.util.dot.*;
22 import ca.mcgill.sable.soot.cfg.model.*;
23 import java.util.*;
24
25 public class AnnotatedCFGSaver {
26
27     private CFGGraph graph;
28     private String JavaDoc fileNameBase;
29     private String JavaDoc title;
30
31     public AnnotatedCFGSaver(CFGGraph graph, String JavaDoc fileNameBase, String JavaDoc title) {
32         this.graph = graph;
33         this.fileNameBase = fileNameBase;
34         this.title = title;
35     }
36
37     public void saveGraph(){
38         DotGraph canvas = initGraph();
39         HashMap nodes = makeNodes(canvas);
40         makeEdges(canvas, nodes);
41         formatNames(canvas, nodes);
42         String JavaDoc fileName = formFileName();
43         System.out.println("cfg fileName: "+fileName);
44         canvas.plot(fileName);
45     }
46     
47     private DotGraph initGraph(){
48         DotGraph canvas = new DotGraph(title);
49         canvas.setGraphLabel(title);
50         canvas.setGraphSize(8.0, 10.0);
51         canvas.setOrientation(DotGraphConstants.GRAPH_ORIENT_PORTRAIT);
52         canvas.setNodeShape(DotGraphConstants.NODE_SHAPE_PLAINTEXT);
53         return canvas;
54     }
55     
56     private HashMap makeNodes(DotGraph canvas){
57         HashMap nodeMap = new HashMap();
58         Iterator it = graph.getChildren().iterator();
59         int count = 0;
60         while (it.hasNext()){
61             CFGNode next = (CFGNode)it.next();
62             String JavaDoc nodeName = "n"+count;
63             nodeMap.put(next, nodeName);
64             canvas.drawNode(nodeName);
65             count++;
66         }
67         return nodeMap;
68     }
69     
70     private void makeEdges(DotGraph canvas, HashMap nodeMap){
71         Iterator it = nodeMap.keySet().iterator();
72         while (it.hasNext()){
73             CFGNode node = (CFGNode)it.next();
74             String JavaDoc nodeName = (String JavaDoc)nodeMap.get(node);
75             Iterator inputs = node.getInputs().iterator();
76             while (inputs.hasNext()){
77                 CFGEdge edge = (CFGEdge)inputs.next();
78                 CFGNode src = edge.getFrom();
79                 String JavaDoc srcName = (String JavaDoc)nodeMap.get(src);
80                 canvas.drawEdge(srcName, nodeName);
81             }
82             
83         }
84     }
85     
86     private void formatNames(DotGraph canvas, HashMap nodeMap){
87         Iterator it = nodeMap.keySet().iterator();
88         while (it.hasNext()){
89             CFGNode node = (CFGNode)it.next();
90             String JavaDoc nodeName = (String JavaDoc)nodeMap.get(node);
91             DotGraphNode dgNode = canvas.getNode(nodeName);
92             dgNode.setHTMLLabel(getNodeLabel(node));
93         }
94     }
95     
96     private String JavaDoc getNodeLabel(CFGNode node){
97         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
98         sb.append("<<TABLE BORDER=\"0\">");
99         sb.append("<TR><TD>");
100         if (node.getBefore() != null) {
101             
102             Iterator before = node.getBefore().getChildren().iterator();
103             while (before.hasNext()){
104                 Iterator pFlowData = ((CFGPartialFlowData)before.next()).getChildren().iterator();
105                 while (pFlowData.hasNext()){
106                     CFGFlowInfo info = (CFGFlowInfo)pFlowData.next();
107                     String JavaDoc temp = info.getText();
108                     temp = soot.util.StringTools.replaceAll(temp, "<", "&lt;");
109                     temp = soot.util.StringTools.replaceAll(temp, ">", "&gt;");
110                     sb.append(temp);
111                     
112                 }
113             }
114         }
115         sb.append("</TD></TR>");
116         sb.append("<TR><TD>");
117         sb.append("<TABLE>");
118         sb.append("<TR><TD>");
119         Iterator data = node.getData().getText().iterator();
120         while (data.hasNext()){
121             String JavaDoc temp = data.next().toString();
122             temp = soot.util.StringTools.replaceAll(temp, "<", "&lt;");
123             temp = soot.util.StringTools.replaceAll(temp, ">", "&gt;");
124             sb.append(temp);
125         }
126         sb.append("</TD></TR>");
127         sb.append("</TABLE>");
128         sb.append("</TD></TR>");
129         sb.append("<TR><TD>");
130         if (node.getAfter() != null){
131             Iterator after = node.getAfter().getChildren().iterator();
132             while (after.hasNext()){
133                 Iterator pFlowData = ((CFGPartialFlowData)after.next()).getChildren().iterator();
134                 while (pFlowData.hasNext()){
135                     CFGFlowInfo info = (CFGFlowInfo)pFlowData.next();
136                     String JavaDoc temp = info.getText();
137                     temp = soot.util.StringTools.replaceAll(temp, "<", "&lt;");
138                     temp = soot.util.StringTools.replaceAll(temp, ">", "&gt;");
139                     sb.append(temp);
140                 }
141             }
142         }
143         sb.append("</TD></TR>");
144         sb.append("</TABLE>>");
145         System.out.println("node data string: "+sb.toString());
146         return sb.toString();
147     }
148     
149     private String JavaDoc formFileName(){
150         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
151         sb.append(fileNameBase);
152         String JavaDoc sep = System.getProperty("file.separator");
153         sb.append(sep);
154         sb.append(title);
155         sb.append(".cfg");
156         return sb.toString();
157     }
158 }
159
160
Popular Tags