package org.apache.bcel.util; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.HashSet; import java.util.Set; import org.apache.bcel.Constants; import org.apache.bcel.classfile.Attribute; import org.apache.bcel.classfile.ClassParser; import org.apache.bcel.classfile.ConstantPool; import org.apache.bcel.classfile.JavaClass; import org.apache.bcel.classfile.Method; import org.apache.bcel.classfile.Utility; import org.apache.bcel.util.AttributeHTML; import org.apache.bcel.util.CodeHTML; import org.apache.bcel.util.ConstantHTML; import org.apache.bcel.util.MethodHTML; public class Class2HTML implements Constants { private static String classPackage; private static String className; private static ConstantPool constantPool; private static final Set basicTypes = new HashSet(); private final JavaClass javaClass; private final String dir; public static void main(String[] argv) throws IOException { String[] fileName = new String[argv.length]; int files = 0; ClassParser parser = null; JavaClass javaClass = null; String zipFile = null; char sep = File.separatorChar; String dir = "." + sep; int i; for(i = 0; i < argv.length; ++i) { if(argv[i].charAt(0) == 45) { if(argv[i].equals("-d")) { ++i; dir = argv[i]; if(!dir.endsWith("" + sep)) { dir = dir + sep; } File store = new File(dir); if(!store.isDirectory()) { boolean created = store.mkdirs(); if(!created && !store.isDirectory()) { System.out.println("Tried to create the directory " + dir + " but failed"); } } } else if(argv[i].equals("-zip")) { ++i; zipFile = argv[i]; } else { System.out.println("Unknown option " + argv[i]); } } else { fileName[files++] = argv[i]; } } if(files == 0) { System.err.println("Class2HTML: No input files specified."); } else { for(i = 0; i < files; ++i) { System.out.print("Processing " + fileName[i] + "..."); if(zipFile == null) { parser = new ClassParser(fileName[i]); } else { parser = new ClassParser(zipFile, fileName[i]); } javaClass = parser.parse(); new Class2HTML(javaClass, dir); System.out.println("Done."); } } } static String referenceClass(int index) { String str = constantPool.getConstantString(index, (byte)7); str = Utility.compactClassName(str); str = Utility.compactClassName(str, classPackage + ".", true); return "" + str + ""; } static String referenceType(String type) { String shortType = Utility.compactClassName(type); shortType = Utility.compactClassName(shortType, classPackage + ".", true); int index = type.indexOf(91); String baseType = type; if(index > -1) { baseType = type.substring(0, index); } return basicTypes.contains(baseType)?"" + type + "":"" + shortType + ""; } static String toHTML(String str) { StringBuilder buf = new StringBuilder(); for(int i = 0; i < str.length(); ++i) { char ch; switch(ch = str.charAt(i)) { case 10: buf.append("\\n"); break; case 13: buf.append("\\r"); break; case 60: buf.append("<"); break; case 62: buf.append(">"); break; default: buf.append(ch); } } return buf.toString(); } public Class2HTML(JavaClass javaClass, String dir) throws IOException { this(javaClass, dir, StandardCharsets.UTF_8); } private Class2HTML(JavaClass javaClass, String dir, Charset charset) throws IOException { Method[] methods = javaClass.getMethods(); this.javaClass = javaClass; this.dir = dir; className = javaClass.getClassName(); constantPool = javaClass.getConstantPool(); int index = className.lastIndexOf(46); if(index > -1) { classPackage = className.substring(0, index); } else { classPackage = ""; } ConstantHTML constantHtml = new ConstantHTML(dir, className, classPackage, methods, constantPool, charset); AttributeHTML attributeHtml = new AttributeHTML(dir, className, constantPool, constantHtml, charset); Throwable var8 = null; try { new MethodHTML(dir, className, methods, javaClass.getFields(), constantHtml, attributeHtml, charset); this.writeMainHTML(attributeHtml, charset); new CodeHTML(dir, className, methods, constantPool, constantHtml, charset); } catch (Throwable var17) { var8 = var17; throw var17; } finally { if(attributeHtml != null) { if(var8 != null) { try { attributeHtml.close(); } catch (Throwable var16) { var8.addSuppressed(var16); } } else { attributeHtml.close(); } } } } private void writeMainHTML(AttributeHTML attributeHtml, Charset charset) throws FileNotFoundException, UnsupportedEncodingException { PrintWriter attributes = new PrintWriter(this.dir + className + ".html", charset.name()); Throwable i = null; try { attributes.println("\nDocumentation for " + className + "\n\n\n\n\n\n\n\n\n"); } catch (Throwable var13) { i = var13; throw var13; } finally { if(attributes != null) { if(i != null) { try { attributes.close(); } catch (Throwable var12) { i.addSuppressed(var12); } } else { attributes.close(); } } } Attribute[] var16 = this.javaClass.getAttributes(); for(int var15 = 0; var15 < var16.length; ++var15) { attributeHtml.writeAttribute(var16[var15], "class" + var15); } } static { basicTypes.add("int"); basicTypes.add("short"); basicTypes.add("boolean"); basicTypes.add("void"); basicTypes.add("char"); basicTypes.add("byte"); basicTypes.add("long"); basicTypes.add("double"); basicTypes.add("float"); } }