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