-
Notifications
You must be signed in to change notification settings - Fork 219
/
Links.java
341 lines (314 loc) · 13.7 KB
/
Links.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.javadoc.internal.doclets.formats.html.markup;
import jdk.javadoc.internal.doclets.formats.html.SectionName;
import jdk.javadoc.internal.doclets.toolkit.Content;
import jdk.javadoc.internal.doclets.toolkit.util.DocLink;
import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
import jdk.javadoc.internal.doclets.toolkit.util.Extern;
/**
* Factory for HTML A elements, both links (with a {@code href} attribute)
* and anchors (with an {@code id} or {@code name} attribute).
*
* Most methods in this class are static factory methods.
* The exceptions are those methods that directly or indirectly depend on the HTML version
* being used, when determining valid HTML names (ids),
* and those methods that generate anchors.
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*/
public class Links {
private final DocPath file;
private final HtmlVersion version;
/**
* Creates a {@code Links} object for a specific file, to be written in a specific HTML version.
* The version is used by the {@link #getName(String) getName} method
* to help determine valid HTML names (ids), and to determine whether
* to use an {@code id} or {@code name} attribute when creating anchors.
*
* @param file the file
* @param version the HTML version
*/
public Links(DocPath file, HtmlVersion version) {
this.file = file;
this.version = version;
}
/**
* Creates an anchor of the form {@code <a id="name"><!-- --></a>}.
* In HTML4, a {@code name} attribute will be generated instead of an {@code id} attribute.
*
* @param name the value for the {@code id} or {@code name} attribute
* @return a content tree for the anchor
*/
public Content createAnchor(String name) {
return createAnchor(getName(name), null);
}
/**
* Creates an anchor of the form {@code <a id="sectionName"><!-- --></a>}.
* In HTML4, a {@code name} attribute will be generated instead of an {@code id} attribute.
*
* @param sectionName the value for the {@code id} or {@code name} attribute
* @return a content tree for the anchor
*/
public Content createAnchor(SectionName sectionName) {
return createAnchor(sectionName.getName(), null);
}
/**
* Creates an anchor of the form {@code <a id="sectionNameName"><!-- --></a>}.
* In HTML4, a {@code name} attribute will be generated instead of an {@code id} attribute.
*
* @param sectionName the first part of the value for the {@code id} or {@code name} attribute
* @param name the second part of the value for the {@code id} or {@code name} attribute
* @return a content tree for the anchor
*/
public Content createAnchor(SectionName sectionName, String name) {
return createAnchor(sectionName.getName() + getName(name), null);
}
/**
* Creates an anchor of the form {@code <a id="anchorName">content</a>}.
* In HTML4, a {@code name} attribute will be generated instead of an {@code id} attribute.
*
* @param name the value for the {@code id} or {@code name} attribute
* @param content the content that should be added to the anchor,
* or null, to use an empty comment
* @return a content tree for the marker anchor
*/
public Content createAnchor(String name, Content content) {
return HtmlTree.A(version, name, (content == null ? EMPTY_COMMENT : content));
}
private static final Content EMPTY_COMMENT = new Comment(" ");
/**
* Creates a link of the form {@code <a href="#where">label</a>}.
*
* @param where the position of the link in the file
* @param label the content for the link
* @return a content tree for the link
*/
public Content createLink(String where, Content label) {
DocLink l = DocLink.fragment(getName(where));
return createLink(l, label, "", "");
}
/**
* Creates a link of the form {@code <a href="#sectionName">label</a>}.
*
* @param sectionName the section name to which the link will be created
* @param label the content for the link
* @return a content tree for the link
*/
public Content createLink(SectionName sectionName, Content label) {
DocLink l = DocLink.fragment(sectionName.getName());
return createLink(l, label, "", "");
}
/**
* Creates a link of the form {@code <a href="#sectionNameWhere">label</a>}.
*
* @param sectionName the section name combined with where to which the link
* will be created
* @param where the fragment combined with sectionName to which the link
* will be created
* @param label the content for the link
* @return a content tree for the link
*/
public Content createLink(SectionName sectionName, String where, Content label) {
DocLink l = DocLink.fragment(sectionName.getName() + getName(where));
return createLink(l, label, "", "");
}
/**
* Creates a link of the form {@code <a href="#stylename" title="title" target="target">label</a>}.
*
* @param sectionName the section name to which the link will be created
* @param label the content for the link
* @param title the title for the link
* @param target the target for the link, or null
* @return a content tree for the link
*/
public Content createLink(SectionName sectionName, Content label, String title, String target) {
DocLink l = DocLink.fragment(sectionName.getName());
return createLink(l, label, title, target);
}
/**
* Creates a link of the form {@code <a href="path">label</a>}.
*
* @param path the path for the link
* @param label the content for the link
* @return a content tree for the link
*/
public Content createLink(DocPath path, String label) {
return createLink(path, new StringContent(label), false, "", "");
}
/**
* Creates a link of the form {@code <a href="path">label</a>}.
*
* @param path the path for the link
* @param label the content for the link
* @return a content tree for the link
*/
public Content createLink(DocPath path, Content label) {
return createLink(path, label, "", "");
}
/**
* Creates a link of the form {@code <a href="path" title="title" target="target">label</a>}.
* If {@code strong} is set, the label will be wrapped in
* {@code <span style="typeNameLink">...</span>}.
*
* @param path the path for the link
* @param label the content for the link
* @param strong whether to wrap the {@code label} in a SPAN element
* @param title the title for the link
* @param target the target for the link, or null
* @return a content tree for the link
*/
public Content createLink(DocPath path, Content label, boolean strong,
String title, String target) {
return createLink(new DocLink(path), label, strong, title, target);
}
/**
* Creates a link of the form {@code <a href="path" title="title" target="target">label</a>}.
*
* @param path the path for the link
* @param label the content for the link
* @param title the title for the link
* @param target the target for the link, or null
* @return a content tree for the link
*/
public Content createLink(DocPath path, Content label, String title, String target) {
return createLink(new DocLink(path), label, title, target);
}
/**
* Creates a link of the form {@code <a href="link">label</a>}.
*
* @param link the details for the link
* @param label the content for the link
* @return a content tree for the link
*/
public Content createLink(DocLink link, Content label) {
return createLink(link, label, "", "");
}
/**
* Creates a link of the form {@code <a href="path" title="title" target="target">label</a>}.
*
* @param link the details for the link
* @param label the content for the link
* @param title the title for the link
* @param target the target for the link, or null
* @return a content tree for the link
*/
public Content createLink(DocLink link, Content label, String title, String target) {
HtmlTree anchor = HtmlTree.A(link.relativizeAgainst(file).toString(), label);
if (title != null && title.length() != 0) {
anchor.addAttr(HtmlAttr.TITLE, title);
}
if (target != null && target.length() != 0) {
anchor.addAttr(HtmlAttr.TARGET, target);
}
return anchor;
}
/**
* Creates a link of the form {@code <a href="link" title="title" target="target">label</a>}.
* If {@code strong} is set, the label will be wrapped in
* {@code <span style="typeNameLink">...</span>}.
*
* @param link the details for the link
* @param label the content for the link
* @param strong whether to wrap the {@code label} in a SPAN element
* @param title the title for the link
* @param target the target for the link, or null
* @return a content tree for the link
*/
public Content createLink(DocLink link, Content label, boolean strong,
String title, String target) {
return createLink(link, label, strong, title, target, false);
}
/**
* Creates a link of the form {@code <a href="link" title="title" target="target">label</a>}.
* If {@code strong} is set, the label will be wrapped in
* {@code <span style="typeNameLink">...</span>}.
*
* @param link the details for the link
* @param label the content for the link
* @param strong whether to wrap the {@code label} in a SPAN element
* @param title the title for the link
* @param target the target for the link, or null
* @param isExternal is the link external to the generated documentation
* @return a content tree for the link
*/
public Content createLink(DocLink link, Content label, boolean strong,
String title, String target, boolean isExternal) {
Content body = label;
if (strong) {
body = HtmlTree.SPAN(HtmlStyle.typeNameLink, body);
}
HtmlTree l = HtmlTree.A(link.relativizeAgainst(file).toString(), body);
if (title != null && title.length() != 0) {
l.addAttr(HtmlAttr.TITLE, title);
}
if (target != null && target.length() != 0) {
l.addAttr(HtmlAttr.TARGET, target);
}
if (isExternal) {
l.setStyle(HtmlStyle.externalLink);
}
return l;
}
/**
* Creates a link.
*
* @param link the details for the link
* @param label the content for the link
* @param isExternal is the link external to the generated documentation
* @return a content tree for the link
*/
public Content createLink(DocLink link, Content label, boolean isExternal) {
HtmlTree anchor = HtmlTree.A(link.relativizeAgainst(file).toString(), label);
anchor.setStyle(HtmlStyle.externalLink);
return anchor;
}
/**
* Converts a name to a valid HTML name (id).
* This depends on the HTML version specified when the {@code Links} object was created.
*
* @param name the string that needs to be converted to a valid HTML name
* @return a valid HTML name
*/
public String getName(String name) {
/* The HTML 4 spec at http://www.w3.org/TR/html4/types.html#h-6.2 mentions
* that the name/id should begin with a letter followed by other valid characters.
* The HTML 5 spec (draft) is more permissive on names/ids where the only restriction
* is that it should be at least one character long and should not contain spaces.
* The spec draft is @ http://www.w3.org/html/wg/drafts/html/master/dom.html#the-id-attribute.
*
* For HTML 4, we need to check for non-characters at the beginning of the name and
* substitute it accordingly, "_" and "$" can appear at the beginning of a member name.
* The method substitutes "$" with "Z:Z:D" and will prefix "_" with "Z:Z".
*/
if (version == HtmlVersion.HTML5) {
return name.replaceAll(" +", "");
}
return Extern.getOldFormHtmlName(name);
}
}