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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial