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