1 package com.sun.org.apache.bcel.internal.util; 2 3 56 57 import java.io.*; 58 import java.util.BitSet ; 59 import com.sun.org.apache.bcel.internal.classfile.*; 60 import com.sun.org.apache.bcel.internal.Constants; 61 62 84 public class Class2HTML implements Constants 85 { 86 private JavaClass java_class; private String dir; 88 89 private static String class_package; private static String class_name; private static ConstantPool constant_pool; 92 93 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(); constant_pool = java_class.getConstantPool(); 106 107 int index = class_name.lastIndexOf('.'); 109 if(index > -1) 110 class_package = class_name.substring(0, index); 111 else 112 class_package = ""; 114 ConstantHTML constant_html = new ConstantHTML(dir, class_name, class_package, methods, 115 constant_pool); 116 117 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 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 = java.io.File.separatorChar; 138 String dir = "." + sep; 140 try { 141 143 for(int i=0; i < argv.length; i++) { 144 if(argv[i].charAt(0) == '-') { if(argv[i].equals("-d")) { dir = argv[++i]; 147 148 if(!dir.endsWith("" + sep)) 149 dir = dir + sep; 150 151 new File(dir).mkdirs(); } 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 file_name[files++] = argv[i]; 160 } 161 162 if(files == 0) 163 System.err.println("Class2HTML: No input files specified."); 164 else { 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]); else 170 parser = new ClassParser(zip_file, file_name[i]); 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 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('['); if(index > -1) 202 type = type.substring(0, index); 204 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 { 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) {} 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 } 263 | Popular Tags |