Tue, 23 Oct 2012 13:20:37 -0700
8000741: refactor javadoc to use abstraction to handle relative paths
Reviewed-by: darcy
1 /*
2 * Copyright (c) 2003, 2012, 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 */
26 package com.sun.tools.doclets.formats.html;
28 import com.sun.javadoc.*;
29 import com.sun.tools.doclets.internal.toolkit.*;
30 import com.sun.tools.doclets.internal.toolkit.builders.SerializedFormBuilder;
31 import com.sun.tools.doclets.internal.toolkit.taglets.*;
32 import com.sun.tools.doclets.internal.toolkit.util.*;
34 /**
35 * The taglet writer that writes HTML.
36 *
37 * <p><b>This is NOT part of any supported API.
38 * If you write code that depends on this, you do so at your own risk.
39 * This code and its internal interfaces are subject to change or
40 * deletion without notice.</b>
41 *
42 * @since 1.5
43 * @author Jamie Ho
44 * @author Bhavesh Patel (Modified)
45 */
47 public class TagletWriterImpl extends TagletWriter {
49 private HtmlDocletWriter htmlWriter;
51 public TagletWriterImpl(HtmlDocletWriter htmlWriter, boolean isFirstSentence) {
52 this.htmlWriter = htmlWriter;
53 this.isFirstSentence = isFirstSentence;
54 }
56 /**
57 * {@inheritDoc}
58 */
59 public TagletOutput getOutputInstance() {
60 return new TagletOutputImpl("");
61 }
63 /**
64 * {@inheritDoc}
65 */
66 public TagletOutput getDocRootOutput() {
67 if (htmlWriter.configuration.docrootparent.length() > 0)
68 return new TagletOutputImpl(htmlWriter.configuration.docrootparent);
69 else if (htmlWriter.pathToRoot.isEmpty())
70 return new TagletOutputImpl(".");
71 else
72 return new TagletOutputImpl(htmlWriter.pathToRoot.getPath());
73 }
75 /**
76 * {@inheritDoc}
77 */
78 public TagletOutput deprecatedTagOutput(Doc doc) {
79 StringBuilder output = new StringBuilder();
80 Tag[] deprs = doc.tags("deprecated");
81 if (doc instanceof ClassDoc) {
82 if (Util.isDeprecated((ProgramElementDoc) doc)) {
83 output.append("<span class=\"strong\">" +
84 ConfigurationImpl.getInstance().
85 getText("doclet.Deprecated") + "</span> ");
86 if (deprs.length > 0) {
87 Tag[] commentTags = deprs[0].inlineTags();
88 if (commentTags.length > 0) {
90 output.append(commentTagsToOutput(null, doc,
91 deprs[0].inlineTags(), false).toString()
92 );
93 }
94 }
95 }
96 } else {
97 MemberDoc member = (MemberDoc) doc;
98 if (Util.isDeprecated((ProgramElementDoc) doc)) {
99 output.append("<span class=\"strong\">" +
100 ConfigurationImpl.getInstance().
101 getText("doclet.Deprecated") + "</span> ");
102 if (deprs.length > 0) {
103 output.append("<i>");
104 output.append(commentTagsToOutput(null, doc,
105 deprs[0].inlineTags(), false).toString());
106 output.append("</i>");
107 }
108 } else {
109 if (Util.isDeprecated(member.containingClass())) {
110 output.append("<span class=\"strong\">" +
111 ConfigurationImpl.getInstance().
112 getText("doclet.Deprecated") + "</span> ");
113 }
114 }
115 }
116 return new TagletOutputImpl(output.toString());
117 }
119 /**
120 * {@inheritDoc}
121 */
122 public MessageRetriever getMsgRetriever() {
123 return htmlWriter.configuration.message;
124 }
126 /**
127 * {@inheritDoc}
128 */
129 public TagletOutput getParamHeader(String header) {
130 StringBuilder result = new StringBuilder();
131 result.append("<dt>");
132 result.append("<span class=\"strong\">").append(header).append("</span></dt>");
133 return new TagletOutputImpl(result.toString());
134 }
136 /**
137 * {@inheritDoc}
138 */
139 public TagletOutput paramTagOutput(ParamTag paramTag, String paramName) {
140 TagletOutput result = new TagletOutputImpl("<dd><code>" + paramName + "</code>"
141 + " - " + htmlWriter.commentTagsToString(paramTag, null, paramTag.inlineTags(), false) + "</dd>");
142 return result;
143 }
145 /**
146 * {@inheritDoc}
147 */
148 public TagletOutput returnTagOutput(Tag returnTag) {
149 TagletOutput result = new TagletOutputImpl(DocletConstants.NL + "<dt>" +
150 "<span class=\"strong\">" + htmlWriter.configuration.getText("doclet.Returns") +
151 "</span>" + "</dt>" + "<dd>" +
152 htmlWriter.commentTagsToString(returnTag, null, returnTag.inlineTags(),
153 false) + "</dd>");
154 return result;
155 }
157 /**
158 * {@inheritDoc}
159 */
160 public TagletOutput seeTagOutput(Doc holder, SeeTag[] seeTags) {
161 String result = "";
162 if (seeTags.length > 0) {
163 result = addSeeHeader(result);
164 for (int i = 0; i < seeTags.length; ++i) {
165 if (i > 0) {
166 result += ", " + DocletConstants.NL;
167 }
168 result += htmlWriter.seeTagToString(seeTags[i]);
169 }
170 }
171 if (holder.isField() && ((FieldDoc)holder).constantValue() != null &&
172 htmlWriter instanceof ClassWriterImpl) {
173 //Automatically add link to constant values page for constant fields.
174 result = addSeeHeader(result);
175 result += htmlWriter.getHyperLinkString(htmlWriter.pathToRoot.resolve(
176 DocPaths.CONSTANT_VALUES),
177 ((ClassWriterImpl) htmlWriter).getClassDoc().qualifiedName()
178 + "." + ((FieldDoc) holder).name(),
179 htmlWriter.configuration.getText("doclet.Constants_Summary"), false);
180 }
181 if (holder.isClass() && ((ClassDoc)holder).isSerializable()) {
182 //Automatically add link to serialized form page for serializable classes.
183 if ((SerializedFormBuilder.serialInclude(holder) &&
184 SerializedFormBuilder.serialInclude(((ClassDoc)holder).containingPackage()))) {
185 result = addSeeHeader(result);
186 result += htmlWriter.getHyperLinkString(htmlWriter.pathToRoot.resolve(DocPaths.SERIALIZED_FORM),
187 ((ClassDoc)holder).qualifiedName(), htmlWriter.configuration.getText("doclet.Serialized_Form"), false);
188 }
189 }
190 return result.equals("") ? null : new TagletOutputImpl(result + "</dd>");
191 }
193 private String addSeeHeader(String result) {
194 if (result != null && result.length() > 0) {
195 return result + ", " + DocletConstants.NL;
196 } else {
197 return "<dt><span class=\"strong\">" +
198 htmlWriter.configuration().getText("doclet.See_Also") + "</span></dt><dd>";
199 }
200 }
202 /**
203 * {@inheritDoc}
204 */
205 public TagletOutput simpleTagOutput(Tag[] simpleTags, String header) {
206 String result = "<dt><span class=\"strong\">" + header + "</span></dt>" + DocletConstants.NL +
207 " <dd>";
208 for (int i = 0; i < simpleTags.length; i++) {
209 if (i > 0) {
210 result += ", ";
211 }
212 result += htmlWriter.commentTagsToString(simpleTags[i], null, simpleTags[i].inlineTags(), false);
213 }
214 result += "</dd>" + DocletConstants.NL;
215 return new TagletOutputImpl(result);
216 }
218 /**
219 * {@inheritDoc}
220 */
221 public TagletOutput simpleTagOutput(Tag simpleTag, String header) {
222 return new TagletOutputImpl("<dt><span class=\"strong\">" + header + "</span></dt>" + " <dd>"
223 + htmlWriter.commentTagsToString(simpleTag, null, simpleTag.inlineTags(), false)
224 + "</dd>" + DocletConstants.NL);
225 }
227 /**
228 * {@inheritDoc}
229 */
230 public TagletOutput getThrowsHeader() {
231 return new TagletOutputImpl(DocletConstants.NL + "<dt>" + "<span class=\"strong\">" +
232 htmlWriter.configuration().getText("doclet.Throws") + "</span></dt>");
233 }
235 /**
236 * {@inheritDoc}
237 */
238 public TagletOutput throwsTagOutput(ThrowsTag throwsTag) {
239 String result = DocletConstants.NL + "<dd>";
240 result += throwsTag.exceptionType() == null ?
241 htmlWriter.codeText(throwsTag.exceptionName()) :
242 htmlWriter.codeText(
243 htmlWriter.getLink(new LinkInfoImpl(LinkInfoImpl.CONTEXT_MEMBER,
244 throwsTag.exceptionType())));
245 TagletOutput text = new TagletOutputImpl(
246 htmlWriter.commentTagsToString(throwsTag, null,
247 throwsTag.inlineTags(), false));
248 if (text != null && text.toString().length() > 0) {
249 result += " - " + text;
250 }
251 result += "</dd>";
252 return new TagletOutputImpl(result);
253 }
255 /**
256 * {@inheritDoc}
257 */
258 public TagletOutput throwsTagOutput(Type throwsType) {
259 return new TagletOutputImpl(DocletConstants.NL + "<dd>" +
260 htmlWriter.codeText(htmlWriter.getLink(
261 new LinkInfoImpl(LinkInfoImpl.CONTEXT_MEMBER, throwsType))) + "</dd>");
262 }
264 /**
265 * {@inheritDoc}
266 */
267 public TagletOutput valueTagOutput(FieldDoc field, String constantVal,
268 boolean includeLink) {
269 return new TagletOutputImpl(includeLink ?
270 htmlWriter.getDocLink(LinkInfoImpl.CONTEXT_VALUE_TAG, field,
271 constantVal, false) : constantVal);
272 }
274 /**
275 * {@inheritDoc}
276 */
277 public TagletOutput commentTagsToOutput(Tag holderTag, Tag[] tags) {
278 return commentTagsToOutput(holderTag, null, tags, false);
279 }
281 /**
282 * {@inheritDoc}
283 */
284 public TagletOutput commentTagsToOutput(Doc holderDoc, Tag[] tags) {
285 return commentTagsToOutput(null, holderDoc, tags, false);
286 }
288 /**
289 * {@inheritDoc}
290 */
291 public TagletOutput commentTagsToOutput(Tag holderTag,
292 Doc holderDoc, Tag[] tags, boolean isFirstSentence) {
293 return new TagletOutputImpl(htmlWriter.commentTagsToString(
294 holderTag, holderDoc, tags, isFirstSentence));
295 }
297 /**
298 * {@inheritDoc}
299 */
300 public Configuration configuration() {
301 return htmlWriter.configuration();
302 }
304 /**
305 * Return an instance of a TagletWriter that knows how to write HTML.
306 *
307 * @return an instance of a TagletWriter that knows how to write HTML.
308 */
309 public TagletOutput getTagletOutputInstance() {
310 return new TagletOutputImpl("");
311 }
312 }