1 /* 2 * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved. 3 */ 4 /* 5 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * contributor license agreements. See the NOTICE file distributed with 7 * this work for additional information regarding copyright ownership. 8 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * (the "License"); you may not use this file except in compliance with 10 * the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package com.sun.org.apache.bcel.internal.util; 21 22 import java.io.File; 23 import java.io.FileOutputStream; 24 import java.io.IOException; 25 import java.io.PrintWriter; 26 import java.util.HashSet; 27 import java.util.Set; 28 29 import com.sun.org.apache.bcel.internal.Const; 30 import com.sun.org.apache.bcel.internal.classfile.Attribute; 31 import com.sun.org.apache.bcel.internal.classfile.ClassParser; 32 import com.sun.org.apache.bcel.internal.classfile.ConstantPool; 33 import com.sun.org.apache.bcel.internal.classfile.JavaClass; 34 import com.sun.org.apache.bcel.internal.classfile.Method; 35 import com.sun.org.apache.bcel.internal.classfile.Utility; 36 37 /** 38 * Read class file(s) and convert them into HTML files. 39 * 40 * Given a JavaClass object "class" that is in package "package" five files 41 * will be created in the specified directory. 42 * 43 * <OL> 44 * <LI> "package"."class".html as the main file which defines the frames for 45 * the following subfiles. 46 * <LI> "package"."class"_attributes.html contains all (known) attributes found in the file 47 * <LI> "package"."class"_cp.html contains the constant pool 48 * <LI> "package"."class"_code.html contains the byte code 49 * <LI> "package"."class"_methods.html contains references to all methods and fields of the class 50 * </OL> 51 * 52 * All subfiles reference each other appropriately, e.g. clicking on a 53 * method in the Method's frame will jump to the appropriate method in 54 * the Code frame. 55 * 56 * @LastModified: Jan 2020 57 */ 58 public class Class2HTML { 59 60 private final JavaClass java_class; // current class object 61 private final String dir; 62 private static String class_package; // name of package, unclean to make it static, but ... 63 private static String class_name; // name of current class, dito 64 private static ConstantPool constant_pool; 65 private static final Set<String> basic_types = new HashSet<>(); 66 67 static { 68 basic_types.add("int"); 69 basic_types.add("short"); 70 basic_types.add("boolean"); 71 basic_types.add("void"); 72 basic_types.add("char"); 73 basic_types.add("byte"); 74 basic_types.add("long"); 75 basic_types.add("double"); 76 basic_types.add("float"); 77 } 78 79 /** 80 * Write contents of the given JavaClass into HTML files. 81 * 82 * @param java_class The class to write 83 * @param dir The directory to put the files in 84 */ 85 public Class2HTML(final JavaClass java_class, final String dir) throws IOException { 86 final Method[] methods = java_class.getMethods(); 87 this.java_class = java_class; 88 this.dir = dir; 89 class_name = java_class.getClassName(); // Remember full name 90 constant_pool = java_class.getConstantPool(); 91 // Get package name by tacking off everything after the last `.' 92 final int index = class_name.lastIndexOf('.'); 93 if (index > -1) { 94 class_package = class_name.substring(0, index); 95 } else { 96 class_package = ""; // default package 97 } 98 final ConstantHTML constant_html = new ConstantHTML(dir, class_name, class_package, methods, 99 constant_pool); 100 /* Attributes can't be written in one step, so we just open a file 101 * which will be written consequently. 102 */ 103 final AttributeHTML attribute_html = new AttributeHTML(dir, class_name, constant_pool, 104 constant_html); 105 new MethodHTML(dir, class_name, methods, java_class.getFields(), 106 constant_html, attribute_html); 107 // Write main file (with frames, yuk) 108 writeMainHTML(attribute_html); 109 new CodeHTML(dir, class_name, methods, constant_pool, constant_html); 110 attribute_html.close(); 111 } 112 113 114 public static void main( final String[] argv ) throws IOException { 115 final String[] file_name = new String[argv.length]; 116 int files = 0; 117 ClassParser parser = null; 118 JavaClass java_class = null; 119 String zip_file = null; 120 final char sep = File.separatorChar; 121 String dir = "." + sep; // Where to store HTML files 122 /* Parse command line arguments. 123 */ 124 for (int i = 0; i < argv.length; i++) { 125 if (argv[i].charAt(0) == '-') { // command line switch 126 if (argv[i].equals("-d")) { // Specify target directory, default '.' 127 dir = argv[++i]; 128 if (!dir.endsWith("" + sep)) { 129 dir = dir + sep; 130 } 131 final File store = new File(dir); 132 if (!store.isDirectory()) { 133 final boolean created = store.mkdirs(); // Create target directory if necessary 134 if (!created) { 135 if (!store.isDirectory()) { 136 System.out.println("Tried to create the directory " + dir + " but failed"); 137 } 138 } 139 } 140 } else if (argv[i].equals("-zip")) { 141 zip_file = argv[++i]; 142 } else { 143 System.out.println("Unknown option " + argv[i]); 144 } 145 } else { 146 file_name[files++] = argv[i]; 147 } 148 } 149 if (files == 0) { 150 System.err.println("Class2HTML: No input files specified."); 151 } else { // Loop through files ... 152 for (int i = 0; i < files; i++) { 153 System.out.print("Processing " + file_name[i] + "..."); 154 if (zip_file == null) { 155 parser = new ClassParser(file_name[i]); // Create parser object from file 156 } else { 157 parser = new ClassParser(zip_file, file_name[i]); // Create parser object from zip file 158 } 159 java_class = parser.parse(); 160 new Class2HTML(java_class, dir); 161 System.out.println("Done."); 162 } 163 } 164 } 165 166 /** 167 * Utility method that converts a class reference in the constant pool, 168 * i.e., an index to a string. 169 */ 170 static String referenceClass(final int index) { 171 String str = constant_pool.getConstantString(index, Const.CONSTANT_Class); 172 str = Utility.compactClassName(str); 173 str = Utility.compactClassName(str, class_package + ".", true); 174 return "<A HREF=\"" + class_name + "_cp.html#cp" + index + "\" TARGET=ConstantPool>" + str 175 + "</A>"; 176 } 177 178 179 static String referenceType( final String type ) { 180 String short_type = Utility.compactClassName(type); 181 short_type = Utility.compactClassName(short_type, class_package + ".", true); 182 final int index = type.indexOf('['); // Type is an array? 183 String base_type = type; 184 if (index > -1) { 185 base_type = type.substring(0, index); // Tack of the `[' 186 } 187 // test for basic type 188 if (basic_types.contains(base_type)) { 189 return "<FONT COLOR=\"#00FF00\">" + type + "</FONT>"; 190 } 191 return "<A HREF=\"" + base_type + ".html\" TARGET=_top>" + short_type + "</A>"; 192 } 193 194 195 static String toHTML( final String str ) { 196 final StringBuilder buf = new StringBuilder(); 197 for (int i = 0; i < str.length(); i++) { 198 char ch; 199 switch (ch = str.charAt(i)) { 200 case '<': 201 buf.append("<"); 202 break; 203 case '>': 204 buf.append(">"); 205 break; 206 case '\n': 207 buf.append("\\n"); 208 break; 209 case '\r': 210 buf.append("\\r"); 211 break; 212 default: 213 buf.append(ch); 214 } 215 } 216 return buf.toString(); 217 } 218 219 220 private void writeMainHTML( final AttributeHTML attribute_html ) throws IOException { 221 try (PrintWriter file = new PrintWriter(new FileOutputStream(dir + class_name + ".html"))) { 222 file.println("<HTML>\n" + "<HEAD><TITLE>Documentation for " + class_name + "</TITLE>" + "</HEAD>\n" 223 + "<FRAMESET BORDER=1 cols=\"30%,*\">\n" + "<FRAMESET BORDER=1 rows=\"80%,*\">\n" 224 + "<FRAME NAME=\"ConstantPool\" SRC=\"" + class_name + "_cp.html" + "\"\n MARGINWIDTH=\"0\" " 225 + "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n" + "<FRAME NAME=\"Attributes\" SRC=\"" 226 + class_name + "_attributes.html" + "\"\n MARGINWIDTH=\"0\" " 227 + "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n" + "</FRAMESET>\n" 228 + "<FRAMESET BORDER=1 rows=\"80%,*\">\n" + "<FRAME NAME=\"Code\" SRC=\"" + class_name 229 + "_code.html\"\n MARGINWIDTH=0 " + "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n" 230 + "<FRAME NAME=\"Methods\" SRC=\"" + class_name + "_methods.html\"\n MARGINWIDTH=0 " 231 + "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n" + "</FRAMESET></FRAMESET></HTML>"); 232 } 233 final Attribute[] attributes = java_class.getAttributes(); 234 for (int i = 0; i < attributes.length; i++) { 235 attribute_html.writeAttribute(attributes[i], "class" + i); 236 } 237 } 238 }