src/share/classes/com/sun/tools/doclets/internal/toolkit/util/SourceToHTMLConverter.java

Tue, 20 Jan 2009 15:17:45 -0800

author
jjg
date
Tue, 20 Jan 2009 15:17:45 -0800
changeset 197
1bf037016426
parent 191
d79ad96ce47c
child 229
03bcd66bd8e7
permissions
-rw-r--r--

6794582: javadoc should read files using a FileManager
Reviewed-by: darcy, bpatel

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

mercurial