Tue, 23 Oct 2012 13:58:56 -0700
8000416: refactor javadoc to provide and use an abstraction for relative URIs
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 DocPath constantsPath =
176 htmlWriter.pathToRoot.resolve(DocPaths.CONSTANT_VALUES);
177 String whichConstant =
178 ((ClassWriterImpl) htmlWriter).getClassDoc().qualifiedName() + "." + ((FieldDoc) holder).name();
179 DocLink link = constantsPath.fragment(whichConstant);
180 result += htmlWriter.getHyperLinkString(link,
181 htmlWriter.configuration.getText("doclet.Constants_Summary"),
182 false);
183 }
184 if (holder.isClass() && ((ClassDoc)holder).isSerializable()) {
185 //Automatically add link to serialized form page for serializable classes.
186 if ((SerializedFormBuilder.serialInclude(holder) &&
187 SerializedFormBuilder.serialInclude(((ClassDoc)holder).containingPackage()))) {
188 result = addSeeHeader(result);
189 DocPath serialPath = htmlWriter.pathToRoot.resolve(DocPaths.SERIALIZED_FORM);
190 DocLink link = serialPath.fragment(((ClassDoc)holder).qualifiedName());
191 result += htmlWriter.getHyperLinkString(link,
192 htmlWriter.configuration.getText("doclet.Serialized_Form"), false);
193 }
194 }
195 return result.equals("") ? null : new TagletOutputImpl(result + "</dd>");
196 }
198 private String addSeeHeader(String result) {
199 if (result != null && result.length() > 0) {
200 return result + ", " + DocletConstants.NL;
201 } else {
202 return "<dt><span class=\"strong\">" +
203 htmlWriter.configuration().getText("doclet.See_Also") + "</span></dt><dd>";
204 }
205 }
207 /**
208 * {@inheritDoc}
209 */
210 public TagletOutput simpleTagOutput(Tag[] simpleTags, String header) {
211 String result = "<dt><span class=\"strong\">" + header + "</span></dt>" + DocletConstants.NL +
212 " <dd>";
213 for (int i = 0; i < simpleTags.length; i++) {
214 if (i > 0) {
215 result += ", ";
216 }
217 result += htmlWriter.commentTagsToString(simpleTags[i], null, simpleTags[i].inlineTags(), false);
218 }
219 result += "</dd>" + DocletConstants.NL;
220 return new TagletOutputImpl(result);
221 }
223 /**
224 * {@inheritDoc}
225 */
226 public TagletOutput simpleTagOutput(Tag simpleTag, String header) {
227 return new TagletOutputImpl("<dt><span class=\"strong\">" + header + "</span></dt>" + " <dd>"
228 + htmlWriter.commentTagsToString(simpleTag, null, simpleTag.inlineTags(), false)
229 + "</dd>" + DocletConstants.NL);
230 }
232 /**
233 * {@inheritDoc}
234 */
235 public TagletOutput getThrowsHeader() {
236 return new TagletOutputImpl(DocletConstants.NL + "<dt>" + "<span class=\"strong\">" +
237 htmlWriter.configuration().getText("doclet.Throws") + "</span></dt>");
238 }
240 /**
241 * {@inheritDoc}
242 */
243 public TagletOutput throwsTagOutput(ThrowsTag throwsTag) {
244 String result = DocletConstants.NL + "<dd>";
245 result += throwsTag.exceptionType() == null ?
246 htmlWriter.codeText(throwsTag.exceptionName()) :
247 htmlWriter.codeText(
248 htmlWriter.getLink(new LinkInfoImpl(LinkInfoImpl.CONTEXT_MEMBER,
249 throwsTag.exceptionType())));
250 TagletOutput text = new TagletOutputImpl(
251 htmlWriter.commentTagsToString(throwsTag, null,
252 throwsTag.inlineTags(), false));
253 if (text != null && text.toString().length() > 0) {
254 result += " - " + text;
255 }
256 result += "</dd>";
257 return new TagletOutputImpl(result);
258 }
260 /**
261 * {@inheritDoc}
262 */
263 public TagletOutput throwsTagOutput(Type throwsType) {
264 return new TagletOutputImpl(DocletConstants.NL + "<dd>" +
265 htmlWriter.codeText(htmlWriter.getLink(
266 new LinkInfoImpl(LinkInfoImpl.CONTEXT_MEMBER, throwsType))) + "</dd>");
267 }
269 /**
270 * {@inheritDoc}
271 */
272 public TagletOutput valueTagOutput(FieldDoc field, String constantVal,
273 boolean includeLink) {
274 return new TagletOutputImpl(includeLink ?
275 htmlWriter.getDocLink(LinkInfoImpl.CONTEXT_VALUE_TAG, field,
276 constantVal, false) : constantVal);
277 }
279 /**
280 * {@inheritDoc}
281 */
282 public TagletOutput commentTagsToOutput(Tag holderTag, Tag[] tags) {
283 return commentTagsToOutput(holderTag, null, tags, false);
284 }
286 /**
287 * {@inheritDoc}
288 */
289 public TagletOutput commentTagsToOutput(Doc holderDoc, Tag[] tags) {
290 return commentTagsToOutput(null, holderDoc, tags, false);
291 }
293 /**
294 * {@inheritDoc}
295 */
296 public TagletOutput commentTagsToOutput(Tag holderTag,
297 Doc holderDoc, Tag[] tags, boolean isFirstSentence) {
298 return new TagletOutputImpl(htmlWriter.commentTagsToString(
299 holderTag, holderDoc, tags, isFirstSentence));
300 }
302 /**
303 * {@inheritDoc}
304 */
305 public Configuration configuration() {
306 return htmlWriter.configuration();
307 }
309 /**
310 * Return an instance of a TagletWriter that knows how to write HTML.
311 *
312 * @return an instance of a TagletWriter that knows how to write HTML.
313 */
314 public TagletOutput getTagletOutputInstance() {
315 return new TagletOutputImpl("");
316 }
317 }