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 package org.w3c.tidy;
55
56 import java.util.Hashtable;
57 import java.util.Map;
58
59
60 /**
61 * HTML attribute hash table.
62 * @author Dave Raggett <a href="mailto:dsr@w3.org">dsr@w3.org </a>
63 * @author Andy Quick <a href="mailto:ac.quick@sympatico.ca">ac.quick@sympatico.ca </a> (translation to Java)
64 * @author Fabrizio Giustina
65 * @version $Revision: 779 $ ($Author: fgiust $)
66 */
67 public class AttributeTable
68 {
69
70 /**
71 * href attribute.
72 */
73 protected static Attribute attrHref;
74
75 /**
76 * src attribute.
77 */
78 protected static Attribute attrSrc;
79
80 /**
81 * id attribute.
82 */
83 protected static Attribute attrId;
84
85 /**
86 * name attribute.
87 */
88 protected static Attribute attrName;
89
90 /**
91 * summary attribute.
92 */
93 protected static Attribute attrSummary;
94
95 /**
96 * alt attribute.
97 */
98 protected static Attribute attrAlt;
99
100 /**
101 * logdesc attribute.
102 */
103 protected static Attribute attrLongdesc;
104
105 /**
106 * usemap attribute.
107 */
108 protected static Attribute attrUsemap;
109
110 /**
111 * ismap attribute.
112 */
113 protected static Attribute attrIsmap;
114
115 /**
116 * language attribute.
117 */
118 protected static Attribute attrLanguage;
119
120 /**
121 * type attribute.
122 */
123 protected static Attribute attrType;
124
125 /**
126 * title attribute.
127 */
128 protected static Attribute attrTitle;
129
130 /**
131 * xmlns attribute.
132 */
133 protected static Attribute attrXmlns;
134
135 /**
136 * value attribute.
137 */
138 protected static Attribute attrValue;
139
140 /**
141 * content attribute.
142 */
143 protected static Attribute attrContent;
144
145 /**
146 * datafld attribute.
147 */
148 protected static Attribute attrDatafld;
149
150 /**
151 * width attribute.
152 */
153 protected static Attribute attrWidth;
154
155 /**
156 * height attribute.
157 */
158 protected static Attribute attrHeight;
159
160 /**
161 * attribute table instance.
162 */
163 private static AttributeTable defaultAttributeTable;
164
165 /**
166 * all the known attributes.
167 */
168 private static final Attribute[] ATTRS = {
169 new Attribute("abbr", Dict.VERS_HTML40, AttrCheckImpl.TEXT),
170 new Attribute("accept-charset", Dict.VERS_HTML40, AttrCheckImpl.CHARSET),
171 new Attribute("accept", Dict.VERS_ALL, AttrCheckImpl.TYPE),
172 new Attribute("accesskey", Dict.VERS_HTML40, AttrCheckImpl.CHARACTER),
173 new Attribute("action", Dict.VERS_ALL, AttrCheckImpl.URL),
174 new Attribute("add_date", Dict.VERS_NETSCAPE, AttrCheckImpl.TEXT),
175 new Attribute("align", Dict.VERS_ALL, AttrCheckImpl.ALIGN),
176 new Attribute("alink", Dict.VERS_LOOSE, AttrCheckImpl.COLOR),
177 new Attribute("alt", Dict.VERS_ALL, AttrCheckImpl.TEXT),
178 new Attribute("archive", Dict.VERS_HTML40, AttrCheckImpl.URLS),
179 new Attribute("axis", Dict.VERS_HTML40, AttrCheckImpl.TEXT),
180 new Attribute("background", Dict.VERS_LOOSE, AttrCheckImpl.URL),
181 new Attribute("bgcolor", Dict.VERS_LOOSE, AttrCheckImpl.COLOR),
182 new Attribute("bgproperties", Dict.VERS_PROPRIETARY, AttrCheckImpl.TEXT),
183 new Attribute("border", Dict.VERS_ALL, AttrCheckImpl.BOOL),
184 new Attribute("bordercolor", Dict.VERS_MICROSOFT, AttrCheckImpl.COLOR),
185 new Attribute("bottommargin", Dict.VERS_MICROSOFT, AttrCheckImpl.NUMBER),
186 new Attribute("cellpadding", Dict.VERS_FROM32, AttrCheckImpl.LENGTH),
187 new Attribute("cellspacing", Dict.VERS_FROM32, AttrCheckImpl.LENGTH),
188 new Attribute("char", Dict.VERS_HTML40, AttrCheckImpl.CHARACTER),
189 new Attribute("charoff", Dict.VERS_HTML40, AttrCheckImpl.LENGTH),
190 new Attribute("charset", Dict.VERS_HTML40, AttrCheckImpl.CHARSET),
191 new Attribute("checked", Dict.VERS_ALL, AttrCheckImpl.BOOL),
192 new Attribute("cite", Dict.VERS_HTML40, AttrCheckImpl.URL),
193 new Attribute("class", Dict.VERS_HTML40, AttrCheckImpl.TEXT),
194 new Attribute("classid", Dict.VERS_HTML40, AttrCheckImpl.URL),
195 new Attribute("clear", Dict.VERS_LOOSE, AttrCheckImpl.CLEAR),
196 new Attribute("code", Dict.VERS_LOOSE, AttrCheckImpl.TEXT),
197 new Attribute("codebase", Dict.VERS_HTML40, AttrCheckImpl.URL),
198 new Attribute("codetype", Dict.VERS_HTML40, AttrCheckImpl.TYPE),
199 new Attribute("color", Dict.VERS_LOOSE, AttrCheckImpl.COLOR),
200 new Attribute("cols", Dict.VERS_IFRAME, AttrCheckImpl.COLS),
201 new Attribute("colspan", Dict.VERS_FROM32, AttrCheckImpl.NUMBER),
202 new Attribute("compact", Dict.VERS_ALL, AttrCheckImpl.BOOL),
203 new Attribute("content", Dict.VERS_ALL, AttrCheckImpl.TEXT),
204 new Attribute("coords", Dict.VERS_FROM32, AttrCheckImpl.COORDS),
205 new Attribute("data", Dict.VERS_HTML40, AttrCheckImpl.URL),
206 new Attribute("datafld", Dict.VERS_MICROSOFT, AttrCheckImpl.TEXT),
207 new Attribute("dataformatas", Dict.VERS_MICROSOFT, AttrCheckImpl.TEXT),
208 new Attribute("datapagesize", Dict.VERS_MICROSOFT, AttrCheckImpl.NUMBER),
209 new Attribute("datasrc", Dict.VERS_MICROSOFT, AttrCheckImpl.URL),
210 new Attribute("datetime", Dict.VERS_HTML40, AttrCheckImpl.DATE),
211 new Attribute("declare", Dict.VERS_HTML40, AttrCheckImpl.BOOL),
212 new Attribute("defer", Dict.VERS_HTML40, AttrCheckImpl.BOOL),
213 new Attribute("dir", Dict.VERS_HTML40, AttrCheckImpl.TEXTDIR),
214 new Attribute("disabled", Dict.VERS_HTML40, AttrCheckImpl.BOOL),
215 new Attribute("enctype", Dict.VERS_ALL, AttrCheckImpl.TYPE),
216 new Attribute("face", Dict.VERS_LOOSE, AttrCheckImpl.TEXT),
217 new Attribute("for", Dict.VERS_HTML40, AttrCheckImpl.IDREF),
218 new Attribute("frame", Dict.VERS_HTML40, AttrCheckImpl.TFRAME),
219 new Attribute("frameborder", (short) (Dict.VERS_FRAMESET | Dict.VERS_IFRAME), AttrCheckImpl.FBORDER),
220 new Attribute("framespacing", Dict.VERS_PROPRIETARY, AttrCheckImpl.NUMBER),
221 new Attribute("gridx", Dict.VERS_PROPRIETARY, AttrCheckImpl.NUMBER),
222 new Attribute("gridy", Dict.VERS_PROPRIETARY, AttrCheckImpl.NUMBER),
223 new Attribute("headers", Dict.VERS_HTML40, AttrCheckImpl.IDREF),
224 new Attribute("height", Dict.VERS_ALL, AttrCheckImpl.LENGTH),
225 new Attribute("href", Dict.VERS_ALL, AttrCheckImpl.URL),
226 new Attribute("hreflang", Dict.VERS_HTML40, AttrCheckImpl.LANG),
227 new Attribute("hspace", Dict.VERS_ALL, AttrCheckImpl.NUMBER),
228 new Attribute("http-equiv", Dict.VERS_ALL, AttrCheckImpl.TEXT),
229 new Attribute("id", Dict.VERS_HTML40, AttrCheckImpl.ID),
230 new Attribute("ismap", Dict.VERS_ALL, AttrCheckImpl.BOOL),
231 new Attribute("label", Dict.VERS_HTML40, AttrCheckImpl.TEXT),
232 new Attribute("lang", Dict.VERS_HTML40, AttrCheckImpl.LANG),
233 new Attribute("language", Dict.VERS_LOOSE, AttrCheckImpl.TEXT),
234 new Attribute("last_modified", Dict.VERS_NETSCAPE, AttrCheckImpl.TEXT),
235 new Attribute("last_visit", Dict.VERS_NETSCAPE, AttrCheckImpl.TEXT),
236 new Attribute("leftmargin", Dict.VERS_MICROSOFT, AttrCheckImpl.NUMBER),
237 new Attribute("link", Dict.VERS_LOOSE, AttrCheckImpl.COLOR),
238 new Attribute("longdesc", Dict.VERS_HTML40, AttrCheckImpl.URL),
239 new Attribute("lowsrc", Dict.VERS_PROPRIETARY, AttrCheckImpl.URL),
240 new Attribute("marginheight", Dict.VERS_IFRAME, AttrCheckImpl.NUMBER),
241 new Attribute("marginwidth", Dict.VERS_IFRAME, AttrCheckImpl.NUMBER),
242 new Attribute("maxlength", Dict.VERS_ALL, AttrCheckImpl.NUMBER),
243 new Attribute("media", Dict.VERS_HTML40, AttrCheckImpl.MEDIA),
244 new Attribute("method", Dict.VERS_ALL, AttrCheckImpl.FSUBMIT),
245 new Attribute("multiple", Dict.VERS_ALL, AttrCheckImpl.BOOL),
246 new Attribute("name", Dict.VERS_ALL, AttrCheckImpl.NAME),
247 new Attribute("nohref", Dict.VERS_FROM32, AttrCheckImpl.BOOL),
248 new Attribute("noresize", Dict.VERS_FRAMESET, AttrCheckImpl.BOOL),
249 new Attribute("noshade", Dict.VERS_LOOSE, AttrCheckImpl.BOOL),
250 new Attribute("nowrap", Dict.VERS_LOOSE, AttrCheckImpl.BOOL),
251 new Attribute("object", Dict.VERS_HTML40_LOOSE, AttrCheckImpl.TEXT),
252 new Attribute("onblur", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
253 new Attribute("onchange", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
254 new Attribute("onclick", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
255 new Attribute("ondblclick", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
256 new Attribute("onkeydown", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
257 new Attribute("onkeypress", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
258 new Attribute("onkeyup", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
259 new Attribute("onload", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
260 new Attribute("onmousedown", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
261 new Attribute("onmousemove", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
262 new Attribute("onmouseout", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
263 new Attribute("onmouseover", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
264 new Attribute("onmouseup", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
265 new Attribute("onsubmit", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
266 new Attribute("onreset", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
267 new Attribute("onselect", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
268 new Attribute("onunload", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
269 new Attribute("onfocus", Dict.VERS_EVENTS, AttrCheckImpl.SCRIPT),
270 new Attribute("onafterupdate", Dict.VERS_MICROSOFT, AttrCheckImpl.SCRIPT),
271 new Attribute("onbeforeupdate", Dict.VERS_MICROSOFT, AttrCheckImpl.SCRIPT),
272 new Attribute("onerrorupdate", Dict.VERS_MICROSOFT, AttrCheckImpl.SCRIPT),
273 new Attribute("onrowenter", Dict.VERS_MICROSOFT, AttrCheckImpl.SCRIPT),
274 new Attribute("onrowexit", Dict.VERS_MICROSOFT, AttrCheckImpl.SCRIPT),
275 new Attribute("onbeforeunload", Dict.VERS_MICROSOFT, AttrCheckImpl.SCRIPT),
276 new Attribute("ondatasetchanged", Dict.VERS_MICROSOFT, AttrCheckImpl.SCRIPT),
277 new Attribute("ondataavailable", Dict.VERS_MICROSOFT, AttrCheckImpl.SCRIPT),
278 new Attribute("ondatasetcomplete", Dict.VERS_MICROSOFT, AttrCheckImpl.SCRIPT),
279 new Attribute("profile", Dict.VERS_HTML40, AttrCheckImpl.URL),
280 new Attribute("prompt", Dict.VERS_LOOSE, AttrCheckImpl.TEXT),
281 new Attribute("readonly", Dict.VERS_HTML40, AttrCheckImpl.BOOL),
282 new Attribute("rel", Dict.VERS_ALL, AttrCheckImpl.LINKTYPES),
283 new Attribute("rev", Dict.VERS_ALL, AttrCheckImpl.LINKTYPES),
284 new Attribute("rightmargin", Dict.VERS_MICROSOFT, AttrCheckImpl.NUMBER),
285 new Attribute("rows", Dict.VERS_ALL, AttrCheckImpl.NUMBER),
286 new Attribute("rowspan", Dict.VERS_ALL, AttrCheckImpl.NUMBER),
287 new Attribute("rules", Dict.VERS_HTML40, AttrCheckImpl.TRULES),
288 new Attribute("scheme", Dict.VERS_HTML40, AttrCheckImpl.TEXT),
289 new Attribute("scope", Dict.VERS_HTML40, AttrCheckImpl.SCOPE),
290 new Attribute("scrolling", Dict.VERS_IFRAME, AttrCheckImpl.SCROLL),
291 new Attribute("selected", Dict.VERS_ALL, AttrCheckImpl.BOOL),
292 new Attribute("shape", Dict.VERS_FROM32, AttrCheckImpl.SHAPE),
293 new Attribute("showgrid", Dict.VERS_PROPRIETARY, AttrCheckImpl.BOOL),
294 new Attribute("showgridx", Dict.VERS_PROPRIETARY, AttrCheckImpl.BOOL),
295 new Attribute("showgridy", Dict.VERS_PROPRIETARY, AttrCheckImpl.BOOL),
296 new Attribute("size", Dict.VERS_LOOSE, AttrCheckImpl.NUMBER),
297 new Attribute("span", Dict.VERS_HTML40, AttrCheckImpl.NUMBER),
298 new Attribute("src", Dict.VERS_ALL, AttrCheckImpl.URL),
299 new Attribute("standby", Dict.VERS_HTML40, AttrCheckImpl.TEXT),
300 new Attribute("start", Dict.VERS_ALL, AttrCheckImpl.NUMBER),
301 new Attribute("style", Dict.VERS_HTML40, AttrCheckImpl.TEXT),
302 new Attribute("summary", Dict.VERS_HTML40, AttrCheckImpl.TEXT),
303 new Attribute("tabindex", Dict.VERS_HTML40, AttrCheckImpl.NUMBER),
304 new Attribute("target", Dict.VERS_HTML40, AttrCheckImpl.TARGET),
305 new Attribute("text", Dict.VERS_LOOSE, AttrCheckImpl.COLOR),
306 new Attribute("title", Dict.VERS_HTML40, AttrCheckImpl.TEXT),
307 new Attribute("topmargin", Dict.VERS_MICROSOFT, AttrCheckImpl.NUMBER),
308 new Attribute("type", Dict.VERS_FROM32, AttrCheckImpl.TYPE),
309 new Attribute("usemap", Dict.VERS_ALL, AttrCheckImpl.BOOL),
310 new Attribute("valign", Dict.VERS_FROM32, AttrCheckImpl.VALIGN),
311 new Attribute("value", Dict.VERS_ALL, AttrCheckImpl.TEXT),
312 new Attribute("valuetype", Dict.VERS_HTML40, AttrCheckImpl.VTYPE),
313 new Attribute("version", Dict.VERS_ALL, AttrCheckImpl.TEXT),
314 new Attribute("vlink", Dict.VERS_LOOSE, AttrCheckImpl.COLOR),
315 new Attribute("vspace", Dict.VERS_LOOSE, AttrCheckImpl.NUMBER),
316 new Attribute("width", Dict.VERS_ALL, AttrCheckImpl.LENGTH),
317 new Attribute("wrap", Dict.VERS_NETSCAPE, AttrCheckImpl.TEXT),
318 new Attribute("xml:lang", Dict.VERS_XML, AttrCheckImpl.TEXT),
319 new Attribute("xml:space", Dict.VERS_XML, AttrCheckImpl.TEXT),
320 new Attribute("xmlns", Dict.VERS_ALL, AttrCheckImpl.TEXT),
321 new Attribute("rbspan", Dict.VERS_XHTML11, AttrCheckImpl.NUMBER),
322 };
323
324 /**
325 * Map containing all the installed attributes.
326 */
327 private Map attributeHashtable = new Hashtable();
328
329 /**
330 * lookup an installed Attribute.
331 * @param name attribute name
332 * @return Attribute or null if the attribute is not found
333 */
334 public Attribute lookup(String name)
335 {
336 return (Attribute) this.attributeHashtable.get(name);
337 }
338
339 /**
340 * installs a new Attribute.
341 * @param attr Atribute
342 * @return installed Attribute
343 */
344 public Attribute install(Attribute attr)
345 {
346 return (Attribute) this.attributeHashtable.put(attr.getName(), attr);
347 }
348
349 /**
350 * public method for finding attribute definition by name.
351 * @param attval AttVal instance
352 * @return Attribute with name = attval.name
353 */
354 public Attribute findAttribute(AttVal attval)
355 {
356 Attribute np;
357
358 if (attval.attribute != null)
359 {
360 np = lookup(attval.attribute);
361 return np;
362 }
363
364 return null;
365 }
366
367 /**
368 * Does the given attibute contains an url?
369 * @param attrname attribute name
370 * @return <code>true</code> if the given attribute is expected to contain an URL
371 */
372 public boolean isUrl(String attrname)
373 {
374 Attribute np;
375
376 np = lookup(attrname);
377 return (np != null && np.getAttrchk() == AttrCheckImpl.URL);
378 }
379
380 /**
381 * Does the given attibute contains a script?
382 * @param attrname attribute name
383 * @return <code>true</code> if the given attribute is expected to contain a script
384 */
385 public boolean isScript(String attrname)
386 {
387 Attribute np;
388
389 np = lookup(attrname);
390 return (np != null && np.getAttrchk() == AttrCheckImpl.SCRIPT);
391 }
392
393 /**
394 * Does the given attibute contains a literal attribute?
395 * @param attrname attribute name
396 * @return <code>true</code> if the given attribute is expected to contain a literal attribute
397 */
398 public boolean isLiteralAttribute(String attrname)
399 {
400 Attribute np;
401
402 np = lookup(attrname);
403 return (np != null && np.isLiteral());
404 }
405
406 /**
407 * Declare a new literal attribute.
408 * @param name atribute name
409 */
410 public void declareLiteralAttrib(String name)
411 {
412
413
414
415 Attribute attrib = lookup(name);
416
417 if (attrib == null)
418 {
419 attrib = install(new Attribute(name, Dict.VERS_PROPRIETARY, null));
420 }
421
422 attrib.setLiteral(true);
423 }
424
425 /**
426 * Returns the default attribute table instance.
427 * @return AttributeTable instance
428 */
429 public static AttributeTable getDefaultAttributeTable()
430 {
431 if (defaultAttributeTable == null)
432 {
433 defaultAttributeTable = new AttributeTable();
434 for (int i = 0; i < ATTRS.length; i++)
435 {
436 defaultAttributeTable.install(ATTRS[i]);
437 }
438 attrHref = defaultAttributeTable.lookup("href");
439 attrSrc = defaultAttributeTable.lookup("src");
440 attrId = defaultAttributeTable.lookup("id");
441 attrName = defaultAttributeTable.lookup("name");
442 attrSummary = defaultAttributeTable.lookup("summary");
443 attrAlt = defaultAttributeTable.lookup("alt");
444 attrLongdesc = defaultAttributeTable.lookup("longdesc");
445 attrUsemap = defaultAttributeTable.lookup("usemap");
446 attrIsmap = defaultAttributeTable.lookup("ismap");
447 attrLanguage = defaultAttributeTable.lookup("language");
448 attrType = defaultAttributeTable.lookup("type");
449 attrTitle = defaultAttributeTable.lookup("title");
450 attrXmlns = defaultAttributeTable.lookup("xmlns");
451 attrValue = defaultAttributeTable.lookup("value");
452 attrContent = defaultAttributeTable.lookup("content");
453 attrDatafld = defaultAttributeTable.lookup("datafld");
454 attrWidth = defaultAttributeTable.lookup("width");
455 attrHeight = defaultAttributeTable.lookup("height");
456
457 attrAlt.setNowrap(true);
458 attrValue.setNowrap(true);
459 attrContent.setNowrap(true);
460 }
461 return defaultAttributeTable;
462 }
463
464 }