langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/SourceToHTMLConverter.java
author jjg
Tue, 15 Jul 2008 19:22:51 -0700
changeset 868 d0f233085cbb
parent 10 06bc494ca11e
child 1264 076a3cde30d5
permissions -rw-r--r--
6657907: javadoc has unchecked warnings Reviewed-by: bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001-2006 Sun Microsystems, Inc.  All Rights Reserved.
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 com.sun.tools.doclets.internal.toolkit.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 * Converts Java Source Code to HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * This code is not part of an API.
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * It is implementation that is subject to change.
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * Do not use it as an API
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * @author Jamie Ho
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * @since 1.4
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
public class SourceToHTMLConverter {
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
     * The background color.
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    protected static final String BGCOLOR = "white";
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
     * The line number color.
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    protected static final String LINE_NO_COLOR = "green";
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     * The number of trailing blank lines at the end of the page.
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     * This is inserted so that anchors at the bottom of small pages
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     * can be reached.
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    protected static final int NUM_BLANK_LINES = 60;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     * Source is converted to HTML using static methods below.
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    private SourceToHTMLConverter() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     * Convert the Classes in the given RootDoc to an HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     * @param configuration the configuration.
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * @param rd the RootDoc to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     * @param outputdir the name of the directory to output to.
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    public static void convertRoot(Configuration configuration, RootDoc rd, String outputdir) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        if (rd == null || outputdir == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        PackageDoc[] pds = rd.specifiedPackages();
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        for (int i = 0; i < pds.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
            convertPackage(configuration, pds[i], outputdir);
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        ClassDoc[] cds = rd.specifiedClasses();
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        for (int i = 0; i < cds.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
            convertClass(configuration, cds[i],
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                getPackageOutputDir(outputdir, cds[i].containingPackage()));
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * Convert the Classes in the given Package to an HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     * @param configuration the configuration.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     * @param pd the Package to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * @param outputdir the name of the directory to output to.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    public static void convertPackage(Configuration configuration, PackageDoc pd, String outputdir) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        if (pd == null || outputdir == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        String classOutputdir = getPackageOutputDir(outputdir, pd);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        ClassDoc[] cds = pd.allClasses();
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        for (int i = 0; i < cds.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            convertClass(configuration, cds[i], classOutputdir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     * Return the directory write output to for the given package.
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * @param outputDir the directory to output to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * @param pd the Package to generate output for.
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    private static String getPackageOutputDir(String outputDir, PackageDoc pd) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        return outputDir + File.separator +
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            DirectoryManager.getDirectoryPath(pd) + File.separator;
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     * Convert the given Class to an HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     * @param configuration the configuration.
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     * @param cd the class to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     * @param outputdir the name of the directory to output to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    public static void convertClass(Configuration configuration, ClassDoc cd, String outputdir) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        if (cd == null || outputdir == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        File file;
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        SourcePosition sp = cd.position();
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        if (sp == null || (file = sp.file()) == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
            int lineno = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            String line;
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
            StringBuffer output = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            LineNumberReader reader = new LineNumberReader(new FileReader(file));
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                while ((line = reader.readLine()) != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                    output.append(formatLine(line, configuration.sourcetab, lineno));
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                    lineno++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
                reader.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
            output = addLineNumbers(output.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            output.insert(0, getHeader());
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            output.append(getFooter());
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            writeToFile(output.toString(), outputdir, cd.name(), configuration);
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        } catch (Exception e){
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
            e.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
     * Write the output to the file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
     * @param output the string to output.
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     * @param outputDir the directory to output to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     * @param className the name of the class that I am converting to HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     * @param configuration the Doclet configuration to pass notices to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    private static void writeToFile(String output, String outputDir, String className, Configuration configuration) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        File dir = new File(outputDir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        dir.mkdirs();
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        File newFile = new File(dir, className + ".html");
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        configuration.message.notice("doclet.Generating_0", newFile.getPath());
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        FileOutputStream fout = new FileOutputStream(newFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout));
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        bw.write(output);
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        bw.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        fout.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
     * Given a <code>String</code>, add line numbers.
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
     * @param s the text to add line numbers to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
     * @return the string buffer with the line numbering for each line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    private static StringBuffer addLineNumbers(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        StringBuffer sb = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        StringTokenizer st = new StringTokenizer(s, "\n", true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        int lineno = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        String current;
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        while(st.hasMoreTokens()){
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            current = st.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
            sb.append(current.equals("\n") ?
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                    getHTMLLineNo(lineno) + current :
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                    getHTMLLineNo(lineno) + current + st.nextToken());
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
            lineno++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        return sb;
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
     * Get the header.
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
     * @return the header to the output file
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
    protected static String getHeader() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        StringBuffer result = new StringBuffer("<HTML>" + DocletConstants.NL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        result.append("<BODY BGCOLOR=\""+ BGCOLOR + "\">" + DocletConstants.NL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        result.append("<PRE>" + DocletConstants.NL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        return result.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
     * Get the footer
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
     * @return the footer to the output file
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
    protected static String getFooter() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        StringBuffer footer = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        for (int i = 0; i < NUM_BLANK_LINES; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
            footer.append(DocletConstants.NL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        footer.append("</PRE>" + DocletConstants.NL + "</BODY>" +
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            DocletConstants.NL + "</HTML>" + DocletConstants.NL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        return footer.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 HTML for the lines.
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
     * @param lineno The line number
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
     * @return the HTML code for the line
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
    protected static String getHTMLLineNo(int lineno) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        StringBuffer result = new StringBuffer("<FONT color=\"" + LINE_NO_COLOR
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
            + "\">");
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        if (lineno < 10) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            result.append("00" + ((new Integer(lineno)).toString()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        } else if (lineno < 100) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            result.append("0" + ((new Integer(lineno)).toString()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            result.append((new Integer(lineno)).toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        result.append("</FONT>    ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        return result.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
     * Format a given line of source. <br>
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
     * Note:  In the future, we will add special colors for constructs in the
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
     * language.
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
     * @param line the string to format.
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
     * @param tabLength the number of spaces for each tab.
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
     * @param currentLineNo the current number.
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
    protected static String formatLine(String line, int tabLength, int currentLineNo) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
        if (line == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
        StringBuffer lineBuffer = new StringBuffer(Util.escapeHtmlChars(line));
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
        //Insert an anchor for the line
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
        lineBuffer.append("<a name=\"line." + Integer.toString(currentLineNo) + "\"></a>");
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        lineBuffer.append(DocletConstants.NL);
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        Util.replaceTabs(tabLength, lineBuffer);
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        return lineBuffer.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
     * Given an array of <code>Doc</code>s, add to the given <code>HashMap</code> the
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
     * line numbers and anchors that should be inserted in the output at those lines.
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
     * @param docs the array of <code>Doc</code>s to add anchors for.
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
     * @param hash the <code>HashMap</code> to add to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
     */
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   263
    protected static void addToHash(Doc[] docs, HashMap<Integer,String> hash) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
        if(docs == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
        for(int i = 0; i < docs.length; i++) {
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   268
            hash.put(docs[i].position().line(), getAnchor(docs[i]));
10
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
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
     * Given a <code>Doc</code>, return an anchor for it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
     * @param d the <code>Doc</code> to check.
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
     * @return an anchor of the form &lt;a name="my_name">&lt;/a>
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
    protected static String getAnchor(Doc d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
        return "    <a name=\"" + getAnchorName(d) + "\"></a>";
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
     * Given a <code>Doc</code>, return an anchor name for it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
     * @param d the <code>Doc</code> to check.
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
     * @return the name of the anchor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    public static String getAnchorName(Doc d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        return "line." + d.position().line();
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
}