Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Class2HTML.java

00001 package de.fub.bytecode.util;
00002 
00003 import java.io.*;
00004 import java.util.BitSet;
00005 import de.fub.bytecode.classfile.*;
00006 import de.fub.bytecode.Constants;
00007 
00008 /**
00009  * Read class file(s) and convert them into HTML files.
00010  *
00011  * Given a JavaClass object "class" that is in package "package" five files
00012  * will be created in the specified directory.
00013  *
00014  * <OL>
00015  * <LI> "package"."class".html as the main file which defines the frames for
00016  * the following subfiles.
00017  * <LI>  "package"."class"_attributes.html contains all (known) attributes found in the file
00018  * <LI>  "package"."class"_cp.html contains the constant pool
00019  * <LI>  "package"."class"_code.html contains the byte code
00020  * <LI>  "package"."class"_methods.html contains references to all methods and fields of the class
00021  * </OL>
00022  *
00023  * All subfiles reference each other appropiately, e.g. clicking on a
00024  * method in the Method's frame will jump to the appropiate method in
00025  * the Code frame.
00026  *
00027  * @version $Id: Class2HTML.java,v 1.1.1.1 2002/01/24 03:44:01 pserver Exp $
00028  * @author <A HREF="http://www.inf.fu-berlin.de/~dahm">M. Dahm</A> 
00029 */
00030 public class Class2HTML implements Constants
00031 {
00032   private JavaClass java_class;     // current class object
00033   private String    dir;
00034 
00035   private static String       class_package;  // name of package, unclean to make it static, but ...
00036   private static String       class_name;     // name of current class, dito
00037   private static ConstantPool constant_pool;
00038 
00039   /**
00040    * Write contents of the given JavaClass into HTML files.
00041    * 
00042    * @param java_class The class to write
00043    * @param dir The directory to put the files in
00044    */
00045   public Class2HTML(JavaClass java_class, String dir) throws IOException {
00046     Method[]     methods       = java_class.getMethods();
00047 
00048     this.java_class = java_class;
00049     this.dir        = dir;
00050     class_name      = java_class.getClassName();     // Remember full name
00051     constant_pool   = java_class.getConstantPool();
00052 
00053     // Get package name by tacking off everything after the last `.'
00054     int index = class_name.lastIndexOf('.');
00055     if(index > -1)
00056       class_package = class_name.substring(0, index);
00057     else
00058       class_package = ""; // default package
00059     
00060     ConstantHTML constant_html = new ConstantHTML(dir, class_name, class_package, methods,
00061                           constant_pool);
00062     
00063     /* Attributes can't be written in one step, so we just open a file
00064      * which will be written consequently.
00065      */
00066     AttributeHTML attribute_html = new AttributeHTML(dir, class_name, constant_pool, constant_html);
00067 
00068     MethodHTML method_html = new MethodHTML(dir, class_name, methods, java_class.getFields(),
00069                         constant_html, attribute_html);
00070     // Write main file (with frames, yuk)
00071     writeMainHTML(attribute_html);
00072     new CodeHTML(dir, class_name, methods, constant_pool, constant_html);
00073     attribute_html.close();
00074   }  
00075   public static void main(String argv[])
00076   { 
00077     String[]    file_name = new String[argv.length];
00078     int         files=0;
00079     ClassParser parser=null;
00080     JavaClass   java_class=null;
00081     String      zip_file = null;
00082     char        sep = System.getProperty("file.separator").toCharArray()[0];
00083     String      dir = "." + sep; // Where to store HTML files
00084     
00085     try {
00086       /* Parse command line arguments.
00087        */
00088       for(int i=0; i < argv.length; i++) {
00089     if(argv[i].charAt(0) == '-') {  // command line switch
00090       if(argv[i].equals("-d")) {   // Specify target directory, default `.´
00091         dir = argv[++i];
00092 
00093         if(!dir.endsWith("" + sep))
00094           dir = dir + sep;
00095                         
00096         new File(dir).mkdirs(); // Create target directory if necessary
00097       }
00098       else if(argv[i].equals("-zip"))
00099         zip_file = argv[++i];
00100       else
00101         System.out.println("Unknown option " + argv[i]);
00102     }
00103     else // add file name to list */
00104       file_name[files++] = argv[i];
00105       }
00106     
00107       if(files == 0)
00108     System.err.println("Class2HTML: No input files specified.");
00109       else { // Loop through files ...
00110     for(int i=0; i < files; i++) {
00111       System.out.print("Processing " + file_name[i] + "...");
00112       if(zip_file == null)
00113         parser = new ClassParser(file_name[i]); // Create parser object from file
00114       else
00115         parser = new ClassParser(zip_file, file_name[i]); // Create parser object from zip file
00116 
00117       java_class = parser.parse();
00118       new Class2HTML(java_class, dir);
00119       System.out.println("Done.");
00120     }       
00121       }   
00122     } catch(Exception e) {
00123       System.out.println(e);
00124       e.printStackTrace(System.out);
00125     }
00126   }  
00127   /**
00128    * Utility method that converts a class reference in the constant pool,
00129    * i.e. an index to a string.
00130    */
00131   static String referenceClass(int index) {
00132     String str = constant_pool.getConstantString(index, CONSTANT_Class);
00133     str = Utility.compactClassName(str);
00134     str = Utility.compactClassName(str, class_package + ".", true);
00135     
00136     return "<A HREF=\"" + class_name + "_cp.html#cp" + index + 
00137       "\" TARGET=ConstantPool>" + str + "</A>";
00138   }  
00139   static final String referenceType(String type) {
00140     String short_type = Utility.compactClassName(type);
00141     short_type = Utility.compactClassName(short_type, class_package + ".", true);
00142 
00143     int index = type.indexOf('['); // Type is an array?
00144     if(index > -1)
00145       type = type.substring(0, index); // Tack of the `['       
00146 
00147     // test for basic type
00148     if(type.equals("int")  || type.equals("short") || type.equals("boolean") || type.equals("void")   ||
00149        type.equals("char") || type.equals("byte")  || type.equals("long")    || type.equals("double") ||
00150        type.equals("float"))
00151       return "<FONT COLOR=\"#00FF00\">" + type + "</FONT>";
00152     else
00153       return "<A HREF=\"" + type + ".html\" TARGET=_top>" + short_type + "</A>";
00154   }  
00155   static String toHTML(String str) {
00156     StringBuffer buf = new StringBuffer();
00157 
00158     try { // Filter any characters HTML doesn't like such as < and > in particular
00159       for(int i=0; i < str.length(); i++) {
00160     char ch;
00161                 
00162     switch(ch=str.charAt(i)) {
00163     case '<': buf.append("&lt;"); break;
00164     case '>': buf.append("&gt;"); break;
00165     case '\n': buf.append("\\n"); break;
00166     case '\r': buf.append("\\r"); break;
00167     default:  buf.append(ch);
00168     }
00169       }
00170     } catch(StringIndexOutOfBoundsException e) {} // Never occurs
00171 
00172     return buf.toString();
00173   }  
00174   private void writeMainHTML(AttributeHTML attribute_html) throws IOException {
00175     PrintWriter file       = new PrintWriter(new FileOutputStream(dir + class_name + ".html"));
00176     Attribute[] attributes = java_class.getAttributes();
00177     
00178     file.println("<HTML>\n" + "<HEAD><TITLE>Documentation for " + class_name + "</TITLE>" +
00179          "</HEAD>\n" +
00180          "<FRAMESET BORDER=1 cols=\"30%,*\">\n" +
00181          "<FRAMESET BORDER=1 rows=\"80%,*\">\n" +
00182         
00183          "<FRAME NAME=\"ConstantPool\" SRC=\"" + class_name + "_cp.html" + "\"\n MARGINWIDTH=\"0\" " +
00184          "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n" +
00185          "<FRAME NAME=\"Attributes\" SRC=\"" + class_name + "_attributes.html" +
00186          "\"\n MARGINWIDTH=\"0\" " +
00187          "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n" +
00188          "</FRAMESET>\n" +
00189     
00190          "<FRAMESET BORDER=1 rows=\"80%,*\">\n" +
00191          "<FRAME NAME=\"Code\" SRC=\"" + class_name + "_code.html\"\n MARGINWIDTH=0 " +
00192          "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n" +
00193          "<FRAME NAME=\"Methods\" SRC=\"" + class_name + "_methods.html\"\n MARGINWIDTH=0 " +
00194          "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n" +
00195          "</FRAMESET></FRAMESET></HTML>"
00196          );
00197 
00198     file.close();
00199 
00200     for(int i=0; i < attributes.length; i++)
00201       attribute_html.writeAttribute(attributes[i], "class" + i);
00202   }  
00203 }

Generated at Thu Feb 7 06:42:12 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001