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 final class Attribute
61: {
62:
65: public static final Attribute ACTION = new Attribute("action");
66:
67:
70: public static final Attribute ALIGN = new Attribute("align");
71:
72:
75: public static final Attribute ALINK = new Attribute("alink");
76:
77:
80: public static final Attribute ALT = new Attribute("alt");
81:
82:
85: public static final Attribute ARCHIVE = new Attribute("archive");
86:
87:
90: public static final Attribute BACKGROUND = new Attribute("background");
91:
92:
95: public static final Attribute BGCOLOR = new Attribute("bgcolor");
96:
97:
100: public static final Attribute BORDER = new Attribute("border");
101:
102:
105: public static final Attribute CELLPADDING = new Attribute("cellpadding");
106:
107:
110: public static final Attribute CELLSPACING = new Attribute("cellspacing");
111:
112:
115: public static final Attribute CHECKED = new Attribute("checked");
116:
117:
120: public static final Attribute CLASS = new Attribute("class");
121:
122:
125: public static final Attribute CLASSID = new Attribute("classid");
126:
127:
130: public static final Attribute CLEAR = new Attribute("clear");
131:
132:
135: public static final Attribute CODE = new Attribute("code");
136:
137:
140: public static final Attribute CODEBASE = new Attribute("codebase");
141:
142:
145: public static final Attribute CODETYPE = new Attribute("codetype");
146:
147:
150: public static final Attribute COLOR = new Attribute("color");
151:
152:
155: public static final Attribute COLS = new Attribute("cols");
156:
157:
160: public static final Attribute COLSPAN = new Attribute("colspan");
161:
162:
165: public static final Attribute COMMENT = new Attribute("comment");
166:
167:
170: public static final Attribute COMPACT = new Attribute("compact");
171:
172:
175: public static final Attribute CONTENT = new Attribute("content");
176:
177:
180: public static final Attribute COORDS = new Attribute("coords");
181:
182:
185: public static final Attribute DATA = new Attribute("data");
186:
187:
190: public static final Attribute DECLARE = new Attribute("declare");
191:
192:
195: public static final Attribute DIR = new Attribute("dir");
196:
197:
200: public static final Attribute DUMMY = new Attribute("dummy");
201:
202:
205: public static final Attribute ENCTYPE = new Attribute("enctype");
206:
207:
210: public static final Attribute ENDTAG = new Attribute("endtag");
211:
212:
215: public static final Attribute FACE = new Attribute("face");
216:
217:
220: public static final Attribute FRAMEBORDER = new Attribute("frameborder");
221:
222:
225: public static final Attribute HALIGN = new Attribute("halign");
226:
227:
230: public static final Attribute HEIGHT = new Attribute("height");
231:
232:
235: public static final Attribute HREF = new Attribute("href");
236:
237:
240: public static final Attribute HSPACE = new Attribute("hspace");
241:
242:
245: public static final Attribute HTTPEQUIV = new Attribute("http-equiv");
246:
247:
250: public static final Attribute ID = new Attribute("id");
251:
252:
255: public static final Attribute ISMAP = new Attribute("ismap");
256:
257:
260: public static final Attribute LANG = new Attribute("lang");
261:
262:
265: public static final Attribute LANGUAGE = new Attribute("language");
266:
267:
270: public static final Attribute LINK = new Attribute("link");
271:
272:
275: public static final Attribute LOWSRC = new Attribute("lowsrc");
276:
277:
280: public static final Attribute MARGINHEIGHT = new Attribute("marginheight");
281:
282:
285: public static final Attribute MARGINWIDTH = new Attribute("marginwidth");
286:
287:
290: public static final Attribute MAXLENGTH = new Attribute("maxlength");
291:
292:
295: static final Attribute MEDIA = new Attribute("media");
296:
297:
300: public static final Attribute METHOD = new Attribute("method");
301:
302:
305: public static final Attribute MULTIPLE = new Attribute("multiple");
306:
307:
310: public static final Attribute N = new Attribute("n");
311:
312:
315: public static final Attribute NAME = new Attribute("name");
316:
317:
320: public static final Attribute NOHREF = new Attribute("nohref");
321:
322:
325: public static final Attribute NORESIZE = new Attribute("noresize");
326:
327:
330: public static final Attribute NOSHADE = new Attribute("noshade");
331:
332:
335: public static final Attribute NOWRAP = new Attribute("nowrap");
336:
337:
340: public static final Attribute PROMPT = new Attribute("prompt");
341:
342:
345: public static final Attribute REL = new Attribute("rel");
346:
347:
350: public static final Attribute REV = new Attribute("rev");
351:
352:
355: public static final Attribute ROWS = new Attribute("rows");
356:
357:
360: public static final Attribute ROWSPAN = new Attribute("rowspan");
361:
362:
365: public static final Attribute SCROLLING = new Attribute("scrolling");
366:
367:
370: public static final Attribute SELECTED = new Attribute("selected");
371:
372:
375: public static final Attribute SHAPE = new Attribute("shape");
376:
377:
380: public static final Attribute SHAPES = new Attribute("shapes");
381:
382:
385: public static final Attribute SIZE = new Attribute("size");
386:
387:
390: public static final Attribute SRC = new Attribute("src");
391:
392:
395: public static final Attribute STANDBY = new Attribute("standby");
396:
397:
400: public static final Attribute START = new Attribute("start");
401:
402:
405: public static final Attribute STYLE = new Attribute("style");
406:
407:
410: public static final Attribute TARGET = new Attribute("target");
411:
412:
415: public static final Attribute TEXT = new Attribute("text");
416:
417:
420: public static final Attribute TITLE = new Attribute("title");
421:
422:
425: public static final Attribute TYPE = new Attribute("type");
426:
427:
430: public static final Attribute USEMAP = new Attribute("usemap");
431:
432:
435: public static final Attribute VALIGN = new Attribute("valign");
436:
437:
440: public static final Attribute VALUE = new Attribute("value");
441:
442:
445: public static final Attribute VALUETYPE = new Attribute("valuetype");
446:
447:
450: public static final Attribute VERSION = new Attribute("version");
451:
452:
455: public static final Attribute VLINK = new Attribute("vlink");
456:
457:
460: public static final Attribute VSPACE = new Attribute("vspace");
461:
462:
465: public static final Attribute WIDTH = new Attribute("width");
466:
467:
470: static final Attribute PSEUDO_CLASS = new Attribute("_pseudo");
471:
472:
475: static final Attribute DYNAMIC_CLASS = new Attribute("_dynamic");
476:
477:
480: private final String name;
481:
482:
485: private Attribute(String a_name)
486: {
487: name = a_name;
488: }
489:
490:
494: public String toString()
495: {
496: return name;
497: }
498:
499:
504: static Attribute[] getAllAttributes()
505: {
506: Field[] f = Attribute.class.getFields();
507: Attribute[] attrs = new Attribute[ f.length ];
508: Field x;
509: int p = 0;
510: Attribute a;
511:
512: for (int i = 0; i < f.length; i++)
513: {
514: x = f [ i ];
515:
516: if ((x.getModifiers() & Modifier.STATIC) != 0)
517: {
518: if (x.getType().equals(Attribute.class))
519: {
520: try
521: {
522: a = (Attribute) x.get(null);
523: attrs [ p++ ] = a;
524: }
525: catch (Exception ex)
526: {
527: ex.printStackTrace(System.err);
528: throw new Error("This should never happen, report a bug");
529: }
530: }
531: }
532: }
533:
534: return attrs;
535: }
536: }
537:
538:
541: public static class Tag
542: {
543:
546: public static final Tag A = new Tag("a");
547:
548:
551: public static final Tag ADDRESS = new Tag("address");
552:
553:
556: public static final Tag APPLET = new Tag("applet");
557:
558:
561: public static final Tag AREA = new Tag("area");
562:
563:
566: public static final Tag B = new Tag("b");
567:
568:
571: public static final Tag BASE = new Tag("base");
572:
573:
576: public static final Tag BASEFONT = new Tag("basefont");
577:
578:
581: public static final Tag BIG = new Tag("big");
582:
583:
586: public static final Tag BLOCKQUOTE = new Tag("blockquote", BREAKS | BLOCK);
587:
588:
591: public static final Tag BODY = new Tag("body", BREAKS | BLOCK);
592:
593:
596: public static final Tag BR = new Tag("br", BREAKS);
597:
598:
601: public static final Tag CAPTION = new Tag("caption");
602:
603:
606: public static final Tag CENTER = new Tag("center", BREAKS);
607:
608:
611: public static final Tag CITE = new Tag("cite");
612:
613:
616: public static final Tag CODE = new Tag("code");
617:
618:
621: public static final Tag DD = new Tag("dd", BREAKS | BLOCK);
622:
623:
626: public static final Tag DFN = new Tag("dfn");
627:
628:
631: public static final Tag DIR = new Tag("dir", BREAKS | BLOCK);
632:
633:
636: public static final Tag DIV = new Tag("div", BREAKS | BLOCK);
637:
638:
641: public static final Tag DL = new Tag("dl", BREAKS | BLOCK);
642:
643:
646: public static final Tag DT = new Tag("dt", BREAKS | BLOCK);
647:
648:
651: public static final Tag EM = new Tag("em");
652:
653:
656: public static final Tag FONT = new Tag("font");
657:
658:
661: public static final Tag FORM = new Tag("form", BREAKS);
662:
663:
666: public static final Tag FRAME = new Tag("frame");
667:
668:
671: public static final Tag FRAMESET = new Tag("frameset");
672:
673:
676: public static final Tag H1 = new Tag("h1", BREAKS | BLOCK);
677:
678:
681: public static final Tag H2 = new Tag("h2", BREAKS | BLOCK);
682:
683:
686: public static final Tag H3 = new Tag("h3", BREAKS | BLOCK);
687:
688:
691: public static final Tag H4 = new Tag("h4", BREAKS | BLOCK);
692:
693:
696: public static final Tag H5 = new Tag("h5", BREAKS | BLOCK);
697:
698:
701: public static final Tag H6 = new Tag("h6", BREAKS | BLOCK);
702:
703:
706: public static final Tag HEAD = new Tag("head", BREAKS | BLOCK);
707:
708:
711: public static final Tag HR = new Tag("hr", BREAKS);
712:
713:
716: public static final Tag HTML = new Tag("html", BREAKS);
717:
718:
721: public static final Tag I = new Tag("i");
722:
723:
726: public static final Tag IMG = new Tag("img");
727:
728:
731: public static final Tag INPUT = new Tag("input");
732:
733:
736: public static final Tag ISINDEX = new Tag("isindex", BREAKS);
737:
738:
741: public static final Tag KBD = new Tag("kbd");
742:
743:
746: public static final Tag LI = new Tag("li", BREAKS | BLOCK);
747:
748:
751: public static final Tag LINK = new Tag("link");
752:
753:
756: public static final Tag MAP = new Tag("map");
757:
758:
761: public static final Tag MENU = new Tag("menu", BREAKS | BLOCK);
762:
763:
766: public static final Tag META = new Tag("meta");
767:
768:
771: static final Tag NOBR = new Tag("nobr");
772:
773:
776: public static final Tag NOFRAMES = new Tag("noframes", BREAKS | BLOCK);
777:
778:
781: public static final Tag OBJECT = new Tag("object");
782:
783:
786: public static final Tag OL = new Tag("ol", BREAKS | BLOCK);
787:
788:
791: public static final Tag OPTION = new Tag("option");
792:
793:
796: public static final Tag P = new Tag("p", BREAKS | BLOCK);
797:
798:
801: public static final Tag PARAM = new Tag("param");
802:
803:
806: public static final Tag PRE = new Tag("pre", BREAKS | BLOCK | PREFORMATTED);
807:
808:
811: public static final Tag S = new Tag("s");
812:
813:
816: public static final Tag SAMP = new Tag("samp");
817:
818:
821: public static final Tag SCRIPT = new Tag("script");
822:
823:
826: public static final Tag SELECT = new Tag("select");
827:
828:
831: public static final Tag SMALL = new Tag("small");
832:
833:
836: public static final Tag SPAN = new Tag("span");
837:
838:
841: public static final Tag STRIKE = new Tag("strike");
842:
843:
846: public static final Tag STRONG = new Tag("strong");
847:
848:
851: public static final Tag STYLE = new Tag("style");
852:
853:
856: public static final Tag SUB = new Tag("sub");
857:
858:
861: public static final Tag SUP = new Tag("sup");
862:
863:
866: public static final Tag TABLE = new Tag("table", BLOCK);
867:
868:
871: public static final Tag TD = new Tag("td", BREAKS | BLOCK);
872:
873:
876: public static final Tag TEXTAREA = new Tag("textarea", PREFORMATTED);
877:
878:
881: public static final Tag TH = new Tag("th", BREAKS | BLOCK);
882:
883:
886: public static final Tag TITLE = new Tag("title", BREAKS | BLOCK);
887:
888:
891: public static final Tag TR = new Tag("tr", BLOCK);
892:
893:
896: public static final Tag TT = new Tag("tt");
897:
898:
901: public static final Tag U = new Tag("u");
902:
903:
906: public static final Tag UL = new Tag("ul", BREAKS | BLOCK);
907:
908:
911: public static final Tag VAR = new Tag("var");
912:
913:
914:
915:
920: private static final int TOTAL_SYNTHETIC_TAGS = 3;
921:
922:
927: public static final Tag COMMENT = new Tag("comment", SYNTHETIC);
928:
929:
934: public static final Tag CONTENT = new Tag("content", SYNTHETIC);
935:
936:
942: public static final Tag IMPLIED = new Tag("p-implied", SYNTHETIC);
943: final String name;
944: final int flags;
945:
946:
955: public Tag()
956: {
957: name = null;
958: flags = 0;
959: }
960:
961:
965: protected Tag(String id)
966: {
967: name = id;
968: flags = 0;
969: }
970:
971:
975: protected Tag(String id, boolean causesBreak, boolean isBlock)
976: {
977: int f = 0;
978:
979: if (causesBreak)
980: {
981: f |= BREAKS;
982: }
983:
984: if (isBlock)
985: {
986: f |= BLOCK;
987: }
988:
989: flags = f;
990: name = id;
991: }
992:
993:
996: Tag(String id, int a_flags)
997: {
998: name = id;
999: flags = a_flags;
1000: }
1001:
1002:
1006: public boolean isBlock()
1007: {
1008: return (flags & BLOCK) != 0;
1009: }
1010:
1011:
1015: public boolean isPreformatted()
1016: {
1017: return (flags & PREFORMATTED) != 0;
1018: }
1019:
1020:
1023: public boolean breaksFlow()
1024: {
1025: return (flags & BREAKS) != 0;
1026: }
1027:
1028:
1032: public String toString()
1033: {
1034: return name;
1035: }
1036:
1037:
1042: static Tag[] getAllTags()
1043: {
1044: Field[] f = Tag.class.getFields();
1045: Field x;
1046:
1047:
1048: Tag[] tags = new Tag[ f.length - TOTAL_SYNTHETIC_TAGS ];
1049: int p = 0;
1050: Tag t;
1051:
1052: for (int i = 0; i < f.length; i++)
1053: {
1054: x = f [ i ];
1055:
1056: if ((x.getModifiers() & Modifier.STATIC) != 0)
1057: {
1058: if (x.getType().equals(Tag.class))
1059: {
1060: try
1061: {
1062: t = (Tag) x.get(null);
1063:
1064: if (!t.isSyntetic())
1065: {
1066: tags [ p++ ] = t;
1067: }
1068: }
1069: catch (IllegalAccessException ex)
1070: {
1071: unexpected(ex);
1072: }
1073: catch (IllegalArgumentException ex)
1074: {
1075: unexpected(ex);
1076: }
1077: }
1078: }
1079: }
1080:
1081: return tags;
1082: }
1083:
1084:
1088: boolean isSyntetic()
1089: {
1090: return (flags & SYNTHETIC) != 0;
1091: }
1092:
1093: private static void unexpected(Exception ex)
1094: throws Error
1095: {
1096: throw new Error("This should never happen, report a bug", ex);
1097: }
1098: }
1099:
1100:
1104: public static class UnknownTag
1105: extends Tag
1106: implements Serializable
1107: {
1108: private static final long serialVersionUID = -1534369342247250625L;
1109:
1110:
1115: public UnknownTag(String name)
1116: {
1117: super(name);
1118: }
1119: }
1120:
1121:
1125: public static final String NULL_ATTRIBUTE_VALUE = "#DEFAULT";
1126:
1127:
1128: static final int BREAKS = 1;
1129: static final int BLOCK = 2;
1130: static final int PREFORMATTED = 4;
1131: static final int SYNTHETIC = 8;
1132: private static Map<String,Tag> tagMap;
1133: private static Map<String,Attribute> attrMap;
1134:
1135:
1140: public HTML()
1141: {
1142:
1143: }
1144:
1145:
1148: public static HTML.Attribute[] getAllAttributeKeys()
1149: {
1150: return Attribute.getAllAttributes();
1151: }
1152:
1153:
1158: public static HTML.Tag[] getAllTags()
1159: {
1160: return Tag.getAllTags();
1161: }
1162:
1163:
1167: public static Attribute getAttributeKey(String attName)
1168: {
1169: if (attrMap == null)
1170: {
1171:
1172: attrMap = new TreeMap<String,Attribute>();
1173:
1174: Attribute[] attrs = getAllAttributeKeys();
1175:
1176: for (int i = 0; i < attrs.length; i++)
1177: {
1178: attrMap.put(attrs [ i ].toString(), attrs [ i ]);
1179: }
1180: }
1181:
1182: return attrMap.get(attName.toLowerCase());
1183: }
1184:
1185:
1209: public static int getIntegerAttributeValue(AttributeSet set,
1210: HTML.Attribute attribute,
1211: int defaultValue
1212: )
1213: {
1214: Object v = set.getAttribute(attribute);
1215:
1216: if (v == null)
1217: {
1218: return defaultValue;
1219: }
1220:
1221: try
1222: {
1223: return Integer.parseInt(v.toString().trim());
1224: }
1225: catch (Exception ex)
1226: {
1227: return defaultValue;
1228: }
1229: }
1230:
1231:
1236: public static Tag getTag(String tagName)
1237: {
1238: if (tagMap == null)
1239: {
1240:
1241: tagMap = new TreeMap<String,Tag>();
1242:
1243: Tag[] tags = getAllTags();
1244:
1245: for (int i = 0; i < tags.length; i++)
1246: {
1247: tagMap.put(tags [ i ].toString(), tags [ i ]);
1248: }
1249: }
1250:
1251: return tagMap.get(tagName.toLowerCase());
1252: }
1253: }