aoqi@0: /* aoqi@0: * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.doclets.formats.html; aoqi@0: aoqi@0: import java.io.*; aoqi@0: aoqi@0: import javax.tools.FileObject; aoqi@0: aoqi@0: import com.sun.javadoc.*; aoqi@0: import com.sun.tools.doclets.formats.html.markup.*; aoqi@0: import com.sun.tools.doclets.internal.toolkit.*; aoqi@0: import com.sun.tools.doclets.internal.toolkit.util.*; aoqi@0: aoqi@0: /** aoqi@0: * Converts Java Source Code to HTML. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own risk. aoqi@0: * This code and its internal interfaces are subject to change or aoqi@0: * deletion without notice. aoqi@0: * aoqi@0: * @author Jamie Ho aoqi@0: * @author Bhavesh Patel (Modified) aoqi@0: * @since 1.4 aoqi@0: */ aoqi@0: public class SourceToHTMLConverter { aoqi@0: aoqi@0: /** aoqi@0: * The number of trailing blank lines at the end of the page. aoqi@0: * This is inserted so that anchors at the bottom of small pages aoqi@0: * can be reached. aoqi@0: */ aoqi@0: private static final int NUM_BLANK_LINES = 60; aoqi@0: aoqi@0: /** aoqi@0: * New line to be added to the documentation. aoqi@0: */ aoqi@0: private static final String NEW_LINE = DocletConstants.NL; aoqi@0: aoqi@0: private final ConfigurationImpl configuration; aoqi@0: aoqi@0: private final RootDoc rootDoc; aoqi@0: aoqi@0: private DocPath outputdir; aoqi@0: aoqi@0: /** aoqi@0: * Relative path from the documentation root to the file that is being aoqi@0: * generated. aoqi@0: */ aoqi@0: private DocPath relativePath = DocPath.empty; aoqi@0: aoqi@0: private SourceToHTMLConverter(ConfigurationImpl configuration, RootDoc rd, aoqi@0: DocPath outputdir) { aoqi@0: this.configuration = configuration; aoqi@0: this.rootDoc = rd; aoqi@0: this.outputdir = outputdir; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Convert the Classes in the given RootDoc to an HTML. aoqi@0: * aoqi@0: * @param configuration the configuration. aoqi@0: * @param rd the RootDoc to convert. aoqi@0: * @param outputdir the name of the directory to output to. aoqi@0: */ aoqi@0: public static void convertRoot(ConfigurationImpl configuration, RootDoc rd, aoqi@0: DocPath outputdir) { aoqi@0: new SourceToHTMLConverter(configuration, rd, outputdir).generate(); aoqi@0: } aoqi@0: aoqi@0: void generate() { aoqi@0: if (rootDoc == null || outputdir == null) { aoqi@0: return; aoqi@0: } aoqi@0: PackageDoc[] pds = rootDoc.specifiedPackages(); aoqi@0: for (int i = 0; i < pds.length; i++) { aoqi@0: // If -nodeprecated option is set and the package is marked as deprecated, aoqi@0: // do not convert the package files to HTML. aoqi@0: if (!(configuration.nodeprecated && Util.isDeprecated(pds[i]))) aoqi@0: convertPackage(pds[i], outputdir); aoqi@0: } aoqi@0: ClassDoc[] cds = rootDoc.specifiedClasses(); aoqi@0: for (int i = 0; i < cds.length; i++) { aoqi@0: // If -nodeprecated option is set and the class is marked as deprecated aoqi@0: // or the containing package is deprecated, do not convert the aoqi@0: // package files to HTML. aoqi@0: if (!(configuration.nodeprecated && aoqi@0: (Util.isDeprecated(cds[i]) || Util.isDeprecated(cds[i].containingPackage())))) aoqi@0: convertClass(cds[i], outputdir); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Convert the Classes in the given Package to an HTML. aoqi@0: * aoqi@0: * @param pd the Package to convert. aoqi@0: * @param outputdir the name of the directory to output to. aoqi@0: */ aoqi@0: public void convertPackage(PackageDoc pd, DocPath outputdir) { aoqi@0: if (pd == null) { aoqi@0: return; aoqi@0: } aoqi@0: ClassDoc[] cds = pd.allClasses(); aoqi@0: for (int i = 0; i < cds.length; i++) { aoqi@0: // If -nodeprecated option is set and the class is marked as deprecated, aoqi@0: // do not convert the package files to HTML. We do not check for aoqi@0: // containing package deprecation since it is already check in aoqi@0: // the calling method above. aoqi@0: if (!(configuration.nodeprecated && Util.isDeprecated(cds[i]))) aoqi@0: convertClass(cds[i], outputdir); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Convert the given Class to an HTML. aoqi@0: * aoqi@0: * @param cd the class to convert. aoqi@0: * @param outputdir the name of the directory to output to. aoqi@0: */ aoqi@0: public void convertClass(ClassDoc cd, DocPath outputdir) { aoqi@0: if (cd == null) { aoqi@0: return; aoqi@0: } aoqi@0: try { aoqi@0: SourcePosition sp = cd.position(); aoqi@0: if (sp == null) aoqi@0: return; aoqi@0: Reader r; aoqi@0: // temp hack until we can update SourcePosition API. aoqi@0: if (sp instanceof com.sun.tools.javadoc.SourcePositionImpl) { aoqi@0: FileObject fo = ((com.sun.tools.javadoc.SourcePositionImpl) sp).fileObject(); aoqi@0: if (fo == null) aoqi@0: return; aoqi@0: r = fo.openReader(true); aoqi@0: } else { aoqi@0: File file = sp.file(); aoqi@0: if (file == null) aoqi@0: return; aoqi@0: r = new FileReader(file); aoqi@0: } aoqi@0: LineNumberReader reader = new LineNumberReader(r); aoqi@0: int lineno = 1; aoqi@0: String line; aoqi@0: relativePath = DocPaths.SOURCE_OUTPUT aoqi@0: .resolve(DocPath.forPackage(cd)) aoqi@0: .invert(); aoqi@0: Content body = getHeader(); aoqi@0: Content pre = new HtmlTree(HtmlTag.PRE); aoqi@0: try { aoqi@0: while ((line = reader.readLine()) != null) { aoqi@0: addLineNo(pre, lineno); aoqi@0: addLine(pre, line, lineno); aoqi@0: lineno++; aoqi@0: } aoqi@0: } finally { aoqi@0: reader.close(); aoqi@0: } aoqi@0: addBlankLines(pre); aoqi@0: Content div = HtmlTree.DIV(HtmlStyle.sourceContainer, pre); aoqi@0: body.addContent(div); aoqi@0: writeToFile(body, outputdir.resolve(DocPath.forClass(cd))); aoqi@0: } catch (IOException e) { aoqi@0: e.printStackTrace(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Write the output to the file. aoqi@0: * aoqi@0: * @param body the documentation content to be written to the file. aoqi@0: * @param path the path for the file. aoqi@0: */ aoqi@0: private void writeToFile(Content body, DocPath path) throws IOException { aoqi@0: Content htmlDocType = DocType.TRANSITIONAL; aoqi@0: Content head = new HtmlTree(HtmlTag.HEAD); aoqi@0: head.addContent(HtmlTree.TITLE(new StringContent( aoqi@0: configuration.getText("doclet.Window_Source_title")))); aoqi@0: head.addContent(getStyleSheetProperties()); aoqi@0: Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(), aoqi@0: head, body); aoqi@0: Content htmlDocument = new HtmlDocument(htmlDocType, htmlTree); aoqi@0: configuration.message.notice("doclet.Generating_0", path.getPath()); aoqi@0: DocFile df = DocFile.createFileForOutput(configuration, path); aoqi@0: Writer w = df.openWriter(); aoqi@0: try { aoqi@0: htmlDocument.write(w, true); aoqi@0: } finally { aoqi@0: w.close(); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns a link to the stylesheet file. aoqi@0: * aoqi@0: * @return an HtmlTree for the lINK tag which provides the stylesheet location aoqi@0: */ aoqi@0: public HtmlTree getStyleSheetProperties() { aoqi@0: String filename = configuration.stylesheetfile; aoqi@0: DocPath stylesheet; aoqi@0: if (filename.length() > 0) { aoqi@0: DocFile file = DocFile.createFileForInput(configuration, filename); aoqi@0: stylesheet = DocPath.create(file.getName()); aoqi@0: } else { aoqi@0: stylesheet = DocPaths.STYLESHEET; aoqi@0: } aoqi@0: DocPath p = relativePath.resolve(stylesheet); aoqi@0: HtmlTree link = HtmlTree.LINK("stylesheet", "text/css", p.getPath(), "Style"); aoqi@0: return link; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Get the header. aoqi@0: * aoqi@0: * @return the header content for the HTML file aoqi@0: */ aoqi@0: private static Content getHeader() { aoqi@0: return new HtmlTree(HtmlTag.BODY); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Add the line numbers for the source code. aoqi@0: * aoqi@0: * @param pre the content tree to which the line number will be added aoqi@0: * @param lineno The line number aoqi@0: */ aoqi@0: private static void addLineNo(Content pre, int lineno) { aoqi@0: HtmlTree span = new HtmlTree(HtmlTag.SPAN); aoqi@0: span.addStyle(HtmlStyle.sourceLineNo); aoqi@0: if (lineno < 10) { aoqi@0: span.addContent("00" + Integer.toString(lineno)); aoqi@0: } else if (lineno < 100) { aoqi@0: span.addContent("0" + Integer.toString(lineno)); aoqi@0: } else { aoqi@0: span.addContent(Integer.toString(lineno)); aoqi@0: } aoqi@0: pre.addContent(span); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Add a line from source to the HTML file that is generated. aoqi@0: * aoqi@0: * @param pre the content tree to which the line will be added. aoqi@0: * @param line the string to format. aoqi@0: * @param currentLineNo the current number. aoqi@0: */ aoqi@0: private void addLine(Content pre, String line, int currentLineNo) { aoqi@0: if (line != null) { aoqi@0: pre.addContent(Util.replaceTabs(configuration, line)); aoqi@0: Content anchor = HtmlTree.A_NAME("line." + Integer.toString(currentLineNo)); aoqi@0: pre.addContent(anchor); aoqi@0: pre.addContent(NEW_LINE); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Add trailing blank lines at the end of the page. aoqi@0: * aoqi@0: * @param pre the content tree to which the blank lines will be added. aoqi@0: */ aoqi@0: private static void addBlankLines(Content pre) { aoqi@0: for (int i = 0; i < NUM_BLANK_LINES; i++) { aoqi@0: pre.addContent(NEW_LINE); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Given a Doc, return an anchor name for it. aoqi@0: * aoqi@0: * @param d the Doc to check. aoqi@0: * @return the name of the anchor. aoqi@0: */ aoqi@0: public static String getAnchorName(Doc d) { aoqi@0: return "line." + d.position().line(); aoqi@0: } aoqi@0: }