1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45:
46: import ;
47: import ;
48:
49: import ;
50:
51:
55: public class HTML
56: {
57:
60: public static class Attribute
61: implements Serializable
62: {
63:
66: public static final Attribute ACTION = new Attribute("action");
67:
68:
71: public static final Attribute ALIGN = new Attribute("align");
72:
73:
76: public static final Attribute ALINK = new Attribute("alink");
77:
78:
81: public static final Attribute ALT = new Attribute("alt");
82:
83:
86: public static final Attribute ARCHIVE = new Attribute("archive");
87:
88:
91: public static final Attribute BACKGROUND = new Attribute("background");
92:
93:
96: public static final Attribute BGCOLOR = new Attribute("bgcolor");
97:
98:
101: public static final Attribute BORDER = new Attribute("border");
102:
103:
106: public static final Attribute CELLPADDING = new Attribute("cellpadding");
107:
108:
111: public static final Attribute CELLSPACING = new Attribute("cellspacing");
112:
113:
116: public static final Attribute CHECKED = new Attribute("checked");
117:
118:
121: public static final Attribute CLASS = new Attribute("class");
122:
123:
126: public static final Attribute CLASSID = new Attribute("classid");
127:
128:
131: public static final Attribute CLEAR = new Attribute("clear");
132:
133:
136: public static final Attribute CODE = new Attribute("code");
137:
138:
141: public static final Attribute CODEBASE = new Attribute("codebase");
142:
143:
146: public static final Attribute CODETYPE = new Attribute("codetype");
147:
148:
151: public static final Attribute COLOR = new Attribute("color");
152:
153:
156: public static final Attribute COLS = new Attribute("cols");
157:
158:
161: public static final Attribute COLSPAN = new Attribute("colspan");
162:
163:
166: public static final Attribute COMMENT = new Attribute("comment");
167:
168:
171: public static final Attribute COMPACT = new Attribute("compact");
172:
173:
176: public static final Attribute CONTENT = new Attribute("content");
177:
178:
181: public static final Attribute COORDS = new Attribute("coords");
182:
183:
186: public static final Attribute DATA = new Attribute("data");
187:
188:
191: public static final Attribute DECLARE = new Attribute("declare");
192:
193:
196: public static final Attribute DIR = new Attribute("dir");
197:
198:
201: public static final Attribute DUMMY = new Attribute("dummy");
202:
203:
206: public static final Attribute ENCTYPE = new Attribute("enctype");
207:
208:
211: public static final Attribute ENDTAG = new Attribute("endtag");
212:
213:
216: public static final Attribute FACE = new Attribute("face");
217:
218:
221: public static final Attribute FRAMEBORDER = new Attribute("frameborder");
222:
223:
226: public static final Attribute HALIGN = new Attribute("halign");
227:
228:
231: public static final Attribute HEIGHT = new Attribute("height");
232:
233:
236: public static final Attribute HREF = new Attribute("href");
237:
238:
241: public static final Attribute HSPACE = new Attribute("hspace");
242:
243:
246: public static final Attribute HTTPEQUIV = new Attribute("http-equiv");
247:
248:
251: public static final Attribute ID = new Attribute("id");
252:
253:
256: public static final Attribute ISMAP = new Attribute("ismap");
257:
258:
261: public static final Attribute LANG = new Attribute("lang");
262:
263:
266: public static final Attribute LANGUAGE = new Attribute("language");
267:
268:
271: public static final Attribute LINK = new Attribute("link");
272:
273:
276: public static final Attribute LOWSRC = new Attribute("lowsrc");
277:
278:
281: public static final Attribute MARGINHEIGHT = new Attribute("marginheight");
282:
283:
286: public static final Attribute MARGINWIDTH = new Attribute("marginwidth");
287:
288:
291: public static final Attribute MAXLENGTH = new Attribute("maxlength");
292:
293:
296: public static final Attribute MEDIA = new Attribute("media");
297:
298:
301: public static final Attribute METHOD = new Attribute("method");
302:
303:
306: public static final Attribute MULTIPLE = new Attribute("multiple");
307:
308:
311: public static final Attribute N = new Attribute("n");
312:
313:
316: public static final Attribute NAME = new Attribute("name");
317:
318:
321: public static final Attribute NOHREF = new Attribute("nohref");
322:
323:
326: public static final Attribute NORESIZE = new Attribute("noresize");
327:
328:
331: public static final Attribute NOSHADE = new Attribute("noshade");
332:
333:
336: public static final Attribute NOWRAP = new Attribute("nowrap");
337:
338:
341: public static final Attribute PROMPT = new Attribute("prompt");
342:
343:
346: public static final Attribute REL = new Attribute("rel");
347:
348:
351: public static final Attribute REV = new Attribute("rev");
352:
353:
356: public static final Attribute ROWS = new Attribute("rows");
357:
358:
361: public static final Attribute ROWSPAN = new Attribute("rowspan");
362:
363:
366: public static final Attribute SCROLLING = new Attribute("scrolling");
367:
368:
371: public static final Attribute SELECTED = new Attribute("selected");
372:
373:
376: public static final Attribute SHAPE = new Attribute("shape");
377:
378:
381: public static final Attribute SHAPES = new Attribute("shapes");
382:
383:
386: public static final Attribute SIZE = new Attribute("size");
387:
388:
391: public static final Attribute SRC = new Attribute("src");
392:
393:
396: public static final Attribute STANDBY = new Attribute("standby");
397:
398:
401: public static final Attribute START = new Attribute("start");
402:
403:
406: public static final Attribute STYLE = new Attribute("style");
407:
408:
411: public static final Attribute TARGET = new Attribute("target");
412:
413:
416: public static final Attribute TEXT = new Attribute("text");
417:
418:
421: public static final Attribute TITLE = new Attribute("title");
422:
423:
426: public static final Attribute TYPE = new Attribute("type");
427:
428:
431: public static final Attribute USEMAP = new Attribute("usemap");
432:
433:
436: public static final Attribute VALIGN = new Attribute("valign");
437:
438:
441: public static final Attribute VALUE = new Attribute("value");
442:
443:
446: public static final Attribute VALUETYPE = new Attribute("valuetype");
447:
448:
451: public static final Attribute VERSION = new Attribute("version");
452:
453:
456: public static final Attribute VLINK = new Attribute("vlink");
457:
458:
461: public static final Attribute VSPACE = new Attribute("vspace");
462:
463:
466: public static final Attribute WIDTH = new Attribute("width");
467: private final String name;
468:
469:
472: protected Attribute(String a_name)
473: {
474: name = a_name;
475: }
476:
477:
480: public int compareTo(Object other)
481: {
482: return name.compareTo(((Attribute) other).name);
483: }
484:
485:
489: public boolean equals(Object other)
490: {
491: if (other == this)
492: return true;
493:
494: if (!(other instanceof Attribute))
495: return false;
496:
497: Attribute that = (Attribute) other;
498:
499: return that.name.equalsIgnoreCase(name);
500: }
501:
502:
505: public int hashCode()
506: {
507: return name == null ? 0 : name.hashCode();
508: }
509:
510:
514: public String toString()
515: {
516: return name;
517: }
518:
519:
524: static Attribute[] getAllAttributes()
525: {
526: Field[] f = Attribute.class.getFields();
527: Attribute[] attrs = new Attribute[ f.length ];
528: Field x;
529: int p = 0;
530: Attribute a;
531:
532: for (int i = 0; i < f.length; i++)
533: {
534: x = f [ i ];
535:
536: if ((x.getModifiers() & Modifier.STATIC) != 0)
537: {
538: if (x.getType().equals(Attribute.class))
539: {
540: try
541: {
542: a = (Attribute) x.get(null);
543: attrs [ p++ ] = a;
544: }
545: catch (Exception ex)
546: {
547: ex.printStackTrace(System.err);
548: throw new Error("This should never happen, report a bug");
549: }
550: }
551: }
552: }
553:
554: return attrs;
555: }
556: }
557:
558:
561: public static class Tag
562: implements Comparable, Serializable
563: {
564:
567: public static final Tag A = new Tag("a");
568:
569:
572: public static final Tag ADDRESS = new Tag("address");
573:
574:
577: public static final Tag APPLET = new Tag("applet");
578:
579:
582: public static final Tag AREA = new Tag("area");
583:
584:
587: public static final Tag B = new Tag("b");
588:
589:
592: public static final Tag BASE = new Tag("base");
593:
594:
597: public static final Tag BASEFONT = new Tag("basefont");
598:
599:
602: public static final Tag BIG = new Tag("big");
603:
604:
607: public static final Tag BLOCKQUOTE = new Tag("blockquote", BREAKS | BLOCK);
608:
609:
612: public static final Tag BODY = new Tag("body", BREAKS | BLOCK);
613:
614:
617: public static final Tag BR = new Tag("br", BREAKS);
618:
619:
622: public static final Tag CAPTION = new Tag("caption");
623:
624:
627: public static final Tag CENTER = new Tag("center", BREAKS);
628:
629:
632: public static final Tag CITE = new Tag("cite");
633:
634:
637: public static final Tag CODE = new Tag("code");
638:
639:
642: public static final Tag DD = new Tag("dd", BREAKS | BLOCK);
643:
644:
647: public static final Tag DFN = new Tag("dfn");
648:
649:
652: public static final Tag DIR = new Tag("dir", BREAKS | BLOCK);
653:
654:
657: public static final Tag DIV = new Tag("div", BREAKS | BLOCK);
658:
659:
662: public static final Tag DL = new Tag("dl", BREAKS | BLOCK);
663:
664:
667: public static final Tag DT = new Tag("dt", BREAKS | BLOCK);
668:
669:
672: public static final Tag EM = new Tag("em");
673:
674:
677: public static final Tag FONT = new Tag("font");
678:
679:
682: public static final Tag FORM = new Tag("form", BREAKS);
683:
684:
687: public static final Tag FRAME = new Tag("frame");
688:
689:
692: public static final Tag FRAMESET = new Tag("frameset");
693:
694:
697: public static final Tag H1 = new Tag("h1", BREAKS | BLOCK);
698:
699:
702: public static final Tag H2 = new Tag("h2", BREAKS | BLOCK);
703:
704:
707: public static final Tag H3 = new Tag("h3", BREAKS | BLOCK);
708:
709:
712: public static final Tag H4 = new Tag("h4", BREAKS | BLOCK);
713:
714:
717: public static final Tag H5 = new Tag("h5", BREAKS | BLOCK);
718:
719:
722: public static final Tag H6 = new Tag("h6", BREAKS | BLOCK);
723:
724:
727: public static final Tag HEAD = new Tag("head", BREAKS | BLOCK);
728:
729:
732: public static final Tag HR = new Tag("hr", BREAKS);
733:
734:
737: public static final Tag HTML = new Tag("html", BREAKS);
738:
739:
742: public static final Tag I = new Tag("i");
743:
744:
747: public static final Tag IMG = new Tag("img");
748:
749:
752: public static final Tag INPUT = new Tag("input");
753:
754:
757: public static final Tag ISINDEX = new Tag("isindex", BREAKS);
758:
759:
762: public static final Tag KBD = new Tag("kbd");
763:
764:
767: public static final Tag LI = new Tag("li", BREAKS | BLOCK);
768:
769:
772: public static final Tag LINK = new Tag("link");
773:
774:
777: public static final Tag MAP = new Tag("map");
778:
779:
782: public static final Tag MENU = new Tag("menu", BREAKS | BLOCK);
783:
784:
787: public static final Tag META = new Tag("meta");
788:
789:
792: public static final Tag NOBR = new Tag("nobr");
793:
794:
797: public static final Tag NOFRAMES = new Tag("noframes", BREAKS | BLOCK);
798:
799:
802: public static final Tag OBJECT = new Tag("object");
803:
804:
807: public static final Tag OL = new Tag("ol", BREAKS | BLOCK);
808:
809:
812: public static final Tag OPTION = new Tag("option");
813:
814:
817: public static final Tag P = new Tag("p", BREAKS | BLOCK);
818:
819:
822: public static final Tag PARAM = new Tag("param");
823:
824:
827: public static final Tag PRE = new Tag("pre", BREAKS | BLOCK | PREFORMATTED);
828:
829:
832: public static final Tag S = new Tag("s");
833:
834:
837: public static final Tag SAMP = new Tag("samp");
838:
839:
842: public static final Tag SCRIPT = new Tag("script");
843:
844:
847: public static final Tag SELECT = new Tag("select");
848:
849:
852: public static final Tag SMALL = new Tag("small");
853:
854:
857: public static final Tag SPAN = new Tag("span");
858:
859:
862: public static final Tag STRIKE = new Tag("strike");
863:
864:
867: public static final Tag STRONG = new Tag("strong");
868:
869:
872: public static final Tag STYLE = new Tag("style");
873:
874:
877: public static final Tag SUB = new Tag("sub");
878:
879:
882: public static final Tag SUP = new Tag("sup");
883:
884:
887: public static final Tag TABLE = new Tag("table", BLOCK);
888:
889:
892: public static final Tag TD = new Tag("td", BREAKS | BLOCK);
893:
894:
897: public static final Tag TEXTAREA = new Tag("textarea", PREFORMATTED);
898:
899:
902: public static final Tag TH = new Tag("th", BREAKS | BLOCK);
903:
904:
907: public static final Tag TITLE = new Tag("title", BREAKS | BLOCK);
908:
909:
912: public static final Tag TR = new Tag("tr", BLOCK);
913:
914:
917: public static final Tag TT = new Tag("tt");
918:
919:
922: public static final Tag U = new Tag("u");
923:
924:
927: public static final Tag UL = new Tag("ul", BREAKS | BLOCK);
928:
929:
932: public static final Tag VAR = new Tag("var");
933:
934:
935:
936:
941: private static final int TOTAL_SYNTHETIC_TAGS = 3;
942:
943:
948: public static final Tag COMMENT = new Tag("comment", SYNTHETIC);
949:
950:
955: public static final Tag CONTENT = new Tag("content", SYNTHETIC);
956:
957:
963: public static final Tag IMPLIED = new Tag("p-implied", SYNTHETIC);
964: final String name;
965: final int flags;
966:
967:
976: public Tag()
977: {
978: name = null;
979: flags = 0;
980: }
981:
982:
986: protected Tag(String id)
987: {
988: name = id;
989: flags = 0;
990: }
991:
992:
996: protected Tag(String id, boolean causesBreak, boolean isBlock)
997: {
998: int f = 0;
999:
1000: if (causesBreak)
1001: {
1002: f |= BREAKS;
1003: }
1004:
1005: if (isBlock)
1006: {
1007: f |= BLOCK;
1008: }
1009:
1010: flags = f;
1011: name = id;
1012: }
1013:
1014:
1017: Tag(String id, int a_flags)
1018: {
1019: name = id;
1020: flags = a_flags;
1021: }
1022:
1023:
1027: public boolean isBlock()
1028: {
1029: return (flags & BLOCK) != 0;
1030: }
1031:
1032:
1036: public boolean isPreformatted()
1037: {
1038: return (flags & PREFORMATTED) != 0;
1039: }
1040:
1041:
1044: public boolean breaksFlow()
1045: {
1046: return (flags & BREAKS) != 0;
1047: }
1048:
1049:
1052: public int compareTo(Object other)
1053: {
1054: return name.compareTo(((Tag) other).name);
1055: }
1056:
1057:
1060: public boolean equals(Object other)
1061: {
1062: if (other == this)
1063: {
1064: return true;
1065: }
1066:
1067: if (!(other instanceof Tag))
1068: {
1069: return false;
1070: }
1071:
1072: Tag that = (Tag) other;
1073:
1074: return that.name.equalsIgnoreCase(name);
1075: }
1076:
1077:
1080: public int hashCode()
1081: {
1082: return name == null ? 0 : name.hashCode();
1083: }
1084:
1085:
1089: public String toString()
1090: {
1091: return name;
1092: }
1093:
1094:
1099: static Tag[] getAllTags()
1100: {
1101: Field[] f = Tag.class.getFields();
1102: Field x;
1103:
1104:
1105: Tag[] tags = new Tag[ f.length - TOTAL_SYNTHETIC_TAGS ];
1106: int p = 0;
1107: Tag t;
1108:
1109: for (int i = 0; i < f.length; i++)
1110: {
1111: x = f [ i ];
1112:
1113: if ((x.getModifiers() & Modifier.STATIC) != 0)
1114: {
1115: if (x.getType().equals(Tag.class))
1116: {
1117: try
1118: {
1119: t = (Tag) x.get(null);
1120:
1121: if (!t.isSyntetic())
1122: {
1123: tags [ p++ ] = t;
1124: }
1125: }
1126: catch (IllegalAccessException ex)
1127: {
1128: unexpected(ex);
1129: }
1130: catch (IllegalArgumentException ex)
1131: {
1132: unexpected(ex);
1133: }
1134: }
1135: }
1136: }
1137:
1138: return tags;
1139: }
1140:
1141:
1145: boolean isSyntetic()
1146: {
1147: return (flags & SYNTHETIC) != 0;
1148: }
1149:
1150: private static void unexpected(Exception ex)
1151: throws Error
1152: {
1153: throw new Error("This should never happen, report a bug", ex);
1154: }
1155: }
1156:
1157:
1161: public static class UnknownTag
1162: extends Tag
1163: implements Serializable
1164: {
1165: private static final long serialVersionUID = -1534369342247250625L;
1166:
1167:
1172: public UnknownTag(String name)
1173: {
1174: super(name);
1175: }
1176: }
1177:
1178:
1182: public static final String NULL_ATTRIBUTE_VALUE = "#DEFAULT";
1183:
1184:
1185: static final int BREAKS = 1;
1186: static final int BLOCK = 2;
1187: static final int PREFORMATTED = 4;
1188: static final int SYNTHETIC = 8;
1189: private static Map tagMap;
1190: private static Map attrMap;
1191:
1192:
1197: public HTML()
1198: {
1199:
1200: }
1201:
1202:
1205: public static HTML.Attribute[] getAllAttributeKeys()
1206: {
1207: return Attribute.getAllAttributes();
1208: }
1209:
1210:
1215: public static HTML.Tag[] getAllTags()
1216: {
1217: return Tag.getAllTags();
1218: }
1219:
1220:
1224: public static Attribute getAttributeKey(String attName)
1225: {
1226: if (attrMap == null)
1227: {
1228:
1229: attrMap = new TreeMap();
1230:
1231: Attribute[] attrs = getAllAttributeKeys();
1232:
1233: for (int i = 0; i < attrs.length; i++)
1234: {
1235: attrMap.put(attrs [ i ].toString(), attrs [ i ]);
1236: }
1237: }
1238:
1239: return (Attribute) attrMap.get(attName.toLowerCase());
1240: }
1241:
1242:
1266: public static int getIntegerAttributeValue(AttributeSet set,
1267: HTML.Attribute attribute,
1268: int defaultValue
1269: )
1270: {
1271: Object v = set.getAttribute(attribute);
1272:
1273: if (v == null)
1274: {
1275: return defaultValue;
1276: }
1277:
1278: try
1279: {
1280: return Integer.parseInt(v.toString().trim());
1281: }
1282: catch (Exception ex)
1283: {
1284: return defaultValue;
1285: }
1286: }
1287:
1288:
1293: public static Tag getTag(String tagName)
1294: {
1295: if (tagMap == null)
1296: {
1297:
1298: tagMap = new TreeMap();
1299:
1300: Tag[] tags = getAllTags();
1301:
1302: for (int i = 0; i < tags.length; i++)
1303: {
1304: tagMap.put(tags [ i ].toString(), tags [ i ]);
1305: }
1306: }
1307:
1308: return (Tag) tagMap.get(tagName.toLowerCase());
1309: }
1310: }