package org.apache.bcel.util; package .tmp; 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; 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"); } 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) == '-') { if (argv[i].equals("-d")) { 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")) { 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 org.apache.bcel.util.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('['); String baseType = type; if (index > -1) baseType = type.substring(0, index); if (basicTypes.contains(baseType)) return "" + type + ""; return "" + 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 '<': 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(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('.'); if (index > -1) { classPackage = className.substring(0, index); } else { classPackage = ""; } ConstantHTML constantHtml = new ConstantHTML(dir, className, classPackage, methods, constantPool, charset); try (AttributeHTML attributeHtml = new AttributeHTML(dir, className, constantPool, constantHtml, charset)) { new MethodHTML(dir, className, methods, javaClass.getFields(), constantHtml, attributeHtml, charset); writeMainHTML(attributeHtml, charset); new CodeHTML(dir, className, methods, constantPool, constantHtml, charset); } } private void writeMainHTML(AttributeHTML attributeHtml, Charset charset) throws FileNotFoundException, UnsupportedEncodingException { try (PrintWriter file = new PrintWriter(this.dir + className + ".html", charset.name())) { file.println("\nDocumentation for " + className + "\n\n\n\n\n\n\n\n\n"); } Attribute[] attributes = this.javaClass.getAttributes(); for (int i = 0; i < attributes.length; i++) attributeHtml.writeAttribute(attributes[i], "class" + i); } }