001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 */
018 package org.apache.bcel.util;
019
020 import java.io.File;
021 import java.io.FileOutputStream;
022 import java.io.IOException;
023 import java.io.PrintWriter;
024 import org.apache.bcel.Constants;
025 import org.apache.bcel.classfile.Attribute;
026 import org.apache.bcel.classfile.ClassParser;
027 import org.apache.bcel.classfile.ConstantPool;
028 import org.apache.bcel.classfile.JavaClass;
029 import org.apache.bcel.classfile.Method;
030 import org.apache.bcel.classfile.Utility;
031
032 /**
033 * Read class file(s) and convert them into HTML files.
034 *
035 * Given a JavaClass object "class" that is in package "package" five files
036 * will be created in the specified directory.
037 *
038 * <OL>
039 * <LI> "package"."class".html as the main file which defines the frames for
040 * the following subfiles.
041 * <LI> "package"."class"_attributes.html contains all (known) attributes found in the file
042 * <LI> "package"."class"_cp.html contains the constant pool
043 * <LI> "package"."class"_code.html contains the byte code
044 * <LI> "package"."class"_methods.html contains references to all methods and fields of the class
045 * </OL>
046 *
047 * All subfiles reference each other appropiately, e.g. clicking on a
048 * method in the Method's frame will jump to the appropiate method in
049 * the Code frame.
050 *
051 * @version $Id: Class2HTML.java 1152077 2011-07-29 02:29:42Z dbrosius $
052 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
053 */
054 public class Class2HTML implements Constants {
055
056 private JavaClass java_class; // current class object
057 private String dir;
058 private static String class_package; // name of package, unclean to make it static, but ...
059 private static String class_name; // name of current class, dito
060 private static ConstantPool constant_pool;
061
062
063 /**
064 * Write contents of the given JavaClass into HTML files.
065 *
066 * @param java_class The class to write
067 * @param dir The directory to put the files in
068 */
069 public Class2HTML(JavaClass java_class, String dir) throws IOException {
070 Method[] methods = java_class.getMethods();
071 this.java_class = java_class;
072 this.dir = dir;
073 class_name = java_class.getClassName(); // Remember full name
074 constant_pool = java_class.getConstantPool();
075 // Get package name by tacking off everything after the last `.'
076 int index = class_name.lastIndexOf('.');
077 if (index > -1) {
078 class_package = class_name.substring(0, index);
079 } else {
080 class_package = ""; // default package
081 }
082 ConstantHTML constant_html = new ConstantHTML(dir, class_name, class_package, methods,
083 constant_pool);
084 /* Attributes can't be written in one step, so we just open a file
085 * which will be written consequently.
086 */
087 AttributeHTML attribute_html = new AttributeHTML(dir, class_name, constant_pool,
088 constant_html);
089 // MethodHTML method_html = new MethodHTML(dir, class_name, methods, java_class.getFields(),
090 // constant_html, attribute_html);
091 // Write main file (with frames, yuk)
092 writeMainHTML(attribute_html);
093 new CodeHTML(dir, class_name, methods, constant_pool, constant_html);
094 attribute_html.close();
095 }
096
097
098 public static void main( String argv[] ) {
099 String[] file_name = new String[argv.length];
100 int files = 0;
101 ClassParser parser = null;
102 JavaClass java_class = null;
103 String zip_file = null;
104 char sep = System.getProperty("file.separator").toCharArray()[0];
105 String dir = "." + sep; // Where to store HTML files
106 try {
107 /* Parse command line arguments.
108 */
109 for (int i = 0; i < argv.length; i++) {
110 if (argv[i].charAt(0) == '-') { // command line switch
111 if (argv[i].equals("-d")) { // Specify target directory, default `.�
112 dir = argv[++i];
113 if (!dir.endsWith("" + sep)) {
114 dir = dir + sep;
115 }
116 new File(dir).mkdirs(); // Create target directory if necessary
117 } else if (argv[i].equals("-zip")) {
118 zip_file = argv[++i];
119 } else {
120 System.out.println("Unknown option " + argv[i]);
121 }
122 } else {
123 file_name[files++] = argv[i];
124 }
125 }
126 if (files == 0) {
127 System.err.println("Class2HTML: No input files specified.");
128 } else { // Loop through files ...
129 for (int i = 0; i < files; i++) {
130 System.out.print("Processing " + file_name[i] + "...");
131 if (zip_file == null) {
132 parser = new ClassParser(file_name[i]); // Create parser object from file
133 } else {
134 parser = new ClassParser(zip_file, file_name[i]); // Create parser object from zip file
135 }
136 java_class = parser.parse();
137 new Class2HTML(java_class, dir);
138 System.out.println("Done.");
139 }
140 }
141 } catch (Exception e) {
142 System.out.println(e);
143 e.printStackTrace(System.out);
144 }
145 }
146
147
148 /**
149 * Utility method that converts a class reference in the constant pool,
150 * i.e., an index to a string.
151 */
152 static String referenceClass( int index ) {
153 String str = constant_pool.getConstantString(index, CONSTANT_Class);
154 str = Utility.compactClassName(str);
155 str = Utility.compactClassName(str, class_package + ".", true);
156 return "<A HREF=\"" + class_name + "_cp.html#cp" + index + "\" TARGET=ConstantPool>" + str
157 + "</A>";
158 }
159
160
161 static final String referenceType( String type ) {
162 String short_type = Utility.compactClassName(type);
163 short_type = Utility.compactClassName(short_type, class_package + ".", true);
164 int index = type.indexOf('['); // Type is an array?
165 String base_type = type;
166 if (index > -1) {
167 base_type = type.substring(0, index); // Tack of the `['
168 }
169 // test for basic type
170 if (base_type.equals("int") || base_type.equals("short") || base_type.equals("boolean")
171 || base_type.equals("void") || base_type.equals("char") || base_type.equals("byte")
172 || base_type.equals("long") || base_type.equals("double")
173 || base_type.equals("float")) {
174 return "<FONT COLOR=\"#00FF00\">" + type + "</FONT>";
175 } else {
176 return "<A HREF=\"" + base_type + ".html\" TARGET=_top>" + short_type + "</A>";
177 }
178 }
179
180
181 static String toHTML( String str ) {
182 StringBuilder buf = new StringBuilder();
183 try { // Filter any characters HTML doesn't like such as < and > in particular
184 for (int i = 0; i < str.length(); i++) {
185 char ch;
186 switch (ch = str.charAt(i)) {
187 case '<':
188 buf.append("<");
189 break;
190 case '>':
191 buf.append(">");
192 break;
193 case '\n':
194 buf.append("\\n");
195 break;
196 case '\r':
197 buf.append("\\r");
198 break;
199 default:
200 buf.append(ch);
201 }
202 }
203 } catch (StringIndexOutOfBoundsException e) {
204 } // Never occurs
205 return buf.toString();
206 }
207
208
209 private void writeMainHTML( AttributeHTML attribute_html ) throws IOException {
210 PrintWriter file = new PrintWriter(new FileOutputStream(dir + class_name + ".html"));
211 Attribute[] attributes = java_class.getAttributes();
212 file.println("<HTML>\n" + "<HEAD><TITLE>Documentation for " + class_name + "</TITLE>"
213 + "</HEAD>\n" + "<FRAMESET BORDER=1 cols=\"30%,*\">\n"
214 + "<FRAMESET BORDER=1 rows=\"80%,*\">\n" + "<FRAME NAME=\"ConstantPool\" SRC=\""
215 + class_name + "_cp.html" + "\"\n MARGINWIDTH=\"0\" "
216 + "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n"
217 + "<FRAME NAME=\"Attributes\" SRC=\"" + class_name + "_attributes.html"
218 + "\"\n MARGINWIDTH=\"0\" "
219 + "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n" + "</FRAMESET>\n"
220 + "<FRAMESET BORDER=1 rows=\"80%,*\">\n" + "<FRAME NAME=\"Code\" SRC=\""
221 + class_name + "_code.html\"\n MARGINWIDTH=0 "
222 + "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n"
223 + "<FRAME NAME=\"Methods\" SRC=\"" + class_name
224 + "_methods.html\"\n MARGINWIDTH=0 "
225 + "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n"
226 + "</FRAMESET></FRAMESET></HTML>");
227 file.close();
228 for (int i = 0; i < attributes.length; i++) {
229 attribute_html.writeAttribute(attributes[i], "class" + i);
230 }
231 }
232 }