|
1 /* |
|
2 * Copyright 2003-2004 Sun Microsystems, Inc. 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. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.doclets.internal.toolkit.util.links; |
|
27 |
|
28 import com.sun.javadoc.*; |
|
29 |
|
30 /** |
|
31 * A factory that constructs links from given link information. |
|
32 * |
|
33 * @author Jamie Ho |
|
34 * @since 1.5 |
|
35 */ |
|
36 public abstract class LinkFactory { |
|
37 |
|
38 /** |
|
39 * Return an empty instance of the link output object. |
|
40 * |
|
41 * @return an empty instance of the link output object. |
|
42 */ |
|
43 protected abstract LinkOutput getOutputInstance(); |
|
44 |
|
45 /** |
|
46 * Constructs a link from the given link information. |
|
47 * |
|
48 * @param linkInfo the information about the link. |
|
49 * @return the output of the link. |
|
50 */ |
|
51 public LinkOutput getLinkOutput(LinkInfo linkInfo) { |
|
52 if (linkInfo.type != null) { |
|
53 Type type = linkInfo.type; |
|
54 LinkOutput linkOutput = getOutputInstance(); |
|
55 if (type.isPrimitive()) { |
|
56 //Just a primitive. |
|
57 linkInfo.displayLength += type.typeName().length(); |
|
58 linkOutput.append(type.typeName()); |
|
59 } else if (type.asWildcardType() != null) { |
|
60 //Wildcard type. |
|
61 linkInfo.isTypeBound = true; |
|
62 linkInfo.displayLength += 1; |
|
63 linkOutput.append("?"); |
|
64 WildcardType wildcardType = type.asWildcardType(); |
|
65 Type[] extendsBounds = wildcardType.extendsBounds(); |
|
66 for (int i = 0; i < extendsBounds.length; i++) { |
|
67 linkInfo.displayLength += i > 0 ? 2 : 9; |
|
68 linkOutput.append(i > 0 ? ", " : " extends "); |
|
69 setBoundsLinkInfo(linkInfo, extendsBounds[i]); |
|
70 linkOutput.append(getLinkOutput(linkInfo)); |
|
71 } |
|
72 Type[] superBounds = wildcardType.superBounds(); |
|
73 for (int i = 0; i < superBounds.length; i++) { |
|
74 linkInfo.displayLength += i > 0 ? 2 : 7; |
|
75 linkOutput.append(i > 0 ? ", " : " super "); |
|
76 setBoundsLinkInfo(linkInfo, superBounds[i]); |
|
77 linkOutput.append(getLinkOutput(linkInfo)); |
|
78 } |
|
79 } else if (type.asTypeVariable()!= null) { |
|
80 linkInfo.isTypeBound = true; |
|
81 //A type variable. |
|
82 Doc owner = type.asTypeVariable().owner(); |
|
83 if ((! linkInfo.excludeTypeParameterLinks) && |
|
84 owner instanceof ClassDoc) { |
|
85 linkInfo.classDoc = (ClassDoc) owner; |
|
86 linkInfo.label = type.typeName(); |
|
87 linkOutput.append(getClassLink((LinkInfo) linkInfo)); |
|
88 } else { |
|
89 //No need to link method type parameters. |
|
90 linkInfo.displayLength += type.typeName().length(); |
|
91 linkOutput.append(type.typeName()); |
|
92 } |
|
93 |
|
94 Type[] bounds = type.asTypeVariable().bounds(); |
|
95 if (! linkInfo.excludeTypeBounds) { |
|
96 linkInfo.excludeTypeBounds = true; |
|
97 for (int i = 0; i < bounds.length; i++) { |
|
98 linkInfo.displayLength += i > 0 ? 2 : 9; |
|
99 linkOutput.append(i > 0 ? " & " : " extends "); |
|
100 setBoundsLinkInfo(linkInfo, bounds[i]); |
|
101 linkOutput.append(getLinkOutput(linkInfo)); |
|
102 } |
|
103 } |
|
104 } else if (type.asClassDoc() != null) { |
|
105 //A class type. |
|
106 if (linkInfo.isTypeBound && |
|
107 linkInfo.excludeTypeBoundsLinks) { |
|
108 //Since we are excluding type parameter links, we should not |
|
109 //be linking to the type bound. |
|
110 linkInfo.displayLength += type.typeName().length(); |
|
111 linkOutput.append(type.typeName()); |
|
112 linkOutput.append(getTypeParameterLinks(linkInfo)); |
|
113 return linkOutput; |
|
114 } else { |
|
115 linkInfo.classDoc = type.asClassDoc(); |
|
116 linkOutput = getClassLink((LinkInfo) linkInfo); |
|
117 if (linkInfo.includeTypeAsSepLink) { |
|
118 linkOutput.append(getTypeParameterLinks(linkInfo, false)); |
|
119 } |
|
120 } |
|
121 } |
|
122 |
|
123 if (linkInfo.isVarArg) { |
|
124 if (type.dimension().length() > 2) { |
|
125 //Javadoc returns var args as array. |
|
126 //Strip out the first [] from the var arg. |
|
127 linkInfo.displayLength += type.dimension().length()-2; |
|
128 linkOutput.append(type.dimension().substring(2)); |
|
129 } |
|
130 linkInfo.displayLength += 3; |
|
131 linkOutput.append("..."); |
|
132 } else { |
|
133 linkInfo.displayLength += type.dimension().length(); |
|
134 linkOutput.append(type.dimension()); |
|
135 } |
|
136 return linkOutput; |
|
137 } else if (linkInfo.classDoc != null) { |
|
138 //Just a class link |
|
139 LinkOutput linkOutput = getClassLink((LinkInfo) linkInfo); |
|
140 if (linkInfo.includeTypeAsSepLink) { |
|
141 linkOutput.append(getTypeParameterLinks(linkInfo, false)); |
|
142 } |
|
143 return linkOutput; |
|
144 } else { |
|
145 return null; |
|
146 } |
|
147 } |
|
148 |
|
149 private void setBoundsLinkInfo(LinkInfo linkInfo, Type bound) { |
|
150 linkInfo.classDoc = null; |
|
151 linkInfo.label = null; |
|
152 linkInfo.type = bound; |
|
153 } |
|
154 |
|
155 /** |
|
156 * Return the link to the given class. |
|
157 * |
|
158 * @param linkInfo the information about the link to construct. |
|
159 * |
|
160 * @return the link for the given class. |
|
161 */ |
|
162 protected abstract LinkOutput getClassLink(LinkInfo linkInfo); |
|
163 |
|
164 /** |
|
165 * Return the link to the given type parameter. |
|
166 * |
|
167 * @param linkInfo the information about the link to construct. |
|
168 * @param typeParam the type parameter to link to. |
|
169 */ |
|
170 protected abstract LinkOutput getTypeParameterLink(LinkInfo linkInfo, |
|
171 Type typeParam); |
|
172 |
|
173 /** |
|
174 * Return the links to the type parameters. |
|
175 * |
|
176 * @param linkInfo the information about the link to construct. |
|
177 * @return the links to the type parameters. |
|
178 */ |
|
179 public LinkOutput getTypeParameterLinks(LinkInfo linkInfo) { |
|
180 return getTypeParameterLinks(linkInfo, true); |
|
181 } |
|
182 |
|
183 /** |
|
184 * Return the links to the type parameters. |
|
185 * |
|
186 * @param linkInfo the information about the link to construct. |
|
187 * @param isClassLabel true if this is a class label. False if it is |
|
188 * the type parameters portion of the link. |
|
189 * @return the links to the type parameters. |
|
190 */ |
|
191 public LinkOutput getTypeParameterLinks(LinkInfo linkInfo, boolean isClassLabel) { |
|
192 LinkOutput output = getOutputInstance(); |
|
193 Type[] vars; |
|
194 if (linkInfo.executableMemberDoc != null) { |
|
195 vars = linkInfo.executableMemberDoc.typeParameters(); |
|
196 } else if (linkInfo.type != null && |
|
197 linkInfo.type.asParameterizedType() != null){ |
|
198 vars = linkInfo.type.asParameterizedType().typeArguments(); |
|
199 } else if (linkInfo.classDoc != null){ |
|
200 vars = linkInfo.classDoc.typeParameters(); |
|
201 } else { |
|
202 //Nothing to document. |
|
203 return output; |
|
204 } |
|
205 if (((linkInfo.includeTypeInClassLinkLabel && isClassLabel) || |
|
206 (linkInfo.includeTypeAsSepLink && ! isClassLabel) |
|
207 ) |
|
208 && vars.length > 0) { |
|
209 linkInfo.displayLength += 1; |
|
210 output.append(getLessThanString()); |
|
211 for (int i = 0; i < vars.length; i++) { |
|
212 if (i > 0) { |
|
213 linkInfo.displayLength += 1; |
|
214 output.append(","); |
|
215 } |
|
216 output.append(getTypeParameterLink(linkInfo, vars[i])); |
|
217 } |
|
218 linkInfo.displayLength += 1; |
|
219 output.append(getGreaterThanString()); |
|
220 } |
|
221 return output; |
|
222 } |
|
223 |
|
224 /** |
|
225 * Return &lt;, which is used in type parameters. Override this |
|
226 * if your doclet uses something different. |
|
227 * |
|
228 * @return return &lt;, which is used in type parameters. |
|
229 */ |
|
230 protected String getLessThanString() { |
|
231 return "<"; |
|
232 } |
|
233 |
|
234 /** |
|
235 * Return &gt;, which is used in type parameters. Override this |
|
236 * if your doclet uses something different. |
|
237 * |
|
238 * @return return &gt;, which is used in type parameters. |
|
239 */ |
|
240 protected String getGreaterThanString() { |
|
241 return ">"; |
|
242 } |
|
243 } |