1 17 package org.apache.bcel.util; 18 19 import java.io.File ; 20 import java.io.FileOutputStream ; 21 import java.io.IOException ; 22 import java.io.PrintWriter ; 23 import org.apache.bcel.Constants; 24 import org.apache.bcel.classfile.Attribute; 25 import org.apache.bcel.classfile.ClassParser; 26 import org.apache.bcel.classfile.ConstantPool; 27 import org.apache.bcel.classfile.JavaClass; 28 import org.apache.bcel.classfile.Method; 29 import org.apache.bcel.classfile.Utility; 30 31 53 public class Class2HTML implements Constants { 54 55 private JavaClass java_class; private String dir; 57 private static String class_package; private static String class_name; private static ConstantPool constant_pool; 60 61 62 68 public Class2HTML(JavaClass java_class, String dir) throws IOException { 69 Method[] methods = java_class.getMethods(); 70 this.java_class = java_class; 71 this.dir = dir; 72 class_name = java_class.getClassName(); constant_pool = java_class.getConstantPool(); 74 int index = class_name.lastIndexOf('.'); 76 if (index > -1) { 77 class_package = class_name.substring(0, index); 78 } else { 79 class_package = ""; } 81 ConstantHTML constant_html = new ConstantHTML(dir, class_name, class_package, methods, 82 constant_pool); 83 86 AttributeHTML attribute_html = new AttributeHTML(dir, class_name, constant_pool, 87 constant_html); 88 MethodHTML method_html = new MethodHTML(dir, class_name, methods, java_class.getFields(), 89 constant_html, attribute_html); 90 writeMainHTML(attribute_html); 92 new CodeHTML(dir, class_name, methods, constant_pool, constant_html); 93 attribute_html.close(); 94 } 95 96 97 public static void main( String argv[] ) { 98 String [] file_name = new String [argv.length]; 99 int files = 0; 100 ClassParser parser = null; 101 JavaClass java_class = null; 102 String zip_file = null; 103 char sep = System.getProperty("file.separator").toCharArray()[0]; 104 String dir = "." + sep; try { 106 108 for (int i = 0; i < argv.length; i++) { 109 if (argv[i].charAt(0) == '-') { if (argv[i].equals("-d")) { dir = argv[++i]; 112 if (!dir.endsWith("" + sep)) { 113 dir = dir + sep; 114 } 115 new File (dir).mkdirs(); } else if (argv[i].equals("-zip")) { 117 zip_file = argv[++i]; 118 } else { 119 System.out.println("Unknown option " + argv[i]); 120 } 121 } else { 122 file_name[files++] = argv[i]; 123 } 124 } 125 if (files == 0) { 126 System.err.println("Class2HTML: No input files specified."); 127 } else { for (int i = 0; i < files; i++) { 129 System.out.print("Processing " + file_name[i] + "..."); 130 if (zip_file == null) { 131 parser = new ClassParser(file_name[i]); } else { 133 parser = new ClassParser(zip_file, file_name[i]); } 135 java_class = parser.parse(); 136 new Class2HTML(java_class, dir); 137 System.out.println("Done."); 138 } 139 } 140 } catch (Exception e) { 141 System.out.println(e); 142 e.printStackTrace(System.out); 143 } 144 } 145 146 147 151 static String referenceClass( int index ) { 152 String str = constant_pool.getConstantString(index, CONSTANT_Class); 153 str = Utility.compactClassName(str); 154 str = Utility.compactClassName(str, class_package + ".", true); 155 return "<A HREF=\"" + class_name + "_cp.html#cp" + index + "\" TARGET=ConstantPool>" + str 156 + "</A>"; 157 } 158 159 160 static final String referenceType( String type ) { 161 String short_type = Utility.compactClassName(type); 162 short_type = Utility.compactClassName(short_type, class_package + ".", true); 163 int index = type.indexOf('['); String base_type = type; 165 if (index > -1) { 166 base_type = type.substring(0, index); } 168 if (base_type.equals("int") || base_type.equals("short") || base_type.equals("boolean") 170 || base_type.equals("void") || base_type.equals("char") || base_type.equals("byte") 171 || base_type.equals("long") || base_type.equals("double") 172 || base_type.equals("float")) { 173 return "<FONT COLOR=\"#00FF00\">" + type + "</FONT>"; 174 } else { 175 return "<A HREF=\"" + base_type + ".html\" TARGET=_top>" + short_type + "</A>"; 176 } 177 } 178 179 180 static String toHTML( String str ) { 181 StringBuffer buf = new StringBuffer (); 182 try { for (int i = 0; i < str.length(); i++) { 184 char ch; 185 switch (ch = str.charAt(i)) { 186 case '<': 187 buf.append("<"); 188 break; 189 case '>': 190 buf.append(">"); 191 break; 192 case '\n': 193 buf.append("\\n"); 194 break; 195 case '\r': 196 buf.append("\\r"); 197 break; 198 default: 199 buf.append(ch); 200 } 201 } 202 } catch (StringIndexOutOfBoundsException e) { 203 } return buf.toString(); 205 } 206 207 208 private void writeMainHTML( AttributeHTML attribute_html ) throws IOException { 209 PrintWriter file = new PrintWriter (new FileOutputStream (dir + class_name + ".html")); 210 Attribute[] attributes = java_class.getAttributes(); 211 file.println("<HTML>\n" + "<HEAD><TITLE>Documentation for " + class_name + "</TITLE>" 212 + "</HEAD>\n" + "<FRAMESET BORDER=1 cols=\"30%,*\">\n" 213 + "<FRAMESET BORDER=1 rows=\"80%,*\">\n" + "<FRAME NAME=\"ConstantPool\" SRC=\"" 214 + class_name + "_cp.html" + "\"\n MARGINWIDTH=\"0\" " 215 + "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n" 216 + "<FRAME NAME=\"Attributes\" SRC=\"" + class_name + "_attributes.html" 217 + "\"\n MARGINWIDTH=\"0\" " 218 + "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n" + "</FRAMESET>\n" 219 + "<FRAMESET BORDER=1 rows=\"80%,*\">\n" + "<FRAME NAME=\"Code\" SRC=\"" 220 + class_name + "_code.html\"\n MARGINWIDTH=0 " 221 + "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n" 222 + "<FRAME NAME=\"Methods\" SRC=\"" + class_name 223 + "_methods.html\"\n MARGINWIDTH=0 " 224 + "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n" 225 + "</FRAMESET></FRAMESET></HTML>"); 226 file.close(); 227 for (int i = 0; i < attributes.length; i++) { 228 attribute_html.writeAttribute(attributes[i], "class" + i); 229 } 230 } 231 } 232 | Popular Tags |