KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > util > ConstantHTML


1
2 package com.sun.org.apache.bcel.internal.util;
3
4 /* ====================================================================
5  * The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 2001 The Apache Software Foundation. All rights
8  * reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in
19  * the documentation and/or other materials provided with the
20  * distribution.
21  *
22  * 3. The end-user documentation included with the redistribution,
23  * if any, must include the following acknowledgment:
24  * "This product includes software developed by the
25  * Apache Software Foundation (http://www.apache.org/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Apache" and "Apache Software Foundation" and
30  * "Apache BCEL" must not be used to endorse or promote products
31  * derived from this software without prior written permission. For
32  * written permission, please contact apache@apache.org.
33  *
34  * 5. Products derived from this software may not be called "Apache",
35  * "Apache BCEL", nor may "Apache" appear in their name, without
36  * prior written permission of the Apache Software Foundation.
37  *
38  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This software consists of voluntary contributions made by many
53  * individuals on behalf of the Apache Software Foundation. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 import com.sun.org.apache.bcel.internal.classfile.*;
59 import java.io.*;
60
61 /**
62  * Convert constant pool into HTML file.
63  *
64  * @version $Id: ConstantHTML.java,v 1.1.1.1 2001/10/29 20:00:30 jvanzyl Exp $
65  * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
66  *
67  */

68 final class ConstantHTML implements com.sun.org.apache.bcel.internal.Constants {
69   private String JavaDoc class_name; // name of current class
70
private String JavaDoc class_package; // name of package
71
private ConstantPool constant_pool; // reference to constant pool
72
private PrintWriter file; // file to write to
73
private String JavaDoc[] constant_ref; // String to return for cp[i]
74
private Constant[] constants; // The constants in the cp
75
private Method[] methods;
76
77   ConstantHTML(String JavaDoc dir, String JavaDoc class_name, String JavaDoc class_package, Method[] methods,
78            ConstantPool constant_pool) throws IOException
79   {
80     this.class_name = class_name;
81     this.class_package = class_package;
82     this.constant_pool = constant_pool;
83     this.methods = methods;
84     constants = constant_pool.getConstantPool();
85     file = new PrintWriter(new FileOutputStream(dir + class_name + "_cp.html"));
86     constant_ref = new String JavaDoc[constants.length];
87     constant_ref[0] = "&lt;unknown&gt;";
88     
89     file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
90     
91     // Loop through constants, constants[0] is reserved
92
for(int i=1; i < constants.length; i++) {
93       if(i % 2 == 0)
94     file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>");
95       else
96     file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
97
98       if(constants[i] != null)
99     writeConstant(i);
100       
101       file.print("</TD></TR>\n");
102     }
103         
104     file.println("</TABLE></BODY></HTML>");
105     file.close();
106   }
107
108   String JavaDoc referenceConstant(int index) {
109     return constant_ref[index];
110   }
111
112   private void writeConstant(int index) {
113     byte tag = constants[index].getTag();
114     int class_index, name_index;
115     String JavaDoc ref;
116     
117     // The header is always the same
118
file.println("<H4> <A NAME=cp" + index + ">" + index + "</A> " + CONSTANT_NAMES[tag] + "</H4>");
119
120     /* For every constant type get the needed parameters and print them appropiately
121      */

122     switch(tag) {
123     case CONSTANT_InterfaceMethodref:
124     case CONSTANT_Methodref:
125       // Get class_index and name_and_type_index, depending on type
126
if(tag == CONSTANT_Methodref) {
127     ConstantMethodref c = (ConstantMethodref)constant_pool.getConstant(index, CONSTANT_Methodref);
128     class_index = c.getClassIndex();
129     name_index = c.getNameAndTypeIndex();
130       }
131       else {
132     ConstantInterfaceMethodref c1 = (ConstantInterfaceMethodref)constant_pool.getConstant(index, CONSTANT_InterfaceMethodref);
133     class_index = c1.getClassIndex();
134     name_index = c1.getNameAndTypeIndex();
135       }
136
137       // Get method name and its class
138
String JavaDoc method_name = constant_pool.constantToString(name_index, CONSTANT_NameAndType);
139       String JavaDoc html_method_name = Class2HTML.toHTML(method_name);
140       
141       // Partially compacted class name, i.e., / -> .
142
String JavaDoc method_class = constant_pool.constantToString(class_index, CONSTANT_Class);
143       String JavaDoc short_method_class = Utility.compactClassName(method_class); // I.e., remove java.lang.
144
short_method_class = Utility.compactClassName(method_class); // I.e., remove java.lang.
145
short_method_class = Utility.compactClassName(short_method_class, class_package + ".", true); // Remove class package prefix
146

147       // Get method signature
148
ConstantNameAndType c2 = (ConstantNameAndType)constant_pool.getConstant(name_index, CONSTANT_NameAndType);
149       String JavaDoc signature = constant_pool.constantToString(c2.getSignatureIndex(), CONSTANT_Utf8);
150       // Get array of strings containing the argument types
151
String JavaDoc[] args = Utility.methodSignatureArgumentTypes(signature, false);
152             
153       // Get return type string
154
String JavaDoc type = Utility.methodSignatureReturnType(signature, false);
155       String JavaDoc ret_type = Class2HTML.referenceType(type);
156
157       StringBuffer JavaDoc buf = new StringBuffer JavaDoc("(");
158       for(int i=0; i < args.length; i++) {
159     buf.append(Class2HTML.referenceType(args[i]));
160     if(i < args.length - 1)
161       buf.append(",&nbsp;");
162       }
163       buf.append(")");
164
165       String JavaDoc arg_types = buf.toString();
166             
167       if(method_class.equals(class_name)) // Method is local to class
168
ref = "<A HREF=\"" + class_name + "_code.html#method" + getMethodNumber(method_name + signature) +
169       "\" TARGET=Code>" + html_method_name + "</A>";
170       else
171     ref = "<A HREF=\"" + method_class + ".html" + "\" TARGET=_top>" + short_method_class +
172       "</A>." + html_method_name;
173
174       constant_ref[index] = ret_type + "&nbsp;<A HREF=\"" + class_name + "_cp.html#cp" + class_index +
175     "\" TARGET=Constants>" +
176     short_method_class + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" +
177     index + "\" TARGET=ConstantPool>" + html_method_name + "</A>&nbsp;" + arg_types;
178
179       file.println("<P><TT>" + ret_type + "&nbsp;" + ref + arg_types + "&nbsp;</TT>\n<UL>" +
180            "<LI><A HREF=\"#cp" + class_index + "\">Class index(" + class_index + ")</A>\n" +
181            "<LI><A HREF=\"#cp" + name_index + "\">NameAndType index(" + name_index + ")</A></UL>");
182       break;
183
184     case CONSTANT_Fieldref:
185       // Get class_index and name_and_type_index
186
ConstantFieldref c3 = (ConstantFieldref)constant_pool.getConstant(index, CONSTANT_Fieldref);
187       class_index = c3.getClassIndex();
188       name_index = c3.getNameAndTypeIndex();
189
190       // Get method name and its class (compacted)
191
String JavaDoc field_class = constant_pool.constantToString(class_index, CONSTANT_Class);
192       String JavaDoc short_field_class = Utility.compactClassName(field_class); // I.e., remove java.lang.
193
short_field_class = Utility.compactClassName(short_field_class, class_package + ".", true); // Remove class package prefix
194

195       String JavaDoc field_name = constant_pool.constantToString(name_index, CONSTANT_NameAndType);
196
197       if(field_class.equals(class_name)) // Field is local to class
198
ref = "<A HREF=\"" + field_class + "_methods.html#field" +
199       field_name + "\" TARGET=Methods>" + field_name + "</A>";
200       else
201     ref = "<A HREF=\"" + field_class + ".html\" TARGET=_top>" +
202       short_field_class + "</A>." + field_name + "\n";
203
204       constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + class_index + "\" TARGET=Constants>" +
205     short_field_class + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" +
206     index + "\" TARGET=ConstantPool>" + field_name + "</A>";
207                                                 
208       file.println("<P><TT>" + ref + "</TT><BR>\n" + "<UL>" +
209            "<LI><A HREF=\"#cp" + class_index + "\">Class(" + class_index + ")</A><BR>\n" +
210            "<LI><A HREF=\"#cp" + name_index + "\">NameAndType(" + name_index + ")</A></UL>");
211       break;
212             
213     case CONSTANT_Class:
214       ConstantClass c4 = (ConstantClass)constant_pool.getConstant(index, CONSTANT_Class);
215       name_index = c4.getNameIndex();
216       String JavaDoc class_name2 = constant_pool.constantToString(index, tag); // / -> .
217
String JavaDoc short_class_name = Utility.compactClassName(class_name2); // I.e., remove java.lang.
218
short_class_name = Utility.compactClassName(short_class_name, class_package + ".", true); // Remove class package prefix
219

220       ref = "<A HREF=\"" + class_name2 + ".html\" TARGET=_top>" + short_class_name + "</A>";
221       constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + index +
222     "\" TARGET=ConstantPool>" + short_class_name + "</A>";
223                                             
224       file.println("<P><TT>" + ref + "</TT><UL>" +
225            "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A></UL>\n");
226       break;
227     
228     case CONSTANT_String:
229       ConstantString c5 = (ConstantString)constant_pool.getConstant(index, CONSTANT_String);
230       name_index = c5.getStringIndex();
231
232       String JavaDoc str = Class2HTML.toHTML(constant_pool.constantToString(index, tag));
233   
234       file.println("<P><TT>" + str + "</TT><UL>" +
235            "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A></UL>\n");
236       break;
237     
238     case CONSTANT_NameAndType:
239       ConstantNameAndType c6 = (ConstantNameAndType)constant_pool.getConstant(index, CONSTANT_NameAndType);
240       name_index = c6.getNameIndex();
241       int signature_index = c6.getSignatureIndex();
242
243       file.println("<P><TT>" + Class2HTML.toHTML(constant_pool.constantToString(index, tag)) + "</TT><UL>" +
244            "<LI><A HREF=\"#cp" + name_index + "\">Name index(" + name_index + ")</A>\n" +
245            "<LI><A HREF=\"#cp" + signature_index + "\">Signature index(" +
246            signature_index + ")</A></UL>\n");
247       break;
248
249     default:
250       file.println("<P><TT>" + Class2HTML.toHTML(constant_pool.constantToString(index, tag)) + "</TT>\n");
251     } // switch
252
}
253
254   private final int getMethodNumber(String JavaDoc str) {
255     for(int i=0; i < methods.length; i++) {
256       String JavaDoc cmp = methods[i].getName() + methods[i].getSignature();
257       if(cmp.equals(str))
258     return i;
259     }
260     return -1;
261   }
262 }
263
Popular Tags