Tue, 14 May 2013 10:14:54 -0700
8012176: reduce use of TagletOutputImpl.toString
Reviewed-by: darcy
1 /*
2 * Copyright (c) 2003, 2013, 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 java.util.List;
30 import com.sun.javadoc.*;
31 import com.sun.tools.doclets.formats.html.markup.ContentBuilder;
32 import com.sun.tools.doclets.formats.html.markup.RawHtml;
33 import com.sun.tools.doclets.formats.html.markup.StringContent;
34 import com.sun.tools.doclets.internal.toolkit.*;
35 import com.sun.tools.doclets.internal.toolkit.util.*;
36 import com.sun.tools.doclets.internal.toolkit.util.links.*;
38 /**
39 * A factory that returns a link given the information about it.
40 *
41 * <p><b>This is NOT part of any supported API.
42 * If you write code that depends on this, you do so at your own risk.
43 * This code and its internal interfaces are subject to change or
44 * deletion without notice.</b>
45 *
46 * @author Jamie Ho
47 * @since 1.5
48 */
49 public class LinkFactoryImpl extends LinkFactory {
51 private HtmlDocletWriter m_writer;
53 public LinkFactoryImpl(HtmlDocletWriter writer) {
54 m_writer = writer;
55 }
57 /**
58 * {@inheritDoc}
59 */
60 protected Content newContent() {
61 return new ContentBuilder();
62 }
64 /**
65 * {@inheritDoc}
66 */
67 protected Content getClassLink(LinkInfo linkInfo) {
68 LinkInfoImpl classLinkInfo = (LinkInfoImpl) linkInfo;
69 boolean noLabel = linkInfo.label == null || linkInfo.label.isEmpty();
70 ClassDoc classDoc = classLinkInfo.classDoc;
71 //Create a tool tip if we are linking to a class or interface. Don't
72 //create one if we are linking to a member.
73 String title =
74 (classLinkInfo.where == null || classLinkInfo.where.length() == 0) ?
75 getClassToolTip(classDoc,
76 classLinkInfo.type != null &&
77 !classDoc.qualifiedTypeName().equals(classLinkInfo.type.qualifiedTypeName())) :
78 "";
79 Content label = classLinkInfo.getClassLinkLabel(m_writer.configuration);
80 Configuration configuration = m_writer.configuration;
81 Content link = new ContentBuilder();
82 if (classDoc.isIncluded()) {
83 if (configuration.isGeneratedDoc(classDoc)) {
84 DocPath filename = getPath(classLinkInfo);
85 if (linkInfo.linkToSelf ||
86 !(DocPath.forName(classDoc)).equals(m_writer.filename)) {
87 link.addContent(m_writer.getHyperLink(
88 filename.fragment(classLinkInfo.where),
89 label,
90 classLinkInfo.isStrong, classLinkInfo.styleName,
91 title, classLinkInfo.target));
92 if (noLabel && !classLinkInfo.excludeTypeParameterLinks) {
93 link.addContent(getTypeParameterLinks(linkInfo));
94 }
95 return link;
96 }
97 }
98 } else {
99 Content crossLink = m_writer.getCrossClassLink(
100 classDoc.qualifiedName(), classLinkInfo.where,
101 label, classLinkInfo.isStrong, classLinkInfo.styleName,
102 true);
103 if (crossLink != null) {
104 link.addContent(crossLink);
105 if (noLabel && !classLinkInfo.excludeTypeParameterLinks) {
106 link.addContent(getTypeParameterLinks(linkInfo));
107 }
108 return link;
109 }
110 }
111 // Can't link so just write label.
112 link.addContent(label.toString());
113 if (noLabel && !classLinkInfo.excludeTypeParameterLinks) {
114 link.addContent(getTypeParameterLinks(linkInfo));
115 }
116 return link;
117 }
119 /**
120 * {@inheritDoc}
121 */
122 protected Content getTypeParameterLink(LinkInfo linkInfo,
123 Type typeParam) {
124 LinkInfoImpl typeLinkInfo = new LinkInfoImpl(m_writer.configuration,
125 ((LinkInfoImpl) linkInfo).getContext(), typeParam);
126 typeLinkInfo.excludeTypeBounds = linkInfo.excludeTypeBounds;
127 typeLinkInfo.excludeTypeParameterLinks = linkInfo.excludeTypeParameterLinks;
128 typeLinkInfo.linkToSelf = linkInfo.linkToSelf;
129 typeLinkInfo.isJava5DeclarationLocation = false;
130 return getLink(typeLinkInfo);
131 }
133 protected Content getTypeAnnotationLink(LinkInfo linkInfo,
134 AnnotationDesc annotation) {
135 throw new RuntimeException("Not implemented yet!");
136 }
138 public Content getTypeAnnotationLinks(LinkInfo linkInfo) {
139 ContentBuilder links = new ContentBuilder();
140 AnnotationDesc[] annotations;
141 if (linkInfo.type instanceof AnnotatedType) {
142 annotations = linkInfo.type.asAnnotatedType().annotations();
143 } else if (linkInfo.type instanceof TypeVariable) {
144 annotations = linkInfo.type.asTypeVariable().annotations();
145 } else {
146 return links;
147 }
149 if (annotations.length == 0)
150 return links;
152 List<String> annos = m_writer.getAnnotations(0, annotations, false, linkInfo.isJava5DeclarationLocation);
154 boolean isFirst = true;
155 for (String anno : annos) {
156 if (!isFirst) {
157 links.addContent(" ");
158 }
159 links.addContent(new RawHtml(anno));
160 isFirst = false;
161 }
162 if (!annos.isEmpty()) {
163 links.addContent(" ");
164 }
166 return links;
167 }
169 /**
170 * Given a class, return the appropriate tool tip.
171 *
172 * @param classDoc the class to get the tool tip for.
173 * @return the tool tip for the appropriate class.
174 */
175 private String getClassToolTip(ClassDoc classDoc, boolean isTypeLink) {
176 Configuration configuration = m_writer.configuration;
177 if (isTypeLink) {
178 return configuration.getText("doclet.Href_Type_Param_Title",
179 classDoc.name());
180 } else if (classDoc.isInterface()){
181 return configuration.getText("doclet.Href_Interface_Title",
182 Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage())));
183 } else if (classDoc.isAnnotationType()) {
184 return configuration.getText("doclet.Href_Annotation_Title",
185 Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage())));
186 } else if (classDoc.isEnum()) {
187 return configuration.getText("doclet.Href_Enum_Title",
188 Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage())));
189 } else {
190 return configuration.getText("doclet.Href_Class_Title",
191 Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage())));
192 }
193 }
195 /**
196 * Return path to the given file name in the given package. So if the name
197 * passed is "Object.html" and the name of the package is "java.lang", and
198 * if the relative path is "../.." then returned string will be
199 * "../../java/lang/Object.html"
200 *
201 * @param linkInfo the information about the link.
202 */
203 private DocPath getPath(LinkInfoImpl linkInfo) {
204 if (linkInfo.context == LinkInfoImpl.Kind.PACKAGE_FRAME) {
205 //Not really necessary to do this but we want to be consistent
206 //with 1.4.2 output.
207 return DocPath.forName(linkInfo.classDoc);
208 }
209 return m_writer.pathToRoot.resolve(DocPath.forClass(linkInfo.classDoc));
210 }
211 }