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

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