src/share/classes/com/sun/tools/doclets/formats/html/SourceToHTMLConverter.java

Wed, 10 Oct 2012 16:48:21 -0700

author
jjg
date
Wed, 10 Oct 2012 16:48:21 -0700
changeset 1359
25e14ad23cef
parent 995
62bc3775d5bb
child 1372
78962d89f283
permissions
-rw-r--r--

8000665: fix "internal API" comments on javadoc files
Reviewed-by: darcy

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

mercurial