src/share/classes/com/sun/tools/doclets/formats/html/SourceToHTMLConverter.java

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 766
90af8d87741f
child 910
ebf7c13df6c0
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

duke@1 1 /*
ohair@798 2 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. 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
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle 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 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
bpatel@766 26 package com.sun.tools.doclets.formats.html;
duke@1 27
duke@1 28 import java.io.*;
jjg@197 29 import javax.tools.FileObject;
jjg@197 30 import com.sun.javadoc.*;
jjg@197 31 import com.sun.tools.doclets.internal.toolkit.*;
bpatel@766 32 import com.sun.tools.doclets.internal.toolkit.util.*;
bpatel@766 33 import com.sun.tools.doclets.formats.html.markup.*;
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
bpatel@766 43 * @author Bhavesh Patel (Modified)
duke@1 44 * @since 1.4
duke@1 45 */
duke@1 46 public class SourceToHTMLConverter {
duke@1 47
duke@1 48 /**
duke@1 49 * The number of trailing blank lines at the end of the page.
duke@1 50 * This is inserted so that anchors at the bottom of small pages
duke@1 51 * can be reached.
duke@1 52 */
bpatel@766 53 private static final int NUM_BLANK_LINES = 60;
duke@1 54
bpatel@766 55 /**
bpatel@766 56 * New line to be added to the documentation.
bpatel@766 57 */
bpatel@766 58 private static final Content NEW_LINE = new RawHtml(DocletConstants.NL);
bpatel@766 59
bpatel@766 60 /**
bpatel@766 61 * Relative path from the documentation root to the file that is being
bpatel@766 62 * generated.
bpatel@766 63 */
bpatel@766 64 private static String relativePath = "";
duke@1 65
duke@1 66 /**
duke@1 67 * Source is converted to HTML using static methods below.
duke@1 68 */
duke@1 69 private SourceToHTMLConverter() {}
duke@1 70
duke@1 71 /**
duke@1 72 * Convert the Classes in the given RootDoc to an HTML.
bpatel@766 73 *
duke@1 74 * @param configuration the configuration.
duke@1 75 * @param rd the RootDoc to convert.
duke@1 76 * @param outputdir the name of the directory to output to.
duke@1 77 */
bpatel@766 78 public static void convertRoot(ConfigurationImpl configuration, RootDoc rd,
bpatel@766 79 String outputdir) {
duke@1 80 if (rd == null || outputdir == null) {
duke@1 81 return;
duke@1 82 }
duke@1 83 PackageDoc[] pds = rd.specifiedPackages();
duke@1 84 for (int i = 0; i < pds.length; i++) {
duke@1 85 convertPackage(configuration, pds[i], outputdir);
duke@1 86 }
duke@1 87 ClassDoc[] cds = rd.specifiedClasses();
duke@1 88 for (int i = 0; i < cds.length; i++) {
duke@1 89 convertClass(configuration, cds[i],
bpatel@766 90 getPackageOutputDir(outputdir, cds[i].containingPackage()));
duke@1 91 }
duke@1 92 }
duke@1 93
duke@1 94 /**
duke@1 95 * Convert the Classes in the given Package to an HTML.
bpatel@766 96 *
duke@1 97 * @param configuration the configuration.
duke@1 98 * @param pd the Package to convert.
duke@1 99 * @param outputdir the name of the directory to output to.
duke@1 100 */
bpatel@766 101 public static void convertPackage(ConfigurationImpl configuration, PackageDoc pd,
bpatel@766 102 String outputdir) {
duke@1 103 if (pd == null || outputdir == null) {
duke@1 104 return;
duke@1 105 }
duke@1 106 String classOutputdir = getPackageOutputDir(outputdir, pd);
duke@1 107 ClassDoc[] cds = pd.allClasses();
duke@1 108 for (int i = 0; i < cds.length; i++) {
duke@1 109 convertClass(configuration, cds[i], classOutputdir);
duke@1 110 }
duke@1 111 }
duke@1 112
duke@1 113 /**
duke@1 114 * Return the directory write output to for the given package.
bpatel@766 115 *
duke@1 116 * @param outputDir the directory to output to.
duke@1 117 * @param pd the Package to generate output for.
bpatel@766 118 * @return the package output directory as a String.
duke@1 119 */
duke@1 120 private static String getPackageOutputDir(String outputDir, PackageDoc pd) {
duke@1 121 return outputDir + File.separator +
duke@1 122 DirectoryManager.getDirectoryPath(pd) + File.separator;
duke@1 123 }
duke@1 124
duke@1 125 /**
duke@1 126 * Convert the given Class to an HTML.
bpatel@766 127 *
duke@1 128 * @param configuration the configuration.
duke@1 129 * @param cd the class to convert.
duke@1 130 * @param outputdir the name of the directory to output to.
duke@1 131 */
bpatel@766 132 public static void convertClass(ConfigurationImpl configuration, ClassDoc cd,
bpatel@766 133 String outputdir) {
duke@1 134 if (cd == null || outputdir == null) {
duke@1 135 return;
duke@1 136 }
duke@1 137 try {
jjg@197 138 SourcePosition sp = cd.position();
jjg@197 139 if (sp == null)
jjg@197 140 return;
jjg@197 141 Reader r;
jjg@197 142 // temp hack until we can update SourcePosition API.
jjg@197 143 if (sp instanceof com.sun.tools.javadoc.SourcePositionImpl) {
jjg@197 144 FileObject fo = ((com.sun.tools.javadoc.SourcePositionImpl) sp).fileObject();
jjg@197 145 if (fo == null)
jjg@197 146 return;
jjg@197 147 r = fo.openReader(true);
jjg@197 148 } else {
jjg@197 149 File file = sp.file();
jjg@197 150 if (file == null)
jjg@197 151 return;
jjg@197 152 r = new FileReader(file);
jjg@197 153 }
jjg@197 154 LineNumberReader reader = new LineNumberReader(r);
duke@1 155 int lineno = 1;
duke@1 156 String line;
bpatel@766 157 relativePath = DirectoryManager.getRelativePath(DocletConstants.SOURCE_OUTPUT_DIR_NAME) +
bpatel@766 158 DirectoryManager.getRelativePath(cd.containingPackage());
bpatel@766 159 Content body = getHeader();
bpatel@766 160 Content pre = new HtmlTree(HtmlTag.PRE);
duke@1 161 try {
duke@1 162 while ((line = reader.readLine()) != null) {
bpatel@766 163 addLineNo(pre, lineno);
bpatel@766 164 addLine(pre, line, configuration.sourcetab, lineno);
duke@1 165 lineno++;
duke@1 166 }
duke@1 167 } finally {
duke@1 168 reader.close();
duke@1 169 }
bpatel@766 170 addBlankLines(pre);
bpatel@766 171 Content div = HtmlTree.DIV(HtmlStyle.sourceContainer, pre);
bpatel@766 172 body.addContent(div);
bpatel@766 173 writeToFile(body, outputdir, cd.name(), configuration);
duke@1 174 } catch (Exception e){
duke@1 175 e.printStackTrace();
duke@1 176 }
duke@1 177 }
duke@1 178
duke@1 179 /**
duke@1 180 * Write the output to the file.
bpatel@766 181 *
bpatel@766 182 * @param body the documentation content to be written to the file.
duke@1 183 * @param outputDir the directory to output to.
duke@1 184 * @param className the name of the class that I am converting to HTML.
duke@1 185 * @param configuration the Doclet configuration to pass notices to.
duke@1 186 */
bpatel@766 187 private static void writeToFile(Content body, String outputDir,
bpatel@766 188 String className, ConfigurationImpl configuration) throws IOException {
bpatel@766 189 Content htmlDocType = DocType.Transitional();
bpatel@766 190 Content head = new HtmlTree(HtmlTag.HEAD);
bpatel@766 191 head.addContent(HtmlTree.TITLE(new StringContent(
bpatel@766 192 configuration.getText("doclet.Window_Source_title"))));
bpatel@766 193 head.addContent(getStyleSheetProperties(configuration));
bpatel@766 194 Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(),
bpatel@766 195 head, body);
bpatel@766 196 Content htmlDocument = new HtmlDocument(htmlDocType, htmlTree);
duke@1 197 File dir = new File(outputDir);
duke@1 198 dir.mkdirs();
duke@1 199 File newFile = new File(dir, className + ".html");
duke@1 200 configuration.message.notice("doclet.Generating_0", newFile.getPath());
duke@1 201 FileOutputStream fout = new FileOutputStream(newFile);
duke@1 202 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout));
bpatel@766 203 bw.write(htmlDocument.toString());
duke@1 204 bw.close();
duke@1 205 fout.close();
duke@1 206 }
duke@1 207
duke@1 208 /**
bpatel@766 209 * Returns a link to the stylesheet file.
duke@1 210 *
bpatel@766 211 * @param configuration the doclet configuration for the current run of javadoc
bpatel@766 212 * @return an HtmlTree for the lINK tag which provides the stylesheet location
duke@1 213 */
bpatel@766 214 public static HtmlTree getStyleSheetProperties(ConfigurationImpl configuration) {
bpatel@766 215 String filename = configuration.stylesheetfile;
bpatel@766 216 if (filename.length() > 0) {
bpatel@766 217 File stylefile = new File(filename);
bpatel@766 218 String parent = stylefile.getParent();
bpatel@766 219 filename = (parent == null)?
bpatel@766 220 filename:
bpatel@766 221 filename.substring(parent.length() + 1);
bpatel@766 222 } else {
bpatel@766 223 filename = "stylesheet.css";
duke@1 224 }
bpatel@766 225 filename = relativePath + filename;
bpatel@766 226 HtmlTree link = HtmlTree.LINK("stylesheet", "text/css", filename, "Style");
bpatel@766 227 return link;
duke@1 228 }
duke@1 229
duke@1 230 /**
duke@1 231 * Get the header.
bpatel@766 232 *
bpatel@766 233 * @return the header content for the HTML file
duke@1 234 */
bpatel@766 235 private static Content getHeader() {
bpatel@766 236 return new HtmlTree(HtmlTag.BODY);
duke@1 237 }
duke@1 238
duke@1 239 /**
bpatel@766 240 * Add the line numbers for the source code.
bpatel@766 241 *
bpatel@766 242 * @param pre the content tree to which the line number will be added
bpatel@766 243 * @param lineno The line number
duke@1 244 */
bpatel@766 245 private static void addLineNo(Content pre, int lineno) {
bpatel@766 246 HtmlTree span = new HtmlTree(HtmlTag.SPAN);
bpatel@766 247 span.addStyle(HtmlStyle.sourceLineNo);
bpatel@766 248 if (lineno < 10) {
bpatel@766 249 span.addContent("00" + Integer.toString(lineno));
bpatel@766 250 } else if (lineno < 100) {
bpatel@766 251 span.addContent("0" + Integer.toString(lineno));
bpatel@766 252 } else {
bpatel@766 253 span.addContent(Integer.toString(lineno));
duke@1 254 }
bpatel@766 255 pre.addContent(span);
duke@1 256 }
duke@1 257
duke@1 258 /**
bpatel@766 259 * Add a line from source to the HTML file that is generated.
bpatel@766 260 *
bpatel@766 261 * @param pre the content tree to which the line will be added.
duke@1 262 * @param line the string to format.
duke@1 263 * @param tabLength the number of spaces for each tab.
duke@1 264 * @param currentLineNo the current number.
duke@1 265 */
bpatel@766 266 private static void addLine(Content pre, String line, int tabLength,
bpatel@766 267 int currentLineNo) {
bpatel@766 268 if (line != null) {
bpatel@766 269 StringBuffer lineBuffer = new StringBuffer(Util.escapeHtmlChars(line));
bpatel@766 270 Util.replaceTabs(tabLength, lineBuffer);
bpatel@766 271 pre.addContent(new RawHtml(lineBuffer.toString()));
bpatel@766 272 Content anchor = HtmlTree.A_NAME("line." + Integer.toString(currentLineNo));
bpatel@766 273 pre.addContent(anchor);
bpatel@766 274 pre.addContent(NEW_LINE);
duke@1 275 }
duke@1 276 }
duke@1 277
duke@1 278 /**
bpatel@766 279 * Add trailing blank lines at the end of the page.
bpatel@766 280 *
bpatel@766 281 * @param pre the content tree to which the blank lines will be added.
duke@1 282 */
bpatel@766 283 private static void addBlankLines(Content pre) {
bpatel@766 284 for (int i = 0; i < NUM_BLANK_LINES; i++) {
bpatel@766 285 pre.addContent(NEW_LINE);
bpatel@766 286 }
duke@1 287 }
duke@1 288
duke@1 289 /**
duke@1 290 * Given a <code>Doc</code>, return an anchor name for it.
bpatel@766 291 *
duke@1 292 * @param d the <code>Doc</code> to check.
duke@1 293 * @return the name of the anchor.
duke@1 294 */
duke@1 295 public static String getAnchorName(Doc d) {
duke@1 296 return "line." + d.position().line();
duke@1 297 }
duke@1 298 }

mercurial