KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > spark > pag > PAG2HTML


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2002 Ondrej 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 soot.jimple.spark.pag;
21 import java.util.*;
22 import soot.jimple.spark.*;
23 import soot.*;
24 import soot.util.*;
25 import soot.jimple.spark.sets.*;
26 import java.io.*;
27 import java.util.jar.*;
28 import java.util.zip.*;
29
30 /** Dumps a pointer assignment graph to a html files.
31  * @author Ondrej Lhotak
32  */

33 public class PAG2HTML {
34     public PAG2HTML( PAG pag, String JavaDoc output_dir ) {
35         this.pag = pag;
36         this.output_dir = output_dir;
37     }
38     public void dump() {
39         for( Iterator vIt = pag.getVarNodeNumberer().iterator(); vIt.hasNext(); ) {
40             final VarNode v = (VarNode) vIt.next();
41             mergedNodes.put( v.getReplacement(), v );
42             if( v instanceof LocalVarNode ) {
43                 SootMethod m = ((LocalVarNode)v).getMethod();
44                 if( m != null ) {
45                     methodToNodes.put( m, v );
46                 }
47             }
48         }
49         try {
50             JarOutputStream jarOut = new JarOutputStream(
51                     new FileOutputStream( new File(output_dir, "pag.jar") ) );
52             for( Iterator vIt = mergedNodes.keySet().iterator(); vIt.hasNext(); ) {
53                 final VarNode v = (VarNode) vIt.next();
54                 dumpVarNode( v, jarOut );
55             }
56             for( Iterator mIt = methodToNodes.keySet().iterator(); mIt.hasNext(); ) {
57                 final SootMethod m = (SootMethod) mIt.next();
58                 dumpMethod( m, jarOut );
59             }
60             addSymLinks( pag.getVarNodeNumberer().iterator(), jarOut );
61             jarOut.close();
62         } catch( IOException e ) {
63             throw new RuntimeException JavaDoc( "Couldn't dump html"+e );
64         }
65     }
66
67
68     /* End of public methods. */
69     /* End of package methods. */
70
71     protected PAG pag;
72     protected String JavaDoc output_dir;
73     protected MultiMap mergedNodes = new HashMultiMap();
74     protected MultiMap methodToNodes = new HashMultiMap();
75
76     protected void dumpVarNode( VarNode v, JarOutputStream jarOut ) throws IOException {
77         jarOut.putNextEntry( new ZipEntry( "nodes/n"+v.getNumber()+".html" ) );
78         final PrintWriter out = new PrintWriter( jarOut );
79         out.println( "<html>" );
80         
81         out.println( "Green node for:" );
82         out.println( varNodeReps( v ) );
83
84         out.println( "Declared type: "+v.getType() );
85         
86         out.println( "<hr>Reaching blue nodes:" );
87         out.println( "<ul>" );
88         v.getP2Set().forall( new P2SetVisitor() {
89         public final void visit( Node n ) {
90                 out.println( "<li>"+htmlify(n.toString()) );
91             }
92         } );
93         out.println( "</ul>" );
94
95         out.println( "<hr>Outgoing edges:" );
96         Node[] succs = pag.simpleLookup( v );
97         for( int i = 0; i < succs.length; i++ ) {
98             VarNode succ = (VarNode) succs[i];
99             out.println( varNodeReps( succ ) );
100         }
101         
102         out.println( "<hr>Incoming edges: " );
103         succs = pag.simpleInvLookup( v );
104         for( int i = 0; i < succs.length; i++ ) {
105             VarNode succ = (VarNode) succs[i];
106             out.println( varNodeReps( succ ) );
107         }
108
109         out.println( "</html>" );
110         out.flush();
111     }
112     protected String JavaDoc varNodeReps( VarNode v ) {
113         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
114         ret.append( "<ul>\n" );
115         for( Iterator vvIt = mergedNodes.get( v ).iterator(); vvIt.hasNext(); ) {
116             final VarNode vv = (VarNode) vvIt.next();
117             ret.append( varNode( "", vv ) );
118         }
119         ret.append( "</ul>\n" );
120         return ret.toString();
121     }
122     protected String JavaDoc varNode( String JavaDoc dirPrefix, VarNode vv ) {
123         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
124         ret.append( "<li><a HREF=\""+dirPrefix+"n"+vv.getNumber()+".html\">" );
125         ret.append( ""+htmlify(vv.getVariable().toString()) );
126         ret.append( "</a><br>" );
127         ret.append( "<li>Context: " );
128         ret.append( ""+(vv.context() == null ?"null":htmlify(vv.context().toString()) ) );
129         ret.append( "</a><br>" );
130         if( vv instanceof LocalVarNode ) {
131             LocalVarNode lvn = (LocalVarNode) vv;
132             SootMethod m = lvn.getMethod();
133             if( m != null ) {
134                 ret.append( "<a HREF=\"../"
135                         +toFileName(m.toString() )+".html\">" );
136                 ret.append( htmlify(m.toString())+"</a><br>" );
137             }
138         }
139         ret.append( htmlify(vv.getType().toString())+"\n" );
140         return ret.toString();
141     }
142     protected static String JavaDoc htmlify( String JavaDoc s ) {
143         StringBuffer JavaDoc b = new StringBuffer JavaDoc( s );
144         for( int i = 0; i < b.length(); i++ ) {
145             if( b.charAt( i ) == '<' ) {
146                 b.replace( i, i+1, "&lt;" );
147             }
148             if( b.charAt( i ) == '>' ) {
149                 b.replace( i, i+1, "&gt;" );
150             }
151         }
152         return b.toString();
153     }
154     protected void dumpMethod( SootMethod m, JarOutputStream jarOut ) throws IOException {
155         jarOut.putNextEntry( new ZipEntry(
156                     ""+toFileName( m.toString() )+".html" ) );
157         final PrintWriter out = new PrintWriter( jarOut );
158         out.println( "<html>" );
159         
160         out.println( "This is method "+htmlify( m.toString() )+"<hr>" );
161         for( Iterator it = methodToNodes.get( m ).iterator(); it.hasNext(); ) {
162             out.println( varNode( "nodes/", (VarNode) it.next() ) );
163         }
164         out.println( "</html>" );
165         out.flush();
166     }
167     protected void addSymLinks( Iterator nodes, JarOutputStream jarOut ) throws IOException {
168         jarOut.putNextEntry( new ZipEntry( "symlinks.sh" ) );
169         final PrintWriter out = new PrintWriter( jarOut );
170         out.println( "#!/bin/sh" );
171         while( nodes.hasNext() ) {
172             VarNode v = (VarNode) nodes.next();
173             VarNode rep = (VarNode) v.getReplacement();
174             if( v != rep ) {
175                 out.println( "ln -s n"+rep.getNumber()+".html n"+v.getNumber()+".html" );
176             }
177         }
178         out.flush();
179     }
180     protected String JavaDoc toFileName( String JavaDoc s ) {
181         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
182         for( int i = 0; i < s.length(); i++ ) {
183             char c = s.charAt( i );
184             if( c == '<' ) ret.append( '{' );
185             else if( c == '>' ) ret.append( '}' );
186             else ret.append( c );
187         }
188         return ret.toString();
189     }
190 }
191
192
Popular Tags