langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/SourceToHTMLConverter.java
author jjg
Tue, 20 Jan 2009 15:17:45 -0800
changeset 1869 0e193a8f3520
parent 1863 e5de9abc0eb1
child 2212 1d3dc0e0ba0c
permissions -rw-r--r--
6794582: javadoc should read files using a FileManager Reviewed-by: darcy, bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
1264
076a3cde30d5 6754988: Update copyright year
xdono
parents: 868
diff changeset
     2
 * Copyright 2001-2008 Sun Microsystems, Inc.  All Rights Reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.doclets.internal.toolkit.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.*;
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
    30
import javax.tools.FileObject;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
    31
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
    32
import com.sun.javadoc.*;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
    33
import com.sun.tools.doclets.internal.toolkit.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * Converts Java Source Code to HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * This code is not part of an API.
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * It is implementation that is subject to change.
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * Do not use it as an API
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * @author Jamie Ho
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * @since 1.4
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
public class SourceToHTMLConverter {
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
     * The background color.
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    protected static final String BGCOLOR = "white";
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
     * The line number color.
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    protected static final String LINE_NO_COLOR = "green";
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     * The number of trailing blank lines at the end of the page.
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     * This is inserted so that anchors at the bottom of small pages
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
     * can be reached.
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    protected static final int NUM_BLANK_LINES = 60;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     * Source is converted to HTML using static methods below.
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    private SourceToHTMLConverter() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * Convert the Classes in the given RootDoc to an HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     * @param configuration the configuration.
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * @param rd the RootDoc to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * @param outputdir the name of the directory to output to.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    public static void convertRoot(Configuration configuration, RootDoc rd, String outputdir) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        if (rd == null || outputdir == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        PackageDoc[] pds = rd.specifiedPackages();
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        for (int i = 0; i < pds.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
            convertPackage(configuration, pds[i], outputdir);
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        ClassDoc[] cds = rd.specifiedClasses();
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        for (int i = 0; i < cds.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            convertClass(configuration, cds[i],
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
                getPackageOutputDir(outputdir, cds[i].containingPackage()));
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     * Convert the Classes in the given Package to an HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * @param configuration the configuration.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     * @param pd the Package to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     * @param outputdir the name of the directory to output to.
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    public static void convertPackage(Configuration configuration, PackageDoc pd, String outputdir) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        if (pd == null || outputdir == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        String classOutputdir = getPackageOutputDir(outputdir, pd);
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        ClassDoc[] cds = pd.allClasses();
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        for (int i = 0; i < cds.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            convertClass(configuration, cds[i], classOutputdir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * Return the directory write output to for the given package.
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * @param outputDir the directory to output to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * @param pd the Package to generate output for.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    private static String getPackageOutputDir(String outputDir, PackageDoc pd) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        return outputDir + File.separator +
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            DirectoryManager.getDirectoryPath(pd) + File.separator;
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     * Convert the given Class to an HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     * @param configuration the configuration.
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
     * @param cd the class to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
     * @param outputdir the name of the directory to output to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    public static void convertClass(Configuration configuration, ClassDoc cd, String outputdir) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        if (cd == null || outputdir == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        try {
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   129
            SourcePosition sp = cd.position();
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   130
            if (sp == null)
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   131
                return;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   132
            Reader r;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   133
            // temp hack until we can update SourcePosition API.
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   134
            if (sp instanceof com.sun.tools.javadoc.SourcePositionImpl) {
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   135
                FileObject fo = ((com.sun.tools.javadoc.SourcePositionImpl) sp).fileObject();
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   136
                if (fo == null)
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   137
                    return;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   138
                r = fo.openReader(true);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   139
            } else {
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   140
                File file = sp.file();
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   141
                if (file == null)
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   142
                    return;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   143
                r = new FileReader(file);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   144
            }
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   145
            LineNumberReader reader = new LineNumberReader(r);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            int lineno = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            String line;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            StringBuffer output = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
                while ((line = reader.readLine()) != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
                    output.append(formatLine(line, configuration.sourcetab, lineno));
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
                    lineno++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
                reader.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
            output = addLineNumbers(output.toString());
1863
e5de9abc0eb1 6786682: Javadoc HTML WCAG 2.0 accessibility issues in standard doclet - HTML tag should have lang attribute
bpatel
parents: 1264
diff changeset
   158
            output.insert(0, getHeader(configuration));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            output.append(getFooter());
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
            writeToFile(output.toString(), outputdir, cd.name(), configuration);
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        } catch (Exception e){
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            e.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
     * Write the output to the file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
     * @param output the string to output.
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
     * @param outputDir the directory to output to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
     * @param className the name of the class that I am converting to HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
     * @param configuration the Doclet configuration to pass notices to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    private static void writeToFile(String output, String outputDir, String className, Configuration configuration) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        File dir = new File(outputDir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        dir.mkdirs();
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        File newFile = new File(dir, className + ".html");
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        configuration.message.notice("doclet.Generating_0", newFile.getPath());
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        FileOutputStream fout = new FileOutputStream(newFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout));
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        bw.write(output);
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        bw.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        fout.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
     * Given a <code>String</code>, add line numbers.
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
     * @param s the text to add line numbers to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
     * @return the string buffer with the line numbering for each line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    private static StringBuffer addLineNumbers(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        StringBuffer sb = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        StringTokenizer st = new StringTokenizer(s, "\n", true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        int lineno = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        String current;
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        while(st.hasMoreTokens()){
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            current = st.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
            sb.append(current.equals("\n") ?
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                    getHTMLLineNo(lineno) + current :
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                    getHTMLLineNo(lineno) + current + st.nextToken());
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            lineno++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        return sb;
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
     * Get the header.
1863
e5de9abc0eb1 6786682: Javadoc HTML WCAG 2.0 accessibility issues in standard doclet - HTML tag should have lang attribute
bpatel
parents: 1264
diff changeset
   208
     * @param configuration the Doclet configuration
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
     * @return the header to the output file
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
     */
1863
e5de9abc0eb1 6786682: Javadoc HTML WCAG 2.0 accessibility issues in standard doclet - HTML tag should have lang attribute
bpatel
parents: 1264
diff changeset
   211
    protected static String getHeader(Configuration configuration) {
e5de9abc0eb1 6786682: Javadoc HTML WCAG 2.0 accessibility issues in standard doclet - HTML tag should have lang attribute
bpatel
parents: 1264
diff changeset
   212
        StringBuffer result = new StringBuffer("<HTML lang=\"" + configuration.getLocale().getLanguage() + "\">" + DocletConstants.NL);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        result.append("<BODY BGCOLOR=\""+ BGCOLOR + "\">" + DocletConstants.NL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        result.append("<PRE>" + DocletConstants.NL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        return result.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
     * Get the footer
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
     * @return the footer to the output file
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
    protected static String getFooter() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        StringBuffer footer = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        for (int i = 0; i < NUM_BLANK_LINES; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
            footer.append(DocletConstants.NL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        footer.append("</PRE>" + DocletConstants.NL + "</BODY>" +
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            DocletConstants.NL + "</HTML>" + DocletConstants.NL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
        return footer.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
     * Get the HTML for the lines.
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
     * @param lineno The line number
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
     * @return the HTML code for the line
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
    protected static String getHTMLLineNo(int lineno) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        StringBuffer result = new StringBuffer("<FONT color=\"" + LINE_NO_COLOR
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            + "\">");
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        if (lineno < 10) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
            result.append("00" + ((new Integer(lineno)).toString()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
        } else if (lineno < 100) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            result.append("0" + ((new Integer(lineno)).toString()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
            result.append((new Integer(lineno)).toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        result.append("</FONT>    ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        return result.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
     * Format a given line of source. <br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
     * Note:  In the future, we will add special colors for constructs in the
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
     * language.
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
     * @param line the string to format.
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
     * @param tabLength the number of spaces for each tab.
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
     * @param currentLineNo the current number.
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
    protected static String formatLine(String line, int tabLength, int currentLineNo) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        if (line == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        StringBuffer lineBuffer = new StringBuffer(Util.escapeHtmlChars(line));
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
        //Insert an anchor for the line
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
        lineBuffer.append("<a name=\"line." + Integer.toString(currentLineNo) + "\"></a>");
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
        lineBuffer.append(DocletConstants.NL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
        Util.replaceTabs(tabLength, lineBuffer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
        return lineBuffer.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
     * Given an array of <code>Doc</code>s, add to the given <code>HashMap</code> the
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
     * line numbers and anchors that should be inserted in the output at those lines.
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
     * @param docs the array of <code>Doc</code>s to add anchors for.
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
     * @param hash the <code>HashMap</code> to add to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
     */
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   277
    protected static void addToHash(Doc[] docs, HashMap<Integer,String> hash) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
        if(docs == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
        for(int i = 0; i < docs.length; i++) {
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   282
            hash.put(docs[i].position().line(), getAnchor(docs[i]));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
     * Given a <code>Doc</code>, return an anchor for it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
     * @param d the <code>Doc</code> to check.
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     * @return an anchor of the form &lt;a name="my_name">&lt;/a>
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
    protected static String getAnchor(Doc d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        return "    <a name=\"" + getAnchorName(d) + "\"></a>";
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
     * Given a <code>Doc</code>, return an anchor name for it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
     * @param d the <code>Doc</code> to check.
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
     * @return the name of the anchor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
    public static String getAnchorName(Doc d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        return "line." + d.position().line();
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
}