Tue, 14 May 2013 10:14:53 -0700
8011668: Allow HTMLWriter.getResource to take Content args
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 classLinkInfo.displayLength += label.charCount();
81 Configuration configuration = m_writer.configuration;
82 Content link = new ContentBuilder();
83 if (classDoc.isIncluded()) {
84 if (configuration.isGeneratedDoc(classDoc)) {
85 DocPath filename = getPath(classLinkInfo);
86 if (linkInfo.linkToSelf ||
87 !(DocPath.forName(classDoc)).equals(m_writer.filename)) {
88 link.addContent(m_writer.getHyperLink(
89 filename.fragment(classLinkInfo.where),
90 label,
91 classLinkInfo.isStrong, classLinkInfo.styleName,
92 title, classLinkInfo.target));
93 if (noLabel && !classLinkInfo.excludeTypeParameterLinks) {
94 link.addContent(getTypeParameterLinks(linkInfo));
95 }
96 return link;
97 }
98 }
99 } else {
100 Content crossLink = m_writer.getCrossClassLink(
101 classDoc.qualifiedName(), classLinkInfo.where,
102 label, classLinkInfo.isStrong, classLinkInfo.styleName,
103 true);
104 if (crossLink != null) {
105 link.addContent(crossLink);
106 if (noLabel && !classLinkInfo.excludeTypeParameterLinks) {
107 link.addContent(getTypeParameterLinks(linkInfo));
108 }
109 return link;
110 }
111 }
112 // Can't link so just write label.
113 link.addContent(label.toString());
114 if (noLabel && !classLinkInfo.excludeTypeParameterLinks) {
115 link.addContent(getTypeParameterLinks(linkInfo));
116 }
117 return link;
118 }
120 /**
121 * {@inheritDoc}
122 */
123 protected Content getTypeParameterLink(LinkInfo linkInfo,
124 Type typeParam) {
125 LinkInfoImpl typeLinkInfo = new LinkInfoImpl(m_writer.configuration,
126 ((LinkInfoImpl) linkInfo).getContext(), typeParam);
127 typeLinkInfo.excludeTypeBounds = linkInfo.excludeTypeBounds;
128 typeLinkInfo.excludeTypeParameterLinks = linkInfo.excludeTypeParameterLinks;
129 typeLinkInfo.linkToSelf = linkInfo.linkToSelf;
130 typeLinkInfo.isJava5DeclarationLocation = false;
131 Content output = getLink(typeLinkInfo);
132 ((LinkInfoImpl) linkInfo).displayLength += typeLinkInfo.displayLength;
133 return output;
134 }
136 protected Content getTypeAnnotationLink(LinkInfo linkInfo,
137 AnnotationDesc annotation) {
138 throw new RuntimeException("Not implemented yet!");
139 }
141 public Content getTypeAnnotationLinks(LinkInfo linkInfo) {
142 ContentBuilder links = new ContentBuilder();
143 AnnotationDesc[] annotations;
144 if (linkInfo.type instanceof AnnotatedType) {
145 annotations = linkInfo.type.asAnnotatedType().annotations();
146 } else if (linkInfo.type instanceof TypeVariable) {
147 annotations = linkInfo.type.asTypeVariable().annotations();
148 } else {
149 return links;
150 }
152 if (annotations.length == 0)
153 return links;
155 List<String> annos = m_writer.getAnnotations(0, annotations, false, linkInfo.isJava5DeclarationLocation);
157 boolean isFirst = true;
158 for (String anno : annos) {
159 if (!isFirst) {
160 linkInfo.displayLength += 1;
161 links.addContent(" ");
162 }
163 links.addContent(new RawHtml(anno));
164 isFirst = false;
165 }
166 if (!annos.isEmpty()) {
167 linkInfo.displayLength += 1;
168 links.addContent(" ");
169 }
171 return links;
172 }
174 /**
175 * Given a class, return the appropriate tool tip.
176 *
177 * @param classDoc the class to get the tool tip for.
178 * @return the tool tip for the appropriate class.
179 */
180 private String getClassToolTip(ClassDoc classDoc, boolean isTypeLink) {
181 Configuration configuration = m_writer.configuration;
182 if (isTypeLink) {
183 return configuration.getText("doclet.Href_Type_Param_Title",
184 classDoc.name());
185 } else if (classDoc.isInterface()){
186 return configuration.getText("doclet.Href_Interface_Title",
187 Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage())));
188 } else if (classDoc.isAnnotationType()) {
189 return configuration.getText("doclet.Href_Annotation_Title",
190 Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage())));
191 } else if (classDoc.isEnum()) {
192 return configuration.getText("doclet.Href_Enum_Title",
193 Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage())));
194 } else {
195 return configuration.getText("doclet.Href_Class_Title",
196 Util.escapeHtmlChars(Util.getPackageName(classDoc.containingPackage())));
197 }
198 }
200 /**
201 * Return path to the given file name in the given package. So if the name
202 * passed is "Object.html" and the name of the package is "java.lang", and
203 * if the relative path is "../.." then returned string will be
204 * "../../java/lang/Object.html"
205 *
206 * @param linkInfo the information about the link.
207 */
208 private DocPath getPath(LinkInfoImpl linkInfo) {
209 if (linkInfo.context == LinkInfoImpl.Kind.PACKAGE_FRAME) {
210 //Not really necessary to do this but we want to be consistent
211 //with 1.4.2 output.
212 return DocPath.forName(linkInfo.classDoc);
213 }
214 return m_writer.pathToRoot.resolve(DocPath.forClass(linkInfo.classDoc));
215 }
216 }