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

Mon, 19 Nov 2012 16:10:34 -0800

author
bpatel
date
Mon, 19 Nov 2012 16:10:34 -0800
changeset 1417
522a1ee72340
parent 1410
bfec2a1cc869
child 1737
7a9ef837e57f
permissions
-rw-r--r--

8002304: Group methods by types in methods summary section
Reviewed-by: jjg

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
jjg@1410 63 private final ConfigurationImpl configuration;
jjg@1410 64
jjg@1410 65 private final RootDoc rootDoc;
jjg@1410 66
jjg@1410 67 private DocPath outputdir;
jjg@1410 68
bpatel@766 69 /**
bpatel@766 70 * Relative path from the documentation root to the file that is being
bpatel@766 71 * generated.
bpatel@766 72 */
jjg@1410 73 private DocPath relativePath = DocPath.empty;
duke@1 74
jjg@1410 75 private SourceToHTMLConverter(ConfigurationImpl configuration, RootDoc rd,
jjg@1410 76 DocPath outputdir) {
jjg@1410 77 this.configuration = configuration;
jjg@1410 78 this.rootDoc = rd;
jjg@1410 79 this.outputdir = outputdir;
jjg@1410 80 }
duke@1 81
duke@1 82 /**
duke@1 83 * Convert the Classes in the given RootDoc to an HTML.
bpatel@766 84 *
duke@1 85 * @param configuration the configuration.
duke@1 86 * @param rd the RootDoc to convert.
duke@1 87 * @param outputdir the name of the directory to output to.
duke@1 88 */
bpatel@766 89 public static void convertRoot(ConfigurationImpl configuration, RootDoc rd,
jjg@1372 90 DocPath outputdir) {
jjg@1410 91 new SourceToHTMLConverter(configuration, rd, outputdir).generate();
jjg@1410 92 }
jjg@1410 93
jjg@1410 94 void generate() {
jjg@1410 95 if (rootDoc == null || outputdir == null) {
duke@1 96 return;
duke@1 97 }
jjg@1410 98 PackageDoc[] pds = rootDoc.specifiedPackages();
duke@1 99 for (int i = 0; i < pds.length; i++) {
bpatel@995 100 // If -nodeprecated option is set and the package is marked as deprecated,
bpatel@995 101 // do not convert the package files to HTML.
bpatel@995 102 if (!(configuration.nodeprecated && Util.isDeprecated(pds[i])))
jjg@1410 103 convertPackage(pds[i], outputdir);
duke@1 104 }
jjg@1410 105 ClassDoc[] cds = rootDoc.specifiedClasses();
duke@1 106 for (int i = 0; i < cds.length; i++) {
bpatel@995 107 // If -nodeprecated option is set and the class is marked as deprecated
bpatel@995 108 // or the containing package is deprecated, do not convert the
bpatel@995 109 // package files to HTML.
bpatel@995 110 if (!(configuration.nodeprecated &&
bpatel@995 111 (Util.isDeprecated(cds[i]) || Util.isDeprecated(cds[i].containingPackage()))))
jjg@1410 112 convertClass(cds[i], outputdir);
duke@1 113 }
duke@1 114 }
duke@1 115
duke@1 116 /**
duke@1 117 * Convert the Classes in the given Package to an HTML.
bpatel@766 118 *
duke@1 119 * @param pd the Package to convert.
duke@1 120 * @param outputdir the name of the directory to output to.
duke@1 121 */
jjg@1410 122 public void convertPackage(PackageDoc pd, DocPath outputdir) {
jjg@1383 123 if (pd == null) {
duke@1 124 return;
duke@1 125 }
duke@1 126 ClassDoc[] cds = pd.allClasses();
duke@1 127 for (int i = 0; i < cds.length; i++) {
bpatel@995 128 // If -nodeprecated option is set and the class is marked as deprecated,
bpatel@995 129 // do not convert the package files to HTML. We do not check for
bpatel@995 130 // containing package deprecation since it is already check in
bpatel@995 131 // the calling method above.
bpatel@995 132 if (!(configuration.nodeprecated && Util.isDeprecated(cds[i])))
jjg@1410 133 convertClass(cds[i], outputdir);
duke@1 134 }
duke@1 135 }
duke@1 136
duke@1 137 /**
duke@1 138 * Convert the given Class to an HTML.
bpatel@766 139 *
duke@1 140 * @param cd the class to convert.
duke@1 141 * @param outputdir the name of the directory to output to.
duke@1 142 */
jjg@1410 143 public void convertClass(ClassDoc cd, DocPath outputdir) {
jjg@1383 144 if (cd == null) {
duke@1 145 return;
duke@1 146 }
duke@1 147 try {
jjg@197 148 SourcePosition sp = cd.position();
jjg@197 149 if (sp == null)
jjg@197 150 return;
jjg@197 151 Reader r;
jjg@197 152 // temp hack until we can update SourcePosition API.
jjg@197 153 if (sp instanceof com.sun.tools.javadoc.SourcePositionImpl) {
jjg@197 154 FileObject fo = ((com.sun.tools.javadoc.SourcePositionImpl) sp).fileObject();
jjg@197 155 if (fo == null)
jjg@197 156 return;
jjg@197 157 r = fo.openReader(true);
jjg@197 158 } else {
jjg@197 159 File file = sp.file();
jjg@197 160 if (file == null)
jjg@197 161 return;
jjg@197 162 r = new FileReader(file);
jjg@197 163 }
jjg@197 164 LineNumberReader reader = new LineNumberReader(r);
duke@1 165 int lineno = 1;
duke@1 166 String line;
jjg@1372 167 relativePath = DocPaths.SOURCE_OUTPUT
jjg@1372 168 .resolve(DocPath.forPackage(cd))
jjg@1372 169 .invert();
bpatel@766 170 Content body = getHeader();
bpatel@766 171 Content pre = new HtmlTree(HtmlTag.PRE);
duke@1 172 try {
duke@1 173 while ((line = reader.readLine()) != null) {
bpatel@766 174 addLineNo(pre, lineno);
jjg@1410 175 addLine(pre, line, lineno);
duke@1 176 lineno++;
duke@1 177 }
duke@1 178 } finally {
duke@1 179 reader.close();
duke@1 180 }
bpatel@766 181 addBlankLines(pre);
bpatel@766 182 Content div = HtmlTree.DIV(HtmlStyle.sourceContainer, pre);
bpatel@766 183 body.addContent(div);
jjg@1410 184 writeToFile(body, outputdir.resolve(DocPath.forClass(cd)));
jjg@1383 185 } catch (IOException e) {
duke@1 186 e.printStackTrace();
duke@1 187 }
duke@1 188 }
duke@1 189
duke@1 190 /**
duke@1 191 * Write the output to the file.
bpatel@766 192 *
bpatel@766 193 * @param body the documentation content to be written to the file.
jjg@1383 194 * @param path the path for the file.
duke@1 195 */
jjg@1410 196 private void writeToFile(Content body, DocPath path) throws IOException {
jjg@1410 197 Content htmlDocType = DocType.TRANSITIONAL;
bpatel@766 198 Content head = new HtmlTree(HtmlTag.HEAD);
bpatel@766 199 head.addContent(HtmlTree.TITLE(new StringContent(
bpatel@766 200 configuration.getText("doclet.Window_Source_title"))));
jjg@1410 201 head.addContent(getStyleSheetProperties());
bpatel@766 202 Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(),
bpatel@766 203 head, body);
bpatel@766 204 Content htmlDocument = new HtmlDocument(htmlDocType, htmlTree);
jjg@1383 205 configuration.message.notice("doclet.Generating_0", path.getPath());
jjg@1383 206 DocFile df = DocFile.createFileForOutput(configuration, path);
jjg@1383 207 Writer w = df.openWriter();
jjg@1383 208 try {
jjg@1383 209 htmlDocument.write(w, true);
jjg@1383 210 } finally {
jjg@1383 211 w.close();
jjg@1383 212 }
jjg@1383 213
duke@1 214 }
duke@1 215
duke@1 216 /**
bpatel@766 217 * Returns a link to the stylesheet file.
duke@1 218 *
bpatel@766 219 * @return an HtmlTree for the lINK tag which provides the stylesheet location
duke@1 220 */
jjg@1410 221 public HtmlTree getStyleSheetProperties() {
bpatel@766 222 String filename = configuration.stylesheetfile;
jjg@1372 223 DocPath stylesheet;
bpatel@766 224 if (filename.length() > 0) {
jjg@1383 225 DocFile file = DocFile.createFileForInput(configuration, filename);
jjg@1383 226 stylesheet = DocPath.create(file.getName());
bpatel@766 227 } else {
jjg@1372 228 stylesheet = DocPaths.STYLESHEET;
duke@1 229 }
jjg@1372 230 DocPath p = relativePath.resolve(stylesheet);
jjg@1372 231 HtmlTree link = HtmlTree.LINK("stylesheet", "text/css", p.getPath(), "Style");
bpatel@766 232 return link;
duke@1 233 }
duke@1 234
duke@1 235 /**
duke@1 236 * Get the header.
bpatel@766 237 *
bpatel@766 238 * @return the header content for the HTML file
duke@1 239 */
bpatel@766 240 private static Content getHeader() {
bpatel@766 241 return new HtmlTree(HtmlTag.BODY);
duke@1 242 }
duke@1 243
duke@1 244 /**
bpatel@766 245 * Add the line numbers for the source code.
bpatel@766 246 *
bpatel@766 247 * @param pre the content tree to which the line number will be added
bpatel@766 248 * @param lineno The line number
duke@1 249 */
bpatel@766 250 private static void addLineNo(Content pre, int lineno) {
bpatel@766 251 HtmlTree span = new HtmlTree(HtmlTag.SPAN);
bpatel@766 252 span.addStyle(HtmlStyle.sourceLineNo);
bpatel@766 253 if (lineno < 10) {
bpatel@766 254 span.addContent("00" + Integer.toString(lineno));
bpatel@766 255 } else if (lineno < 100) {
bpatel@766 256 span.addContent("0" + Integer.toString(lineno));
bpatel@766 257 } else {
bpatel@766 258 span.addContent(Integer.toString(lineno));
duke@1 259 }
bpatel@766 260 pre.addContent(span);
duke@1 261 }
duke@1 262
duke@1 263 /**
bpatel@766 264 * Add a line from source to the HTML file that is generated.
bpatel@766 265 *
bpatel@766 266 * @param pre the content tree to which the line will be added.
duke@1 267 * @param line the string to format.
duke@1 268 * @param currentLineNo the current number.
duke@1 269 */
jjg@1410 270 private void addLine(Content pre, String line, int currentLineNo) {
bpatel@766 271 if (line != null) {
jjg@1410 272 StringBuilder lineBuffer = new StringBuilder(line);
jjg@1410 273 Util.replaceTabs(configuration, lineBuffer);
jjg@1410 274 Util.escapeHtmlChars(lineBuffer);
bpatel@766 275 pre.addContent(new RawHtml(lineBuffer.toString()));
bpatel@766 276 Content anchor = HtmlTree.A_NAME("line." + Integer.toString(currentLineNo));
bpatel@766 277 pre.addContent(anchor);
bpatel@766 278 pre.addContent(NEW_LINE);
duke@1 279 }
duke@1 280 }
duke@1 281
duke@1 282 /**
bpatel@766 283 * Add trailing blank lines at the end of the page.
bpatel@766 284 *
bpatel@766 285 * @param pre the content tree to which the blank lines will be added.
duke@1 286 */
bpatel@766 287 private static void addBlankLines(Content pre) {
bpatel@766 288 for (int i = 0; i < NUM_BLANK_LINES; i++) {
bpatel@766 289 pre.addContent(NEW_LINE);
bpatel@766 290 }
duke@1 291 }
duke@1 292
duke@1 293 /**
duke@1 294 * Given a <code>Doc</code>, return an anchor name for it.
bpatel@766 295 *
duke@1 296 * @param d the <code>Doc</code> to check.
duke@1 297 * @return the name of the anchor.
duke@1 298 */
duke@1 299 public static String getAnchorName(Doc d) {
duke@1 300 return "line." + d.position().line();
duke@1 301 }
duke@1 302 }

mercurial