1. package com.sun.org.apache.bcel.internal.util;
  2. /* ====================================================================
  3. * The Apache Software License, Version 1.1
  4. *
  5. * Copyright (c) 2001 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Apache" and "Apache Software Foundation" and
  28. * "Apache BCEL" must not be used to endorse or promote products
  29. * derived from this software without prior written permission. For
  30. * written permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * "Apache BCEL", nor may "Apache" appear in their name, without
  34. * prior written permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation. For more
  52. * information on the Apache Software Foundation, please see
  53. * <http://www.apache.org/>.
  54. */
  55. import java.io.*;
  56. import java.util.BitSet;
  57. import com.sun.org.apache.bcel.internal.classfile.*;
  58. import com.sun.org.apache.bcel.internal.Constants;
  59. /**
  60. * Read class file(s) and convert them into HTML files.
  61. *
  62. * Given a JavaClass object "class" that is in package "package" five files
  63. * will be created in the specified directory.
  64. *
  65. * <OL>
  66. * <LI> "package"."class".html as the main file which defines the frames for
  67. * the following subfiles.
  68. * <LI> "package"."class"_attributes.html contains all (known) attributes found in the file
  69. * <LI> "package"."class"_cp.html contains the constant pool
  70. * <LI> "package"."class"_code.html contains the byte code
  71. * <LI> "package"."class"_methods.html contains references to all methods and fields of the class
  72. * </OL>
  73. *
  74. * All subfiles reference each other appropiately, e.g. clicking on a
  75. * method in the Method's frame will jump to the appropiate method in
  76. * the Code frame.
  77. *
  78. * @version $Id: Class2HTML.java,v 1.1 2003/12/12 09:01:28 rameshm Exp $
  79. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  80. */
  81. public class Class2HTML implements Constants
  82. {
  83. private JavaClass java_class; // current class object
  84. private String dir;
  85. private static String class_package; // name of package, unclean to make it static, but ...
  86. private static String class_name; // name of current class, dito
  87. private static ConstantPool constant_pool;
  88. /**
  89. * Write contents of the given JavaClass into HTML files.
  90. *
  91. * @param java_class The class to write
  92. * @param dir The directory to put the files in
  93. */
  94. public Class2HTML(JavaClass java_class, String dir) throws IOException {
  95. Method[] methods = java_class.getMethods();
  96. this.java_class = java_class;
  97. this.dir = dir;
  98. class_name = java_class.getClassName(); // Remember full name
  99. constant_pool = java_class.getConstantPool();
  100. // Get package name by tacking off everything after the last `.'
  101. int index = class_name.lastIndexOf('.');
  102. if(index > -1)
  103. class_package = class_name.substring(0, index);
  104. else
  105. class_package = ""; // default package
  106. ConstantHTML constant_html = new ConstantHTML(dir, class_name, class_package, methods,
  107. constant_pool);
  108. /* Attributes can't be written in one step, so we just open a file
  109. * which will be written consequently.
  110. */
  111. AttributeHTML attribute_html = new AttributeHTML(dir, class_name, constant_pool, constant_html);
  112. MethodHTML method_html = new MethodHTML(dir, class_name, methods, java_class.getFields(),
  113. constant_html, attribute_html);
  114. // Write main file (with frames, yuk)
  115. writeMainHTML(attribute_html);
  116. new CodeHTML(dir, class_name, methods, constant_pool, constant_html);
  117. attribute_html.close();
  118. }
  119. public static void _main(String argv[])
  120. {
  121. String[] file_name = new String[argv.length];
  122. int files=0;
  123. ClassParser parser=null;
  124. JavaClass java_class=null;
  125. String zip_file = null;
  126. char sep = java.io.File.separatorChar;
  127. String dir = "." + sep; // Where to store HTML files
  128. try {
  129. /* Parse command line arguments.
  130. */
  131. for(int i=0; i < argv.length; i++) {
  132. if(argv[i].charAt(0) == '-') { // command line switch
  133. if(argv[i].equals("-d")) { // Specify target directory, default `.Ĉ
  134. dir = argv[++i];
  135. if(!dir.endsWith("" + sep))
  136. dir = dir + sep;
  137. new File(dir).mkdirs(); // Create target directory if necessary
  138. }
  139. else if(argv[i].equals("-zip"))
  140. zip_file = argv[++i];
  141. else
  142. System.out.println("Unknown option " + argv[i]);
  143. }
  144. else // add file name to list */
  145. file_name[files++] = argv[i];
  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. java_class = parser.parse();
  157. new Class2HTML(java_class, dir);
  158. System.out.println("Done.");
  159. }
  160. }
  161. } catch(Exception e) {
  162. System.out.println(e);
  163. e.printStackTrace(System.out);
  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(int index) {
  171. String str = constant_pool.getConstantString(index, 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 +
  175. "\" TARGET=ConstantPool>" + str + "</A>";
  176. }
  177. static final String referenceType(String type) {
  178. String short_type = Utility.compactClassName(type);
  179. short_type = Utility.compactClassName(short_type, class_package + ".", true);
  180. int index = type.indexOf('['); // Type is an array?
  181. if(index > -1)
  182. type = type.substring(0, index); // Tack of the `['
  183. // test for basic type
  184. if(type.equals("int") || type.equals("short") || type.equals("boolean") || type.equals("void") ||
  185. type.equals("char") || type.equals("byte") || type.equals("long") || type.equals("double") ||
  186. type.equals("float"))
  187. return "<FONT COLOR=\"#00FF00\">" + type + "</FONT>";
  188. else
  189. return "<A HREF=\"" + type + ".html\" TARGET=_top>" + short_type + "</A>";
  190. }
  191. static String toHTML(String str) {
  192. StringBuffer buf = new StringBuffer();
  193. try { // Filter any characters HTML doesn't like such as < and > in particular
  194. for(int i=0; i < str.length(); i++) {
  195. char ch;
  196. switch(ch=str.charAt(i)) {
  197. case '<': buf.append("<"); break;
  198. case '>': buf.append(">"); break;
  199. case '\n': buf.append("\\n"); break;
  200. case '\r': buf.append("\\r"); break;
  201. default: buf.append(ch);
  202. }
  203. }
  204. } catch(StringIndexOutOfBoundsException e) {} // Never occurs
  205. return buf.toString();
  206. }
  207. private void writeMainHTML(AttributeHTML attribute_html) throws IOException {
  208. PrintWriter file = new PrintWriter(new FileOutputStream(dir + class_name + ".html"));
  209. Attribute[] attributes = java_class.getAttributes();
  210. file.println("<HTML>\n" + "<HEAD><TITLE>Documentation for " + class_name + "</TITLE>" +
  211. "</HEAD>\n" +
  212. "<FRAMESET BORDER=1 cols=\"30%,*\">\n" +
  213. "<FRAMESET BORDER=1 rows=\"80%,*\">\n" +
  214. "<FRAME NAME=\"ConstantPool\" SRC=\"" + 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" +
  219. "</FRAMESET>\n" +
  220. "<FRAMESET BORDER=1 rows=\"80%,*\">\n" +
  221. "<FRAME NAME=\"Code\" SRC=\"" + class_name + "_code.html\"\n MARGINWIDTH=0 " +
  222. "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n" +
  223. "<FRAME NAME=\"Methods\" SRC=\"" + class_name + "_methods.html\"\n MARGINWIDTH=0 " +
  224. "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n" +
  225. "</FRAMESET></FRAMESET></HTML>"
  226. );
  227. file.close();
  228. for(int i=0; i < attributes.length; i++)
  229. attribute_html.writeAttribute(attributes[i], "class" + i);
  230. }
  231. }