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
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 public class Class2HTML implements Constants
00031 {
00032 private JavaClass java_class;
00033 private String dir;
00034
00035 private static String class_package;
00036 private static String class_name;
00037 private static ConstantPool constant_pool;
00038
00039
00040
00041
00042
00043
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();
00051 constant_pool = java_class.getConstantPool();
00052
00053
00054 int index = class_name.lastIndexOf('.');
00055 if(index > -1)
00056 class_package = class_name.substring(0, index);
00057 else
00058 class_package = "";
00059
00060 ConstantHTML constant_html = new ConstantHTML(dir, class_name, class_package, methods,
00061 constant_pool);
00062
00063
00064
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
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;
00084
00085 try {
00086
00087
00088 for(int i=0; i < argv.length; i++) {
00089 if(argv[i].charAt(0) == '-') {
00090 if(argv[i].equals("-d")) {
00091 dir = argv[++i];
00092
00093 if(!dir.endsWith("" + sep))
00094 dir = dir + sep;
00095
00096 new File(dir).mkdirs();
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
00104 file_name[files++] = argv[i];
00105 }
00106
00107 if(files == 0)
00108 System.err.println("Class2HTML: No input files specified.");
00109 else {
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]);
00114 else
00115 parser = new ClassParser(zip_file, file_name[i]);
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
00129
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('[');
00144 if(index > -1)
00145 type = type.substring(0, index);
00146
00147
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 {
00159 for(int i=0; i < str.length(); i++) {
00160 char ch;
00161
00162 switch(ch=str.charAt(i)) {
00163 case '<': buf.append("<"); break;
00164 case '>': buf.append(">"); 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) {}
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 }