1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.util;
20
21 import java.io.File;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.io.UnsupportedEncodingException;
26 import java.nio.charset.Charset;
27 import java.nio.charset.StandardCharsets;
28 import java.util.HashSet;
29 import java.util.Set;
30
31 import org.apache.bcel.Const;
32 import org.apache.bcel.Constants;
33 import org.apache.bcel.classfile.Attribute;
34 import org.apache.bcel.classfile.ClassParser;
35 import org.apache.bcel.classfile.ConstantPool;
36 import org.apache.bcel.classfile.JavaClass;
37 import org.apache.bcel.classfile.Method;
38 import org.apache.bcel.classfile.Utility;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public class Class2HTML implements Constants {
57
58 private static String classPackage;
59 private static String className;
60 private static ConstantPool constantPool;
61 private static final Set<String> basicTypes = new HashSet<>();
62 static {
63 basicTypes.add("int");
64 basicTypes.add("short");
65 basicTypes.add("boolean");
66 basicTypes.add("void");
67 basicTypes.add("char");
68 basicTypes.add("byte");
69 basicTypes.add("long");
70 basicTypes.add("double");
71 basicTypes.add("float");
72 }
73
74 public static void main(final String[] argv) throws IOException {
75 final String[] fileName = new String[argv.length];
76 int files = 0;
77 ClassParser parser = null;
78 JavaClass javaClass = null;
79 String zipFile = null;
80 final char sep = File.separatorChar;
81 String dir = "." + sep;
82
83
84
85 for (int i = 0; i < argv.length; i++) {
86 if (argv[i].charAt(0) == '-') {
87 if (argv[i].equals("-d")) {
88 dir = argv[++i];
89 if (!dir.endsWith("" + sep)) {
90 dir += sep;
91 }
92 final File store = new File(dir);
93 if (!store.isDirectory()) {
94 final boolean created = store.mkdirs();
95 if (!created && !store.isDirectory()) {
96 System.out.println("Tried to create the directory " + dir + " but failed");
97 }
98 }
99 } else if (argv[i].equals("-zip")) {
100 zipFile = argv[++i];
101 } else {
102 System.out.println("Unknown option " + argv[i]);
103 }
104 } else {
105 fileName[files++] = argv[i];
106 }
107 }
108 if (files == 0) {
109 System.err.println("Class2HTML: No input files specified.");
110 } else {
111 for (int i = 0; i < files; i++) {
112 System.out.print("Processing " + fileName[i] + "...");
113 if (zipFile == null) {
114 parser = new ClassParser(fileName[i]);
115 } else {
116 parser = new ClassParser(zipFile, fileName[i]);
117 }
118 javaClass = parser.parse();
119 new Class2HTML(javaClass, dir);
120 System.out.println("Done.");
121 }
122 }
123 }
124
125
126
127
128 static String referenceClass(final int index) {
129 String str = constantPool.getConstantString(index, Const.CONSTANT_Class);
130 str = Utility.compactClassName(str);
131 str = Utility.compactClassName(str, classPackage + ".", true);
132 return "<A HREF=\"" + className + "_cp.html#cp" + index + "\" TARGET=ConstantPool>" + str + "</A>";
133 }
134
135 static String referenceType(final String type) {
136 String shortType = Utility.compactClassName(type);
137 shortType = Utility.compactClassName(shortType, classPackage + ".", true);
138 final int index = type.indexOf('[');
139 String baseType = type;
140 if (index > -1) {
141 baseType = type.substring(0, index);
142 }
143
144 if (basicTypes.contains(baseType)) {
145 return "<FONT COLOR=\"#00FF00\">" + type + "</FONT>";
146 }
147 return "<A HREF=\"" + baseType + ".html\" TARGET=_top>" + shortType + "</A>";
148 }
149
150 static String toHTML(final String str) {
151 final StringBuilder buf = new StringBuilder();
152 for (int i = 0; i < str.length(); i++) {
153 final char ch;
154 switch (ch = str.charAt(i)) {
155 case '<':
156 buf.append("<");
157 break;
158 case '>':
159 buf.append(">");
160 break;
161 case '\n':
162 buf.append("\\n");
163 break;
164 case '\r':
165 buf.append("\\r");
166 break;
167 default:
168 buf.append(ch);
169 }
170 }
171 return buf.toString();
172 }
173
174 private final JavaClass javaClass;
175
176 private final String dir;
177
178
179
180
181
182
183
184
185 public Class2HTML(final JavaClass javaClass, final String dir) throws IOException {
186 this(javaClass, dir, StandardCharsets.UTF_8);
187 }
188
189 private Class2HTML(final JavaClass javaClass, final String dir, final Charset charset) throws IOException {
190 final Method[] methods = javaClass.getMethods();
191 this.javaClass = javaClass;
192 this.dir = dir;
193 className = javaClass.getClassName();
194 constantPool = javaClass.getConstantPool();
195
196 final int index = className.lastIndexOf('.');
197 if (index > -1) {
198 classPackage = className.substring(0, index);
199 } else {
200 classPackage = "";
201 }
202 final ConstantHTML constantHtml = new ConstantHTML(dir, className, classPackage, methods, constantPool, charset);
203
204
205
206 try (AttributeHTML attributeHtml = new AttributeHTML(dir, className, constantPool, constantHtml, charset)) {
207 new MethodHTML(dir, className, methods, javaClass.getFields(), constantHtml, attributeHtml, charset);
208
209 writeMainHTML(attributeHtml, charset);
210 new CodeHTML(dir, className, methods, constantPool, constantHtml, charset);
211 }
212 }
213
214 private void writeMainHTML(final AttributeHTML attributeHtml, final Charset charset) throws FileNotFoundException, UnsupportedEncodingException {
215 try (PrintWriter file = new PrintWriter(dir + className + ".html", charset.name())) {
216
217 file.println("<HTML>\n"
218 + "<HEAD><TITLE>Documentation for " + className + "</TITLE></HEAD>\n"
219 + "<FRAMESET BORDER=1 cols=\"30%,*\">\n"
220 + "<FRAMESET BORDER=1 rows=\"80%,*\">\n"
221 + "<FRAME NAME=\"ConstantPool\" SRC=\"" + className + "_cp.html" + "\"\n"
222 + "MARGINWIDTH=\"0\" "
223 + "MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n"
224 + "<FRAME NAME=\"Attributes\" SRC=\"" + className + "_attributes.html\"\n"
225 + " MARGINWIDTH=\"0\" MARGINHEIGHT=\"0\" FRAMEBORDER=\"1\" SCROLLING=\"AUTO\">\n"
226 + "</FRAMESET>\n"
227 + "<FRAMESET BORDER=1 rows=\"80%,*\">\n"
228 + "<FRAME NAME=\"Code\" SRC=\"" + className + "_code.html\"\n"
229 + " MARGINWIDTH=0 "
230 + "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n"
231 + "<FRAME NAME=\"Methods\" SRC=\"" + className + "_methods.html\"\n"
232 + " MARGINWIDTH=0 "
233 + "MARGINHEIGHT=0 FRAMEBORDER=1 SCROLLING=\"AUTO\">\n"
234 + "</FRAMESET></FRAMESET></HTML>");
235
236 }
237 final Attribute[] attributes = javaClass.getAttributes();
238 for (int i = 0; i < attributes.length; i++) {
239 attributeHtml.writeAttribute(attributes[i], "class" + i);
240 }
241 }
242 }