1 /* HTML.java -- HTML document tag constants
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.text.html;
41 import java.io.Serializable;
43 import java.lang.reflect.Field;
44 import java.lang.reflect.Modifier;
47 import java.util.TreeMap;
49 import javax.swing.text.AttributeSet;
52 * HTML attribute and tag definitions.
53 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
58 * Represents a HTML attribute.
60 public static final class Attribute
63 * The action attribute
65 public static final Attribute ACTION = new Attribute("action");
70 public static final Attribute ALIGN = new Attribute("align");
75 public static final Attribute ALINK = new Attribute("alink");
80 public static final Attribute ALT = new Attribute("alt");
83 * The archive attribute
85 public static final Attribute ARCHIVE = new Attribute("archive");
88 * The background attribute
90 public static final Attribute BACKGROUND = new Attribute("background");
93 * The bgcolor attribute
95 public static final Attribute BGCOLOR = new Attribute("bgcolor");
98 * The border attribute
100 public static final Attribute BORDER = new Attribute("border");
103 * The cellpadding attribute
105 public static final Attribute CELLPADDING = new Attribute("cellpadding");
108 * The cellspacing attribute
110 public static final Attribute CELLSPACING = new Attribute("cellspacing");
113 * The checked attribute
115 public static final Attribute CHECKED = new Attribute("checked");
118 * The class attribute
120 public static final Attribute CLASS = new Attribute("class");
123 * The classid attribute
125 public static final Attribute CLASSID = new Attribute("classid");
128 * The clear attribute
130 public static final Attribute CLEAR = new Attribute("clear");
135 public static final Attribute CODE = new Attribute("code");
138 * The codebase attribute
140 public static final Attribute CODEBASE = new Attribute("codebase");
143 * The codetype attribute
145 public static final Attribute CODETYPE = new Attribute("codetype");
148 * The color attribute
150 public static final Attribute COLOR = new Attribute("color");
155 public static final Attribute COLS = new Attribute("cols");
158 * The colspan attribute
160 public static final Attribute COLSPAN = new Attribute("colspan");
163 * The comment attribute
165 public static final Attribute COMMENT = new Attribute("comment");
168 * The compact attribute
170 public static final Attribute COMPACT = new Attribute("compact");
173 * The content attribute
175 public static final Attribute CONTENT = new Attribute("content");
178 * The coords attribute
180 public static final Attribute COORDS = new Attribute("coords");
185 public static final Attribute DATA = new Attribute("data");
188 * The declare attribute
190 public static final Attribute DECLARE = new Attribute("declare");
195 public static final Attribute DIR = new Attribute("dir");
198 * The dummy attribute
200 public static final Attribute DUMMY = new Attribute("dummy");
203 * The enctype attribute
205 public static final Attribute ENCTYPE = new Attribute("enctype");
208 * The endtag attribute
210 public static final Attribute ENDTAG = new Attribute("endtag");
215 public static final Attribute FACE = new Attribute("face");
218 * The frameborder attribute
220 public static final Attribute FRAMEBORDER = new Attribute("frameborder");
223 * The halign attribute
225 public static final Attribute HALIGN = new Attribute("halign");
228 * The height attribute
230 public static final Attribute HEIGHT = new Attribute("height");
235 public static final Attribute HREF = new Attribute("href");
238 * The hspace attribute
240 public static final Attribute HSPACE = new Attribute("hspace");
243 * The http-equiv attribute
245 public static final Attribute HTTPEQUIV = new Attribute("http-equiv");
250 public static final Attribute ID = new Attribute("id");
253 * The ismap attribute
255 public static final Attribute ISMAP = new Attribute("ismap");
260 public static final Attribute LANG = new Attribute("lang");
263 * The language attribute
265 public static final Attribute LANGUAGE = new Attribute("language");
270 public static final Attribute LINK = new Attribute("link");
273 * The lowsrc attribute
275 public static final Attribute LOWSRC = new Attribute("lowsrc");
278 * The marginheight attribute
280 public static final Attribute MARGINHEIGHT = new Attribute("marginheight");
283 * The marginwidth attribute
285 public static final Attribute MARGINWIDTH = new Attribute("marginwidth");
288 * The maxlength attribute
290 public static final Attribute MAXLENGTH = new Attribute("maxlength");
293 * The media attribute
295 static final Attribute MEDIA = new Attribute("media");
298 * The method attribute
300 public static final Attribute METHOD = new Attribute("method");
303 * The multiple attribute
305 public static final Attribute MULTIPLE = new Attribute("multiple");
310 public static final Attribute N = new Attribute("n");
315 public static final Attribute NAME = new Attribute("name");
318 * The nohref attribute
320 public static final Attribute NOHREF = new Attribute("nohref");
323 * The noresize attribute
325 public static final Attribute NORESIZE = new Attribute("noresize");
328 * The noshade attribute
330 public static final Attribute NOSHADE = new Attribute("noshade");
333 * The nowrap attribute
335 public static final Attribute NOWRAP = new Attribute("nowrap");
338 * The prompt attribute
340 public static final Attribute PROMPT = new Attribute("prompt");
345 public static final Attribute REL = new Attribute("rel");
350 public static final Attribute REV = new Attribute("rev");
355 public static final Attribute ROWS = new Attribute("rows");
358 * The rowspan attribute
360 public static final Attribute ROWSPAN = new Attribute("rowspan");
363 * The scrolling attribute
365 public static final Attribute SCROLLING = new Attribute("scrolling");
368 * The selected attribute
370 public static final Attribute SELECTED = new Attribute("selected");
373 * The shape attribute
375 public static final Attribute SHAPE = new Attribute("shape");
378 * The shapes attribute
380 public static final Attribute SHAPES = new Attribute("shapes");
385 public static final Attribute SIZE = new Attribute("size");
390 public static final Attribute SRC = new Attribute("src");
393 * The standby attribute
395 public static final Attribute STANDBY = new Attribute("standby");
398 * The start attribute
400 public static final Attribute START = new Attribute("start");
403 * The style attribute
405 public static final Attribute STYLE = new Attribute("style");
408 * The target attribute
410 public static final Attribute TARGET = new Attribute("target");
415 public static final Attribute TEXT = new Attribute("text");
418 * The title attribute
420 public static final Attribute TITLE = new Attribute("title");
425 public static final Attribute TYPE = new Attribute("type");
428 * The usemap attribute
430 public static final Attribute USEMAP = new Attribute("usemap");
433 * The valign attribute
435 public static final Attribute VALIGN = new Attribute("valign");
438 * The value attribute
440 public static final Attribute VALUE = new Attribute("value");
443 * The valuetype attribute
445 public static final Attribute VALUETYPE = new Attribute("valuetype");
448 * The version attribute
450 public static final Attribute VERSION = new Attribute("version");
453 * The vlink attribute
455 public static final Attribute VLINK = new Attribute("vlink");
458 * The vspace attribute
460 public static final Attribute VSPACE = new Attribute("vspace");
463 * The width attribute
465 public static final Attribute WIDTH = new Attribute("width");
468 * This is used to reflect the pseudo class for the a tag.
470 static final Attribute PSEUDO_CLASS = new Attribute("_pseudo");
473 * This is used to reflect the dynamic class for the a tag.
475 static final Attribute DYNAMIC_CLASS = new Attribute("_dynamic");
478 * The attribute name.
480 private final String name;
483 * Creates the attribute with the given name.
485 private Attribute(String a_name)
491 * Returns the attribute name. The names of the built-in attributes
492 * are always returned in lowercase.
494 public String toString()
500 * Return an array of all attributes, declared in the HTML.Attribute
501 * class. WARNING: attributes are the only public fields,
502 * expected in this class.
504 static Attribute[] getAllAttributes()
506 Field[] f = Attribute.class.getFields();
507 Attribute[] attrs = new Attribute[ f.length ];
512 for (int i = 0; i < f.length; i++)
516 if ((x.getModifiers() & Modifier.STATIC) != 0)
518 if (x.getType().equals(Attribute.class))
522 a = (Attribute) x.get(null);
527 ex.printStackTrace(System.err);
528 throw new Error("This should never happen, report a bug");
539 * Represents a HTML tag.
541 public static class Tag
546 public static final Tag A = new Tag("a");
549 * The <address> tag
551 public static final Tag ADDRESS = new Tag("address");
554 * The <applet> tag
556 public static final Tag APPLET = new Tag("applet");
559 * The <area> tag
561 public static final Tag AREA = new Tag("area");
566 public static final Tag B = new Tag("b");
569 * The <base> tag
571 public static final Tag BASE = new Tag("base");
574 * The <basefont> tag
576 public static final Tag BASEFONT = new Tag("basefont");
579 * The <big> tag
581 public static final Tag BIG = new Tag("big");
584 * The <blockquote> tag , breaks flow, block tag.
586 public static final Tag BLOCKQUOTE = new Tag("blockquote", BREAKS | BLOCK);
589 * The <body> tag , breaks flow, block tag.
591 public static final Tag BODY = new Tag("body", BREAKS | BLOCK);
594 * The <br> tag , breaks flow.
596 public static final Tag BR = new Tag("br", BREAKS);
599 * The <caption> tag
601 public static final Tag CAPTION = new Tag("caption");
604 * The <center> tag , breaks flow.
606 public static final Tag CENTER = new Tag("center", BREAKS);
609 * The <cite> tag
611 public static final Tag CITE = new Tag("cite");
614 * The <code> tag
616 public static final Tag CODE = new Tag("code");
619 * The <dd> tag , breaks flow, block tag.
621 public static final Tag DD = new Tag("dd", BREAKS | BLOCK);
624 * The <dfn> tag
626 public static final Tag DFN = new Tag("dfn");
629 * The <dir> tag , breaks flow, block tag.
631 public static final Tag DIR = new Tag("dir", BREAKS | BLOCK);
634 * The <div> tag , breaks flow, block tag.
636 public static final Tag DIV = new Tag("div", BREAKS | BLOCK);
639 * The <dl> tag , breaks flow, block tag.
641 public static final Tag DL = new Tag("dl", BREAKS | BLOCK);
644 * The <dt> tag , breaks flow, block tag.
646 public static final Tag DT = new Tag("dt", BREAKS | BLOCK);
651 public static final Tag EM = new Tag("em");
654 * The <font> tag
656 public static final Tag FONT = new Tag("font");
659 * The <form> tag , breaks flow.
661 public static final Tag FORM = new Tag("form", BREAKS);
664 * The <frame> tag
666 public static final Tag FRAME = new Tag("frame");
669 * The <frameset> tag
671 public static final Tag FRAMESET = new Tag("frameset");
674 * The <h1> tag , breaks flow, block tag.
676 public static final Tag H1 = new Tag("h1", BREAKS | BLOCK);
679 * The <h2> tag , breaks flow, block tag.
681 public static final Tag H2 = new Tag("h2", BREAKS | BLOCK);
684 * The <h3> tag , breaks flow, block tag.
686 public static final Tag H3 = new Tag("h3", BREAKS | BLOCK);
689 * The <h4> tag , breaks flow, block tag.
691 public static final Tag H4 = new Tag("h4", BREAKS | BLOCK);
694 * The <h5> tag , breaks flow, block tag.
696 public static final Tag H5 = new Tag("h5", BREAKS | BLOCK);
699 * The <h6> tag , breaks flow, block tag.
701 public static final Tag H6 = new Tag("h6", BREAKS | BLOCK);
704 * The <head> tag , breaks flow, block tag.
706 public static final Tag HEAD = new Tag("head", BREAKS | BLOCK);
709 * The <hr> tag , breaks flow.
711 public static final Tag HR = new Tag("hr", BREAKS);
714 * The <html> tag , breaks flow.
716 public static final Tag HTML = new Tag("html", BREAKS);
721 public static final Tag I = new Tag("i");
724 * The <img> tag
726 public static final Tag IMG = new Tag("img");
729 * The <input> tag
731 public static final Tag INPUT = new Tag("input");
734 * The <isindex> tag , breaks flow.
736 public static final Tag ISINDEX = new Tag("isindex", BREAKS);
739 * The <kbd> tag
741 public static final Tag KBD = new Tag("kbd");
744 * The <li> tag , breaks flow, block tag.
746 public static final Tag LI = new Tag("li", BREAKS | BLOCK);
749 * The <link> tag
751 public static final Tag LINK = new Tag("link");
754 * The <map> tag
756 public static final Tag MAP = new Tag("map");
759 * The <menu> tag , breaks flow, block tag.
761 public static final Tag MENU = new Tag("menu", BREAKS | BLOCK);
764 * The <meta> tag
766 public static final Tag META = new Tag("meta");
769 * The <nobr> tag
771 static final Tag NOBR = new Tag("nobr");
774 * The <noframes> tag , breaks flow, block tag.
776 public static final Tag NOFRAMES = new Tag("noframes", BREAKS | BLOCK);
779 * The <object> tag
781 public static final Tag OBJECT = new Tag("object");
784 * The <ol> tag , breaks flow, block tag.
786 public static final Tag OL = new Tag("ol", BREAKS | BLOCK);
789 * The <option> tag
791 public static final Tag OPTION = new Tag("option");
794 * The <p> tag , breaks flow, block tag.
796 public static final Tag P = new Tag("p", BREAKS | BLOCK);
799 * The <param> tag
801 public static final Tag PARAM = new Tag("param");
804 * The <pre> tag , breaks flow, block tag, preformatted.
806 public static final Tag PRE = new Tag("pre", BREAKS | BLOCK | PREFORMATTED);
811 public static final Tag S = new Tag("s");
814 * The <samp> tag
816 public static final Tag SAMP = new Tag("samp");
819 * The <script> tag
821 public static final Tag SCRIPT = new Tag("script");
824 * The <select> tag
826 public static final Tag SELECT = new Tag("select");
829 * The <small> tag
831 public static final Tag SMALL = new Tag("small");
834 * The <span> tag
836 public static final Tag SPAN = new Tag("span");
839 * The <strike> tag
841 public static final Tag STRIKE = new Tag("strike");
844 * The <strong> tag
846 public static final Tag STRONG = new Tag("strong");
849 * The <style> tag
851 public static final Tag STYLE = new Tag("style");
854 * The <sub> tag
856 public static final Tag SUB = new Tag("sub");
859 * The <sup> tag
861 public static final Tag SUP = new Tag("sup");
864 * The <table> tag , block tag.
866 public static final Tag TABLE = new Tag("table", BLOCK);
869 * The <td> tag , breaks flow, block tag.
871 public static final Tag TD = new Tag("td", BREAKS | BLOCK);
874 * The <textarea> tag , preformatted.
876 public static final Tag TEXTAREA = new Tag("textarea", PREFORMATTED);
879 * The <th> tag , breaks flow, block tag.
881 public static final Tag TH = new Tag("th", BREAKS | BLOCK);
884 * The <title> tag , breaks flow, block tag.
886 public static final Tag TITLE = new Tag("title", BREAKS | BLOCK);
889 * The <tr> tag , block tag.
891 public static final Tag TR = new Tag("tr", BLOCK);
896 public static final Tag TT = new Tag("tt");
901 public static final Tag U = new Tag("u");
904 * The <ul> tag , breaks flow, block tag.
906 public static final Tag UL = new Tag("ul", BREAKS | BLOCK);
909 * The <var> tag
911 public static final Tag VAR = new Tag("var");
916 * Total number of syntetic tags, delared in the Tag class.
917 * This must be adjusted if the new synthetic tags are declared.
918 * Otherwise the HTML.getAllTags() will not work as expected.
920 private static final int TOTAL_SYNTHETIC_TAGS = 3;
923 * All comments are labeled with this tag.
924 * This tag is not included into the array, returned by getAllTags().
925 * toString() returns 'comment'. HTML reader synthesizes this tag.
927 public static final Tag COMMENT = new Tag("comment", SYNTHETIC);
930 * All text content is labeled with this tag.
931 * This tag is not included into the array, returned by getAllTags().
932 * toString() returns 'content'. HTML reader synthesizes this tag.
934 public static final Tag CONTENT = new Tag("content", SYNTHETIC);
937 * All text content must be in a paragraph element.
938 * If a paragraph didn't exist when content was encountered,
939 * a paragraph is manufactured.
940 * toString() returns 'p-implied'. HTML reader synthesizes this tag.
942 public static final Tag IMPLIED = new Tag("p-implied", SYNTHETIC);
947 * Create the unitialised instance of HTML.Tag.
949 * The {@link #breaksFlow()}, {@link #isBlock()}
950 * and {@link #isPreformatted()} will always return false.
951 * The {@link #toString()} will return <code>null</code>.
962 * Creates a new Tag with the specified id, and with causesBreak
963 * and isBlock set to false.
965 protected Tag(String id)
972 * Creates a new Tag with the specified tag name and
973 * causesBreak and isBlock properties.
975 protected Tag(String id, boolean causesBreak, boolean isBlock)
994 * Create a tag taking flags.
996 Tag(String id, int a_flags)
1003 * Returns true if this tag is a block tag, which is a tag used to
1004 * add structure to a document.
1006 public boolean isBlock()
1008 return (flags & BLOCK) != 0;
1012 * Returns true if this tag is pre-formatted, which is true if
1013 * the tag is either PRE or TEXTAREA
1015 public boolean isPreformatted()
1017 return (flags & PREFORMATTED) != 0;
1021 * Returns true if this tag causes a line break to the flow of text
1023 public boolean breaksFlow()
1025 return (flags & BREAKS) != 0;
1029 * Returns the tag name. The names of the built-in tags are always
1030 * returned in lowercase.
1032 public String toString()
1038 * Return an array of HTML tags, declared in HTML.Tag class.
1039 * WARNING: This method expects that the Tags are the only
1040 * public fields declared in the Tag class.
1042 static Tag[] getAllTags()
1044 Field[] f = Tag.class.getFields();
1047 // The syntetic tags are not included.
1048 Tag[] tags = new Tag[ f.length - TOTAL_SYNTHETIC_TAGS ];
1052 for (int i = 0; i < f.length; i++)
1056 if ((x.getModifiers() & Modifier.STATIC) != 0)
1058 if (x.getType().equals(Tag.class))
1062 t = (Tag) x.get(null);
1064 if (!t.isSyntetic())
1069 catch (IllegalAccessException ex)
1073 catch (IllegalArgumentException ex)
1085 * Returns true for tags, generated by the html reader
1086 * (COMMENT, CONTENT and IMPLIED).
1088 boolean isSyntetic()
1090 return (flags & SYNTHETIC) != 0;
1093 private static void unexpected(Exception ex)
1096 throw new Error("This should never happen, report a bug", ex);
1101 * Represents an unknown HTML tag.
1102 * @author Mark Wielaard (mark@klomp.org)
1104 public static class UnknownTag
1106 implements Serializable
1108 private static final long serialVersionUID = -1534369342247250625L;
1111 * Creates a new UnknownTag with the specified name
1112 * @param name The tag name.
1115 public UnknownTag(String name)
1122 * This value is returned for attributes without value that have no
1123 * default value defined in the DTD.
1125 public static final String NULL_ATTRIBUTE_VALUE = "#DEFAULT";
1127 /* Package level html tag flags */
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;
1136 * The public constructor (does nothing). It it seldom required to have
1137 * an instance of this class, because all public fields and methods
1142 // Nothing to do here.
1146 * Returns the set of the recognized HTML attributes.
1148 public static HTML.Attribute[] getAllAttributeKeys()
1150 return Attribute.getAllAttributes();
1154 * Returns the set of actual HTML tags that are recognized by
1155 * the default HTML reader. The returned array does not include the
1156 * COMMENT, CONTENT and IMPLIED tags.
1158 public static HTML.Tag[] getAllTags()
1160 return Tag.getAllTags();
1164 * Returns an htl attribute constant for the given attribute name.
1165 * @param attName the attribute name, case insensitive
1167 public static Attribute getAttributeKey(String attName)
1169 if (attrMap == null)
1171 // Create the map on demand.
1172 attrMap = new TreeMap<String,Attribute>();
1174 Attribute[] attrs = getAllAttributeKeys();
1176 for (int i = 0; i < attrs.length; i++)
1178 attrMap.put(attrs [ i ].toString(), attrs [ i ]);
1182 return attrMap.get(attName.toLowerCase());
1186 * Searches the value of given attribute in the provided set.
1187 * If the value is found (String type expected), tries to parse it as
1188 * an integer value. If succeded, returns the obtained integer value.
1190 * For example:<p><code>
1191 * SimpleAttributeSet ase = new SimpleAttributeSet();
1192 * ase.addAttribute(HTML.getAttributeKey("size"),"222");
1193 * System.out.println(
1194 * HTML.getIntegerAttributeValue
1195 * (ase, HTML.getAttributeKey("size"), 333)); // prints "222"
1196 * System.out.println(
1197 * HTML.getIntegerAttributeValue
1198 * (ase, HTML.getAttributeKey("width"), 333)); // prints "333".
1202 * @param set The attribute set to search in. If the set contains the
1203 * given attribute, it must by a type of String.
1204 * @param attribute The html attribute to search in
1205 * @param defaultValue The value that is returned if the attribute is not
1206 * found in the given set or if the NumberFormatException was thrown
1207 * during the parsing.
1209 public static int getIntegerAttributeValue(AttributeSet set,
1210 HTML.Attribute attribute,
1214 Object v = set.getAttribute(attribute);
1218 return defaultValue;
1223 return Integer.parseInt(v.toString().trim());
1225 catch (Exception ex)
1227 return defaultValue;
1232 * Returns a HTML tag constant for the given HTML attribute name.
1233 * If the tag is unknown, the null is returned.
1234 * @param tagName the tag name, case insensitive
1236 public static Tag getTag(String tagName)
1240 // Create the mao on demand.
1241 tagMap = new TreeMap<String,Tag>();
1243 Tag[] tags = getAllTags();
1245 for (int i = 0; i < tags.length; i++)
1247 tagMap.put(tags [ i ].toString(), tags [ i ]);
1251 return tagMap.get(tagName.toLowerCase());