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