View Javadoc

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