001: /*
002: * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.doclets.internal.toolkit.util;
027:
028: import com.sun.tools.doclets.internal.toolkit.*;
029: import com.sun.javadoc.*;
030: import java.io.*;
031: import java.util.*;
032:
033: /**
034: * Converts Java Source Code to HTML.
035: *
036: * This code is not part of an API.
037: * It is implementation that is subject to change.
038: * Do not use it as an API
039: *
040: * @author Jamie Ho
041: * @since 1.4
042: */
043: public class SourceToHTMLConverter {
044:
045: /**
046: * The background color.
047: */
048: protected static final String BGCOLOR = "white";
049:
050: /**
051: * The line number color.
052: */
053: protected static final String LINE_NO_COLOR = "green";
054:
055: /**
056: * The number of trailing blank lines at the end of the page.
057: * This is inserted so that anchors at the bottom of small pages
058: * can be reached.
059: */
060: protected static final int NUM_BLANK_LINES = 60;
061:
062: /**
063: * Source is converted to HTML using static methods below.
064: */
065: private SourceToHTMLConverter() {
066: }
067:
068: /**
069: * Convert the Classes in the given RootDoc to an HTML.
070: * @param configuration the configuration.
071: * @param rd the RootDoc to convert.
072: * @param outputdir the name of the directory to output to.
073: */
074: public static void convertRoot(Configuration configuration,
075: RootDoc rd, String outputdir) {
076: if (rd == null || outputdir == null) {
077: return;
078: }
079: PackageDoc[] pds = rd.specifiedPackages();
080: for (int i = 0; i < pds.length; i++) {
081: convertPackage(configuration, pds[i], outputdir);
082: }
083: ClassDoc[] cds = rd.specifiedClasses();
084: for (int i = 0; i < cds.length; i++) {
085: convertClass(configuration, cds[i], getPackageOutputDir(
086: outputdir, cds[i].containingPackage()));
087: }
088: }
089:
090: /**
091: * Convert the Classes in the given Package to an HTML.
092: * @param configuration the configuration.
093: * @param pd the Package to convert.
094: * @param outputdir the name of the directory to output to.
095: */
096: public static void convertPackage(Configuration configuration,
097: PackageDoc pd, String outputdir) {
098: if (pd == null || outputdir == null) {
099: return;
100: }
101: String classOutputdir = getPackageOutputDir(outputdir, pd);
102: ClassDoc[] cds = pd.allClasses();
103: for (int i = 0; i < cds.length; i++) {
104: convertClass(configuration, cds[i], classOutputdir);
105: }
106: }
107:
108: /**
109: * Return the directory write output to for the given package.
110: * @param outputDir the directory to output to.
111: * @param pd the Package to generate output for.
112: */
113: private static String getPackageOutputDir(String outputDir,
114: PackageDoc pd) {
115: return outputDir + File.separator
116: + DirectoryManager.getDirectoryPath(pd)
117: + File.separator;
118: }
119:
120: /**
121: * Convert the given Class to an HTML.
122: * @param configuration the configuration.
123: * @param cd the class to convert.
124: * @param outputdir the name of the directory to output to.
125: */
126: public static void convertClass(Configuration configuration,
127: ClassDoc cd, String outputdir) {
128: if (cd == null || outputdir == null) {
129: return;
130: }
131: File file;
132: SourcePosition sp = cd.position();
133: if (sp == null || (file = sp.file()) == null) {
134: return;
135: }
136: try {
137: int lineno = 1;
138: String line;
139: StringBuffer output = new StringBuffer();
140: LineNumberReader reader = new LineNumberReader(
141: new FileReader(file));
142: try {
143: while ((line = reader.readLine()) != null) {
144: output.append(formatLine(line,
145: configuration.sourcetab, lineno));
146: lineno++;
147: }
148: } finally {
149: reader.close();
150: }
151: output = addLineNumbers(output.toString());
152: output.insert(0, getHeader());
153: output.append(getFooter());
154: writeToFile(output.toString(), outputdir, cd.name(),
155: configuration);
156: } catch (Exception e) {
157: e.printStackTrace();
158: }
159: }
160:
161: /**
162: * Write the output to the file.
163: * @param output the string to output.
164: * @param outputDir the directory to output to.
165: * @param className the name of the class that I am converting to HTML.
166: * @param configuration the Doclet configuration to pass notices to.
167: */
168: private static void writeToFile(String output, String outputDir,
169: String className, Configuration configuration)
170: throws IOException {
171: File dir = new File(outputDir);
172: dir.mkdirs();
173: File newFile = new File(dir, className + ".html");
174: configuration.message.notice("doclet.Generating_0", newFile
175: .getPath());
176: FileOutputStream fout = new FileOutputStream(newFile);
177: BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
178: fout));
179: bw.write(output);
180: bw.close();
181: fout.close();
182: }
183:
184: /**
185: * Given a <code>String</code>, add line numbers.
186: * @param s the text to add line numbers to.
187: *
188: * @return the string buffer with the line numbering for each line.
189: */
190: private static StringBuffer addLineNumbers(String s) {
191: StringBuffer sb = new StringBuffer();
192: StringTokenizer st = new StringTokenizer(s, "\n", true);
193: int lineno = 1;
194: String current;
195: while (st.hasMoreTokens()) {
196: current = st.nextToken();
197: sb.append(current.equals("\n") ? getHTMLLineNo(lineno)
198: + current : getHTMLLineNo(lineno) + current
199: + st.nextToken());
200: lineno++;
201: }
202: return sb;
203: }
204:
205: /**
206: * Get the header.
207: * @return the header to the output file
208: */
209: protected static String getHeader() {
210: StringBuffer result = new StringBuffer("<HTML>"
211: + DocletConstants.NL);
212: result.append("<BODY BGCOLOR=\"" + BGCOLOR + "\">"
213: + DocletConstants.NL);
214: result.append("<PRE>" + DocletConstants.NL);
215: return result.toString();
216: }
217:
218: /**
219: * Get the footer
220: * @return the footer to the output file
221: */
222: protected static String getFooter() {
223: StringBuffer footer = new StringBuffer();
224: for (int i = 0; i < NUM_BLANK_LINES; i++) {
225: footer.append(DocletConstants.NL);
226: }
227: footer.append("</PRE>" + DocletConstants.NL + "</BODY>"
228: + DocletConstants.NL + "</HTML>" + DocletConstants.NL);
229: return footer.toString();
230: }
231:
232: /**
233: * Get the HTML for the lines.
234: * @param lineno The line number
235: * @return the HTML code for the line
236: */
237: protected static String getHTMLLineNo(int lineno) {
238: StringBuffer result = new StringBuffer("<FONT color=\""
239: + LINE_NO_COLOR + "\">");
240: if (lineno < 10) {
241: result.append("00" + ((new Integer(lineno)).toString()));
242: } else if (lineno < 100) {
243: result.append("0" + ((new Integer(lineno)).toString()));
244: } else {
245: result.append((new Integer(lineno)).toString());
246: }
247: result.append("</FONT> ");
248: return result.toString();
249: }
250:
251: /**
252: * Format a given line of source. <br>
253: * Note: In the future, we will add special colors for constructs in the
254: * language.
255: * @param line the string to format.
256: * @param tabLength the number of spaces for each tab.
257: * @param currentLineNo the current number.
258: */
259: protected static String formatLine(String line, int tabLength,
260: int currentLineNo) {
261: if (line == null) {
262: return null;
263: }
264: StringBuffer lineBuffer = new StringBuffer(Util
265: .escapeHtmlChars(line));
266: //Insert an anchor for the line
267: lineBuffer.append("<a name=\"line."
268: + Integer.toString(currentLineNo) + "\"></a>");
269: lineBuffer.append(DocletConstants.NL);
270: Util.replaceTabs(tabLength, lineBuffer);
271: return lineBuffer.toString();
272: }
273:
274: /**
275: * Given an array of <code>Doc</code>s, add to the given <code>HashMap</code> the
276: * line numbers and anchors that should be inserted in the output at those lines.
277: * @param docs the array of <code>Doc</code>s to add anchors for.
278: * @param hash the <code>HashMap</code> to add to.
279: */
280: protected static void addToHash(Doc[] docs, HashMap hash) {
281: if (docs == null) {
282: return;
283: }
284: for (int i = 0; i < docs.length; i++) {
285: hash.put(new Integer(docs[i].position().line()),
286: getAnchor(docs[i]));
287: }
288: }
289:
290: /**
291: * Given a <code>Doc</code>, return an anchor for it.
292: * @param d the <code>Doc</code> to check.
293: * @return an anchor of the form <a name="my_name"></a>
294: */
295: protected static String getAnchor(Doc d) {
296: return " <a name=\"" + getAnchorName(d) + "\"></a>";
297: }
298:
299: /**
300: * Given a <code>Doc</code>, return an anchor name for it.
301: * @param d the <code>Doc</code> to check.
302: * @return the name of the anchor.
303: */
304: public static String getAnchorName(Doc d) {
305: return "line." + d.position().line();
306: }
307: }
|