package org.apache.bcel.util; import java.util.HashSet; import java.io.UnsupportedEncodingException; import java.io.FileNotFoundException; import org.apache.bcel.classfile.Attribute; import java.io.PrintWriter; import org.apache.bcel.classfile.Method; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import org.apache.bcel.classfile.Utility; import java.io.IOException; import org.apache.bcel.classfile.ClassParser; import java.io.File; import org.apache.bcel.classfile.JavaClass; import java.util.Set; import org.apache.bcel.classfile.ConstantPool; import org.apache.bcel.Constants; public class Class2HTML implements Constants { private static String classPackage; private static String className; private static ConstantPool constantPool; private static final Set basicTypes; private final JavaClass javaClass; private final String dir; public static void main(final String[] argv) throws IOException { final String[] fileName = new String[argv.length]; int files = 0; ClassParser parser = null; JavaClass javaClass = null; String zipFile = null; final char sep = File.separatorChar; String dir = "." + sep; for (int i = 0; i < argv.length; ++i) { if (argv[i].charAt(0) == '-') { if (argv[i].equals("-d")) { dir = argv[++i]; if (!dir.endsWith("" + sep)) { dir += sep; } final File store = new File(dir); if (!store.isDirectory()) { final 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")) { 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 (int 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(final int index) { String str = Class2HTML.constantPool.getConstantString(index, (byte)7); str = Utility.compactClassName(str); str = Utility.compactClassName(str, Class2HTML.classPackage + ".", true); return "" + str + ""; } static String referenceType(final String type) { String shortType = Utility.compactClassName(type); shortType = Utility.compactClassName(shortType, Class2HTML.classPackage + ".", true); final int index = type.indexOf(91); String baseType = type; if (index > -1) { baseType = type.substring(0, index); } if (Class2HTML.basicTypes.contains(baseType)) { return "" + type + ""; } return "" + shortType + ""; } static String toHTML(final String str) { final StringBuilder buf = new StringBuilder(); for (int i = 0; i < str.length(); ++i) { final char ch; switch (ch = str.charAt(i)) { case '<': { buf.append("<"); break; } case '>': { buf.append(">"); break; } case '\n': { buf.append("\\n"); break; } case '\r': { buf.append("\\r"); break; } default: { buf.append(ch); break; } } } return buf.toString(); } public Class2HTML(final JavaClass javaClass, final String dir) throws IOException { this(javaClass, dir, StandardCharsets.UTF_8); } private Class2HTML(final JavaClass javaClass, final String dir, final Charset charset) throws IOException { final Method[] methods = javaClass.getMethods(); this.javaClass = javaClass; this.dir = dir; Class2HTML.className = javaClass.getClassName(); Class2HTML.constantPool = javaClass.getConstantPool(); final int index = Class2HTML.className.lastIndexOf(46); if (index > -1) { Class2HTML.classPackage = Class2HTML.className.substring(0, index); } else { Class2HTML.classPackage = ""; } final ConstantHTML constantHtml = new ConstantHTML(dir, Class2HTML.className, Class2HTML.classPackage, methods, Class2HTML.constantPool, charset); try (AttributeHTML attributeHtml = new AttributeHTML(dir, Class2HTML.className, Class2HTML.constantPool, constantHtml, charset)) { new MethodHTML(dir, Class2HTML.className, methods, javaClass.getFields(), constantHtml, attributeHtml, charset); this.writeMainHTML(attributeHtml, charset); new CodeHTML(dir, Class2HTML.className, methods, Class2HTML.constantPool, constantHtml, charset); } } private void writeMainHTML(final AttributeHTML attributeHtml, final Charset charset) throws FileNotFoundException, UnsupportedEncodingException { try (PrintWriter file = new PrintWriter(this.dir + Class2HTML.className + ".html", charset.name())) { file.println("\nDocumentation for " + Class2HTML.className + "\n\n\n\n\n\n\n\n\n"); } final Attribute[] attributes = this.javaClass.getAttributes(); for (int i = 0; i < attributes.length; ++i) { attributeHtml.writeAttribute(attributes[i], "class" + i); } } static { (basicTypes = new HashSet()).add("int"); Class2HTML.basicTypes.add("short"); Class2HTML.basicTypes.add("boolean"); Class2HTML.basicTypes.add("void"); Class2HTML.basicTypes.add("char"); Class2HTML.basicTypes.add("byte"); Class2HTML.basicTypes.add("long"); Class2HTML.basicTypes.add("double"); Class2HTML.basicTypes.add("float"); } }