KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > w3c > tidy > CheckAttribsImpl


1 /*
2  * @(#)CheckAttribsImpl.java 1.11 2000/08/16
3  *
4  */

5
6 package org.w3c.tidy;
7
8 /**
9  *
10  * Check HTML attributes implementation
11  *
12  * (c) 1998-2000 (W3C) MIT, INRIA, Keio University
13  * See Tidy.java for the copyright notice.
14  * Derived from <a HREF="http://www.w3.org/People/Raggett/tidy">
15  * HTML Tidy Release 4 Aug 2000</a>
16  *
17  * @author Dave Raggett <dsr@w3.org>
18  * @author Andy Quick <ac.quick@sympatico.ca> (translation to Java)
19  * @version 1.0, 1999/05/22
20  * @version 1.0.1, 1999/05/29
21  * @version 1.1, 1999/06/18 Java Bean
22  * @version 1.2, 1999/07/10 Tidy Release 7 Jul 1999
23  * @version 1.3, 1999/07/30 Tidy Release 26 Jul 1999
24  * @version 1.4, 1999/09/04 DOM support
25  * @version 1.5, 1999/10/23 Tidy Release 27 Sep 1999
26  * @version 1.6, 1999/11/01 Tidy Release 22 Oct 1999
27  * @version 1.7, 1999/12/06 Tidy Release 30 Nov 1999
28  * @version 1.8, 2000/01/22 Tidy Release 13 Jan 2000
29  * @version 1.9, 2000/06/03 Tidy Release 30 Apr 2000
30  * @version 1.10, 2000/07/22 Tidy Release 8 Jul 2000
31  * @version 1.11, 2000/08/16 Tidy Release 4 Aug 2000
32  */

33
34 public class CheckAttribsImpl {
35
36     public static class CheckHTML implements CheckAttribs {
37
38         public void check( Lexer lexer, Node node )
39         {
40             AttVal attval;
41             Attribute attribute;
42
43             node.checkUniqueAttributes(lexer);
44
45             for (attval = node.attributes; attval != null; attval = attval.next)
46             {
47                 attribute = attval.checkAttribute(lexer, node );
48
49                 if (attribute == AttributeTable.attrXmlns)
50                     lexer.isvoyager = true;
51             }
52         }
53
54     };
55
56     public static class CheckSCRIPT implements CheckAttribs {
57
58         public void check( Lexer lexer, Node node )
59         {
60             Attribute attribute;
61             AttVal lang, type;
62
63             node.checkUniqueAttributes(lexer);
64
65             lang = node.getAttrByName("language");
66             type = node.getAttrByName("type");
67
68             if (type == null)
69             {
70                 Report.attrError(lexer, node, "type", Report.MISSING_ATTRIBUTE);
71
72                 /* check for javascript */
73
74                 if (lang != null)
75                 {
76                     String JavaDoc str = lang.value;
77                     if (str.length() > 10)
78                         str = str.substring(0, 10);
79                     if ( (Lexer.wstrcasecmp(str, "javascript") == 0) ||
80                          (Lexer.wstrcasecmp(str, "jscript") == 0) )
81                     {
82                         node.addAttribute("type", "text/javascript");
83                     }
84                 }
85                 else
86                     node.addAttribute("type", "text/javascript");
87             }
88         }
89
90     };
91
92     public static class CheckTABLE implements CheckAttribs {
93
94         public void check( Lexer lexer, Node node )
95         {
96             AttVal attval;
97             Attribute attribute;
98             boolean hasSummary = false;
99
100             node.checkUniqueAttributes(lexer);
101
102             for (attval = node.attributes; attval != null; attval = attval.next)
103             {
104                 attribute = attval.checkAttribute(lexer, node);
105
106                 if (attribute == AttributeTable.attrSummary)
107                     hasSummary = true;
108             }
109
110             /* suppress warning for missing summary for HTML 2.0 and HTML 3.2 */
111             if (!hasSummary && lexer.doctype != Dict.VERS_HTML20 && lexer.doctype != Dict.VERS_HTML32)
112             {
113                 lexer.badAccess |= Report.MISSING_SUMMARY;
114                 Report.attrError(lexer, node, "summary", Report.MISSING_ATTRIBUTE);
115             }
116
117             /* convert <table border> to <table border="1"> */
118             if (lexer.configuration.XmlOut)
119             {
120                 attval = node.getAttrByName("border");
121                 if (attval != null)
122                 {
123                     if (attval.value == null)
124                         attval.value = "1";
125                 }
126             }
127         }
128
129     };
130
131     public static class CheckCaption implements CheckAttribs {
132
133         public void check( Lexer lexer, Node node )
134         {
135             AttVal attval;
136             String JavaDoc value = null;
137
138             node.checkUniqueAttributes(lexer);
139
140             for (attval = node.attributes; attval != null; attval = attval.next)
141             {
142                 if ( Lexer.wstrcasecmp(attval.attribute, "align") == 0 )
143                 {
144                     value = attval.value;
145                     break;
146                 }
147             }
148
149             if (value != null)
150             {
151                 if (Lexer.wstrcasecmp(value, "left") == 0 || Lexer.wstrcasecmp(value, "right") == 0)
152                     lexer.versions &= (short)(Dict.VERS_HTML40_LOOSE|Dict.VERS_FRAMES);
153                 else if (Lexer.wstrcasecmp(value, "top") == 0 || Lexer.wstrcasecmp(value, "bottom") == 0)
154                     lexer.versions &= Dict.VERS_FROM32;
155                 else
156                     Report.attrError(lexer, node, value, Report.BAD_ATTRIBUTE_VALUE);
157             }
158         }
159
160     };
161
162     public static class CheckHR implements CheckAttribs {
163
164         public void check( Lexer lexer, Node node )
165         {
166             if (node.getAttrByName("src") != null)
167                 Report.attrError(lexer, node, "src", Report.PROPRIETARY_ATTR_VALUE);
168         }
169     };
170
171     public static class CheckIMG implements CheckAttribs {
172
173         public void check( Lexer lexer, Node node )
174         {
175             AttVal attval;
176             Attribute attribute;
177             boolean hasAlt = false;
178             boolean hasSrc = false;
179             boolean hasUseMap = false;
180             boolean hasIsMap = false;
181             boolean hasDataFld = false;
182
183             node.checkUniqueAttributes(lexer);
184
185             for (attval = node.attributes; attval != null; attval = attval.next)
186             {
187                 attribute = attval.checkAttribute( lexer, node );
188
189                 if (attribute == AttributeTable.attrAlt)
190                     hasAlt = true;
191                 else if (attribute == AttributeTable.attrSrc)
192                     hasSrc = true;
193                 else if (attribute == AttributeTable.attrUsemap)
194                     hasUseMap = true;
195                 else if (attribute == AttributeTable.attrIsmap)
196                     hasIsMap = true;
197                 else if (attribute == AttributeTable.attrDatafld)
198                     hasDataFld = true;
199                 else if (attribute == AttributeTable.attrWidth ||
200                          attribute == AttributeTable.attrHeight)
201                     lexer.versions &= ~Dict.VERS_HTML20;
202             }
203
204             if (!hasAlt)
205             {
206                 lexer.badAccess |= Report.MISSING_IMAGE_ALT;
207                 Report.attrError(lexer, node, "alt", Report.MISSING_ATTRIBUTE);
208                 if (lexer.configuration.altText != null)
209                     node.addAttribute("alt", lexer.configuration.altText);
210             }
211
212             if (!hasSrc && !hasDataFld)
213                 Report.attrError(lexer, node, "src", Report.MISSING_ATTRIBUTE);
214
215             if (hasIsMap && !hasUseMap)
216                 Report.attrError(lexer, node, "ismap", Report.MISSING_IMAGEMAP);
217         }
218
219     };
220
221     public static class CheckAREA implements CheckAttribs {
222
223         public void check( Lexer lexer, Node node )
224         {
225             AttVal attval;
226             Attribute attribute;
227             boolean hasAlt = false;
228             boolean hasHref = false;
229
230             node.checkUniqueAttributes(lexer);
231
232             for (attval = node.attributes; attval != null; attval = attval.next)
233             {
234                 attribute = attval.checkAttribute( lexer, node );
235
236                 if (attribute == AttributeTable.attrAlt)
237                     hasAlt = true;
238                 else if (attribute == AttributeTable.attrHref)
239                     hasHref = true;
240             }
241
242             if (!hasAlt)
243             {
244                 lexer.badAccess |= Report.MISSING_LINK_ALT;
245                 Report.attrError(lexer, node, "alt", Report.MISSING_ATTRIBUTE);
246             }
247             if (!hasHref)
248                 Report.attrError(lexer, node, "href", Report.MISSING_ATTRIBUTE);
249         }
250
251     };
252
253     public static class CheckAnchor implements CheckAttribs {
254
255         public void check( Lexer lexer, Node node )
256         {
257             node.checkUniqueAttributes(lexer);
258
259             lexer.fixId(node);
260         }
261     };
262
263     public static class CheckMap implements CheckAttribs {
264
265         public void check( Lexer lexer, Node node )
266         {
267             node.checkUniqueAttributes(lexer);
268
269             lexer.fixId(node);
270         }
271     }
272
273     public static class CheckSTYLE implements CheckAttribs {
274
275         public void check( Lexer lexer, Node node )
276         {
277             AttVal type = node.getAttrByName("type");
278
279             node.checkUniqueAttributes(lexer);
280
281             if (type == null)
282             {
283                 Report.attrError(lexer, node, "type", Report.MISSING_ATTRIBUTE);
284
285                 node.addAttribute("type", "text/css");
286             }
287         }
288     }
289
290     public static class CheckTableCell implements CheckAttribs {
291
292         public void check( Lexer lexer, Node node )
293         {
294             node.checkUniqueAttributes(lexer);
295
296             /*
297               HTML4 strict doesn't allow mixed content for
298               elements with %block; as their content model
299             */

300             if (node.getAttrByName("width") != null || node.getAttrByName("height") != null)
301                 lexer.versions &= ~Dict.VERS_HTML40_STRICT;
302         }
303     }
304
305     /* add missing type attribute when appropriate */
306     public static class CheckLINK implements CheckAttribs {
307
308         public void check( Lexer lexer, Node node )
309         {
310             AttVal rel = node.getAttrByName("rel");
311
312             node.checkUniqueAttributes(lexer);
313
314             if (rel != null && rel.value != null &&
315                   rel.value.equals("stylesheet"))
316             {
317                 AttVal type = node.getAttrByName("type");
318
319                 if (type == null)
320                 {
321                     Report.attrError(lexer, node, "type", Report.MISSING_ATTRIBUTE);
322
323                     node.addAttribute("type", "text/css");
324                 }
325             }
326         }
327     }
328
329     public static CheckAttribs getCheckHTML()
330     {
331         return _checkHTML;
332     }
333
334     public static CheckAttribs getCheckSCRIPT()
335     {
336         return _checkSCRIPT;
337     }
338
339     public static CheckAttribs getCheckTABLE()
340     {
341         return _checkTABLE;
342     }
343
344     public static CheckAttribs getCheckCaption()
345     {
346         return _checkCaption;
347     }
348
349     public static CheckAttribs getCheckIMG()
350     {
351         return _checkIMG;
352     }
353
354     public static CheckAttribs getCheckAREA()
355     {
356         return _checkAREA;
357     }
358
359     public static CheckAttribs getCheckAnchor()
360     {
361         return _checkAnchor;
362     }
363
364     public static CheckAttribs getCheckMap()
365     {
366         return _checkMap;
367     }
368
369     public static CheckAttribs getCheckSTYLE()
370     {
371         return _checkStyle;
372     }
373
374     public static CheckAttribs getCheckTableCell()
375     {
376         return _checkTableCell;
377     }
378
379     public static CheckAttribs getCheckLINK()
380     {
381         return _checkLINK;
382     }
383
384     public static CheckAttribs getCheckHR()
385     {
386         return _checkHR;
387     }
388
389
390     private static CheckAttribs _checkHTML = new CheckHTML();
391     private static CheckAttribs _checkSCRIPT = new CheckSCRIPT();
392     private static CheckAttribs _checkTABLE = new CheckTABLE();
393     private static CheckAttribs _checkCaption = new CheckCaption();
394     private static CheckAttribs _checkIMG = new CheckIMG();
395     private static CheckAttribs _checkAREA = new CheckAREA();
396     private static CheckAttribs _checkAnchor = new CheckAnchor();
397     private static CheckAttribs _checkMap = new CheckMap();
398     private static CheckAttribs _checkStyle = new CheckSTYLE();
399     private static CheckAttribs _checkTableCell = new CheckTableCell();
400     private static CheckAttribs _checkLINK = new CheckLINK();
401     private static CheckAttribs _checkHR = new CheckHR();
402
403 }
404
Popular Tags