View Javadoc
1 package org.apache.bcel.util; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache BCEL" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache BCEL", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.*; 58 import java.util.BitSet; 59 import org.apache.bcel.classfile.*; 60 import org.apache.bcel.Constants; 61 62 /*** 63 * Read class file(s) and convert them into HTML files. 64 * 65 * Given a JavaClass object "class" that is in package "package" five files 66 * will be created in the specified directory. 67 * 68 * <OL> 69 * <LI> "package"."class".html as the main file which defines the frames for 70 * the following subfiles. 71 * <LI> "package"."class"_attributes.html contains all (known) attributes found in the file 72 * <LI> "package"."class"_cp.html contains the constant pool 73 * <LI> "package"."class"_code.html contains the byte code 74 * <LI> "package"."class"_methods.html contains references to all methods and fields of the class 75 * </OL> 76 * 77 * All subfiles reference each other appropiately, e.g. clicking on a 78 * method in the Method's frame will jump to the appropiate method in 79 * the Code frame. 80 * 81 * @version $Id: Class2HTML.java,v 1.1.1.1 2001/10/29 20:00:29 jvanzyl Exp $ 82 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 83 */ 84 public class Class2HTML implements Constants 85 { 86 private JavaClass java_class; // current class object 87 private String dir; 88 89 private static String class_package/package-summary.html">b> static String class_package; // name of package, unclean to make it static, but ... 90 private static String class_name; // name of current class, dito 91 private static ConstantPool constant_pool; 92 93 /*** 94 * Write contents of the given JavaClass into HTML files. 95 * 96 * @param java_class The class to write 97 * @param dir The directory to put the files in 98 */ 99 public Class2HTML(JavaClass java_class, String dir) throws IOException { 100 Method[] methods = java_class.getMethods(); 101 102 this.java_class = java_class; 103 this.dir = dir; 104 class_name = java_class.getClassName(); // Remember full name 105 constant_pool = java_class.getConstantPool(); 106 107 // Get package name by tacking off everything after the last `.' 108 int index = class_name.lastIndexOf('.'); 109 if(index > -1) 110 class_package = class_name.substring(0, index); 111 else 112 class_package = ""; // default package 113 114 ConstantHTML constant_html = new ConstantHTML(dir, class_name, class_package, methods,/package-summary.html">ConstantHTML constant_html = new ConstantHTML(dir, class_name, class_package, methods, 115 constant_pool); 116 117 /* Attributes can't be written in one step, so we just open a file 118 * which will be written consequently. 119 */ 120 AttributeHTML attribute_html = new AttributeHTML(dir, class_name, constant_pool, constant_html); 121 122 MethodHTML method_html = new MethodHTML(dir, class_name, methods, java_class.getFields(), 123 constant_html, attribute_html); 124 // Write main file (with frames, yuk) 125 writeMainHTML(attribute_html); 126 new CodeHTML(dir, class_name, methods, constant_pool, constant_html); 127 attribute_html.close(); 128 } 129 130 public static void main(String argv[]) 131 { 132 String[] file_name = new String[argv.length]; 133 int files=0; 134 ClassParser parser=null; 135 JavaClass java_class=null; 136 String zip_file = null; 137 char sep = System.getProperty("file.separator").toCharArray()[0]; 138 String dir = "." + sep; // Where to store HTML files 139 140 try { 141 /* Parse command line arguments. 142 */ 143 for(int i=0; i < argv.length; i++) { 144 if(argv[i].charAt(0) == '-') { // command line switch 145 if(argv[i].equals("-d")) { // Specify target directory, default `.´ 146 dir = argv[++i]; 147 148 if(!dir.endsWith("" + sep)) 149 dir = dir + sep; 150 151 new File(dir).mkdirs(); // Create target directory if necessary 152 } 153 else if(argv[i].equals("-zip")) 154 zip_file = argv[++i]; 155 else 156 System.out.println("Unknown option " + argv[i]); 157 } 158 else // add file name to list */ 159 file_name[files++] = argv[i]; 160 } 161 162 if(files == 0) 163 System.err.println("Class2HTML: No input files specified."); 164 else { // Loop through files ... 165 for(int i=0; i < files; i++) { 166 System.out.print("Processing " + file_name[i] + "..."); 167 if(zip_file == null) 168 parser = new ClassParser(file_name[i]); // Create parser object from file 169 else 170 parser = new ClassParser(zip_file, file_name[i]); // Create parser object from zip file 171 172 java_class = parser.parse(); 173 new Class2HTML(java_class, dir); 174 System.out.println("Done."); 175 } 176 } 177 } catch(Exception e) { 178 System.out.println(e); 179 e.printStackTrace(System.out); 180 } 181 } 182 183 /*** 184 * Utility method that converts a class reference in the constant pool, 185 * i.e., an index to a string. 186 */ 187 static String referenceClass(int index) { 188 String str = constant_pool.getConstantString(index, CONSTANT_Class); 189 str = Utility.compactClassName(str); 190 str = Utility.compactClassName(str, class_package + ".", true); 191 192 return "<A HREF=\"" + class_name + "_cp.html#cp" + index + 193 "\" TARGET=ConstantPool>" + str + "</A>"; 194 } 195 196 static final String referenceType(String type) { 197 String short_type = Utility.compactClassName(type); 198 short_type = Utility.compactClassName(short_type, class_package + ".", true); 199 200 int index = type.indexOf('['); // Type is an array? 201 if(index > -1) 202 type = type.substring(0, index); // Tack of the `[' 203 204 // test for basic type 205 if(type.equals("int") || type.equals("short") || type.equals("boolean") || type.equals("void") || 206 type.equals("char") || type.equals("byte") || type.equals("long") || type.equals("double") || 207 type.equals("float")) 208 return "<FONT COLOR=\"#00FF00\">" + type + "</FONT>"; 209 else 210 return "<A HREF=\"" + type + ".html\" TARGET=_top>" + short_type + "</A>"; 211 } 212 213 static String toHTML(String str) { 214 StringBuffer buf = new StringBuffer(); 215 216 try { // Filter any characters HTML doesn't like such as < and > in particular 217 for(int i=0; i < str.length(); i++) { 218 char ch; 219 220 switch(ch=str.charAt(i)) { 221 case '<': buf.append("<"); break; 222 case '>': buf.append(">"); break; 223 case '\n': buf.append("//n"); break; 224 case '\r': buf.append("//r"); break; 225 default: buf.append(ch); 226 } 227 } 228 } catch(StringIndexOutOfBoundsException e) {} // Never occurs 229 230 return buf.toString(); 231 } 232 233 private void writeMainHTML(AttributeHTML attribute_html) throws IOException { 234 PrintWriter file = new PrintWriter(new FileOutputStream(dir + class_name + ".html")); 235 Attribute[] attributes = java_class.getAttributes(); 236 237 file.println("<HTML>\n" + "<HEAD><TITLE>Documentation for " + class_name + "</TITLE>" + 238 "</HEAD>\n" + 239 "<FRAMESET BORDER=1 cols=\"30%,*\">\n" + 240 "<FRAMESET BORDER=1 rows=\"80%,*\">\n" + 241 242 "<FRAME NAME=\"ConstantPool\" SRC=\"" + class_name + "_cp.html" + "\"\n MARGINWIDTH=\"0\" " + 243 "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n" + 244 "<FRAME NAME=\"Attributes\" SRC=\"" + class_name + "_attributes.html" + 245 "\"\n MARGINWIDTH=\"0\" " + 246 "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n" + 247 "</FRAMESET>\n" + 248 249 "<FRAMESET BORDER=1 rows=\"80%,*\">\n" + 250 "<FRAME NAME=\"Code\" SRC=\"" + class_name + "_code.html\"\n MARGINWIDTH=0 " + 251 "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n" + 252 "<FRAME NAME=\"Methods\" SRC=\"" + class_name + "_methods.html\"\n MARGINWIDTH=0 " + 253 "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n" + 254 "</FRAMESET></FRAMESET></HTML>" 255 ); 256 257 file.close(); 258 259 for(int i=0; i < attributes.length; i++) 260 attribute_html.writeAttribute(attributes[i], "class" + i); 261 } 262 }

This page was automatically generated by Maven