duke@1: /*
duke@1: * Copyright 2001-2006 Sun Microsystems, Inc. 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
duke@1: * published by the Free Software Foundation. Sun designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
duke@1: * by Sun 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: *
duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1: * have any questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.doclets.internal.toolkit.util;
duke@1:
duke@1: import com.sun.tools.doclets.internal.toolkit.*;
duke@1: import com.sun.javadoc.*;
duke@1: import java.io.*;
duke@1: import java.util.*;
duke@1:
duke@1: /**
duke@1: * Converts Java Source Code to HTML.
duke@1: *
duke@1: * This code is not part of an API.
duke@1: * It is implementation that is subject to change.
duke@1: * Do not use it as an API
duke@1: *
duke@1: * @author Jamie Ho
duke@1: * @since 1.4
duke@1: */
duke@1: public class SourceToHTMLConverter {
duke@1:
duke@1: /**
duke@1: * The background color.
duke@1: */
duke@1: protected static final String BGCOLOR = "white";
duke@1:
duke@1: /**
duke@1: * The line number color.
duke@1: */
duke@1: protected static final String LINE_NO_COLOR = "green";
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: */
duke@1: protected static final int NUM_BLANK_LINES = 60;
duke@1:
duke@1:
duke@1: /**
duke@1: * Source is converted to HTML using static methods below.
duke@1: */
duke@1: private SourceToHTMLConverter() {}
duke@1:
duke@1: /**
duke@1: * Convert the Classes in the given RootDoc to an HTML.
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: */
duke@1: public static void convertRoot(Configuration configuration, RootDoc rd, String outputdir) {
duke@1: if (rd == null || outputdir == null) {
duke@1: return;
duke@1: }
duke@1: PackageDoc[] pds = rd.specifiedPackages();
duke@1: for (int i = 0; i < pds.length; i++) {
duke@1: convertPackage(configuration, pds[i], outputdir);
duke@1: }
duke@1: ClassDoc[] cds = rd.specifiedClasses();
duke@1: for (int i = 0; i < cds.length; i++) {
duke@1: convertClass(configuration, cds[i],
duke@1: getPackageOutputDir(outputdir, cds[i].containingPackage()));
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Convert the Classes in the given Package to an HTML.
duke@1: * @param configuration the configuration.
duke@1: * @param pd the Package to convert.
duke@1: * @param outputdir the name of the directory to output to.
duke@1: */
duke@1: public static void convertPackage(Configuration configuration, PackageDoc pd, String outputdir) {
duke@1: if (pd == null || outputdir == null) {
duke@1: return;
duke@1: }
duke@1: String classOutputdir = getPackageOutputDir(outputdir, pd);
duke@1: ClassDoc[] cds = pd.allClasses();
duke@1: for (int i = 0; i < cds.length; i++) {
duke@1: convertClass(configuration, cds[i], classOutputdir);
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Return the directory write output to for the given package.
duke@1: * @param outputDir the directory to output to.
duke@1: * @param pd the Package to generate output for.
duke@1: */
duke@1: private static String getPackageOutputDir(String outputDir, PackageDoc pd) {
duke@1: return outputDir + File.separator +
duke@1: DirectoryManager.getDirectoryPath(pd) + File.separator;
duke@1: }
duke@1:
duke@1: /**
duke@1: * Convert the given Class to an HTML.
duke@1: * @param configuration the configuration.
duke@1: * @param cd the class to convert.
duke@1: * @param outputdir the name of the directory to output to.
duke@1: */
duke@1: public static void convertClass(Configuration configuration, ClassDoc cd, String outputdir) {
duke@1: if (cd == null || outputdir == null) {
duke@1: return;
duke@1: }
duke@1: File file;
duke@1: SourcePosition sp = cd.position();
duke@1: if (sp == null || (file = sp.file()) == null) {
duke@1: return;
duke@1: }
duke@1: try {
duke@1: int lineno = 1;
duke@1: String line;
duke@1: StringBuffer output = new StringBuffer();
duke@1: LineNumberReader reader = new LineNumberReader(new FileReader(file));
duke@1: try {
duke@1: while ((line = reader.readLine()) != null) {
duke@1: output.append(formatLine(line, configuration.sourcetab, lineno));
duke@1: lineno++;
duke@1: }
duke@1: } finally {
duke@1: reader.close();
duke@1: }
duke@1: output = addLineNumbers(output.toString());
duke@1: output.insert(0, getHeader());
duke@1: output.append(getFooter());
duke@1: writeToFile(output.toString(), outputdir, cd.name(), configuration);
duke@1: } catch (Exception e){
duke@1: e.printStackTrace();
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Write the output to the file.
duke@1: * @param output the string to output.
duke@1: * @param outputDir the directory to output to.
duke@1: * @param className the name of the class that I am converting to HTML.
duke@1: * @param configuration the Doclet configuration to pass notices to.
duke@1: */
duke@1: private static void writeToFile(String output, String outputDir, String className, Configuration configuration) throws IOException {
duke@1: File dir = new File(outputDir);
duke@1: dir.mkdirs();
duke@1: File newFile = new File(dir, className + ".html");
duke@1: configuration.message.notice("doclet.Generating_0", newFile.getPath());
duke@1: FileOutputStream fout = new FileOutputStream(newFile);
duke@1: BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout));
duke@1: bw.write(output);
duke@1: bw.close();
duke@1: fout.close();
duke@1: }
duke@1:
duke@1: /**
duke@1: * Given a String
, add line numbers.
duke@1: * @param s the text to add line numbers to.
duke@1: *
duke@1: * @return the string buffer with the line numbering for each line.
duke@1: */
duke@1: private static StringBuffer addLineNumbers(String s) {
duke@1: StringBuffer sb = new StringBuffer();
duke@1: StringTokenizer st = new StringTokenizer(s, "\n", true);
duke@1: int lineno = 1;
duke@1: String current;
duke@1: while(st.hasMoreTokens()){
duke@1: current = st.nextToken();
duke@1: sb.append(current.equals("\n") ?
duke@1: getHTMLLineNo(lineno) + current :
duke@1: getHTMLLineNo(lineno) + current + st.nextToken());
duke@1: lineno++;
duke@1: }
duke@1: return sb;
duke@1: }
duke@1:
duke@1: /**
duke@1: * Get the header.
duke@1: * @return the header to the output file
duke@1: */
duke@1: protected static String getHeader() {
duke@1: StringBuffer result = new StringBuffer("" + DocletConstants.NL);
duke@1: result.append("
" + DocletConstants.NL); duke@1: return result.toString(); duke@1: } duke@1: duke@1: /** duke@1: * Get the footer duke@1: * @return the footer to the output file duke@1: */ duke@1: protected static String getFooter() { duke@1: StringBuffer footer = new StringBuffer(); duke@1: for (int i = 0; i < NUM_BLANK_LINES; i++) { duke@1: footer.append(DocletConstants.NL); duke@1: } duke@1: footer.append("" + DocletConstants.NL + "" + duke@1: DocletConstants.NL + "" + DocletConstants.NL); duke@1: return footer.toString(); duke@1: } duke@1: duke@1: /** duke@1: * Get the HTML for the lines. duke@1: * @param lineno The line number duke@1: * @return the HTML code for the line duke@1: */ duke@1: protected static String getHTMLLineNo(int lineno) { duke@1: StringBuffer result = new StringBuffer(""); duke@1: if (lineno < 10) { duke@1: result.append("00" + ((new Integer(lineno)).toString())); duke@1: } else if (lineno < 100) { duke@1: result.append("0" + ((new Integer(lineno)).toString())); duke@1: } else { duke@1: result.append((new Integer(lineno)).toString()); duke@1: } duke@1: result.append(" "); duke@1: return result.toString(); duke@1: } duke@1: duke@1: /** duke@1: * Format a given line of source.
Doc
s, add to the given HashMap
the
duke@1: * line numbers and anchors that should be inserted in the output at those lines.
duke@1: * @param docs the array of Doc
s to add anchors for.
duke@1: * @param hash the HashMap
to add to.
duke@1: */
duke@1: protected static void addToHash(Doc[] docs, HashMap hash) {
duke@1: if(docs == null) {
duke@1: return;
duke@1: }
duke@1: for(int i = 0; i < docs.length; i++) {
duke@1: hash.put(new Integer(docs[i].position().line()), getAnchor(docs[i]));
duke@1: }
duke@1: }
duke@1:
duke@1: /**
duke@1: * Given a Doc
, return an anchor for it.
duke@1: * @param d the Doc
to check.
duke@1: * @return an anchor of the form <a name="my_name"></a>
duke@1: */
duke@1: protected static String getAnchor(Doc d) {
duke@1: return " ";
duke@1: }
duke@1:
duke@1: /**
duke@1: * Given a Doc
, return an anchor name for it.
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: }