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

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

mercurial