10
|
1 |
/*
|
|
2 |
* Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.tools.doclets.internal.toolkit.util;
|
|
27 |
|
|
28 |
import com.sun.tools.doclets.internal.toolkit.*;
|
|
29 |
import com.sun.javadoc.*;
|
|
30 |
import java.io.*;
|
|
31 |
import java.util.*;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* Converts Java Source Code to HTML.
|
|
35 |
*
|
|
36 |
* This code is not part of an API.
|
|
37 |
* It is implementation that is subject to change.
|
|
38 |
* Do not use it as an API
|
|
39 |
*
|
|
40 |
* @author Jamie Ho
|
|
41 |
* @since 1.4
|
|
42 |
*/
|
|
43 |
public class SourceToHTMLConverter {
|
|
44 |
|
|
45 |
/**
|
|
46 |
* The background color.
|
|
47 |
*/
|
|
48 |
protected static final String BGCOLOR = "white";
|
|
49 |
|
|
50 |
/**
|
|
51 |
* The line number color.
|
|
52 |
*/
|
|
53 |
protected static final String LINE_NO_COLOR = "green";
|
|
54 |
|
|
55 |
/**
|
|
56 |
* The number of trailing blank lines at the end of the page.
|
|
57 |
* This is inserted so that anchors at the bottom of small pages
|
|
58 |
* can be reached.
|
|
59 |
*/
|
|
60 |
protected static final int NUM_BLANK_LINES = 60;
|
|
61 |
|
|
62 |
|
|
63 |
/**
|
|
64 |
* Source is converted to HTML using static methods below.
|
|
65 |
*/
|
|
66 |
private SourceToHTMLConverter() {}
|
|
67 |
|
|
68 |
/**
|
|
69 |
* Convert the Classes in the given RootDoc to an HTML.
|
|
70 |
* @param configuration the configuration.
|
|
71 |
* @param rd the RootDoc to convert.
|
|
72 |
* @param outputdir the name of the directory to output to.
|
|
73 |
*/
|
|
74 |
public static void convertRoot(Configuration configuration, RootDoc rd, String outputdir) {
|
|
75 |
if (rd == null || outputdir == null) {
|
|
76 |
return;
|
|
77 |
}
|
|
78 |
PackageDoc[] pds = rd.specifiedPackages();
|
|
79 |
for (int i = 0; i < pds.length; i++) {
|
|
80 |
convertPackage(configuration, pds[i], outputdir);
|
|
81 |
}
|
|
82 |
ClassDoc[] cds = rd.specifiedClasses();
|
|
83 |
for (int i = 0; i < cds.length; i++) {
|
|
84 |
convertClass(configuration, cds[i],
|
|
85 |
getPackageOutputDir(outputdir, cds[i].containingPackage()));
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
/**
|
|
90 |
* Convert the Classes in the given Package to an HTML.
|
|
91 |
* @param configuration the configuration.
|
|
92 |
* @param pd the Package to convert.
|
|
93 |
* @param outputdir the name of the directory to output to.
|
|
94 |
*/
|
|
95 |
public static void convertPackage(Configuration configuration, PackageDoc pd, String outputdir) {
|
|
96 |
if (pd == null || outputdir == null) {
|
|
97 |
return;
|
|
98 |
}
|
|
99 |
String classOutputdir = getPackageOutputDir(outputdir, pd);
|
|
100 |
ClassDoc[] cds = pd.allClasses();
|
|
101 |
for (int i = 0; i < cds.length; i++) {
|
|
102 |
convertClass(configuration, cds[i], classOutputdir);
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
/**
|
|
107 |
* Return the directory write output to for the given package.
|
|
108 |
* @param outputDir the directory to output to.
|
|
109 |
* @param pd the Package to generate output for.
|
|
110 |
*/
|
|
111 |
private static String getPackageOutputDir(String outputDir, PackageDoc pd) {
|
|
112 |
return outputDir + File.separator +
|
|
113 |
DirectoryManager.getDirectoryPath(pd) + File.separator;
|
|
114 |
}
|
|
115 |
|
|
116 |
/**
|
|
117 |
* Convert the given Class to an HTML.
|
|
118 |
* @param configuration the configuration.
|
|
119 |
* @param cd the class to convert.
|
|
120 |
* @param outputdir the name of the directory to output to.
|
|
121 |
*/
|
|
122 |
public static void convertClass(Configuration configuration, ClassDoc cd, String outputdir) {
|
|
123 |
if (cd == null || outputdir == null) {
|
|
124 |
return;
|
|
125 |
}
|
|
126 |
File file;
|
|
127 |
SourcePosition sp = cd.position();
|
|
128 |
if (sp == null || (file = sp.file()) == null) {
|
|
129 |
return;
|
|
130 |
}
|
|
131 |
try {
|
|
132 |
int lineno = 1;
|
|
133 |
String line;
|
|
134 |
StringBuffer output = new StringBuffer();
|
|
135 |
LineNumberReader reader = new LineNumberReader(new FileReader(file));
|
|
136 |
try {
|
|
137 |
while ((line = reader.readLine()) != null) {
|
|
138 |
output.append(formatLine(line, configuration.sourcetab, lineno));
|
|
139 |
lineno++;
|
|
140 |
}
|
|
141 |
} finally {
|
|
142 |
reader.close();
|
|
143 |
}
|
|
144 |
output = addLineNumbers(output.toString());
|
|
145 |
output.insert(0, getHeader());
|
|
146 |
output.append(getFooter());
|
|
147 |
writeToFile(output.toString(), outputdir, cd.name(), configuration);
|
|
148 |
} catch (Exception e){
|
|
149 |
e.printStackTrace();
|
|
150 |
}
|
|
151 |
}
|
|
152 |
|
|
153 |
/**
|
|
154 |
* Write the output to the file.
|
|
155 |
* @param output the string to output.
|
|
156 |
* @param outputDir the directory to output to.
|
|
157 |
* @param className the name of the class that I am converting to HTML.
|
|
158 |
* @param configuration the Doclet configuration to pass notices to.
|
|
159 |
*/
|
|
160 |
private static void writeToFile(String output, String outputDir, String className, Configuration configuration) throws IOException {
|
|
161 |
File dir = new File(outputDir);
|
|
162 |
dir.mkdirs();
|
|
163 |
File newFile = new File(dir, className + ".html");
|
|
164 |
configuration.message.notice("doclet.Generating_0", newFile.getPath());
|
|
165 |
FileOutputStream fout = new FileOutputStream(newFile);
|
|
166 |
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout));
|
|
167 |
bw.write(output);
|
|
168 |
bw.close();
|
|
169 |
fout.close();
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
* Given a <code>String</code>, add line numbers.
|
|
174 |
* @param s the text to add line numbers to.
|
|
175 |
*
|
|
176 |
* @return the string buffer with the line numbering for each line.
|
|
177 |
*/
|
|
178 |
private static StringBuffer addLineNumbers(String s) {
|
|
179 |
StringBuffer sb = new StringBuffer();
|
|
180 |
StringTokenizer st = new StringTokenizer(s, "\n", true);
|
|
181 |
int lineno = 1;
|
|
182 |
String current;
|
|
183 |
while(st.hasMoreTokens()){
|
|
184 |
current = st.nextToken();
|
|
185 |
sb.append(current.equals("\n") ?
|
|
186 |
getHTMLLineNo(lineno) + current :
|
|
187 |
getHTMLLineNo(lineno) + current + st.nextToken());
|
|
188 |
lineno++;
|
|
189 |
}
|
|
190 |
return sb;
|
|
191 |
}
|
|
192 |
|
|
193 |
/**
|
|
194 |
* Get the header.
|
|
195 |
* @return the header to the output file
|
|
196 |
*/
|
|
197 |
protected static String getHeader() {
|
|
198 |
StringBuffer result = new StringBuffer("<HTML>" + DocletConstants.NL);
|
|
199 |
result.append("<BODY BGCOLOR=\""+ BGCOLOR + "\">" + DocletConstants.NL);
|
|
200 |
result.append("<PRE>" + DocletConstants.NL);
|
|
201 |
return result.toString();
|
|
202 |
}
|
|
203 |
|
|
204 |
/**
|
|
205 |
* Get the footer
|
|
206 |
* @return the footer to the output file
|
|
207 |
*/
|
|
208 |
protected static String getFooter() {
|
|
209 |
StringBuffer footer = new StringBuffer();
|
|
210 |
for (int i = 0; i < NUM_BLANK_LINES; i++) {
|
|
211 |
footer.append(DocletConstants.NL);
|
|
212 |
}
|
|
213 |
footer.append("</PRE>" + DocletConstants.NL + "</BODY>" +
|
|
214 |
DocletConstants.NL + "</HTML>" + DocletConstants.NL);
|
|
215 |
return footer.toString();
|
|
216 |
}
|
|
217 |
|
|
218 |
/**
|
|
219 |
* Get the HTML for the lines.
|
|
220 |
* @param lineno The line number
|
|
221 |
* @return the HTML code for the line
|
|
222 |
*/
|
|
223 |
protected static String getHTMLLineNo(int lineno) {
|
|
224 |
StringBuffer result = new StringBuffer("<FONT color=\"" + LINE_NO_COLOR
|
|
225 |
+ "\">");
|
|
226 |
if (lineno < 10) {
|
|
227 |
result.append("00" + ((new Integer(lineno)).toString()));
|
|
228 |
} else if (lineno < 100) {
|
|
229 |
result.append("0" + ((new Integer(lineno)).toString()));
|
|
230 |
} else {
|
|
231 |
result.append((new Integer(lineno)).toString());
|
|
232 |
}
|
|
233 |
result.append("</FONT> ");
|
|
234 |
return result.toString();
|
|
235 |
}
|
|
236 |
|
|
237 |
/**
|
|
238 |
* Format a given line of source. <br>
|
|
239 |
* Note: In the future, we will add special colors for constructs in the
|
|
240 |
* language.
|
|
241 |
* @param line the string to format.
|
|
242 |
* @param tabLength the number of spaces for each tab.
|
|
243 |
* @param currentLineNo the current number.
|
|
244 |
*/
|
|
245 |
protected static String formatLine(String line, int tabLength, int currentLineNo) {
|
|
246 |
if (line == null) {
|
|
247 |
return null;
|
|
248 |
}
|
|
249 |
StringBuffer lineBuffer = new StringBuffer(Util.escapeHtmlChars(line));
|
|
250 |
//Insert an anchor for the line
|
|
251 |
lineBuffer.append("<a name=\"line." + Integer.toString(currentLineNo) + "\"></a>");
|
|
252 |
lineBuffer.append(DocletConstants.NL);
|
|
253 |
Util.replaceTabs(tabLength, lineBuffer);
|
|
254 |
return lineBuffer.toString();
|
|
255 |
}
|
|
256 |
|
|
257 |
/**
|
|
258 |
* Given an array of <code>Doc</code>s, add to the given <code>HashMap</code> the
|
|
259 |
* line numbers and anchors that should be inserted in the output at those lines.
|
|
260 |
* @param docs the array of <code>Doc</code>s to add anchors for.
|
|
261 |
* @param hash the <code>HashMap</code> to add to.
|
|
262 |
*/
|
868
|
263 |
protected static void addToHash(Doc[] docs, HashMap<Integer,String> hash) {
|
10
|
264 |
if(docs == null) {
|
|
265 |
return;
|
|
266 |
}
|
|
267 |
for(int i = 0; i < docs.length; i++) {
|
868
|
268 |
hash.put(docs[i].position().line(), getAnchor(docs[i]));
|
10
|
269 |
}
|
|
270 |
}
|
|
271 |
|
|
272 |
/**
|
|
273 |
* Given a <code>Doc</code>, return an anchor for it.
|
|
274 |
* @param d the <code>Doc</code> to check.
|
|
275 |
* @return an anchor of the form <a name="my_name"></a>
|
|
276 |
*/
|
|
277 |
protected static String getAnchor(Doc d) {
|
|
278 |
return " <a name=\"" + getAnchorName(d) + "\"></a>";
|
|
279 |
}
|
|
280 |
|
|
281 |
/**
|
|
282 |
* Given a <code>Doc</code>, return an anchor name for it.
|
|
283 |
* @param d the <code>Doc</code> to check.
|
|
284 |
* @return the name of the anchor.
|
|
285 |
*/
|
|
286 |
public static String getAnchorName(Doc d) {
|
|
287 |
return "line." + d.position().line();
|
|
288 |
}
|
|
289 |
}
|