src/java.xml/share/classes/com/sun/org/apache/bcel/internal/util/Class2HTML.java
changeset 55496 8e0ae3830fca
parent 47216 71c04702a3d5
equal deleted inserted replaced
55495:badfa812b82a 55496:8e0ae3830fca
     1 /*
     1 /*
     2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
     3  */
     3  */
     4 /*
     4 /*
     5  * Licensed to the Apache Software Foundation (ASF) under one or more
     5  * Licensed to the Apache Software Foundation (ASF) under one or more
     6  * contributor license agreements.  See the NOTICE file distributed with
     6  * contributor license agreements.  See the NOTICE file distributed with
     7  * this work for additional information regarding copyright ownership.
     7  * this work for additional information regarding copyright ownership.
    35 import com.sun.org.apache.bcel.internal.classfile.Utility;
    35 import com.sun.org.apache.bcel.internal.classfile.Utility;
    36 
    36 
    37 /**
    37 /**
    38  * Read class file(s) and convert them into HTML files.
    38  * Read class file(s) and convert them into HTML files.
    39  *
    39  *
    40  * Given a JavaClass object "class" that is in package "package" five files will
    40  * Given a JavaClass object "class" that is in package "package" five files
    41  * be created in the specified directory.
    41  * will be created in the specified directory.
    42  *
    42  *
    43  * <OL>
    43  * <OL>
    44  * <LI> "package"."class".html as the main file which defines the frames for the
    44  * <LI> "package"."class".html as the main file which defines the frames for
    45  * following subfiles.
    45  * the following subfiles.
    46  * <LI> "package"."class"_attributes.html contains all (known) attributes found
    46  * <LI>  "package"."class"_attributes.html contains all (known) attributes found in the file
    47  * in the file
    47  * <LI>  "package"."class"_cp.html contains the constant pool
    48  * <LI> "package"."class"_cp.html contains the constant pool
    48  * <LI>  "package"."class"_code.html contains the byte code
    49  * <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  * <LI> "package"."class"_methods.html contains references to all methods and
       
    51  * fields of the class
       
    52  * </OL>
    50  * </OL>
    53  *
    51  *
    54  * All subfiles reference each other appropriately, e.g. clicking on a method in
    52  * All subfiles reference each other appropriately, e.g. clicking on a
    55  * the Method's frame will jump to the appropriate method in the Code frame.
    53  * method in the Method's frame will jump to the appropriate method in
    56  *
    54  * the Code frame.
    57  * @version $Id: Class2HTML.java 1749603 2016-06-21 20:50:19Z ggregory $
    55  *
       
    56  * @version $Id$
       
    57  * @LastModified: Jun 2019
    58  */
    58  */
    59 public class Class2HTML {
    59 public class Class2HTML {
    60 
    60 
    61     private final JavaClass java_class; // current class object
    61     private final JavaClass java_class; // current class object
    62     private final String dir;
    62     private final String dir;
   109         writeMainHTML(attribute_html);
   109         writeMainHTML(attribute_html);
   110         new CodeHTML(dir, class_name, methods, constant_pool, constant_html);
   110         new CodeHTML(dir, class_name, methods, constant_pool, constant_html);
   111         attribute_html.close();
   111         attribute_html.close();
   112     }
   112     }
   113 
   113 
   114     public static void _main(final String[] argv) throws IOException {
   114 
       
   115     public static void main( final String[] argv ) throws IOException {
   115         final String[] file_name = new String[argv.length];
   116         final String[] file_name = new String[argv.length];
   116         int files = 0;
   117         int files = 0;
   117         ClassParser parser = null;
   118         ClassParser parser = null;
   118         JavaClass java_class = null;
   119         JavaClass java_class = null;
   119         String zip_file = null;
   120         String zip_file = null;
   127                     dir = argv[++i];
   128                     dir = argv[++i];
   128                     if (!dir.endsWith("" + sep)) {
   129                     if (!dir.endsWith("" + sep)) {
   129                         dir = dir + sep;
   130                         dir = dir + sep;
   130                     }
   131                     }
   131                     final File store = new File(dir);
   132                     final File store = new File(dir);
   132 
       
   133                     if (!store.isDirectory()) {
   133                     if (!store.isDirectory()) {
   134                         final boolean created = store.mkdirs(); // Create target directory if necessary
   134                         final boolean created = store.mkdirs(); // Create target directory if necessary
   135                         if (!created) {
   135                         if (!created) {
   136                             if (!store.isDirectory()) {
   136                             if (!store.isDirectory()) {
   137                                 System.out.println("Tried to create the directory " + dir + " but failed");
   137                                 System.out.println("Tried to create the directory " + dir + " but failed");
   174         str = Utility.compactClassName(str, class_package + ".", true);
   174         str = Utility.compactClassName(str, class_package + ".", true);
   175         return "<A HREF=\"" + class_name + "_cp.html#cp" + index + "\" TARGET=ConstantPool>" + str
   175         return "<A HREF=\"" + class_name + "_cp.html#cp" + index + "\" TARGET=ConstantPool>" + str
   176                 + "</A>";
   176                 + "</A>";
   177     }
   177     }
   178 
   178 
   179     static String referenceType(final String type) {
   179 
       
   180     static String referenceType( final String type ) {
   180         String short_type = Utility.compactClassName(type);
   181         String short_type = Utility.compactClassName(type);
   181         short_type = Utility.compactClassName(short_type, class_package + ".", true);
   182         short_type = Utility.compactClassName(short_type, class_package + ".", true);
   182         final int index = type.indexOf('['); // Type is an array?
   183         final int index = type.indexOf('['); // Type is an array?
   183         String base_type = type;
   184         String base_type = type;
   184         if (index > -1) {
   185         if (index > -1) {
   189             return "<FONT COLOR=\"#00FF00\">" + type + "</FONT>";
   190             return "<FONT COLOR=\"#00FF00\">" + type + "</FONT>";
   190         }
   191         }
   191         return "<A HREF=\"" + base_type + ".html\" TARGET=_top>" + short_type + "</A>";
   192         return "<A HREF=\"" + base_type + ".html\" TARGET=_top>" + short_type + "</A>";
   192     }
   193     }
   193 
   194 
   194     static String toHTML(final String str) {
   195 
       
   196     static String toHTML( final String str ) {
   195         final StringBuilder buf = new StringBuilder();
   197         final StringBuilder buf = new StringBuilder();
   196         for (int i = 0; i < str.length(); i++) {
   198         for (int i = 0; i < str.length(); i++) {
   197             char ch;
   199             char ch;
   198             switch (ch = str.charAt(i)) {
   200             switch (ch = str.charAt(i)) {
   199                 case '<':
   201                 case '<':
   213             }
   215             }
   214         }
   216         }
   215         return buf.toString();
   217         return buf.toString();
   216     }
   218     }
   217 
   219 
   218     private void writeMainHTML(final AttributeHTML attribute_html) throws IOException {
   220 
       
   221     private void writeMainHTML( final AttributeHTML attribute_html ) throws IOException {
   219         try (PrintWriter file = new PrintWriter(new FileOutputStream(dir + class_name + ".html"))) {
   222         try (PrintWriter file = new PrintWriter(new FileOutputStream(dir + class_name + ".html"))) {
   220             file.println("<HTML>\n" + "<HEAD><TITLE>Documentation for " + class_name + "</TITLE>" + "</HEAD>\n"
   223             file.println("<HTML>\n" + "<HEAD><TITLE>Documentation for " + class_name + "</TITLE>" + "</HEAD>\n"
   221                     + "<FRAMESET BORDER=1 cols=\"30%,*\">\n" + "<FRAMESET BORDER=1 rows=\"80%,*\">\n"
   224                     + "<FRAMESET BORDER=1 cols=\"30%,*\">\n" + "<FRAMESET BORDER=1 rows=\"80%,*\">\n"
   222                     + "<FRAME NAME=\"ConstantPool\" SRC=\"" + class_name + "_cp.html" + "\"\n MARGINWIDTH=\"0\" "
   225                     + "<FRAME NAME=\"ConstantPool\" SRC=\"" + class_name + "_cp.html" + "\"\n MARGINWIDTH=\"0\" "
   223                     + "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n" + "<FRAME NAME=\"Attributes\" SRC=\""
   226                     + "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n" + "<FRAME NAME=\"Attributes\" SRC=\""