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

Wed, 31 Oct 2012 13:48:15 -0700

author
jjg
date
Wed, 31 Oct 2012 13:48:15 -0700
changeset 1383
b980e8e6aabf
parent 1372
78962d89f283
child 1410
bfec2a1cc869
permissions
-rw-r--r--

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

mercurial