1 7 package javax.swing.text.html; 8 9 import java.io.*; 10 import java.util.Hashtable ; 11 import javax.swing.text.AttributeSet ; 12 import javax.swing.text.StyleConstants ; 13 import javax.swing.text.StyleContext ; 14 15 24 public class HTML { 25 26 33 public static class Tag { 34 35 36 public Tag() {} 37 38 45 protected Tag(String id) { 46 this(id, false, false); 47 } 48 49 60 protected Tag(String id, boolean causesBreak, boolean isBlock) { 61 name = id; 62 this.breakTag = causesBreak; 63 this.blockTag = isBlock; 64 } 65 66 74 public boolean isBlock() { 75 return blockTag; 76 } 77 78 87 public boolean breaksFlow() { 88 return breakTag; 89 } 90 91 99 public boolean isPreformatted() { 100 return (this == PRE || this == TEXTAREA); 101 } 102 103 109 public String toString() { 110 return name; 111 } 112 113 boolean blockTag; 114 boolean breakTag; 115 String name; 116 boolean unknown; 117 118 120 public static final Tag A = new Tag("a"); 121 public static final Tag ADDRESS = new Tag("address"); 122 public static final Tag APPLET = new Tag("applet"); 123 public static final Tag AREA = new Tag("area"); 124 public static final Tag B = new Tag("b"); 125 public static final Tag BASE = new Tag("base"); 126 public static final Tag BASEFONT = new Tag("basefont"); 127 public static final Tag BIG = new Tag("big"); 128 public static final Tag BLOCKQUOTE = new Tag("blockquote", true, true); 129 public static final Tag BODY = new Tag("body", true, true); 130 public static final Tag BR = new Tag("br", true, false); 131 public static final Tag CAPTION = new Tag("caption"); 132 public static final Tag CENTER = new Tag("center", true, false); 133 public static final Tag CITE = new Tag("cite"); 134 public static final Tag CODE = new Tag("code"); 135 public static final Tag DD = new Tag("dd", true, true); 136 public static final Tag DFN = new Tag("dfn"); 137 public static final Tag DIR = new Tag("dir", true, true); 138 public static final Tag DIV = new Tag("div", true, true); 139 public static final Tag DL = new Tag("dl", true, true); 140 public static final Tag DT = new Tag("dt", true, true); 141 public static final Tag EM = new Tag("em"); 142 public static final Tag FONT = new Tag("font"); 143 public static final Tag FORM = new Tag("form", true, false); 144 public static final Tag FRAME = new Tag("frame"); 145 public static final Tag FRAMESET = new Tag("frameset"); 146 public static final Tag H1 = new Tag("h1", true, true); 147 public static final Tag H2 = new Tag("h2", true, true); 148 public static final Tag H3 = new Tag("h3", true, true); 149 public static final Tag H4 = new Tag("h4", true, true); 150 public static final Tag H5 = new Tag("h5", true, true); 151 public static final Tag H6 = new Tag("h6", true, true); 152 public static final Tag HEAD = new Tag("head", true, true); 153 public static final Tag HR = new Tag("hr", true, false); 154 public static final Tag HTML = new Tag("html", true, false); 155 public static final Tag I = new Tag("i"); 156 public static final Tag IMG = new Tag("img"); 157 public static final Tag INPUT = new Tag("input"); 158 public static final Tag ISINDEX = new Tag("isindex", true, false); 159 public static final Tag KBD = new Tag("kbd"); 160 public static final Tag LI = new Tag("li", true, true); 161 public static final Tag LINK = new Tag("link"); 162 public static final Tag MAP = new Tag("map"); 163 public static final Tag MENU = new Tag("menu", true, true); 164 public static final Tag META = new Tag("meta"); 165 static final Tag NOBR = new Tag("nobr"); 166 public static final Tag NOFRAMES = new Tag("noframes", true, true); 167 public static final Tag OBJECT = new Tag("object"); 168 public static final Tag OL = new Tag("ol", true, true); 169 public static final Tag OPTION = new Tag("option"); 170 public static final Tag P = new Tag("p", true, true); 171 public static final Tag PARAM = new Tag("param"); 172 public static final Tag PRE = new Tag("pre", true, true); 173 public static final Tag SAMP = new Tag("samp"); 174 public static final Tag SCRIPT = new Tag("script"); 175 public static final Tag SELECT = new Tag("select"); 176 public static final Tag SMALL = new Tag("small"); 177 public static final Tag SPAN = new Tag("span"); 178 public static final Tag STRIKE = new Tag("strike"); 179 public static final Tag S = new Tag("s"); 180 public static final Tag STRONG = new Tag("strong"); 181 public static final Tag STYLE = new Tag("style"); 182 public static final Tag SUB = new Tag("sub"); 183 public static final Tag SUP = new Tag("sup"); 184 public static final Tag TABLE = new Tag("table", false, true); 185 public static final Tag TD = new Tag("td", true, true); 186 public static final Tag TEXTAREA = new Tag("textarea"); 187 public static final Tag TH = new Tag("th", true, true); 188 public static final Tag TITLE = new Tag("title", true, true); 189 public static final Tag TR = new Tag("tr", false, true); 190 public static final Tag TT = new Tag("tt"); 191 public static final Tag U = new Tag("u"); 192 public static final Tag UL = new Tag("ul", true, true); 193 public static final Tag VAR = new Tag("var"); 194 195 205 public static final Tag IMPLIED = new Tag("p-implied"); 206 207 215 public static final Tag CONTENT = new Tag("content"); 216 217 225 public static final Tag COMMENT = new Tag("comment"); 226 227 static final Tag allTags[] = { 228 A, ADDRESS, APPLET, AREA, B, BASE, BASEFONT, BIG, 229 BLOCKQUOTE, BODY, BR, CAPTION, CENTER, CITE, CODE, 230 DD, DFN, DIR, DIV, DL, DT, EM, FONT, FORM, FRAME, 231 FRAMESET, H1, H2, H3, H4, H5, H6, HEAD, HR, HTML, 232 I, IMG, INPUT, ISINDEX, KBD, LI, LINK, MAP, MENU, 233 META, NOBR, NOFRAMES, OBJECT, OL, OPTION, P, PARAM, 234 PRE, SAMP, SCRIPT, SELECT, SMALL, SPAN, STRIKE, S, 235 STRONG, STYLE, SUB, SUP, TABLE, TD, TEXTAREA, 236 TH, TITLE, TR, TT, U, UL, VAR 237 }; 238 239 static { 240 getTag("html"); 242 } 243 } 244 245 public static class UnknownTag extends Tag implements Serializable { 248 249 254 public UnknownTag(String id) { 255 super(id); 256 } 257 258 262 public int hashCode() { 263 return toString().hashCode(); 264 } 265 266 276 public boolean equals(Object obj) { 277 if (obj instanceof UnknownTag) { 278 return toString().equals(obj.toString()); 279 } 280 return false; 281 } 282 283 private void writeObject(java.io.ObjectOutputStream s) 284 throws IOException { 285 s.defaultWriteObject(); 286 s.writeBoolean(blockTag); 287 s.writeBoolean(breakTag); 288 s.writeBoolean(unknown); 289 s.writeObject(name); 290 } 291 292 private void readObject(ObjectInputStream s) 293 throws ClassNotFoundException , IOException { 294 s.defaultReadObject(); 295 blockTag = s.readBoolean(); 296 breakTag = s.readBoolean(); 297 unknown = s.readBoolean(); 298 name = (String )s.readObject(); 299 } 300 } 301 302 306 public static final class Attribute { 307 308 314 Attribute(String id) { 315 name = id; 316 } 317 318 322 public String toString() { 323 return name; 324 } 325 326 private String name; 327 328 public static final Attribute SIZE = new Attribute("size"); 329 public static final Attribute COLOR = new Attribute("color"); 330 public static final Attribute CLEAR = new Attribute("clear"); 331 public static final Attribute BACKGROUND = new Attribute("background"); 332 public static final Attribute BGCOLOR = new Attribute("bgcolor"); 333 public static final Attribute TEXT = new Attribute("text"); 334 public static final Attribute LINK = new Attribute("link"); 335 public static final Attribute VLINK = new Attribute("vlink"); 336 public static final Attribute ALINK = new Attribute("alink"); 337 public static final Attribute WIDTH = new Attribute("width"); 338 public static final Attribute HEIGHT = new Attribute("height"); 339 public static final Attribute ALIGN = new Attribute("align"); 340 public static final Attribute NAME = new Attribute("name"); 341 public static final Attribute HREF = new Attribute("href"); 342 public static final Attribute REL = new Attribute("rel"); 343 public static final Attribute REV = new Attribute("rev"); 344 public static final Attribute TITLE = new Attribute("title"); 345 public static final Attribute TARGET = new Attribute("target"); 346 public static final Attribute SHAPE = new Attribute("shape"); 347 public static final Attribute COORDS = new Attribute("coords"); 348 public static final Attribute ISMAP = new Attribute("ismap"); 349 public static final Attribute NOHREF = new Attribute("nohref"); 350 public static final Attribute ALT = new Attribute("alt"); 351 public static final Attribute ID = new Attribute("id"); 352 public static final Attribute SRC = new Attribute("src"); 353 public static final Attribute HSPACE = new Attribute("hspace"); 354 public static final Attribute VSPACE = new Attribute("vspace"); 355 public static final Attribute USEMAP = new Attribute("usemap"); 356 public static final Attribute LOWSRC = new Attribute("lowsrc"); 357 public static final Attribute CODEBASE = new Attribute("codebase"); 358 public static final Attribute CODE = new Attribute("code"); 359 public static final Attribute ARCHIVE = new Attribute("archive"); 360 public static final Attribute VALUE = new Attribute("value"); 361 public static final Attribute VALUETYPE = new Attribute("valuetype"); 362 public static final Attribute TYPE = new Attribute("type"); 363 public static final Attribute CLASS = new Attribute("class"); 364 public static final Attribute STYLE = new Attribute("style"); 365 public static final Attribute LANG = new Attribute("lang"); 366 public static final Attribute FACE = new Attribute("face"); 367 public static final Attribute DIR = new Attribute("dir"); 368 public static final Attribute DECLARE = new Attribute("declare"); 369 public static final Attribute CLASSID = new Attribute("classid"); 370 public static final Attribute DATA = new Attribute("data"); 371 public static final Attribute CODETYPE = new Attribute("codetype"); 372 public static final Attribute STANDBY = new Attribute("standby"); 373 public static final Attribute BORDER = new Attribute("border"); 374 public static final Attribute SHAPES = new Attribute("shapes"); 375 public static final Attribute NOSHADE = new Attribute("noshade"); 376 public static final Attribute COMPACT = new Attribute("compact"); 377 public static final Attribute START = new Attribute("start"); 378 public static final Attribute ACTION = new Attribute("action"); 379 public static final Attribute METHOD = new Attribute("method"); 380 public static final Attribute ENCTYPE = new Attribute("enctype"); 381 public static final Attribute CHECKED = new Attribute("checked"); 382 public static final Attribute MAXLENGTH = new Attribute("maxlength"); 383 public static final Attribute MULTIPLE = new Attribute("multiple"); 384 public static final Attribute SELECTED = new Attribute("selected"); 385 public static final Attribute ROWS = new Attribute("rows"); 386 public static final Attribute COLS = new Attribute("cols"); 387 public static final Attribute DUMMY = new Attribute("dummy"); 388 public static final Attribute CELLSPACING = new Attribute("cellspacing"); 389 public static final Attribute CELLPADDING = new Attribute("cellpadding"); 390 public static final Attribute VALIGN = new Attribute("valign"); 391 public static final Attribute HALIGN = new Attribute("halign"); 392 public static final Attribute NOWRAP = new Attribute("nowrap"); 393 public static final Attribute ROWSPAN = new Attribute("rowspan"); 394 public static final Attribute COLSPAN = new Attribute("colspan"); 395 public static final Attribute PROMPT = new Attribute("prompt"); 396 public static final Attribute HTTPEQUIV = new Attribute("http-equiv"); 397 public static final Attribute CONTENT = new Attribute("content"); 398 public static final Attribute LANGUAGE = new Attribute("language"); 399 public static final Attribute VERSION = new Attribute("version"); 400 public static final Attribute N = new Attribute("n"); 401 public static final Attribute FRAMEBORDER = new Attribute("frameborder"); 402 public static final Attribute MARGINWIDTH = new Attribute("marginwidth"); 403 public static final Attribute MARGINHEIGHT = new Attribute("marginheight"); 404 public static final Attribute SCROLLING = new Attribute("scrolling"); 405 public static final Attribute NORESIZE = new Attribute("noresize"); 406 public static final Attribute ENDTAG = new Attribute("endtag"); 407 public static final Attribute COMMENT = new Attribute("comment"); 408 static final Attribute MEDIA = new Attribute("media"); 409 410 static final Attribute allAttributes[] = { 411 FACE, 412 COMMENT, 413 SIZE, 414 COLOR, 415 CLEAR, 416 BACKGROUND, 417 BGCOLOR, 418 TEXT, 419 LINK, 420 VLINK, 421 ALINK, 422 WIDTH, 423 HEIGHT, 424 ALIGN, 425 NAME, 426 HREF, 427 REL, 428 REV, 429 TITLE, 430 TARGET, 431 SHAPE, 432 COORDS, 433 ISMAP, 434 NOHREF, 435 ALT, 436 ID, 437 SRC, 438 HSPACE, 439 VSPACE, 440 USEMAP, 441 LOWSRC, 442 CODEBASE, 443 CODE, 444 ARCHIVE, 445 VALUE, 446 VALUETYPE, 447 TYPE, 448 CLASS, 449 STYLE, 450 LANG, 451 DIR, 452 DECLARE, 453 CLASSID, 454 DATA, 455 CODETYPE, 456 STANDBY, 457 BORDER, 458 SHAPES, 459 NOSHADE, 460 COMPACT, 461 START, 462 ACTION, 463 METHOD, 464 ENCTYPE, 465 CHECKED, 466 MAXLENGTH, 467 MULTIPLE, 468 SELECTED, 469 ROWS, 470 COLS, 471 DUMMY, 472 CELLSPACING, 473 CELLPADDING, 474 VALIGN, 475 HALIGN, 476 NOWRAP, 477 ROWSPAN, 478 COLSPAN, 479 PROMPT, 480 HTTPEQUIV, 481 CONTENT, 482 LANGUAGE, 483 VERSION, 484 N, 485 FRAMEBORDER, 486 MARGINWIDTH, 487 MARGINHEIGHT, 488 SCROLLING, 489 NORESIZE, 490 MEDIA, 491 ENDTAG 492 }; 493 } 494 495 private static final Hashtable tagHashtable = new Hashtable (73); 501 502 503 private static final Hashtable scMapping = new Hashtable (8); 504 505 static { 506 507 for (int i = 0; i < Tag.allTags.length; i++ ) { 508 tagHashtable.put(Tag.allTags[i].toString(), Tag.allTags[i]); 509 StyleContext.registerStaticAttributeKey(Tag.allTags[i]); 510 } 511 StyleContext.registerStaticAttributeKey(Tag.IMPLIED); 512 StyleContext.registerStaticAttributeKey(Tag.CONTENT); 513 StyleContext.registerStaticAttributeKey(Tag.COMMENT); 514 for (int i = 0; i < Attribute.allAttributes.length; i++) { 515 StyleContext.registerStaticAttributeKey(Attribute. 516 allAttributes[i]); 517 } 518 StyleContext.registerStaticAttributeKey(HTML.NULL_ATTRIBUTE_VALUE); 519 scMapping.put(StyleConstants.Bold, Tag.B); 520 scMapping.put(StyleConstants.Italic, Tag.I); 521 scMapping.put(StyleConstants.Underline, Tag.U); 522 scMapping.put(StyleConstants.StrikeThrough, Tag.STRIKE); 523 scMapping.put(StyleConstants.Superscript, Tag.SUP); 524 scMapping.put(StyleConstants.Subscript, Tag.SUB); 525 scMapping.put(StyleConstants.FontFamily, Tag.FONT); 526 scMapping.put(StyleConstants.FontSize, Tag.FONT); 527 } 528 529 535 public static Tag[] getAllTags() { 536 Tag[] tags = new Tag[Tag.allTags.length]; 537 System.arraycopy(Tag.allTags, 0, tags, 0, Tag.allTags.length); 538 return tags; 539 } 540 541 560 public static Tag getTag(String tagName) { 561 562 Object t = tagHashtable.get(tagName); 563 return (t == null ? null : (Tag)t); 564 } 565 566 576 static Tag getTagForStyleConstantsKey(StyleConstants sc) { 577 return (Tag)scMapping.get(sc); 578 } 579 580 590 public static int getIntegerAttributeValue(AttributeSet attr, 591 Attribute key, int def) { 592 int value = def; 593 String istr = (String ) attr.getAttribute(key); 594 if (istr != null) { 595 try { 596 value = Integer.valueOf(istr).intValue(); 597 } catch (NumberFormatException e) { 598 value = def; 599 } 600 } 601 return value; 602 } 603 604 public static final String NULL_ATTRIBUTE_VALUE = "#DEFAULT"; 608 609 private static final Hashtable attHashtable = new Hashtable (77); 611 612 static { 613 614 for (int i = 0; i < Attribute.allAttributes.length; i++ ) { 615 attHashtable.put(Attribute.allAttributes[i].toString(), Attribute.allAttributes[i]); 616 } 617 } 618 619 623 public static Attribute[] getAllAttributeKeys() { 624 Attribute[] attributes = new Attribute[Attribute.allAttributes.length]; 625 System.arraycopy(Attribute.allAttributes, 0, 626 attributes, 0, Attribute.allAttributes.length); 627 return attributes; 628 } 629 630 650 public static Attribute getAttributeKey(String attName) { 651 Object a = attHashtable.get(attName); 652 if (a == null) { 653 return null; 654 } 655 return (Attribute)a; 656 } 657 658 } 659 | Popular Tags |