|
1 /* |
|
2 * Copyright (c) 2010, 2012, 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 com.sun.tools.doclint; |
|
27 |
|
28 import java.util.Set; |
|
29 import java.util.Collections; |
|
30 import java.util.EnumMap; |
|
31 import java.util.EnumSet; |
|
32 import java.util.HashMap; |
|
33 import java.util.Map; |
|
34 |
|
35 import javax.lang.model.element.Name; |
|
36 |
|
37 import static com.sun.tools.doclint.HtmlTag.Attr.*; |
|
38 |
|
39 /** |
|
40 * Enum representing HTML tags. |
|
41 * |
|
42 * The intent of this class is to embody the semantics of W3C HTML 4.01 |
|
43 * to the extent supported/used by javadoc. |
|
44 * |
|
45 * This is derivative of com.sun.tools.doclets.formats.html.markup.HtmlTag. |
|
46 * Eventually, these two should be merged back together, and possibly made |
|
47 * public. |
|
48 * |
|
49 * @see <a href="http://www.w3.org/TR/REC-html40/">HTML 4.01 Specification</a> |
|
50 * @author Bhavesh Patel |
|
51 * @author Jonathan Gibbons (revised) |
|
52 */ |
|
53 public enum HtmlTag { |
|
54 A(BlockType.INLINE, EndKind.REQUIRED, |
|
55 attrs(AttrKind.OK, HREF, TARGET, NAME)), |
|
56 |
|
57 B(BlockType.INLINE, EndKind.REQUIRED, |
|
58 EnumSet.of(Flag.EXPECT_CONTENT, Flag.NO_NEST)), |
|
59 |
|
60 BLOCKQUOTE, |
|
61 |
|
62 BODY(BlockType.OTHER, EndKind.REQUIRED), |
|
63 |
|
64 BR(BlockType.INLINE, EndKind.NONE, |
|
65 attrs(AttrKind.USE_CSS, CLEAR)), |
|
66 |
|
67 CAPTION(EnumSet.of(Flag.EXPECT_CONTENT)), |
|
68 |
|
69 CENTER, |
|
70 |
|
71 CITE(BlockType.INLINE, EndKind.REQUIRED, |
|
72 EnumSet.of(Flag.EXPECT_CONTENT, Flag.NO_NEST)), |
|
73 |
|
74 CODE(BlockType.INLINE, EndKind.REQUIRED, |
|
75 EnumSet.of(Flag.EXPECT_CONTENT, Flag.NO_NEST)), |
|
76 |
|
77 DD(BlockType.BLOCK, EndKind.OPTIONAL, |
|
78 EnumSet.of(Flag.EXPECT_CONTENT)), |
|
79 |
|
80 DIV, |
|
81 |
|
82 DL(BlockType.BLOCK, EndKind.REQUIRED, |
|
83 EnumSet.of(Flag.EXPECT_CONTENT, Flag.NO_TEXT), |
|
84 attrs(AttrKind.USE_CSS, COMPACT)), |
|
85 |
|
86 DT(BlockType.BLOCK, EndKind.OPTIONAL, |
|
87 EnumSet.of(Flag.EXPECT_CONTENT)), |
|
88 |
|
89 EM(BlockType.INLINE, EndKind.REQUIRED, |
|
90 EnumSet.of(Flag.NO_NEST)), |
|
91 |
|
92 FONT(BlockType.INLINE, EndKind.REQUIRED, // tag itself is deprecated |
|
93 EnumSet.of(Flag.EXPECT_CONTENT), |
|
94 attrs(AttrKind.USE_CSS, SIZE, COLOR, FACE)), |
|
95 |
|
96 FRAME(BlockType.OTHER, EndKind.NONE), |
|
97 |
|
98 FRAMESET(BlockType.OTHER, EndKind.REQUIRED), |
|
99 |
|
100 H1, |
|
101 H2, |
|
102 H3, |
|
103 H4, |
|
104 H5, |
|
105 H6, |
|
106 |
|
107 HEAD(BlockType.OTHER, EndKind.REQUIRED), |
|
108 |
|
109 HR(BlockType.BLOCK, EndKind.NONE), |
|
110 |
|
111 HTML(BlockType.OTHER, EndKind.REQUIRED), |
|
112 |
|
113 I(BlockType.INLINE, EndKind.REQUIRED, |
|
114 EnumSet.of(Flag.EXPECT_CONTENT, Flag.NO_NEST)), |
|
115 |
|
116 IMG(BlockType.INLINE, EndKind.NONE, |
|
117 attrs(AttrKind.OK, SRC, ALT, HEIGHT, WIDTH), |
|
118 attrs(AttrKind.OBSOLETE, NAME), |
|
119 attrs(AttrKind.USE_CSS, ALIGN, HSPACE, VSPACE, BORDER)), |
|
120 |
|
121 LI(BlockType.BLOCK, EndKind.OPTIONAL), |
|
122 |
|
123 LINK(BlockType.OTHER, EndKind.NONE), |
|
124 |
|
125 MENU, |
|
126 |
|
127 META(BlockType.OTHER, EndKind.NONE), |
|
128 |
|
129 NOFRAMES(BlockType.OTHER, EndKind.REQUIRED), |
|
130 |
|
131 NOSCRIPT(BlockType.OTHER, EndKind.REQUIRED), |
|
132 |
|
133 OL(BlockType.BLOCK, EndKind.REQUIRED, |
|
134 EnumSet.of(Flag.EXPECT_CONTENT, Flag.NO_TEXT), |
|
135 attrs(AttrKind.USE_CSS, START, TYPE)), |
|
136 |
|
137 P(BlockType.BLOCK, EndKind.OPTIONAL, |
|
138 EnumSet.of(Flag.EXPECT_CONTENT), |
|
139 attrs(AttrKind.USE_CSS, ALIGN)), |
|
140 |
|
141 PRE(EnumSet.of(Flag.EXPECT_CONTENT)), |
|
142 |
|
143 SCRIPT(BlockType.OTHER, EndKind.REQUIRED), |
|
144 |
|
145 SMALL(BlockType.INLINE, EndKind.REQUIRED), |
|
146 |
|
147 SPAN(BlockType.INLINE, EndKind.REQUIRED, |
|
148 EnumSet.of(Flag.EXPECT_CONTENT)), |
|
149 |
|
150 STRONG(BlockType.INLINE, EndKind.REQUIRED, |
|
151 EnumSet.of(Flag.EXPECT_CONTENT)), |
|
152 |
|
153 SUB(BlockType.INLINE, EndKind.REQUIRED, |
|
154 EnumSet.of(Flag.EXPECT_CONTENT, Flag.NO_NEST)), |
|
155 |
|
156 SUP(BlockType.INLINE, EndKind.REQUIRED, |
|
157 EnumSet.of(Flag.EXPECT_CONTENT, Flag.NO_NEST)), |
|
158 |
|
159 TABLE(BlockType.BLOCK, EndKind.REQUIRED, |
|
160 EnumSet.of(Flag.EXPECT_CONTENT, Flag.NO_TEXT), |
|
161 attrs(AttrKind.OK, SUMMARY, Attr.FRAME, RULES, BORDER, |
|
162 CELLPADDING, CELLSPACING), |
|
163 attrs(AttrKind.USE_CSS, ALIGN, WIDTH, BGCOLOR)), |
|
164 |
|
165 TBODY(BlockType.BLOCK, EndKind.REQUIRED, |
|
166 EnumSet.of(Flag.EXPECT_CONTENT, Flag.NO_TEXT), |
|
167 attrs(AttrKind.OK, ALIGN, CHAR, CHAROFF, VALIGN)), |
|
168 |
|
169 TD(BlockType.BLOCK, EndKind.OPTIONAL, |
|
170 attrs(AttrKind.OK, COLSPAN, ROWSPAN, HEADERS, SCOPE, ABBR, AXIS, |
|
171 ALIGN, CHAR, CHAROFF, VALIGN), |
|
172 attrs(AttrKind.USE_CSS, WIDTH, BGCOLOR, HEIGHT, NOWRAP)), |
|
173 |
|
174 TFOOT(BlockType.BLOCK, EndKind.REQUIRED, |
|
175 attrs(AttrKind.OK, ALIGN, CHAR, CHAROFF, VALIGN)), |
|
176 |
|
177 TH(BlockType.BLOCK, EndKind.OPTIONAL, |
|
178 attrs(AttrKind.OK, COLSPAN, ROWSPAN, HEADERS, SCOPE, ABBR, AXIS, |
|
179 ALIGN, CHAR, CHAROFF, VALIGN), |
|
180 attrs(AttrKind.USE_CSS, WIDTH, BGCOLOR, HEIGHT, NOWRAP)), |
|
181 |
|
182 THEAD(BlockType.BLOCK, EndKind.REQUIRED, |
|
183 attrs(AttrKind.OK, ALIGN, CHAR, CHAROFF, VALIGN)), |
|
184 |
|
185 TITLE(BlockType.OTHER, EndKind.REQUIRED), |
|
186 |
|
187 TR(BlockType.BLOCK, EndKind.OPTIONAL, |
|
188 EnumSet.of(Flag.NO_TEXT), |
|
189 attrs(AttrKind.OK, ALIGN, CHAR, CHAROFF, VALIGN), |
|
190 attrs(AttrKind.USE_CSS, BGCOLOR)), |
|
191 |
|
192 TT(BlockType.INLINE, EndKind.REQUIRED, |
|
193 EnumSet.of(Flag.EXPECT_CONTENT, Flag.NO_NEST)), |
|
194 |
|
195 U(BlockType.INLINE, EndKind.REQUIRED, |
|
196 EnumSet.of(Flag.EXPECT_CONTENT, Flag.NO_NEST)), |
|
197 |
|
198 UL(BlockType.BLOCK, EndKind.REQUIRED, |
|
199 EnumSet.of(Flag.EXPECT_CONTENT, Flag.NO_TEXT), |
|
200 attrs(AttrKind.USE_CSS, COMPACT, TYPE)), |
|
201 |
|
202 VAR(BlockType.INLINE, EndKind.REQUIRED); |
|
203 |
|
204 /** |
|
205 * Enum representing the type of HTML element. |
|
206 */ |
|
207 public static enum BlockType { |
|
208 BLOCK, |
|
209 INLINE, |
|
210 OTHER; |
|
211 } |
|
212 |
|
213 /** |
|
214 * Enum representing HTML end tag requirement. |
|
215 */ |
|
216 public static enum EndKind { |
|
217 NONE, |
|
218 OPTIONAL, |
|
219 REQUIRED; |
|
220 } |
|
221 |
|
222 public static enum Flag { |
|
223 EXPECT_CONTENT, |
|
224 NO_NEST, |
|
225 NO_TEXT |
|
226 } |
|
227 |
|
228 public static enum Attr { |
|
229 ABBR, |
|
230 ALIGN, |
|
231 ALT, |
|
232 AXIS, |
|
233 BGCOLOR, |
|
234 BORDER, |
|
235 CELLSPACING, |
|
236 CELLPADDING, |
|
237 CHAR, |
|
238 CHAROFF, |
|
239 CLEAR, |
|
240 CLASS, |
|
241 COLOR, |
|
242 COLSPAN, |
|
243 COMPACT, |
|
244 FACE, |
|
245 FRAME, |
|
246 HEADERS, |
|
247 HEIGHT, |
|
248 HREF, |
|
249 HSPACE, |
|
250 ID, |
|
251 NAME, |
|
252 NOWRAP, |
|
253 REVERSED, |
|
254 ROWSPAN, |
|
255 RULES, |
|
256 SCOPE, |
|
257 SIZE, |
|
258 SPACE, |
|
259 SRC, |
|
260 START, |
|
261 STYLE, |
|
262 SUMMARY, |
|
263 TARGET, |
|
264 TYPE, |
|
265 VALIGN, |
|
266 VSPACE, |
|
267 WIDTH; |
|
268 |
|
269 public String getText() { |
|
270 return name().toLowerCase(); |
|
271 } |
|
272 |
|
273 static final Map<String,Attr> index = new HashMap<String,Attr>(); |
|
274 static { |
|
275 for (Attr t: values()) { |
|
276 index.put(t.name().toLowerCase(), t); |
|
277 } |
|
278 } |
|
279 } |
|
280 |
|
281 public static enum AttrKind { |
|
282 INVALID, |
|
283 OBSOLETE, |
|
284 USE_CSS, |
|
285 OK |
|
286 } |
|
287 |
|
288 // This class exists to avoid warnings from using parameterized vararg type |
|
289 // Map<Attr,AttrKind> in signature of HtmlTag constructor. |
|
290 private static class AttrMap extends EnumMap<Attr,AttrKind> { |
|
291 private static final long serialVersionUID = 0; |
|
292 AttrMap() { |
|
293 super(Attr.class); |
|
294 } |
|
295 } |
|
296 |
|
297 |
|
298 public final BlockType blockType; |
|
299 public final EndKind endKind; |
|
300 public final Set<Flag> flags; |
|
301 private final Map<Attr,AttrKind> attrs; |
|
302 |
|
303 |
|
304 HtmlTag() { |
|
305 this(BlockType.BLOCK, EndKind.REQUIRED); |
|
306 } |
|
307 |
|
308 HtmlTag(Set<Flag> flags) { |
|
309 this(BlockType.BLOCK, EndKind.REQUIRED, flags); |
|
310 } |
|
311 |
|
312 HtmlTag(BlockType blockType, EndKind endKind, AttrMap... attrMaps) { |
|
313 this(blockType, endKind, Collections.<Flag>emptySet(), attrMaps); |
|
314 } |
|
315 |
|
316 HtmlTag(BlockType blockType, EndKind endKind, Set<Flag> flags, AttrMap... attrMaps) { |
|
317 this.blockType = blockType; |
|
318 this.endKind = endKind;this.flags = flags; |
|
319 this.attrs = new EnumMap<Attr,AttrKind>(Attr.class); |
|
320 for (Map<Attr,AttrKind> m: attrMaps) |
|
321 this.attrs.putAll(m); |
|
322 attrs.put(Attr.CLASS, AttrKind.OK); |
|
323 attrs.put(Attr.ID, AttrKind.OK); |
|
324 attrs.put(Attr.STYLE, AttrKind.OK); |
|
325 } |
|
326 |
|
327 public String getText() { |
|
328 return name().toLowerCase(); |
|
329 } |
|
330 |
|
331 public Attr getAttr(Name attrName) { |
|
332 return Attr.index.get(attrName.toString().toLowerCase()); |
|
333 } |
|
334 |
|
335 public AttrKind getAttrKind(Name attrName) { |
|
336 AttrKind k = attrs.get(getAttr(attrName)); // null-safe |
|
337 return (k == null) ? AttrKind.INVALID : k; |
|
338 } |
|
339 |
|
340 private static AttrMap attrs(AttrKind k, Attr... attrs) { |
|
341 AttrMap map = new AttrMap(); |
|
342 for (Attr a: attrs) map.put(a, k); |
|
343 return map; |
|
344 } |
|
345 |
|
346 private static final Map<String,HtmlTag> index = new HashMap<String,HtmlTag>(); |
|
347 static { |
|
348 for (HtmlTag t: values()) { |
|
349 index.put(t.name().toLowerCase(), t); |
|
350 } |
|
351 } |
|
352 |
|
353 static HtmlTag get(Name tagName) { |
|
354 return index.get(tagName.toString().toLowerCase()); |
|
355 } |
|
356 } |