1. /*
  2. * @(#)HTML.java 1.27 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.text.html;
  8. import java.io.Serializable;
  9. import java.util.Hashtable;
  10. import javax.swing.text.AttributeSet;
  11. import javax.swing.text.StyleContext;
  12. /**
  13. * Constants used in the HTMLDocument. These
  14. * are basically tag and attribute definitions.
  15. *
  16. * @author Timothy Prinzing
  17. * @author Sunita Mani
  18. *
  19. * @version 1.27 11/29/01
  20. */
  21. public class HTML {
  22. /**
  23. * Typesafe enumeration for an html tag. Although the
  24. * set of html tags is a closed set, we have let the
  25. * set open so that people can add their own tag types
  26. * to their custom parser and still communicate to the
  27. * reader.
  28. */
  29. public static class Tag {
  30. protected Tag(String id) {
  31. this(id, false, false);
  32. }
  33. protected Tag(String id, boolean causesBreak, boolean isBlock) {
  34. name = id;
  35. this.breakTag = causesBreak;
  36. this.blockTag = isBlock;
  37. }
  38. /**
  39. * Block tags are tags that add structure to
  40. * a document.
  41. */
  42. public boolean isBlock() {
  43. return blockTag;
  44. }
  45. /**
  46. * If the tag causes a line break, to the flow of
  47. * data, then this method will return true.
  48. */
  49. public boolean breaksFlow() {
  50. return breakTag;
  51. }
  52. public boolean isPreformatted() {
  53. return (this == PRE || this == TEXTAREA);
  54. }
  55. /**
  56. * Return the string representation of the
  57. * tag.
  58. */
  59. public String toString() {
  60. return name;
  61. }
  62. private boolean blockTag;
  63. private boolean breakTag;
  64. private String name;
  65. private boolean unknown;
  66. // --- Tag Names -----------------------------------
  67. public static final Tag A = new Tag("a");
  68. public static final Tag ADDRESS = new Tag("address");
  69. public static final Tag APPLET = new Tag("applet");
  70. public static final Tag AREA = new Tag("area");
  71. public static final Tag B = new Tag("b");
  72. public static final Tag BASE = new Tag("base");
  73. public static final Tag BASEFONT = new Tag("basefont");
  74. public static final Tag BIG = new Tag("big");
  75. public static final Tag BLOCKQUOTE = new Tag("blockquote", true, true);
  76. public static final Tag BODY = new Tag("body", true, true);
  77. public static final Tag BR = new Tag("br", true, false);
  78. public static final Tag CAPTION = new Tag("caption");
  79. public static final Tag CENTER = new Tag("center", true, false);
  80. public static final Tag CITE = new Tag("cite");
  81. public static final Tag CODE = new Tag("code");
  82. public static final Tag DD = new Tag("dd", true, true);
  83. public static final Tag DFN = new Tag("dfn");
  84. public static final Tag DIR = new Tag("dir", true, true);
  85. public static final Tag DIV = new Tag("div", true, true);
  86. public static final Tag DL = new Tag("dl", true, true);
  87. public static final Tag DT = new Tag("dt", true, true);
  88. public static final Tag EM = new Tag("em");
  89. public static final Tag FONT = new Tag("font");
  90. public static final Tag FORM = new Tag("form", true, false);
  91. public static final Tag FRAME = new Tag("frame");
  92. public static final Tag FRAMESET = new Tag("frameset");
  93. public static final Tag H1 = new Tag("h1", true, true);
  94. public static final Tag H2 = new Tag("h2", true, true);
  95. public static final Tag H3 = new Tag("h3", true, true);
  96. public static final Tag H4 = new Tag("h4", true, true);
  97. public static final Tag H5 = new Tag("h5", true, true);
  98. public static final Tag H6 = new Tag("h6", true, true);
  99. public static final Tag HEAD = new Tag("head", true, true);
  100. public static final Tag HR = new Tag("hr", true, false);
  101. public static final Tag HTML = new Tag("html", true, false);
  102. public static final Tag I = new Tag("i");
  103. public static final Tag IMG = new Tag("img");
  104. public static final Tag INPUT = new Tag("input");
  105. public static final Tag ISINDEX = new Tag("isindex", true, false);
  106. public static final Tag KBD = new Tag("kbd");
  107. public static final Tag LI = new Tag("li", true, true);
  108. public static final Tag LINK = new Tag("link");
  109. public static final Tag MAP = new Tag("map");
  110. public static final Tag MENU = new Tag("menu", true, true);
  111. public static final Tag META = new Tag("meta");
  112. public static final Tag NOFRAMES = new Tag("noframes", true, true);
  113. public static final Tag OBJECT = new Tag("object");
  114. public static final Tag OL = new Tag("ol", true, true);
  115. public static final Tag OPTION = new Tag("option");
  116. public static final Tag P = new Tag("p", true, true);
  117. public static final Tag PARAM = new Tag("param");
  118. public static final Tag PRE = new Tag("pre", true, true);
  119. public static final Tag SAMP = new Tag("samp");
  120. public static final Tag SCRIPT = new Tag("script");
  121. public static final Tag SELECT = new Tag("select");
  122. public static final Tag SMALL = new Tag("small");
  123. public static final Tag STRIKE = new Tag("strike");
  124. public static final Tag S = new Tag("s");
  125. public static final Tag STRONG = new Tag("strong");
  126. public static final Tag STYLE = new Tag("style");
  127. public static final Tag SUB = new Tag("sub");
  128. public static final Tag SUP = new Tag("sup");
  129. public static final Tag TABLE = new Tag("table", false, true);
  130. public static final Tag TD = new Tag("td", true, true);
  131. public static final Tag TEXTAREA = new Tag("textarea");
  132. public static final Tag TH = new Tag("th", true, true);
  133. public static final Tag TITLE = new Tag("title", true, true);
  134. public static final Tag TR = new Tag("tr", false, true);
  135. public static final Tag TT = new Tag("tt");
  136. public static final Tag U = new Tag("u");
  137. public static final Tag UL = new Tag("ul", true, true);
  138. public static final Tag VAR = new Tag("var");
  139. /**
  140. * All text content must be in a paragraph element.
  141. * If a paragraph didn't exist when content was
  142. * encountered, a paragraph is manufactured.
  143. * <p>
  144. * This is a tag synthesized by the html reader.
  145. * Since elements are identified by their tag type,
  146. * we create a some fake tag types to mark the elements
  147. * that were manufactured.
  148. */
  149. public static final Tag IMPLIED = new Tag("p-implied");
  150. /**
  151. * All text content is labeled with this tag.
  152. * <p>
  153. * This is a tag synthesized by the html reader.
  154. * Since elements are identified by their tag type,
  155. * we create a some fake tag types to mark the elements
  156. * that were manufactured.
  157. */
  158. public static final Tag CONTENT = new Tag("content");
  159. /**
  160. * All comments are labeled with this tag.
  161. * <p>
  162. * This is a tag synthesized by the html reader.
  163. * Since elements are identified by their tag type,
  164. * we create a some fake tag types to mark the elements
  165. * that were manufactured.
  166. */
  167. public static final Tag COMMENT = new Tag("comment");
  168. static final Tag allTags[] = {
  169. A, ADDRESS, APPLET, AREA, B, BASE, BASEFONT, BIG,
  170. BLOCKQUOTE, BODY, BR, CAPTION, CENTER, CITE, CODE,
  171. DD, DFN, DIR, DIV, DL, DT, EM, FONT, FORM, FRAME,
  172. FRAMESET, H1, H2, H3, H4, H5, H6, HEAD, HR, HTML,
  173. I, IMG, INPUT, ISINDEX, KBD, LI, LINK, MAP, MENU,
  174. META, NOFRAMES, OBJECT, OL, OPTION, P, PARAM,
  175. PRE, SAMP, SCRIPT, SELECT, SMALL, STRIKE, S,
  176. STRONG, STYLE, SUB, SUP, TABLE, TD, TEXTAREA,
  177. TH, TITLE, TR, TT, U, UL, VAR
  178. };
  179. }
  180. // There is no unique instance of UnknownTag, so we allow it to be
  181. // Serializable.
  182. public static class UnknownTag extends Tag implements Serializable {
  183. public UnknownTag(String id) {
  184. super(id);
  185. }
  186. public int hashCode() {
  187. return toString().hashCode();
  188. }
  189. /**
  190. * Compares this object to the specifed object.
  191. * The result is <code>true</code> if and only if the argument is not
  192. * <code>null</code> and is a <code>UnknownTag</code> object with the same
  193. * name.
  194. * @param obj the object to compare this tag with.
  195. * @return <code>true</code> if the objects are equal;
  196. * <code>false</code> otherwise.
  197. */
  198. public boolean equals(Object obj) {
  199. if (obj instanceof UnknownTag) {
  200. return toString().equals(obj.toString());
  201. }
  202. return false;
  203. }
  204. }
  205. /**
  206. * Typesafe enumeration representing an html
  207. * attribute.
  208. */
  209. public static final class Attribute {
  210. Attribute(String id) {
  211. name = id;
  212. }
  213. public String toString() {
  214. return name;
  215. }
  216. private String name;
  217. public static final Attribute SIZE = new Attribute("size");
  218. public static final Attribute COLOR = new Attribute("color");
  219. public static final Attribute CLEAR = new Attribute("clear");
  220. public static final Attribute BACKGROUND = new Attribute("background");
  221. public static final Attribute BGCOLOR = new Attribute("bgcolor");
  222. public static final Attribute TEXT = new Attribute("text");
  223. public static final Attribute LINK = new Attribute("link");
  224. public static final Attribute VLINK = new Attribute("vlink");
  225. public static final Attribute ALINK = new Attribute("alink");
  226. public static final Attribute WIDTH = new Attribute("width");
  227. public static final Attribute HEIGHT = new Attribute("height");
  228. public static final Attribute ALIGN = new Attribute("align");
  229. public static final Attribute NAME = new Attribute("name");
  230. public static final Attribute HREF = new Attribute("href");
  231. public static final Attribute REL = new Attribute("rel");
  232. public static final Attribute REV = new Attribute("rev");
  233. public static final Attribute TITLE = new Attribute("title");
  234. public static final Attribute TARGET = new Attribute("target");
  235. public static final Attribute SHAPE = new Attribute("shape");
  236. public static final Attribute COORDS = new Attribute("coords");
  237. public static final Attribute ISMAP = new Attribute("ismap");
  238. public static final Attribute NOHREF = new Attribute("nohref");
  239. public static final Attribute ALT = new Attribute("alt");
  240. public static final Attribute ID = new Attribute("id");
  241. public static final Attribute SRC = new Attribute("src");
  242. public static final Attribute HSPACE = new Attribute("hspace");
  243. public static final Attribute VSPACE = new Attribute("vspace");
  244. public static final Attribute USEMAP = new Attribute("usemap");
  245. public static final Attribute LOWSRC = new Attribute("lowsrc");
  246. public static final Attribute CODEBASE = new Attribute("codebase");
  247. public static final Attribute CODE = new Attribute("code");
  248. public static final Attribute ARCHIVE = new Attribute("archive");
  249. public static final Attribute VALUE = new Attribute("value");
  250. public static final Attribute VALUETYPE = new Attribute("valuetype");
  251. public static final Attribute TYPE = new Attribute("type");
  252. public static final Attribute CLASS = new Attribute("class");
  253. public static final Attribute STYLE = new Attribute("style");
  254. public static final Attribute LANG = new Attribute("lang");
  255. public static final Attribute FACE = new Attribute("face");
  256. public static final Attribute DIR = new Attribute("dir");
  257. public static final Attribute DECLARE = new Attribute("declare");
  258. public static final Attribute CLASSID = new Attribute("classid");
  259. public static final Attribute DATA = new Attribute("data");
  260. public static final Attribute CODETYPE = new Attribute("codetype");
  261. public static final Attribute STANDBY = new Attribute("standby");
  262. public static final Attribute BORDER = new Attribute("border");
  263. public static final Attribute SHAPES = new Attribute("shapes");
  264. public static final Attribute NOSHADE = new Attribute("noshade");
  265. public static final Attribute COMPACT = new Attribute("compact");
  266. public static final Attribute START = new Attribute("start");
  267. public static final Attribute ACTION = new Attribute("action");
  268. public static final Attribute METHOD = new Attribute("method");
  269. public static final Attribute ENCTYPE = new Attribute("enctype");
  270. public static final Attribute CHECKED = new Attribute("checked");
  271. public static final Attribute MAXLENGTH = new Attribute("maxlength");
  272. public static final Attribute MULTIPLE = new Attribute("multiple");
  273. public static final Attribute SELECTED = new Attribute("selected");
  274. public static final Attribute ROWS = new Attribute("rows");
  275. public static final Attribute COLS = new Attribute("cols");
  276. public static final Attribute DUMMY = new Attribute("dummy");
  277. public static final Attribute CELLSPACING = new Attribute("cellspacing");
  278. public static final Attribute CELLPADDING = new Attribute("cellpadding");
  279. public static final Attribute VALIGN = new Attribute("valign");
  280. public static final Attribute HALIGN = new Attribute("halign");
  281. public static final Attribute NOWRAP = new Attribute("nowrap");
  282. public static final Attribute ROWSPAN = new Attribute("rowspan");
  283. public static final Attribute COLSPAN = new Attribute("colspan");
  284. public static final Attribute PROMPT = new Attribute("prompt");
  285. public static final Attribute HTTPEQUIV = new Attribute("http-equiv");
  286. public static final Attribute CONTENT = new Attribute("content");
  287. public static final Attribute LANGUAGE = new Attribute("language");
  288. public static final Attribute VERSION = new Attribute("version");
  289. public static final Attribute N = new Attribute("n");
  290. public static final Attribute FRAMEBORDER = new Attribute("frameborder");
  291. public static final Attribute MARGINWIDTH = new Attribute("marginwidth");
  292. public static final Attribute MARGINHEIGHT = new Attribute("marginheight");
  293. public static final Attribute SCROLLING = new Attribute("scrolling");
  294. public static final Attribute NORESIZE = new Attribute("noresize");
  295. public static final Attribute ENDTAG = new Attribute("endtag");
  296. public static final Attribute COMMENT = new Attribute("comment");
  297. static final Attribute MEDIA = new Attribute("media");
  298. static final Attribute allAttributes[] = {
  299. FACE,
  300. COMMENT,
  301. SIZE,
  302. COLOR,
  303. CLEAR,
  304. BACKGROUND,
  305. BGCOLOR,
  306. TEXT,
  307. LINK,
  308. VLINK,
  309. ALINK,
  310. WIDTH,
  311. HEIGHT,
  312. ALIGN,
  313. NAME,
  314. HREF,
  315. REL,
  316. REV,
  317. TITLE,
  318. TARGET,
  319. SHAPE,
  320. COORDS,
  321. ISMAP,
  322. NOHREF,
  323. ALT,
  324. ID,
  325. SRC,
  326. HSPACE,
  327. VSPACE,
  328. USEMAP,
  329. LOWSRC,
  330. CODEBASE,
  331. CODE,
  332. ARCHIVE,
  333. VALUE,
  334. VALUETYPE,
  335. TYPE,
  336. CLASS,
  337. STYLE,
  338. LANG,
  339. DIR,
  340. DECLARE,
  341. CLASSID,
  342. DATA,
  343. CODETYPE,
  344. STANDBY,
  345. BORDER,
  346. SHAPES,
  347. NOSHADE,
  348. COMPACT,
  349. START,
  350. ACTION,
  351. METHOD,
  352. ENCTYPE,
  353. CHECKED,
  354. MAXLENGTH,
  355. MULTIPLE,
  356. SELECTED,
  357. ROWS,
  358. COLS,
  359. DUMMY,
  360. CELLSPACING,
  361. CELLPADDING,
  362. VALIGN,
  363. HALIGN,
  364. NOWRAP,
  365. ROWSPAN,
  366. COLSPAN,
  367. PROMPT,
  368. HTTPEQUIV,
  369. CONTENT,
  370. LANGUAGE,
  371. VERSION,
  372. N,
  373. FRAMEBORDER,
  374. MARGINWIDTH,
  375. MARGINHEIGHT,
  376. SCROLLING,
  377. NORESIZE,
  378. MEDIA,
  379. ENDTAG
  380. };
  381. }
  382. // The secret to 71, is that, given that the Hashtable contents
  383. // never change once the static initialization happens, the initial size
  384. // that the hashtable grew to was determined, and then that very size
  385. // is used.
  386. //
  387. private static final Hashtable tagHashtable = new Hashtable(71);
  388. static {
  389. for (int i = 0; i < Tag.allTags.length; i++ ) {
  390. tagHashtable.put(Tag.allTags[i].toString(), Tag.allTags[i]);
  391. StyleContext.registerStaticAttributeKey(Tag.allTags[i]);
  392. }
  393. StyleContext.registerStaticAttributeKey(Tag.IMPLIED);
  394. StyleContext.registerStaticAttributeKey(Tag.CONTENT);
  395. StyleContext.registerStaticAttributeKey(Tag.COMMENT);
  396. for (int i = 0; i < Attribute.allAttributes.length; i++) {
  397. StyleContext.registerStaticAttributeKey(Attribute.
  398. allAttributes[i]);
  399. }
  400. }
  401. /**
  402. * This is the set of actual html tags that
  403. * are known about the the default html reader.
  404. * This set does not include tags that are
  405. * manufactured by the reader.
  406. */
  407. public static Tag[] getAllTags() {
  408. Tag[] tags = new Tag[Tag.allTags.length];
  409. System.arraycopy(Tag.allTags, 0, tags, 0, Tag.allTags.length);
  410. return tags;
  411. }
  412. /**
  413. * Fetch a tag constant for a well-known tag name (i.e. one of
  414. * the tags in the set <code>allTags</code>). If the given
  415. * name does not represent one of the well-known tags, then
  416. * null will be returned.
  417. */
  418. public static Tag getTag(String tagName) {
  419. Object t = tagHashtable.get(tagName);
  420. return (t == null ? null : (Tag)t);
  421. }
  422. /**
  423. * Fetch an integer attribute value. Attribute values
  424. * are stored as a string, and this is a convenience method
  425. * to convert to an actual integer.
  426. *
  427. * @param attr the set of attributes to use to try to fetch a value
  428. * @param key the key to use to fetch the value.
  429. * @param def the default value to use if the attribute isn't
  430. * defined or there is an error converting to an integer.
  431. */
  432. public static int getIntegerAttributeValue(AttributeSet attr,
  433. Attribute key, int def) {
  434. int value = def;
  435. String istr = (String) attr.getAttribute(key);
  436. if (istr != null) {
  437. try {
  438. value = Integer.valueOf(istr).intValue();
  439. } catch (NumberFormatException e) {
  440. value = def;
  441. }
  442. }
  443. return value;
  444. }
  445. // This is used in cases where the value for the attribute has not
  446. // been specified.
  447. //
  448. public static final String NULL_ATTRIBUTE_VALUE = "#DEFAULT";
  449. // size determined similar to size of tagHashtable
  450. private static final Hashtable attHashtable = new Hashtable(77);
  451. static {
  452. for (int i = 0; i < Attribute.allAttributes.length; i++ ) {
  453. attHashtable.put(Attribute.allAttributes[i].toString(), Attribute.allAttributes[i]);
  454. }
  455. }
  456. /**
  457. * This is the set of html attributes recognized.
  458. */
  459. public static Attribute[] getAllAttributeKeys() {
  460. Attribute[] attributes = new Attribute[Attribute.allAttributes.length];
  461. System.arraycopy(Attribute.allAttributes, 0,
  462. attributes, 0, Attribute.allAttributes.length);
  463. return attributes;
  464. }
  465. /**
  466. * Fetch an attribute constant for a well-known attribute name
  467. * (i.e. one of the attributes in the set <code>allAttributes</code>).
  468. * If the given name does not represent one of the well-known attributes,
  469. * then null will be returned.
  470. */
  471. public static Attribute getAttributeKey(String attName) {
  472. Object a = attHashtable.get(attName);
  473. if (a == null) {
  474. return null;
  475. }
  476. return (Attribute)a;
  477. }
  478. }