Wed, 10 Oct 2012 18:44:21 -0700
8000310: Clean up use of StringBuffer in langtools
Reviewed-by: bpatel
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
70 return new TagletOutputImpl(htmlWriter.relativepathNoSlash);
71 }
73 /**
74 * {@inheritDoc}
75 */
76 public TagletOutput deprecatedTagOutput(Doc doc) {
77 StringBuilder output = new StringBuilder();
78 Tag[] deprs = doc.tags("deprecated");
79 if (doc instanceof ClassDoc) {
80 if (Util.isDeprecated((ProgramElementDoc) doc)) {
81 output.append("<span class=\"strong\">" +
82 ConfigurationImpl.getInstance().
83 getText("doclet.Deprecated") + "</span> ");
84 if (deprs.length > 0) {
85 Tag[] commentTags = deprs[0].inlineTags();
86 if (commentTags.length > 0) {
88 output.append(commentTagsToOutput(null, doc,
89 deprs[0].inlineTags(), false).toString()
90 );
91 }
92 }
93 }
94 } else {
95 MemberDoc member = (MemberDoc) doc;
96 if (Util.isDeprecated((ProgramElementDoc) doc)) {
97 output.append("<span class=\"strong\">" +
98 ConfigurationImpl.getInstance().
99 getText("doclet.Deprecated") + "</span> ");
100 if (deprs.length > 0) {
101 output.append("<i>");
102 output.append(commentTagsToOutput(null, doc,
103 deprs[0].inlineTags(), false).toString());
104 output.append("</i>");
105 }
106 } else {
107 if (Util.isDeprecated(member.containingClass())) {
108 output.append("<span class=\"strong\">" +
109 ConfigurationImpl.getInstance().
110 getText("doclet.Deprecated") + "</span> ");
111 }
112 }
113 }
114 return new TagletOutputImpl(output.toString());
115 }
117 /**
118 * {@inheritDoc}
119 */
120 public MessageRetriever getMsgRetriever() {
121 return htmlWriter.configuration.message;
122 }
124 /**
125 * {@inheritDoc}
126 */
127 public TagletOutput getParamHeader(String header) {
128 StringBuilder result = new StringBuilder();
129 result.append("<dt>");
130 result.append("<span class=\"strong\">").append(header).append("</span></dt>");
131 return new TagletOutputImpl(result.toString());
132 }
134 /**
135 * {@inheritDoc}
136 */
137 public TagletOutput paramTagOutput(ParamTag paramTag, String paramName) {
138 TagletOutput result = new TagletOutputImpl("<dd><code>" + paramName + "</code>"
139 + " - " + htmlWriter.commentTagsToString(paramTag, null, paramTag.inlineTags(), false) + "</dd>");
140 return result;
141 }
143 /**
144 * {@inheritDoc}
145 */
146 public TagletOutput returnTagOutput(Tag returnTag) {
147 TagletOutput result = new TagletOutputImpl(DocletConstants.NL + "<dt>" +
148 "<span class=\"strong\">" + htmlWriter.configuration.getText("doclet.Returns") +
149 "</span>" + "</dt>" + "<dd>" +
150 htmlWriter.commentTagsToString(returnTag, null, returnTag.inlineTags(),
151 false) + "</dd>");
152 return result;
153 }
155 /**
156 * {@inheritDoc}
157 */
158 public TagletOutput seeTagOutput(Doc holder, SeeTag[] seeTags) {
159 String result = "";
160 if (seeTags.length > 0) {
161 result = addSeeHeader(result);
162 for (int i = 0; i < seeTags.length; ++i) {
163 if (i > 0) {
164 result += ", " + DocletConstants.NL;
165 }
166 result += htmlWriter.seeTagToString(seeTags[i]);
167 }
168 }
169 if (holder.isField() && ((FieldDoc)holder).constantValue() != null &&
170 htmlWriter instanceof ClassWriterImpl) {
171 //Automatically add link to constant values page for constant fields.
172 result = addSeeHeader(result);
173 result += htmlWriter.getHyperLinkString(htmlWriter.relativePath +
174 ConfigurationImpl.CONSTANTS_FILE_NAME
175 + "#" + ((ClassWriterImpl) htmlWriter).getClassDoc().qualifiedName()
176 + "." + ((FieldDoc) holder).name(),
177 htmlWriter.configuration.getText("doclet.Constants_Summary"));
178 }
179 if (holder.isClass() && ((ClassDoc)holder).isSerializable()) {
180 //Automatically add link to serialized form page for serializable classes.
181 if ((SerializedFormBuilder.serialInclude(holder) &&
182 SerializedFormBuilder.serialInclude(((ClassDoc)holder).containingPackage()))) {
183 result = addSeeHeader(result);
184 result += htmlWriter.getHyperLinkString(htmlWriter.relativePath + "serialized-form.html",
185 ((ClassDoc)holder).qualifiedName(), htmlWriter.configuration.getText("doclet.Serialized_Form"), false);
186 }
187 }
188 return result.equals("") ? null : new TagletOutputImpl(result + "</dd>");
189 }
191 private String addSeeHeader(String result) {
192 if (result != null && result.length() > 0) {
193 return result + ", " + DocletConstants.NL;
194 } else {
195 return "<dt><span class=\"strong\">" +
196 htmlWriter.configuration().getText("doclet.See_Also") + "</span></dt><dd>";
197 }
198 }
200 /**
201 * {@inheritDoc}
202 */
203 public TagletOutput simpleTagOutput(Tag[] simpleTags, String header) {
204 String result = "<dt><span class=\"strong\">" + header + "</span></dt>" + DocletConstants.NL +
205 " <dd>";
206 for (int i = 0; i < simpleTags.length; i++) {
207 if (i > 0) {
208 result += ", ";
209 }
210 result += htmlWriter.commentTagsToString(simpleTags[i], null, simpleTags[i].inlineTags(), false);
211 }
212 result += "</dd>" + DocletConstants.NL;
213 return new TagletOutputImpl(result);
214 }
216 /**
217 * {@inheritDoc}
218 */
219 public TagletOutput simpleTagOutput(Tag simpleTag, String header) {
220 return new TagletOutputImpl("<dt><span class=\"strong\">" + header + "</span></dt>" + " <dd>"
221 + htmlWriter.commentTagsToString(simpleTag, null, simpleTag.inlineTags(), false)
222 + "</dd>" + DocletConstants.NL);
223 }
225 /**
226 * {@inheritDoc}
227 */
228 public TagletOutput getThrowsHeader() {
229 return new TagletOutputImpl(DocletConstants.NL + "<dt>" + "<span class=\"strong\">" +
230 htmlWriter.configuration().getText("doclet.Throws") + "</span></dt>");
231 }
233 /**
234 * {@inheritDoc}
235 */
236 public TagletOutput throwsTagOutput(ThrowsTag throwsTag) {
237 String result = DocletConstants.NL + "<dd>";
238 result += throwsTag.exceptionType() == null ?
239 htmlWriter.codeText(throwsTag.exceptionName()) :
240 htmlWriter.codeText(
241 htmlWriter.getLink(new LinkInfoImpl(LinkInfoImpl.CONTEXT_MEMBER,
242 throwsTag.exceptionType())));
243 TagletOutput text = new TagletOutputImpl(
244 htmlWriter.commentTagsToString(throwsTag, null,
245 throwsTag.inlineTags(), false));
246 if (text != null && text.toString().length() > 0) {
247 result += " - " + text;
248 }
249 result += "</dd>";
250 return new TagletOutputImpl(result);
251 }
253 /**
254 * {@inheritDoc}
255 */
256 public TagletOutput throwsTagOutput(Type throwsType) {
257 return new TagletOutputImpl(DocletConstants.NL + "<dd>" +
258 htmlWriter.codeText(htmlWriter.getLink(
259 new LinkInfoImpl(LinkInfoImpl.CONTEXT_MEMBER, throwsType))) + "</dd>");
260 }
262 /**
263 * {@inheritDoc}
264 */
265 public TagletOutput valueTagOutput(FieldDoc field, String constantVal,
266 boolean includeLink) {
267 return new TagletOutputImpl(includeLink ?
268 htmlWriter.getDocLink(LinkInfoImpl.CONTEXT_VALUE_TAG, field,
269 constantVal, false) : constantVal);
270 }
272 /**
273 * {@inheritDoc}
274 */
275 public TagletOutput commentTagsToOutput(Tag holderTag, Tag[] tags) {
276 return commentTagsToOutput(holderTag, null, tags, false);
277 }
279 /**
280 * {@inheritDoc}
281 */
282 public TagletOutput commentTagsToOutput(Doc holderDoc, Tag[] tags) {
283 return commentTagsToOutput(null, holderDoc, tags, false);
284 }
286 /**
287 * {@inheritDoc}
288 */
289 public TagletOutput commentTagsToOutput(Tag holderTag,
290 Doc holderDoc, Tag[] tags, boolean isFirstSentence) {
291 return new TagletOutputImpl(htmlWriter.commentTagsToString(
292 holderTag, holderDoc, tags, isFirstSentence));
293 }
295 /**
296 * {@inheritDoc}
297 */
298 public Configuration configuration() {
299 return htmlWriter.configuration();
300 }
302 /**
303 * Return an instance of a TagletWriter that knows how to write HTML.
304 *
305 * @return an instance of a TagletWriter that knows how to write HTML.
306 */
307 public TagletOutput getTagletOutputInstance() {
308 return new TagletOutputImpl("");
309 }
310 }