0N/A/*
909N/A * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
765N/Apackage com.sun.tools.doclets.formats.html;
0N/A
0N/Aimport java.io.*;
196N/Aimport javax.tools.FileObject;
196N/Aimport com.sun.javadoc.*;
196N/Aimport com.sun.tools.doclets.internal.toolkit.*;
765N/Aimport com.sun.tools.doclets.internal.toolkit.util.*;
765N/Aimport com.sun.tools.doclets.formats.html.markup.*;
0N/A
0N/A/**
0N/A * Converts Java Source Code to HTML.
0N/A *
0N/A * This code is not part of an API.
0N/A * It is implementation that is subject to change.
0N/A * Do not use it as an API
0N/A *
0N/A * @author Jamie Ho
765N/A * @author Bhavesh Patel (Modified)
0N/A * @since 1.4
0N/A */
0N/Apublic class SourceToHTMLConverter {
0N/A
0N/A /**
0N/A * The number of trailing blank lines at the end of the page.
0N/A * This is inserted so that anchors at the bottom of small pages
0N/A * can be reached.
0N/A */
765N/A private static final int NUM_BLANK_LINES = 60;
0N/A
765N/A /**
765N/A * New line to be added to the documentation.
765N/A */
765N/A private static final Content NEW_LINE = new RawHtml(DocletConstants.NL);
765N/A
765N/A /**
765N/A * Relative path from the documentation root to the file that is being
765N/A * generated.
765N/A */
765N/A private static String relativePath = "";
0N/A
0N/A /**
0N/A * Source is converted to HTML using static methods below.
0N/A */
0N/A private SourceToHTMLConverter() {}
0N/A
0N/A /**
0N/A * Convert the Classes in the given RootDoc to an HTML.
765N/A *
0N/A * @param configuration the configuration.
0N/A * @param rd the RootDoc to convert.
0N/A * @param outputdir the name of the directory to output to.
0N/A */
765N/A public static void convertRoot(ConfigurationImpl configuration, RootDoc rd,
765N/A String outputdir) {
0N/A if (rd == null || outputdir == null) {
0N/A return;
0N/A }
0N/A PackageDoc[] pds = rd.specifiedPackages();
0N/A for (int i = 0; i < pds.length; i++) {
994N/A // If -nodeprecated option is set and the package is marked as deprecated,
994N/A // do not convert the package files to HTML.
994N/A if (!(configuration.nodeprecated && Util.isDeprecated(pds[i])))
994N/A convertPackage(configuration, pds[i], outputdir);
0N/A }
0N/A ClassDoc[] cds = rd.specifiedClasses();
0N/A for (int i = 0; i < cds.length; i++) {
994N/A // If -nodeprecated option is set and the class is marked as deprecated
994N/A // or the containing package is deprecated, do not convert the
994N/A // package files to HTML.
994N/A if (!(configuration.nodeprecated &&
994N/A (Util.isDeprecated(cds[i]) || Util.isDeprecated(cds[i].containingPackage()))))
994N/A convertClass(configuration, cds[i],
994N/A getPackageOutputDir(outputdir, cds[i].containingPackage()));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Convert the Classes in the given Package to an HTML.
765N/A *
0N/A * @param configuration the configuration.
0N/A * @param pd the Package to convert.
0N/A * @param outputdir the name of the directory to output to.
0N/A */
765N/A public static void convertPackage(ConfigurationImpl configuration, PackageDoc pd,
765N/A String outputdir) {
0N/A if (pd == null || outputdir == null) {
0N/A return;
0N/A }
0N/A String classOutputdir = getPackageOutputDir(outputdir, pd);
0N/A ClassDoc[] cds = pd.allClasses();
0N/A for (int i = 0; i < cds.length; i++) {
994N/A // If -nodeprecated option is set and the class is marked as deprecated,
994N/A // do not convert the package files to HTML. We do not check for
994N/A // containing package deprecation since it is already check in
994N/A // the calling method above.
994N/A if (!(configuration.nodeprecated && Util.isDeprecated(cds[i])))
994N/A convertClass(configuration, cds[i], classOutputdir);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Return the directory write output to for the given package.
765N/A *
0N/A * @param outputDir the directory to output to.
0N/A * @param pd the Package to generate output for.
765N/A * @return the package output directory as a String.
0N/A */
0N/A private static String getPackageOutputDir(String outputDir, PackageDoc pd) {
0N/A return outputDir + File.separator +
0N/A DirectoryManager.getDirectoryPath(pd) + File.separator;
0N/A }
0N/A
0N/A /**
0N/A * Convert the given Class to an HTML.
765N/A *
0N/A * @param configuration the configuration.
0N/A * @param cd the class to convert.
0N/A * @param outputdir the name of the directory to output to.
0N/A */
765N/A public static void convertClass(ConfigurationImpl configuration, ClassDoc cd,
765N/A String outputdir) {
0N/A if (cd == null || outputdir == null) {
0N/A return;
0N/A }
0N/A try {
196N/A SourcePosition sp = cd.position();
196N/A if (sp == null)
196N/A return;
196N/A Reader r;
196N/A // temp hack until we can update SourcePosition API.
196N/A if (sp instanceof com.sun.tools.javadoc.SourcePositionImpl) {
196N/A FileObject fo = ((com.sun.tools.javadoc.SourcePositionImpl) sp).fileObject();
196N/A if (fo == null)
196N/A return;
196N/A r = fo.openReader(true);
196N/A } else {
196N/A File file = sp.file();
196N/A if (file == null)
196N/A return;
196N/A r = new FileReader(file);
196N/A }
196N/A LineNumberReader reader = new LineNumberReader(r);
0N/A int lineno = 1;
0N/A String line;
765N/A relativePath = DirectoryManager.getRelativePath(DocletConstants.SOURCE_OUTPUT_DIR_NAME) +
765N/A DirectoryManager.getRelativePath(cd.containingPackage());
765N/A Content body = getHeader();
765N/A Content pre = new HtmlTree(HtmlTag.PRE);
0N/A try {
0N/A while ((line = reader.readLine()) != null) {
765N/A addLineNo(pre, lineno);
765N/A addLine(pre, line, configuration.sourcetab, lineno);
0N/A lineno++;
0N/A }
0N/A } finally {
0N/A reader.close();
0N/A }
765N/A addBlankLines(pre);
765N/A Content div = HtmlTree.DIV(HtmlStyle.sourceContainer, pre);
765N/A body.addContent(div);
765N/A writeToFile(body, outputdir, cd.name(), configuration);
0N/A } catch (Exception e){
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Write the output to the file.
765N/A *
765N/A * @param body the documentation content to be written to the file.
0N/A * @param outputDir the directory to output to.
0N/A * @param className the name of the class that I am converting to HTML.
0N/A * @param configuration the Doclet configuration to pass notices to.
0N/A */
765N/A private static void writeToFile(Content body, String outputDir,
765N/A String className, ConfigurationImpl configuration) throws IOException {
765N/A Content htmlDocType = DocType.Transitional();
765N/A Content head = new HtmlTree(HtmlTag.HEAD);
765N/A head.addContent(HtmlTree.TITLE(new StringContent(
765N/A configuration.getText("doclet.Window_Source_title"))));
765N/A head.addContent(getStyleSheetProperties(configuration));
765N/A Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(),
765N/A head, body);
765N/A Content htmlDocument = new HtmlDocument(htmlDocType, htmlTree);
0N/A File dir = new File(outputDir);
0N/A dir.mkdirs();
0N/A File newFile = new File(dir, className + ".html");
0N/A configuration.message.notice("doclet.Generating_0", newFile.getPath());
0N/A FileOutputStream fout = new FileOutputStream(newFile);
0N/A BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout));
765N/A bw.write(htmlDocument.toString());
0N/A bw.close();
0N/A fout.close();
0N/A }
0N/A
0N/A /**
765N/A * Returns a link to the stylesheet file.
0N/A *
765N/A * @param configuration the doclet configuration for the current run of javadoc
765N/A * @return an HtmlTree for the lINK tag which provides the stylesheet location
0N/A */
765N/A public static HtmlTree getStyleSheetProperties(ConfigurationImpl configuration) {
765N/A String filename = configuration.stylesheetfile;
765N/A if (filename.length() > 0) {
765N/A File stylefile = new File(filename);
765N/A String parent = stylefile.getParent();
765N/A filename = (parent == null)?
765N/A filename:
765N/A filename.substring(parent.length() + 1);
765N/A } else {
765N/A filename = "stylesheet.css";
0N/A }
765N/A filename = relativePath + filename;
765N/A HtmlTree link = HtmlTree.LINK("stylesheet", "text/css", filename, "Style");
765N/A return link;
0N/A }
0N/A
0N/A /**
0N/A * Get the header.
765N/A *
765N/A * @return the header content for the HTML file
0N/A */
765N/A private static Content getHeader() {
765N/A return new HtmlTree(HtmlTag.BODY);
0N/A }
0N/A
0N/A /**
765N/A * Add the line numbers for the source code.
765N/A *
765N/A * @param pre the content tree to which the line number will be added
765N/A * @param lineno The line number
0N/A */
765N/A private static void addLineNo(Content pre, int lineno) {
765N/A HtmlTree span = new HtmlTree(HtmlTag.SPAN);
765N/A span.addStyle(HtmlStyle.sourceLineNo);
765N/A if (lineno < 10) {
765N/A span.addContent("00" + Integer.toString(lineno));
765N/A } else if (lineno < 100) {
765N/A span.addContent("0" + Integer.toString(lineno));
765N/A } else {
765N/A span.addContent(Integer.toString(lineno));
0N/A }
765N/A pre.addContent(span);
0N/A }
0N/A
0N/A /**
765N/A * Add a line from source to the HTML file that is generated.
765N/A *
765N/A * @param pre the content tree to which the line will be added.
0N/A * @param line the string to format.
0N/A * @param tabLength the number of spaces for each tab.
0N/A * @param currentLineNo the current number.
0N/A */
765N/A private static void addLine(Content pre, String line, int tabLength,
765N/A int currentLineNo) {
765N/A if (line != null) {
909N/A StringBuilder lineBuffer = new StringBuilder(Util.escapeHtmlChars(line));
765N/A Util.replaceTabs(tabLength, lineBuffer);
765N/A pre.addContent(new RawHtml(lineBuffer.toString()));
765N/A Content anchor = HtmlTree.A_NAME("line." + Integer.toString(currentLineNo));
765N/A pre.addContent(anchor);
765N/A pre.addContent(NEW_LINE);
0N/A }
0N/A }
0N/A
0N/A /**
765N/A * Add trailing blank lines at the end of the page.
765N/A *
765N/A * @param pre the content tree to which the blank lines will be added.
0N/A */
765N/A private static void addBlankLines(Content pre) {
765N/A for (int i = 0; i < NUM_BLANK_LINES; i++) {
765N/A pre.addContent(NEW_LINE);
765N/A }
0N/A }
0N/A
0N/A /**
0N/A * Given a <code>Doc</code>, return an anchor name for it.
765N/A *
0N/A * @param d the <code>Doc</code> to check.
0N/A * @return the name of the anchor.
0N/A */
0N/A public static String getAnchorName(Doc d) {
0N/A return "line." + d.position().line();
0N/A }
0N/A}