author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58286 | e4d90117c5de |
permissions | -rw-r--r-- |
47850 | 1 |
/* |
54060
53a95878619f
8220202: Simplify/standardize method naming for HtmlTree
jjg
parents:
54051
diff
changeset
|
2 |
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved. |
47850 | 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 |
||
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
49 |
private final DocPath file; |
47850 | 50 |
|
51 |
/** |
|
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
52 |
* Creates a {@code Links} object for a specific file, to be written in a specific HTML version. |
47850 | 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 |
* |
|
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
57 |
* @param file the file |
47850 | 58 |
*/ |
53562 | 59 |
public Links(DocPath file) { |
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
60 |
this.file = file; |
47850 | 61 |
} |
62 |
||
63 |
/** |
|
64 |
* Creates an anchor of the form {@code <a id="name"><!-- --></a>}. |
|
65 |
* |
|
66 |
* @param name the value for the {@code id} or {@code name} attribute |
|
67 |
* @return a content tree for the anchor |
|
68 |
*/ |
|
69 |
public Content createAnchor(String name) { |
|
70 |
return createAnchor(getName(name), null); |
|
71 |
} |
|
72 |
||
73 |
/** |
|
74 |
* Creates an anchor of the form {@code <a id="sectionName"><!-- --></a>}. |
|
75 |
* |
|
76 |
* @param sectionName the value for the {@code id} or {@code name} attribute |
|
77 |
* @return a content tree for the anchor |
|
78 |
*/ |
|
79 |
public Content createAnchor(SectionName sectionName) { |
|
80 |
return createAnchor(sectionName.getName(), null); |
|
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* Creates an anchor of the form {@code <a id="sectionNameName"><!-- --></a>}. |
|
85 |
* |
|
86 |
* @param sectionName the first part of the value for the {@code id} or {@code name} attribute |
|
87 |
* @param name the second part of the value for the {@code id} or {@code name} attribute |
|
88 |
* @return a content tree for the anchor |
|
89 |
*/ |
|
90 |
public Content createAnchor(SectionName sectionName, String name) { |
|
91 |
return createAnchor(sectionName.getName() + getName(name), null); |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Creates an anchor of the form {@code <a id="anchorName">content</a>}. |
|
96 |
* |
|
97 |
* @param name the value for the {@code id} or {@code name} attribute |
|
98 |
* @param content the content that should be added to the anchor, |
|
99 |
* or null, to use an empty comment |
|
100 |
* @return a content tree for the marker anchor |
|
101 |
*/ |
|
102 |
public Content createAnchor(String name, Content content) { |
|
55339
0530705ca300
8223364: Bad placement of anchor relative to header
hannesw
parents:
54060
diff
changeset
|
103 |
return HtmlTree.A_ID(getName(name), (content == null ? EMPTY_COMMENT : content)); |
47850 | 104 |
} |
105 |
||
106 |
private static final Content EMPTY_COMMENT = new Comment(" "); |
|
107 |
||
108 |
/** |
|
109 |
* Creates a link of the form {@code <a href="#where">label</a>}. |
|
110 |
* |
|
111 |
* @param where the position of the link in the file |
|
112 |
* @param label the content for the link |
|
113 |
* @return a content tree for the link |
|
114 |
*/ |
|
115 |
public Content createLink(String where, Content label) { |
|
116 |
DocLink l = DocLink.fragment(getName(where)); |
|
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
117 |
return createLink(l, label, "", ""); |
47850 | 118 |
} |
119 |
||
120 |
/** |
|
121 |
* Creates a link of the form {@code <a href="#sectionName">label</a>}. |
|
122 |
* |
|
123 |
* @param sectionName the section name to which the link will be created |
|
124 |
* @param label the content for the link |
|
125 |
* @return a content tree for the link |
|
126 |
*/ |
|
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
127 |
public Content createLink(SectionName sectionName, Content label) { |
47850 | 128 |
DocLink l = DocLink.fragment(sectionName.getName()); |
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
129 |
return createLink(l, label, "", ""); |
47850 | 130 |
} |
131 |
||
132 |
/** |
|
133 |
* Creates a link of the form {@code <a href="#sectionNameWhere">label</a>}. |
|
134 |
* |
|
135 |
* @param sectionName the section name combined with where to which the link |
|
136 |
* will be created |
|
137 |
* @param where the fragment combined with sectionName to which the link |
|
138 |
* will be created |
|
139 |
* @param label the content for the link |
|
140 |
* @return a content tree for the link |
|
141 |
*/ |
|
142 |
public Content createLink(SectionName sectionName, String where, Content label) { |
|
143 |
DocLink l = DocLink.fragment(sectionName.getName() + getName(where)); |
|
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
144 |
return createLink(l, label, "", ""); |
47850 | 145 |
} |
146 |
||
147 |
/** |
|
148 |
* Creates a link of the form {@code <a href="#stylename" title="title" target="target">label</a>}. |
|
149 |
* |
|
150 |
* @param sectionName the section name to which the link will be created |
|
151 |
* @param label the content for the link |
|
152 |
* @param title the title for the link |
|
153 |
* @param target the target for the link, or null |
|
154 |
* @return a content tree for the link |
|
155 |
*/ |
|
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
156 |
public Content createLink(SectionName sectionName, Content label, String title, String target) { |
47850 | 157 |
DocLink l = DocLink.fragment(sectionName.getName()); |
158 |
return createLink(l, label, title, target); |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Creates a link of the form {@code <a href="path">label</a>}. |
|
163 |
* |
|
164 |
* @param path the path for the link |
|
165 |
* @param label the content for the link |
|
166 |
* @return a content tree for the link |
|
167 |
*/ |
|
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
168 |
public Content createLink(DocPath path, String label) { |
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
169 |
return createLink(path, new StringContent(label), false, "", ""); |
47850 | 170 |
} |
171 |
||
172 |
/** |
|
173 |
* Creates a link of the form {@code <a href="path">label</a>}. |
|
174 |
* |
|
175 |
* @param path the path for the link |
|
176 |
* @param label the content for the link |
|
177 |
* @return a content tree for the link |
|
178 |
*/ |
|
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
179 |
public Content createLink(DocPath path, Content label) { |
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
180 |
return createLink(path, label, "", ""); |
47850 | 181 |
} |
182 |
||
183 |
/** |
|
184 |
* Creates a link of the form {@code <a href="path" title="title" target="target">label</a>}. |
|
185 |
* If {@code strong} is set, the label will be wrapped in |
|
186 |
* {@code <span style="typeNameLink">...</span>}. |
|
187 |
* |
|
188 |
* @param path the path for the link |
|
189 |
* @param label the content for the link |
|
190 |
* @param strong whether to wrap the {@code label} in a SPAN element |
|
191 |
* @param title the title for the link |
|
192 |
* @param target the target for the link, or null |
|
193 |
* @return a content tree for the link |
|
194 |
*/ |
|
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
195 |
public Content createLink(DocPath path, Content label, boolean strong, |
47850 | 196 |
String title, String target) { |
47851
6a9bb4f77d50
8190822: Remove dead code that could lead to invalid HTML
jjg
parents:
47850
diff
changeset
|
197 |
return createLink(new DocLink(path), label, strong, title, target); |
47850 | 198 |
} |
199 |
||
200 |
/** |
|
201 |
* Creates a link of the form {@code <a href="path" title="title" target="target">label</a>}. |
|
202 |
* |
|
203 |
* @param path the path for the link |
|
204 |
* @param label the content for the link |
|
205 |
* @param title the title for the link |
|
206 |
* @param target the target for the link, or null |
|
207 |
* @return a content tree for the link |
|
208 |
*/ |
|
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
209 |
public Content createLink(DocPath path, Content label, String title, String target) { |
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
210 |
return createLink(new DocLink(path), label, title, target); |
47850 | 211 |
} |
212 |
||
213 |
/** |
|
214 |
* Creates a link of the form {@code <a href="link">label</a>}. |
|
215 |
* |
|
216 |
* @param link the details for the link |
|
217 |
* @param label the content for the link |
|
218 |
* @return a content tree for the link |
|
219 |
*/ |
|
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
220 |
public Content createLink(DocLink link, Content label) { |
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
221 |
return createLink(link, label, "", ""); |
47850 | 222 |
} |
223 |
||
224 |
/** |
|
225 |
* Creates a link of the form {@code <a href="path" title="title" target="target">label</a>}. |
|
226 |
* |
|
227 |
* @param link the details for the link |
|
228 |
* @param label the content for the link |
|
229 |
* @param title the title for the link |
|
230 |
* @param target the target for the link, or null |
|
231 |
* @return a content tree for the link |
|
232 |
*/ |
|
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
233 |
public Content createLink(DocLink link, Content label, String title, String target) { |
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
234 |
HtmlTree anchor = HtmlTree.A(link.relativizeAgainst(file).toString(), label); |
47850 | 235 |
if (title != null && title.length() != 0) { |
54060
53a95878619f
8220202: Simplify/standardize method naming for HtmlTree
jjg
parents:
54051
diff
changeset
|
236 |
anchor.put(HtmlAttr.TITLE, title); |
47850 | 237 |
} |
238 |
if (target != null && target.length() != 0) { |
|
54060
53a95878619f
8220202: Simplify/standardize method naming for HtmlTree
jjg
parents:
54051
diff
changeset
|
239 |
anchor.put(HtmlAttr.TARGET, target); |
47850 | 240 |
} |
241 |
return anchor; |
|
242 |
} |
|
243 |
||
244 |
/** |
|
245 |
* Creates a link of the form {@code <a href="link" title="title" target="target">label</a>}. |
|
246 |
* If {@code strong} is set, the label will be wrapped in |
|
247 |
* {@code <span style="typeNameLink">...</span>}. |
|
248 |
* |
|
249 |
* @param link the details for the link |
|
250 |
* @param label the content for the link |
|
251 |
* @param strong whether to wrap the {@code label} in a SPAN element |
|
252 |
* @param title the title for the link |
|
253 |
* @param target the target for the link, or null |
|
254 |
* @return a content tree for the link |
|
255 |
*/ |
|
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
256 |
public Content createLink(DocLink link, Content label, boolean strong, |
47850 | 257 |
String title, String target) { |
48295
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
258 |
return createLink(link, label, strong, title, target, false); |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
259 |
} |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
260 |
|
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
261 |
/** |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
262 |
* Creates a link of the form {@code <a href="link" title="title" target="target">label</a>}. |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
263 |
* If {@code strong} is set, the label will be wrapped in |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
264 |
* {@code <span style="typeNameLink">...</span>}. |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
265 |
* |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
266 |
* @param link the details for the link |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
267 |
* @param label the content for the link |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
268 |
* @param strong whether to wrap the {@code label} in a SPAN element |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
269 |
* @param title the title for the link |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
270 |
* @param target the target for the link, or null |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
271 |
* @param isExternal is the link external to the generated documentation |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
272 |
* @return a content tree for the link |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
273 |
*/ |
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
274 |
public Content createLink(DocLink link, Content label, boolean strong, |
48295
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
275 |
String title, String target, boolean isExternal) { |
47850 | 276 |
Content body = label; |
277 |
if (strong) { |
|
278 |
body = HtmlTree.SPAN(HtmlStyle.typeNameLink, body); |
|
279 |
} |
|
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
280 |
HtmlTree l = HtmlTree.A(link.relativizeAgainst(file).toString(), body); |
47850 | 281 |
if (title != null && title.length() != 0) { |
54060
53a95878619f
8220202: Simplify/standardize method naming for HtmlTree
jjg
parents:
54051
diff
changeset
|
282 |
l.put(HtmlAttr.TITLE, title); |
47850 | 283 |
} |
284 |
if (target != null && target.length() != 0) { |
|
54060
53a95878619f
8220202: Simplify/standardize method naming for HtmlTree
jjg
parents:
54051
diff
changeset
|
285 |
l.put(HtmlAttr.TARGET, target); |
47850 | 286 |
} |
48295
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
287 |
if (isExternal) { |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
288 |
l.setStyle(HtmlStyle.externalLink); |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
289 |
} |
47850 | 290 |
return l; |
291 |
} |
|
292 |
||
48295
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
293 |
/** |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
294 |
* Creates a link. |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
295 |
* |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
296 |
* @param link the details for the link |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
297 |
* @param label the content for the link |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
298 |
* @param isExternal is the link external to the generated documentation |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
299 |
* @return a content tree for the link |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
300 |
*/ |
48654
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
301 |
public Content createLink(DocLink link, Content label, boolean isExternal) { |
36f58bd6269f
8195796: Reduce the size of relative URLs in generated docs
jjg
parents:
48295
diff
changeset
|
302 |
HtmlTree anchor = HtmlTree.A(link.relativizeAgainst(file).toString(), label); |
48295
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
303 |
anchor.setStyle(HtmlStyle.externalLink); |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
304 |
return anchor; |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
305 |
} |
c79d31ba84b9
8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents:
47851
diff
changeset
|
306 |
|
47850 | 307 |
/** |
308 |
* Converts a name to a valid HTML name (id). |
|
309 |
* This depends on the HTML version specified when the {@code Links} object was created. |
|
310 |
* |
|
311 |
* @param name the string that needs to be converted to a valid HTML name |
|
312 |
* @return a valid HTML name |
|
313 |
*/ |
|
314 |
public String getName(String name) { |
|
58286
e4d90117c5de
8231122: @index tag with newline causes tag search to fail
hannesw
parents:
55339
diff
changeset
|
315 |
return name.replaceAll("\\s+", ""); |
47850 | 316 |
} |
317 |
||
318 |
} |