author | jjg |
Thu, 16 Nov 2017 15:17:47 -0800 | |
changeset 47851 | 6a9bb4f77d50 |
parent 47850 | 4a28dc8a86c2 |
child 48295 | c79d31ba84b9 |
permissions | -rw-r--r-- |
47850 | 1 |
/* |
2 |
* Copyright (c) 1997, 2017, 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 |
*/ |
|
25 |
||
26 |
package jdk.javadoc.internal.doclets.formats.html.markup; |
|
27 |
||
28 |
import jdk.javadoc.internal.doclets.formats.html.SectionName; |
|
29 |
import jdk.javadoc.internal.doclets.toolkit.Content; |
|
30 |
import jdk.javadoc.internal.doclets.toolkit.util.DocLink; |
|
31 |
import jdk.javadoc.internal.doclets.toolkit.util.DocPath; |
|
32 |
||
33 |
/** |
|
34 |
* Factory for HTML A elements, both links (with a {@code href} attribute) |
|
35 |
* and anchors (with an {@code id} or {@code name} attribute). |
|
36 |
* |
|
37 |
* Most methods in this class are static factory methods. |
|
38 |
* The exceptions are those methods that directly or indirectly depend on the HTML version |
|
39 |
* being used, when determining valid HTML names (ids), |
|
40 |
* and those methods that generate anchors. |
|
41 |
* |
|
42 |
* <p><b>This is NOT part of any supported API. |
|
43 |
* If you write code that depends on this, you do so at your own risk. |
|
44 |
* This code and its internal interfaces are subject to change or |
|
45 |
* deletion without notice.</b> |
|
46 |
*/ |
|
47 |
public class Links { |
|
48 |
||
49 |
private final HtmlVersion version; |
|
50 |
||
51 |
/** |
|
52 |
* Creates a {@code Links} object for a specific HTML version. |
|
53 |
* The version is used by the {@link #getName(String) getName} method |
|
54 |
* to help determine valid HTML names (ids), and to determine whether |
|
55 |
* to use an {@code id} or {@code name} attribute when creating anchors. |
|
56 |
* |
|
57 |
* @param version the HTML version |
|
58 |
*/ |
|
59 |
public Links(HtmlVersion version) { |
|
60 |
this.version = version; |
|
61 |
} |
|
62 |
||
63 |
/** |
|
64 |
* Creates an anchor of the form {@code <a id="name"><!-- --></a>}. |
|
65 |
* In HTML4, a {@code name} attribute will be generated instead of an {@code id} attribute. |
|
66 |
* |
|
67 |
* @param name the value for the {@code id} or {@code name} attribute |
|
68 |
* @return a content tree for the anchor |
|
69 |
*/ |
|
70 |
public Content createAnchor(String name) { |
|
71 |
return createAnchor(getName(name), null); |
|
72 |
} |
|
73 |
||
74 |
/** |
|
75 |
* Creates an anchor of the form {@code <a id="sectionName"><!-- --></a>}. |
|
76 |
* In HTML4, a {@code name} attribute will be generated instead of an {@code id} attribute. |
|
77 |
* |
|
78 |
* @param sectionName the value for the {@code id} or {@code name} attribute |
|
79 |
* @return a content tree for the anchor |
|
80 |
*/ |
|
81 |
public Content createAnchor(SectionName sectionName) { |
|
82 |
return createAnchor(sectionName.getName(), null); |
|
83 |
} |
|
84 |
||
85 |
/** |
|
86 |
* Creates an anchor of the form {@code <a id="sectionNameName"><!-- --></a>}. |
|
87 |
* In HTML4, a {@code name} attribute will be generated instead of an {@code id} attribute. |
|
88 |
* |
|
89 |
* @param sectionName the first part of the value for the {@code id} or {@code name} attribute |
|
90 |
* @param name the second part of the value for the {@code id} or {@code name} attribute |
|
91 |
* @return a content tree for the anchor |
|
92 |
*/ |
|
93 |
public Content createAnchor(SectionName sectionName, String name) { |
|
94 |
return createAnchor(sectionName.getName() + getName(name), null); |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* Creates an anchor of the form {@code <a id="anchorName">content</a>}. |
|
99 |
* In HTML4, a {@code name} attribute will be generated instead of an {@code id} attribute. |
|
100 |
* |
|
101 |
* @param name the value for the {@code id} or {@code name} attribute |
|
102 |
* @param content the content that should be added to the anchor, |
|
103 |
* or null, to use an empty comment |
|
104 |
* @return a content tree for the marker anchor |
|
105 |
*/ |
|
106 |
public Content createAnchor(String name, Content content) { |
|
107 |
return HtmlTree.A(version, name, (content == null ? EMPTY_COMMENT : content)); |
|
108 |
} |
|
109 |
||
110 |
private static final Content EMPTY_COMMENT = new Comment(" "); |
|
111 |
||
112 |
/** |
|
113 |
* Creates a link of the form {@code <a href="#where">label</a>}. |
|
114 |
* |
|
115 |
* @param where the position of the link in the file |
|
116 |
* @param label the content for the link |
|
117 |
* @return a content tree for the link |
|
118 |
*/ |
|
119 |
public Content createLink(String where, Content label) { |
|
120 |
DocLink l = DocLink.fragment(getName(where)); |
|
121 |
return Links.createLink(l, label, "", ""); |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Creates a link of the form {@code <a href="#sectionName">label</a>}. |
|
126 |
* |
|
127 |
* @param sectionName the section name to which the link will be created |
|
128 |
* @param label the content for the link |
|
129 |
* @return a content tree for the link |
|
130 |
*/ |
|
131 |
public static Content createLink(SectionName sectionName, Content label) { |
|
132 |
DocLink l = DocLink.fragment(sectionName.getName()); |
|
133 |
return Links.createLink(l, label, "", ""); |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* Creates a link of the form {@code <a href="#sectionNameWhere">label</a>}. |
|
138 |
* |
|
139 |
* @param sectionName the section name combined with where to which the link |
|
140 |
* will be created |
|
141 |
* @param where the fragment combined with sectionName to which the link |
|
142 |
* will be created |
|
143 |
* @param label the content for the link |
|
144 |
* @return a content tree for the link |
|
145 |
*/ |
|
146 |
public Content createLink(SectionName sectionName, String where, Content label) { |
|
147 |
DocLink l = DocLink.fragment(sectionName.getName() + getName(where)); |
|
148 |
return Links.createLink(l, label, "", ""); |
|
149 |
} |
|
150 |
||
151 |
/** |
|
152 |
* Creates a link of the form {@code <a href="#stylename" title="title" target="target">label</a>}. |
|
153 |
* |
|
154 |
* @param sectionName the section name to which the link will be created |
|
155 |
* @param label the content for the link |
|
156 |
* @param title the title for the link |
|
157 |
* @param target the target for the link, or null |
|
158 |
* @return a content tree for the link |
|
159 |
*/ |
|
160 |
public static Content createLink(SectionName sectionName, Content label, String title, String target) { |
|
161 |
DocLink l = DocLink.fragment(sectionName.getName()); |
|
162 |
return createLink(l, label, title, target); |
|
163 |
} |
|
164 |
||
165 |
/** |
|
166 |
* Creates a link of the form {@code <a href="path">label</a>}. |
|
167 |
* |
|
168 |
* @param path the path for the link |
|
169 |
* @param label the content for the link |
|
170 |
* @return a content tree for the link |
|
171 |
*/ |
|
172 |
public static Content createLink(DocPath path, String label) { |
|
47851
6a9bb4f77d50
8190822: Remove dead code that could lead to invalid HTML
jjg
parents:
47850
diff
changeset
|
173 |
return Links.createLink(path, new StringContent(label), false, "", ""); |
47850 | 174 |
} |
175 |
||
176 |
/** |
|
177 |
* Creates a link of the form {@code <a href="path">label</a>}. |
|
178 |
* |
|
179 |
* @param path the path for the link |
|
180 |
* @param label the content for the link |
|
181 |
* @return a content tree for the link |
|
182 |
*/ |
|
183 |
public static Content createLink(DocPath path, Content label) { |
|
184 |
return Links.createLink(path, label, "", ""); |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* Creates a link of the form {@code <a href="path" title="title" target="target">label</a>}. |
|
189 |
* If {@code strong} is set, the label will be wrapped in |
|
190 |
* {@code <span style="typeNameLink">...</span>}. |
|
191 |
* |
|
192 |
* @param path the path for the link |
|
193 |
* @param label the content for the link |
|
194 |
* @param strong whether to wrap the {@code label} in a SPAN element |
|
195 |
* @param title the title for the link |
|
196 |
* @param target the target for the link, or null |
|
197 |
* @return a content tree for the link |
|
198 |
*/ |
|
47851
6a9bb4f77d50
8190822: Remove dead code that could lead to invalid HTML
jjg
parents:
47850
diff
changeset
|
199 |
public static Content createLink(DocPath path, Content label, boolean strong, |
47850 | 200 |
String title, String target) { |
47851
6a9bb4f77d50
8190822: Remove dead code that could lead to invalid HTML
jjg
parents:
47850
diff
changeset
|
201 |
return createLink(new DocLink(path), label, strong, title, target); |
47850 | 202 |
} |
203 |
||
204 |
/** |
|
205 |
* Creates a link of the form {@code <a href="path" title="title" target="target">label</a>}. |
|
206 |
* |
|
207 |
* @param path the path for the link |
|
208 |
* @param label the content for the link |
|
209 |
* @param title the title for the link |
|
210 |
* @param target the target for the link, or null |
|
211 |
* @return a content tree for the link |
|
212 |
*/ |
|
213 |
public static Content createLink(DocPath path, Content label, String title, String target) { |
|
214 |
return Links.createLink(new DocLink(path), label, title, target); |
|
215 |
} |
|
216 |
||
217 |
/** |
|
218 |
* Creates a link of the form {@code <a href="link">label</a>}. |
|
219 |
* |
|
220 |
* @param link the details for the link |
|
221 |
* @param label the content for the link |
|
222 |
* @return a content tree for the link |
|
223 |
*/ |
|
224 |
public static Content createLink(DocLink link, Content label) { |
|
225 |
return Links.createLink(link, label, "", ""); |
|
226 |
} |
|
227 |
||
228 |
/** |
|
229 |
* Creates a link of the form {@code <a href="path" title="title" target="target">label</a>}. |
|
230 |
* |
|
231 |
* @param link the details for the link |
|
232 |
* @param label the content for the link |
|
233 |
* @param title the title for the link |
|
234 |
* @param target the target for the link, or null |
|
235 |
* @return a content tree for the link |
|
236 |
*/ |
|
237 |
public static Content createLink(DocLink link, Content label, String title, String target) { |
|
238 |
HtmlTree anchor = HtmlTree.A(link.toString(), label); |
|
239 |
if (title != null && title.length() != 0) { |
|
240 |
anchor.addAttr(HtmlAttr.TITLE, title); |
|
241 |
} |
|
242 |
if (target != null && target.length() != 0) { |
|
243 |
anchor.addAttr(HtmlAttr.TARGET, target); |
|
244 |
} |
|
245 |
return anchor; |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* Creates a link of the form {@code <a href="link" title="title" target="target">label</a>}. |
|
250 |
* If {@code strong} is set, the label will be wrapped in |
|
251 |
* {@code <span style="typeNameLink">...</span>}. |
|
252 |
* |
|
253 |
* @param link the details for the link |
|
254 |
* @param label the content for the link |
|
255 |
* @param strong whether to wrap the {@code label} in a SPAN element |
|
256 |
* @param title the title for the link |
|
257 |
* @param target the target for the link, or null |
|
258 |
* @return a content tree for the link |
|
259 |
*/ |
|
47851
6a9bb4f77d50
8190822: Remove dead code that could lead to invalid HTML
jjg
parents:
47850
diff
changeset
|
260 |
public static Content createLink(DocLink link, Content label, boolean strong, |
47850 | 261 |
String title, String target) { |
262 |
Content body = label; |
|
263 |
if (strong) { |
|
264 |
body = HtmlTree.SPAN(HtmlStyle.typeNameLink, body); |
|
265 |
} |
|
266 |
HtmlTree l = HtmlTree.A(link.toString(), body); |
|
267 |
if (title != null && title.length() != 0) { |
|
268 |
l.addAttr(HtmlAttr.TITLE, title); |
|
269 |
} |
|
270 |
if (target != null && target.length() != 0) { |
|
271 |
l.addAttr(HtmlAttr.TARGET, target); |
|
272 |
} |
|
273 |
return l; |
|
274 |
} |
|
275 |
||
276 |
||
277 |
/** |
|
278 |
* Converts a name to a valid HTML name (id). |
|
279 |
* This depends on the HTML version specified when the {@code Links} object was created. |
|
280 |
* |
|
281 |
* @param name the string that needs to be converted to a valid HTML name |
|
282 |
* @return a valid HTML name |
|
283 |
*/ |
|
284 |
public String getName(String name) { |
|
285 |
/* The HTML 4 spec at http://www.w3.org/TR/html4/types.html#h-6.2 mentions |
|
286 |
* that the name/id should begin with a letter followed by other valid characters. |
|
287 |
* The HTML 5 spec (draft) is more permissive on names/ids where the only restriction |
|
288 |
* is that it should be at least one character long and should not contain spaces. |
|
289 |
* The spec draft is @ http://www.w3.org/html/wg/drafts/html/master/dom.html#the-id-attribute. |
|
290 |
* |
|
291 |
* For HTML 4, we need to check for non-characters at the beginning of the name and |
|
292 |
* substitute it accordingly, "_" and "$" can appear at the beginning of a member name. |
|
293 |
* The method substitutes "$" with "Z:Z:D" and will prefix "_" with "Z:Z". |
|
294 |
*/ |
|
295 |
||
296 |
if (version == HtmlVersion.HTML5) { |
|
297 |
return name.replaceAll(" +", ""); |
|
298 |
} |
|
299 |
||
300 |
StringBuilder sb = new StringBuilder(); |
|
301 |
for (int i = 0; i < name.length(); i++) { |
|
302 |
char ch = name.charAt(i); |
|
303 |
switch (ch) { |
|
304 |
case '(': |
|
305 |
case ')': |
|
306 |
case '<': |
|
307 |
case '>': |
|
308 |
case ',': |
|
309 |
sb.append('-'); |
|
310 |
break; |
|
311 |
case ' ': |
|
312 |
case '[': |
|
313 |
break; |
|
314 |
case ']': |
|
315 |
sb.append(":A"); |
|
316 |
break; |
|
317 |
// Any appearance of $ needs to be substituted with ":D" and not with hyphen |
|
318 |
// since a field name "P$$ and a method P(), both valid member names, can end |
|
319 |
// up as "P--". A member name beginning with $ needs to be substituted with |
|
320 |
// "Z:Z:D". |
|
321 |
case '$': |
|
322 |
if (i == 0) |
|
323 |
sb.append("Z:Z"); |
|
324 |
sb.append(":D"); |
|
325 |
break; |
|
326 |
// A member name beginning with _ needs to be prefixed with "Z:Z" since valid anchor |
|
327 |
// names can only begin with a letter. |
|
328 |
case '_': |
|
329 |
if (i == 0) |
|
330 |
sb.append("Z:Z"); |
|
331 |
sb.append(ch); |
|
332 |
break; |
|
333 |
default: |
|
334 |
sb.append(ch); |
|
335 |
} |
|
336 |
} |
|
337 |
return sb.toString(); |
|
338 |
} |
|
339 |
||
340 |
} |