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