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

Tue, 23 Oct 2012 13:20:37 -0700

author
jjg
date
Tue, 23 Oct 2012 13:20:37 -0700
changeset 1372
78962d89f283
parent 1359
25e14ad23cef
child 1383
b980e8e6aabf
permissions
-rw-r--r--

8000741: refactor javadoc to use abstraction to handle relative paths
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 */
jjg@1372 65 private static DocPath relativePath = DocPath.empty;
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,
jjg@1372 80 DocPath 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,
jjg@1372 111 DocPath outputdir) {
duke@1 112 if (pd == null || outputdir == null) {
duke@1 113 return;
duke@1 114 }
jjg@1372 115 DocPath 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 */
jjg@1372 134 private static DocPath getPackageOutputDir(DocPath outputDir, PackageDoc pd) {
jjg@1372 135 return outputDir.resolve(DocPath.forPackage(pd));
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,
jjg@1372 146 DocPath 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;
jjg@1372 170 relativePath = DocPaths.SOURCE_OUTPUT
jjg@1372 171 .resolve(DocPath.forPackage(cd))
jjg@1372 172 .invert();
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 */
jjg@1372 201 private static void writeToFile(Content body, DocPath 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);
jjg@1372 211 File dir = outputDir.resolveAgainst(configuration.destDirName);
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;
jjg@1372 230 DocPath stylesheet;
bpatel@766 231 if (filename.length() > 0) {
jjg@1372 232 stylesheet = DocPath.create(new File(filename).getName());
bpatel@766 233 } else {
jjg@1372 234 stylesheet = DocPaths.STYLESHEET;
duke@1 235 }
jjg@1372 236 DocPath p = relativePath.resolve(stylesheet);
jjg@1372 237 HtmlTree link = HtmlTree.LINK("stylesheet", "text/css", p.getPath(), "Style");
bpatel@766 238 return link;
duke@1 239 }
duke@1 240
duke@1 241 /**
duke@1 242 * Get the header.
bpatel@766 243 *
bpatel@766 244 * @return the header content for the HTML file
duke@1 245 */
bpatel@766 246 private static Content getHeader() {
bpatel@766 247 return new HtmlTree(HtmlTag.BODY);
duke@1 248 }
duke@1 249
duke@1 250 /**
bpatel@766 251 * Add the line numbers for the source code.
bpatel@766 252 *
bpatel@766 253 * @param pre the content tree to which the line number will be added
bpatel@766 254 * @param lineno The line number
duke@1 255 */
bpatel@766 256 private static void addLineNo(Content pre, int lineno) {
bpatel@766 257 HtmlTree span = new HtmlTree(HtmlTag.SPAN);
bpatel@766 258 span.addStyle(HtmlStyle.sourceLineNo);
bpatel@766 259 if (lineno < 10) {
bpatel@766 260 span.addContent("00" + Integer.toString(lineno));
bpatel@766 261 } else if (lineno < 100) {
bpatel@766 262 span.addContent("0" + Integer.toString(lineno));
bpatel@766 263 } else {
bpatel@766 264 span.addContent(Integer.toString(lineno));
duke@1 265 }
bpatel@766 266 pre.addContent(span);
duke@1 267 }
duke@1 268
duke@1 269 /**
bpatel@766 270 * Add a line from source to the HTML file that is generated.
bpatel@766 271 *
bpatel@766 272 * @param pre the content tree to which the line will be added.
duke@1 273 * @param line the string to format.
duke@1 274 * @param tabLength the number of spaces for each tab.
duke@1 275 * @param currentLineNo the current number.
duke@1 276 */
bpatel@766 277 private static void addLine(Content pre, String line, int tabLength,
bpatel@766 278 int currentLineNo) {
bpatel@766 279 if (line != null) {
jjg@910 280 StringBuilder lineBuffer = new StringBuilder(Util.escapeHtmlChars(line));
bpatel@766 281 Util.replaceTabs(tabLength, lineBuffer);
bpatel@766 282 pre.addContent(new RawHtml(lineBuffer.toString()));
bpatel@766 283 Content anchor = HtmlTree.A_NAME("line." + Integer.toString(currentLineNo));
bpatel@766 284 pre.addContent(anchor);
bpatel@766 285 pre.addContent(NEW_LINE);
duke@1 286 }
duke@1 287 }
duke@1 288
duke@1 289 /**
bpatel@766 290 * Add trailing blank lines at the end of the page.
bpatel@766 291 *
bpatel@766 292 * @param pre the content tree to which the blank lines will be added.
duke@1 293 */
bpatel@766 294 private static void addBlankLines(Content pre) {
bpatel@766 295 for (int i = 0; i < NUM_BLANK_LINES; i++) {
bpatel@766 296 pre.addContent(NEW_LINE);
bpatel@766 297 }
duke@1 298 }
duke@1 299
duke@1 300 /**
duke@1 301 * Given a <code>Doc</code>, return an anchor name for it.
bpatel@766 302 *
duke@1 303 * @param d the <code>Doc</code> to check.
duke@1 304 * @return the name of the anchor.
duke@1 305 */
duke@1 306 public static String getAnchorName(Doc d) {
duke@1 307 return "line." + d.position().line();
duke@1 308 }
duke@1 309 }

mercurial