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