Wed, 10 Oct 2012 16:48:21 -0700
8000665: fix "internal API" comments on javadoc files
Reviewed-by: darcy
1 /*
2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.tools.doclets.formats.html;
28 import java.io.*;
29 import javax.tools.FileObject;
30 import com.sun.javadoc.*;
31 import com.sun.tools.doclets.internal.toolkit.*;
32 import com.sun.tools.doclets.internal.toolkit.util.*;
33 import com.sun.tools.doclets.formats.html.markup.*;
35 /**
36 * Converts Java Source Code to HTML.
37 *
38 * <p><b>This is NOT part of any supported API.
39 * If you write code that depends on this, you do so at your own risk.
40 * This code and its internal interfaces are subject to change or
41 * deletion without notice.</b>
42 *
43 * @author Jamie Ho
44 * @author Bhavesh Patel (Modified)
45 * @since 1.4
46 */
47 public class SourceToHTMLConverter {
49 /**
50 * The number of trailing blank lines at the end of the page.
51 * This is inserted so that anchors at the bottom of small pages
52 * can be reached.
53 */
54 private static final int NUM_BLANK_LINES = 60;
56 /**
57 * New line to be added to the documentation.
58 */
59 private static final Content NEW_LINE = new RawHtml(DocletConstants.NL);
61 /**
62 * Relative path from the documentation root to the file that is being
63 * generated.
64 */
65 private static String relativePath = "";
67 /**
68 * Source is converted to HTML using static methods below.
69 */
70 private SourceToHTMLConverter() {}
72 /**
73 * Convert the Classes in the given RootDoc to an HTML.
74 *
75 * @param configuration the configuration.
76 * @param rd the RootDoc to convert.
77 * @param outputdir the name of the directory to output to.
78 */
79 public static void convertRoot(ConfigurationImpl configuration, RootDoc rd,
80 String outputdir) {
81 if (rd == null || outputdir == null) {
82 return;
83 }
84 PackageDoc[] pds = rd.specifiedPackages();
85 for (int i = 0; i < pds.length; i++) {
86 // If -nodeprecated option is set and the package is marked as deprecated,
87 // do not convert the package files to HTML.
88 if (!(configuration.nodeprecated && Util.isDeprecated(pds[i])))
89 convertPackage(configuration, pds[i], outputdir);
90 }
91 ClassDoc[] cds = rd.specifiedClasses();
92 for (int i = 0; i < cds.length; i++) {
93 // If -nodeprecated option is set and the class is marked as deprecated
94 // or the containing package is deprecated, do not convert the
95 // package files to HTML.
96 if (!(configuration.nodeprecated &&
97 (Util.isDeprecated(cds[i]) || Util.isDeprecated(cds[i].containingPackage()))))
98 convertClass(configuration, cds[i],
99 getPackageOutputDir(outputdir, cds[i].containingPackage()));
100 }
101 }
103 /**
104 * Convert the Classes in the given Package to an HTML.
105 *
106 * @param configuration the configuration.
107 * @param pd the Package to convert.
108 * @param outputdir the name of the directory to output to.
109 */
110 public static void convertPackage(ConfigurationImpl configuration, PackageDoc pd,
111 String outputdir) {
112 if (pd == null || outputdir == null) {
113 return;
114 }
115 String classOutputdir = getPackageOutputDir(outputdir, pd);
116 ClassDoc[] cds = pd.allClasses();
117 for (int i = 0; i < cds.length; i++) {
118 // If -nodeprecated option is set and the class is marked as deprecated,
119 // do not convert the package files to HTML. We do not check for
120 // containing package deprecation since it is already check in
121 // the calling method above.
122 if (!(configuration.nodeprecated && Util.isDeprecated(cds[i])))
123 convertClass(configuration, cds[i], classOutputdir);
124 }
125 }
127 /**
128 * Return the directory write output to for the given package.
129 *
130 * @param outputDir the directory to output to.
131 * @param pd the Package to generate output for.
132 * @return the package output directory as a String.
133 */
134 private static String getPackageOutputDir(String outputDir, PackageDoc pd) {
135 return outputDir + File.separator +
136 DirectoryManager.getDirectoryPath(pd) + File.separator;
137 }
139 /**
140 * Convert the given Class to an HTML.
141 *
142 * @param configuration the configuration.
143 * @param cd the class to convert.
144 * @param outputdir the name of the directory to output to.
145 */
146 public static void convertClass(ConfigurationImpl configuration, ClassDoc cd,
147 String outputdir) {
148 if (cd == null || outputdir == null) {
149 return;
150 }
151 try {
152 SourcePosition sp = cd.position();
153 if (sp == null)
154 return;
155 Reader r;
156 // temp hack until we can update SourcePosition API.
157 if (sp instanceof com.sun.tools.javadoc.SourcePositionImpl) {
158 FileObject fo = ((com.sun.tools.javadoc.SourcePositionImpl) sp).fileObject();
159 if (fo == null)
160 return;
161 r = fo.openReader(true);
162 } else {
163 File file = sp.file();
164 if (file == null)
165 return;
166 r = new FileReader(file);
167 }
168 LineNumberReader reader = new LineNumberReader(r);
169 int lineno = 1;
170 String line;
171 relativePath = DirectoryManager.getRelativePath(DocletConstants.SOURCE_OUTPUT_DIR_NAME) +
172 DirectoryManager.getRelativePath(cd.containingPackage());
173 Content body = getHeader();
174 Content pre = new HtmlTree(HtmlTag.PRE);
175 try {
176 while ((line = reader.readLine()) != null) {
177 addLineNo(pre, lineno);
178 addLine(pre, line, configuration.sourcetab, lineno);
179 lineno++;
180 }
181 } finally {
182 reader.close();
183 }
184 addBlankLines(pre);
185 Content div = HtmlTree.DIV(HtmlStyle.sourceContainer, pre);
186 body.addContent(div);
187 writeToFile(body, outputdir, cd.name(), configuration);
188 } catch (Exception e){
189 e.printStackTrace();
190 }
191 }
193 /**
194 * Write the output to the file.
195 *
196 * @param body the documentation content to be written to the file.
197 * @param outputDir the directory to output to.
198 * @param className the name of the class that I am converting to HTML.
199 * @param configuration the Doclet configuration to pass notices to.
200 */
201 private static void writeToFile(Content body, String outputDir,
202 String className, ConfigurationImpl configuration) throws IOException {
203 Content htmlDocType = DocType.Transitional();
204 Content head = new HtmlTree(HtmlTag.HEAD);
205 head.addContent(HtmlTree.TITLE(new StringContent(
206 configuration.getText("doclet.Window_Source_title"))));
207 head.addContent(getStyleSheetProperties(configuration));
208 Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(),
209 head, body);
210 Content htmlDocument = new HtmlDocument(htmlDocType, htmlTree);
211 File dir = new File(outputDir);
212 dir.mkdirs();
213 File newFile = new File(dir, className + ".html");
214 configuration.message.notice("doclet.Generating_0", newFile.getPath());
215 FileOutputStream fout = new FileOutputStream(newFile);
216 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout));
217 bw.write(htmlDocument.toString());
218 bw.close();
219 fout.close();
220 }
222 /**
223 * Returns a link to the stylesheet file.
224 *
225 * @param configuration the doclet configuration for the current run of javadoc
226 * @return an HtmlTree for the lINK tag which provides the stylesheet location
227 */
228 public static HtmlTree getStyleSheetProperties(ConfigurationImpl configuration) {
229 String filename = configuration.stylesheetfile;
230 if (filename.length() > 0) {
231 File stylefile = new File(filename);
232 String parent = stylefile.getParent();
233 filename = (parent == null)?
234 filename:
235 filename.substring(parent.length() + 1);
236 } else {
237 filename = "stylesheet.css";
238 }
239 filename = relativePath + filename;
240 HtmlTree link = HtmlTree.LINK("stylesheet", "text/css", filename, "Style");
241 return link;
242 }
244 /**
245 * Get the header.
246 *
247 * @return the header content for the HTML file
248 */
249 private static Content getHeader() {
250 return new HtmlTree(HtmlTag.BODY);
251 }
253 /**
254 * Add the line numbers for the source code.
255 *
256 * @param pre the content tree to which the line number will be added
257 * @param lineno The line number
258 */
259 private static void addLineNo(Content pre, int lineno) {
260 HtmlTree span = new HtmlTree(HtmlTag.SPAN);
261 span.addStyle(HtmlStyle.sourceLineNo);
262 if (lineno < 10) {
263 span.addContent("00" + Integer.toString(lineno));
264 } else if (lineno < 100) {
265 span.addContent("0" + Integer.toString(lineno));
266 } else {
267 span.addContent(Integer.toString(lineno));
268 }
269 pre.addContent(span);
270 }
272 /**
273 * Add a line from source to the HTML file that is generated.
274 *
275 * @param pre the content tree to which the line will be added.
276 * @param line the string to format.
277 * @param tabLength the number of spaces for each tab.
278 * @param currentLineNo the current number.
279 */
280 private static void addLine(Content pre, String line, int tabLength,
281 int currentLineNo) {
282 if (line != null) {
283 StringBuilder lineBuffer = new StringBuilder(Util.escapeHtmlChars(line));
284 Util.replaceTabs(tabLength, lineBuffer);
285 pre.addContent(new RawHtml(lineBuffer.toString()));
286 Content anchor = HtmlTree.A_NAME("line." + Integer.toString(currentLineNo));
287 pre.addContent(anchor);
288 pre.addContent(NEW_LINE);
289 }
290 }
292 /**
293 * Add trailing blank lines at the end of the page.
294 *
295 * @param pre the content tree to which the blank lines will be added.
296 */
297 private static void addBlankLines(Content pre) {
298 for (int i = 0; i < NUM_BLANK_LINES; i++) {
299 pre.addContent(NEW_LINE);
300 }
301 }
303 /**
304 * Given a <code>Doc</code>, return an anchor name for it.
305 *
306 * @param d the <code>Doc</code> to check.
307 * @return the name of the anchor.
308 */
309 public static String getAnchorName(Doc d) {
310 return "line." + d.position().line();
311 }
312 }