Source for javax.swing.text.html.HTML

   1: /* HTML.java -- HTML document tag constants
   2:    Copyright (C) 2002 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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)
   9: any later version.
  10: 
  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.
  15: 
  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
  19: 02110-1301 USA.
  20: 
  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
  24: combination.
  25: 
  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. */
  37: 
  38: 
  39: package javax.swing.text.html;
  40: 
  41: import java.io.Serializable;
  42: 
  43: import java.lang.reflect.Field;
  44: import java.lang.reflect.Modifier;
  45: 
  46: import java.util.Map;
  47: import java.util.TreeMap;
  48: 
  49: import javax.swing.text.AttributeSet;
  50: 
  51: /**
  52:  * HTML attribute and tag definitions.
  53:  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  54:  */
  55: public class HTML
  56: {
  57:   /**
  58:    * Represents a HTML attribute.
  59:    */
  60:   public static final class Attribute
  61:   {
  62:     /**
  63:      * The action attribute
  64:      */
  65:     public static final Attribute ACTION = new Attribute("action");
  66: 
  67:     /**
  68:      * The align attribute
  69:      */
  70:     public static final Attribute ALIGN = new Attribute("align");
  71: 
  72:     /**
  73:      * The alink attribute
  74:      */
  75:     public static final Attribute ALINK = new Attribute("alink");
  76: 
  77:     /**
  78:      * The alt attribute
  79:      */
  80:     public static final Attribute ALT = new Attribute("alt");
  81: 
  82:     /**
  83:      * The archive attribute
  84:      */
  85:     public static final Attribute ARCHIVE = new Attribute("archive");
  86: 
  87:     /**
  88:      * The background attribute
  89:      */
  90:     public static final Attribute BACKGROUND = new Attribute("background");
  91: 
  92:     /**
  93:      * The bgcolor attribute
  94:      */
  95:     public static final Attribute BGCOLOR = new Attribute("bgcolor");
  96: 
  97:     /**
  98:      * The border attribute
  99:      */
 100:     public static final Attribute BORDER = new Attribute("border");
 101: 
 102:     /**
 103:      * The cellpadding attribute
 104:      */
 105:     public static final Attribute CELLPADDING = new Attribute("cellpadding");
 106: 
 107:     /**
 108:      * The cellspacing attribute
 109:      */
 110:     public static final Attribute CELLSPACING = new Attribute("cellspacing");
 111: 
 112:     /**
 113:      * The checked attribute
 114:      */
 115:     public static final Attribute CHECKED = new Attribute("checked");
 116: 
 117:     /**
 118:      * The class attribute
 119:      */
 120:     public static final Attribute CLASS = new Attribute("class");
 121: 
 122:     /**
 123:      * The classid attribute
 124:      */
 125:     public static final Attribute CLASSID = new Attribute("classid");
 126: 
 127:     /**
 128:      * The clear attribute
 129:      */
 130:     public static final Attribute CLEAR = new Attribute("clear");
 131: 
 132:     /**
 133:      * The code attribute
 134:      */
 135:     public static final Attribute CODE = new Attribute("code");
 136: 
 137:     /**
 138:      * The codebase attribute
 139:      */
 140:     public static final Attribute CODEBASE = new Attribute("codebase");
 141: 
 142:     /**
 143:      * The codetype attribute
 144:      */
 145:     public static final Attribute CODETYPE = new Attribute("codetype");
 146: 
 147:     /**
 148:      * The color attribute
 149:      */
 150:     public static final Attribute COLOR = new Attribute("color");
 151: 
 152:     /**
 153:      * The cols attribute
 154:      */
 155:     public static final Attribute COLS = new Attribute("cols");
 156: 
 157:     /**
 158:      * The colspan attribute
 159:      */
 160:     public static final Attribute COLSPAN = new Attribute("colspan");
 161: 
 162:     /**
 163:      * The comment attribute
 164:      */
 165:     public static final Attribute COMMENT = new Attribute("comment");
 166: 
 167:     /**
 168:      * The compact attribute
 169:      */
 170:     public static final Attribute COMPACT = new Attribute("compact");
 171: 
 172:     /**
 173:      * The content attribute
 174:      */
 175:     public static final Attribute CONTENT = new Attribute("content");
 176: 
 177:     /**
 178:      * The coords attribute
 179:      */
 180:     public static final Attribute COORDS = new Attribute("coords");
 181: 
 182:     /**
 183:      * The data attribute
 184:      */
 185:     public static final Attribute DATA = new Attribute("data");
 186: 
 187:     /**
 188:      * The declare attribute
 189:      */
 190:     public static final Attribute DECLARE = new Attribute("declare");
 191: 
 192:     /**
 193:      * The dir attribute
 194:      */
 195:     public static final Attribute DIR = new Attribute("dir");
 196: 
 197:     /**
 198:      * The dummy attribute
 199:      */
 200:     public static final Attribute DUMMY = new Attribute("dummy");
 201: 
 202:     /**
 203:      * The enctype attribute
 204:      */
 205:     public static final Attribute ENCTYPE = new Attribute("enctype");
 206: 
 207:     /**
 208:      * The endtag attribute
 209:      */
 210:     public static final Attribute ENDTAG = new Attribute("endtag");
 211: 
 212:     /**
 213:      *  The face attribute
 214:      */
 215:     public static final Attribute FACE = new Attribute("face");
 216: 
 217:     /**
 218:      *  The frameborder attribute
 219:      */
 220:     public static final Attribute FRAMEBORDER = new Attribute("frameborder");
 221: 
 222:     /**
 223:      *  The halign attribute
 224:      */
 225:     public static final Attribute HALIGN = new Attribute("halign");
 226: 
 227:     /**
 228:      *  The height attribute
 229:      */
 230:     public static final Attribute HEIGHT = new Attribute("height");
 231: 
 232:     /**
 233:      *  The href attribute
 234:      */
 235:     public static final Attribute HREF = new Attribute("href");
 236: 
 237:     /**
 238:      *  The hspace attribute
 239:      */
 240:     public static final Attribute HSPACE = new Attribute("hspace");
 241: 
 242:     /**
 243:      *  The http-equiv attribute
 244:      */
 245:     public static final Attribute HTTPEQUIV = new Attribute("http-equiv");
 246: 
 247:     /**
 248:      *  The id attribute
 249:      */
 250:     public static final Attribute ID = new Attribute("id");
 251: 
 252:     /**
 253:      *  The ismap attribute
 254:      */
 255:     public static final Attribute ISMAP = new Attribute("ismap");
 256: 
 257:     /**
 258:      *  The lang attribute
 259:      */
 260:     public static final Attribute LANG = new Attribute("lang");
 261: 
 262:     /**
 263:      *  The language attribute
 264:      */
 265:     public static final Attribute LANGUAGE = new Attribute("language");
 266: 
 267:     /**
 268:      *  The link attribute
 269:      */
 270:     public static final Attribute LINK = new Attribute("link");
 271: 
 272:     /**
 273:      *  The lowsrc attribute
 274:      */
 275:     public static final Attribute LOWSRC = new Attribute("lowsrc");
 276: 
 277:     /**
 278:      *  The marginheight attribute
 279:      */
 280:     public static final Attribute MARGINHEIGHT = new Attribute("marginheight");
 281: 
 282:     /**
 283:      *  The marginwidth attribute
 284:      */
 285:     public static final Attribute MARGINWIDTH = new Attribute("marginwidth");
 286: 
 287:     /**
 288:      *  The maxlength attribute
 289:      */
 290:     public static final Attribute MAXLENGTH = new Attribute("maxlength");
 291: 
 292:     /**
 293:      *  The media attribute
 294:      */
 295:     static final Attribute MEDIA = new Attribute("media");
 296: 
 297:     /**
 298:      *  The method attribute
 299:      */
 300:     public static final Attribute METHOD = new Attribute("method");
 301: 
 302:     /**
 303:      *  The multiple attribute
 304:      */
 305:     public static final Attribute MULTIPLE = new Attribute("multiple");
 306: 
 307:     /**
 308:      *  The n attribute
 309:      */
 310:     public static final Attribute N = new Attribute("n");
 311: 
 312:     /**
 313:      *  The name attribute
 314:      */
 315:     public static final Attribute NAME = new Attribute("name");
 316: 
 317:     /**
 318:      *  The nohref attribute
 319:      */
 320:     public static final Attribute NOHREF = new Attribute("nohref");
 321: 
 322:     /**
 323:      *  The noresize attribute
 324:      */
 325:     public static final Attribute NORESIZE = new Attribute("noresize");
 326: 
 327:     /**
 328:      *  The noshade attribute
 329:      */
 330:     public static final Attribute NOSHADE = new Attribute("noshade");
 331: 
 332:     /**
 333:      *  The nowrap attribute
 334:      */
 335:     public static final Attribute NOWRAP = new Attribute("nowrap");
 336: 
 337:     /**
 338:      *  The prompt attribute
 339:      */
 340:     public static final Attribute PROMPT = new Attribute("prompt");
 341: 
 342:     /**
 343:      *  The rel attribute
 344:      */
 345:     public static final Attribute REL = new Attribute("rel");
 346: 
 347:     /**
 348:      *  The rev attribute
 349:      */
 350:     public static final Attribute REV = new Attribute("rev");
 351: 
 352:     /**
 353:      *  The rows attribute
 354:      */
 355:     public static final Attribute ROWS = new Attribute("rows");
 356: 
 357:     /**
 358:      *  The rowspan attribute
 359:      */
 360:     public static final Attribute ROWSPAN = new Attribute("rowspan");
 361: 
 362:     /**
 363:      *  The scrolling attribute
 364:      */
 365:     public static final Attribute SCROLLING = new Attribute("scrolling");
 366: 
 367:     /**
 368:      *  The selected attribute
 369:      */
 370:     public static final Attribute SELECTED = new Attribute("selected");
 371: 
 372:     /**
 373:      *  The shape attribute
 374:      */
 375:     public static final Attribute SHAPE = new Attribute("shape");
 376: 
 377:     /**
 378:      *  The shapes attribute
 379:      */
 380:     public static final Attribute SHAPES = new Attribute("shapes");
 381: 
 382:     /**
 383:      *  The size attribute
 384:      */
 385:     public static final Attribute SIZE = new Attribute("size");
 386: 
 387:     /**
 388:      *  The src attribute
 389:      */
 390:     public static final Attribute SRC = new Attribute("src");
 391: 
 392:     /**
 393:      *  The standby attribute
 394:      */
 395:     public static final Attribute STANDBY = new Attribute("standby");
 396: 
 397:     /**
 398:      *  The start attribute
 399:      */
 400:     public static final Attribute START = new Attribute("start");
 401: 
 402:     /**
 403:      *  The style attribute
 404:      */
 405:     public static final Attribute STYLE = new Attribute("style");
 406: 
 407:     /**
 408:      *  The target attribute
 409:      */
 410:     public static final Attribute TARGET = new Attribute("target");
 411: 
 412:     /**
 413:      *  The text attribute
 414:      */
 415:     public static final Attribute TEXT = new Attribute("text");
 416: 
 417:     /**
 418:      *  The title attribute
 419:      */
 420:     public static final Attribute TITLE = new Attribute("title");
 421: 
 422:     /**
 423:      *  The type attribute
 424:      */
 425:     public static final Attribute TYPE = new Attribute("type");
 426: 
 427:     /**
 428:      *  The usemap attribute
 429:      */
 430:     public static final Attribute USEMAP = new Attribute("usemap");
 431: 
 432:     /**
 433:      *  The valign attribute
 434:      */
 435:     public static final Attribute VALIGN = new Attribute("valign");
 436: 
 437:     /**
 438:      *  The value attribute
 439:      */
 440:     public static final Attribute VALUE = new Attribute("value");
 441: 
 442:     /**
 443:      *  The valuetype attribute
 444:      */
 445:     public static final Attribute VALUETYPE = new Attribute("valuetype");
 446: 
 447:     /**
 448:      *  The version attribute
 449:      */
 450:     public static final Attribute VERSION = new Attribute("version");
 451: 
 452:     /**
 453:      *  The vlink attribute
 454:      */
 455:     public static final Attribute VLINK = new Attribute("vlink");
 456: 
 457:     /**
 458:      *  The vspace attribute
 459:      */
 460:     public static final Attribute VSPACE = new Attribute("vspace");
 461: 
 462:     /**
 463:      *  The width attribute
 464:      */
 465:     public static final Attribute WIDTH = new Attribute("width");
 466: 
 467:     /**
 468:      * The attribute name.
 469:      */
 470:     private final String name;
 471: 
 472:     /**
 473:      * Creates the attribute with the given name.
 474:      */
 475:     private Attribute(String a_name)
 476:     {
 477:       name = a_name;
 478:     }
 479: 
 480:     /**
 481:      * Returns the attribute name. The names of the built-in attributes
 482:      * are always returned in lowercase.
 483:      */
 484:     public String toString()
 485:     {
 486:       return name;
 487:     }
 488: 
 489:     /**
 490:      *  Return an array of all attributes, declared in the HTML.Attribute
 491:      *  class. WARNING: attributes are the only public fields,
 492:      *  expected in this class.
 493:      */
 494:     static Attribute[] getAllAttributes()
 495:     {
 496:       Field[] f = Attribute.class.getFields();
 497:       Attribute[] attrs = new Attribute[ f.length ];
 498:       Field x;
 499:       int p = 0;
 500:       Attribute a;
 501: 
 502:       for (int i = 0; i < f.length; i++)
 503:         {
 504:           x = f [ i ];
 505: 
 506:           if ((x.getModifiers() & Modifier.STATIC) != 0)
 507:             {
 508:               if (x.getType().equals(Attribute.class))
 509:                 {
 510:                   try
 511:                     {
 512:                       a = (Attribute) x.get(null);
 513:                       attrs [ p++ ] = a;
 514:                     }
 515:                   catch (Exception ex)
 516:                     {
 517:                       ex.printStackTrace(System.err);
 518:                       throw new Error("This should never happen, report a bug");
 519:                     }
 520:                 }
 521:             }
 522:         }
 523: 
 524:       return attrs;
 525:     }
 526:   }
 527: 
 528:   /**
 529:    * Represents a HTML tag.
 530:    */
 531:   public static class Tag
 532:   {
 533:     /**
 534:      * The &lt;a&gt; tag
 535:      */
 536:     public static final Tag A = new Tag("a");
 537: 
 538:     /**
 539:      * The &lt;address&gt; tag
 540:      */
 541:     public static final Tag ADDRESS = new Tag("address");
 542: 
 543:     /**
 544:      * The &lt;applet&gt; tag
 545:      */
 546:     public static final Tag APPLET = new Tag("applet");
 547: 
 548:     /**
 549:      * The &lt;area&gt; tag
 550:      */
 551:     public static final Tag AREA = new Tag("area");
 552: 
 553:     /**
 554:      * The &lt;b&gt; tag
 555:      */
 556:     public static final Tag B = new Tag("b");
 557: 
 558:     /**
 559:      * The &lt;base&gt; tag
 560:      */
 561:     public static final Tag BASE = new Tag("base");
 562: 
 563:     /**
 564:      * The &lt;basefont&gt; tag
 565:      */
 566:     public static final Tag BASEFONT = new Tag("basefont");
 567: 
 568:     /**
 569:      * The &lt;big&gt; tag
 570:      */
 571:     public static final Tag BIG = new Tag("big");
 572: 
 573:     /**
 574:      * The &lt;blockquote&gt; tag , breaks flow, block tag.
 575:      */
 576:     public static final Tag BLOCKQUOTE = new Tag("blockquote", BREAKS | BLOCK);
 577: 
 578:     /**
 579:      * The &lt;body&gt; tag , breaks flow, block tag.
 580:      */
 581:     public static final Tag BODY = new Tag("body", BREAKS | BLOCK);
 582: 
 583:     /**
 584:      * The &lt;br&gt; tag , breaks flow.
 585:      */
 586:     public static final Tag BR = new Tag("br", BREAKS);
 587: 
 588:     /**
 589:      * The &lt;caption&gt; tag
 590:      */
 591:     public static final Tag CAPTION = new Tag("caption");
 592: 
 593:     /**
 594:      * The &lt;center&gt; tag , breaks flow.
 595:      */
 596:     public static final Tag CENTER = new Tag("center", BREAKS);
 597: 
 598:     /**
 599:      * The &lt;cite&gt; tag
 600:      */
 601:     public static final Tag CITE = new Tag("cite");
 602: 
 603:     /**
 604:      * The &lt;code&gt; tag
 605:      */
 606:     public static final Tag CODE = new Tag("code");
 607: 
 608:     /**
 609:      * The &lt;dd&gt; tag , breaks flow, block tag.
 610:      */
 611:     public static final Tag DD = new Tag("dd", BREAKS | BLOCK);
 612: 
 613:     /**
 614:      * The &lt;dfn&gt; tag
 615:      */
 616:     public static final Tag DFN = new Tag("dfn");
 617: 
 618:     /**
 619:      * The &lt;dir&gt; tag , breaks flow, block tag.
 620:      */
 621:     public static final Tag DIR = new Tag("dir", BREAKS | BLOCK);
 622: 
 623:     /**
 624:      * The &lt;div&gt; tag , breaks flow, block tag.
 625:      */
 626:     public static final Tag DIV = new Tag("div", BREAKS | BLOCK);
 627: 
 628:     /**
 629:      * The &lt;dl&gt; tag , breaks flow, block tag.
 630:      */
 631:     public static final Tag DL = new Tag("dl", BREAKS | BLOCK);
 632: 
 633:     /**
 634:      * The &lt;dt&gt; tag , breaks flow, block tag.
 635:      */
 636:     public static final Tag DT = new Tag("dt", BREAKS | BLOCK);
 637: 
 638:     /**
 639:      * The &lt;em&gt; tag
 640:      */
 641:     public static final Tag EM = new Tag("em");
 642: 
 643:     /**
 644:      * The &lt;font&gt; tag
 645:      */
 646:     public static final Tag FONT = new Tag("font");
 647: 
 648:     /**
 649:      * The &lt;form&gt; tag , breaks flow.
 650:      */
 651:     public static final Tag FORM = new Tag("form", BREAKS);
 652: 
 653:     /**
 654:      * The &lt;frame&gt; tag
 655:      */
 656:     public static final Tag FRAME = new Tag("frame");
 657: 
 658:     /**
 659:      * The &lt;frameset&gt; tag
 660:      */
 661:     public static final Tag FRAMESET = new Tag("frameset");
 662: 
 663:     /**
 664:      * The &lt;h1&gt; tag , breaks flow, block tag.
 665:      */
 666:     public static final Tag H1 = new Tag("h1", BREAKS | BLOCK);
 667: 
 668:     /**
 669:      * The &lt;h2&gt; tag , breaks flow, block tag.
 670:      */
 671:     public static final Tag H2 = new Tag("h2", BREAKS | BLOCK);
 672: 
 673:     /**
 674:      * The &lt;h3&gt; tag , breaks flow, block tag.
 675:      */
 676:     public static final Tag H3 = new Tag("h3", BREAKS | BLOCK);
 677: 
 678:     /**
 679:      * The &lt;h4&gt; tag , breaks flow, block tag.
 680:      */
 681:     public static final Tag H4 = new Tag("h4", BREAKS | BLOCK);
 682: 
 683:     /**
 684:      * The &lt;h5&gt; tag , breaks flow, block tag.
 685:      */
 686:     public static final Tag H5 = new Tag("h5", BREAKS | BLOCK);
 687: 
 688:     /**
 689:      * The &lt;h6&gt; tag , breaks flow, block tag.
 690:      */
 691:     public static final Tag H6 = new Tag("h6", BREAKS | BLOCK);
 692: 
 693:     /**
 694:      * The &lt;head&gt; tag , breaks flow, block tag.
 695:      */
 696:     public static final Tag HEAD = new Tag("head", BREAKS | BLOCK);
 697: 
 698:     /**
 699:      * The &lt;hr&gt; tag , breaks flow.
 700:      */
 701:     public static final Tag HR = new Tag("hr", BREAKS);
 702: 
 703:     /**
 704:      * The &lt;html&gt; tag , breaks flow.
 705:      */
 706:     public static final Tag HTML = new Tag("html", BREAKS);
 707: 
 708:     /**
 709:      * The &lt;i&gt; tag
 710:      */
 711:     public static final Tag I = new Tag("i");
 712: 
 713:     /**
 714:      * The &lt;img&gt; tag
 715:      */
 716:     public static final Tag IMG = new Tag("img");
 717: 
 718:     /**
 719:      * The &lt;input&gt; tag
 720:      */
 721:     public static final Tag INPUT = new Tag("input");
 722: 
 723:     /**
 724:      * The &lt;isindex&gt; tag , breaks flow.
 725:      */
 726:     public static final Tag ISINDEX = new Tag("isindex", BREAKS);
 727: 
 728:     /**
 729:      * The &lt;kbd&gt; tag
 730:      */
 731:     public static final Tag KBD = new Tag("kbd");
 732: 
 733:     /**
 734:      * The &lt;li&gt; tag , breaks flow, block tag.
 735:      */
 736:     public static final Tag LI = new Tag("li", BREAKS | BLOCK);
 737: 
 738:     /**
 739:      * The &lt;link&gt; tag
 740:      */
 741:     public static final Tag LINK = new Tag("link");
 742: 
 743:     /**
 744:      * The &lt;map&gt; tag
 745:      */
 746:     public static final Tag MAP = new Tag("map");
 747: 
 748:     /**
 749:      * The &lt;menu&gt; tag , breaks flow, block tag.
 750:      */
 751:     public static final Tag MENU = new Tag("menu", BREAKS | BLOCK);
 752: 
 753:     /**
 754:      * The &lt;meta&gt; tag
 755:      */
 756:     public static final Tag META = new Tag("meta");
 757: 
 758:     /**
 759:      * The &lt;nobr&gt; tag
 760:      */
 761:     static final Tag NOBR = new Tag("nobr");
 762: 
 763:     /**
 764:      * The &lt;noframes&gt; tag , breaks flow, block tag.
 765:      */
 766:     public static final Tag NOFRAMES = new Tag("noframes", BREAKS | BLOCK);
 767: 
 768:     /**
 769:      * The &lt;object&gt; tag
 770:      */
 771:     public static final Tag OBJECT = new Tag("object");
 772: 
 773:     /**
 774:      * The &lt;ol&gt; tag , breaks flow, block tag.
 775:      */
 776:     public static final Tag OL = new Tag("ol", BREAKS | BLOCK);
 777: 
 778:     /**
 779:      * The &lt;option&gt; tag
 780:      */
 781:     public static final Tag OPTION = new Tag("option");
 782: 
 783:     /**
 784:      * The &lt;p&gt; tag , breaks flow, block tag.
 785:      */
 786:     public static final Tag P = new Tag("p", BREAKS | BLOCK);
 787: 
 788:     /**
 789:      * The &lt;param&gt; tag
 790:      */
 791:     public static final Tag PARAM = new Tag("param");
 792: 
 793:     /**
 794:      * The &lt;pre&gt; tag , breaks flow, block tag, preformatted.
 795:      */
 796:     public static final Tag PRE = new Tag("pre", BREAKS | BLOCK | PREFORMATTED);
 797: 
 798:     /**
 799:      * The &lt;s&gt; tag
 800:      */
 801:     public static final Tag S = new Tag("s");
 802: 
 803:     /**
 804:      * The &lt;samp&gt; tag
 805:      */
 806:     public static final Tag SAMP = new Tag("samp");
 807: 
 808:     /**
 809:      * The &lt;script&gt; tag
 810:      */
 811:     public static final Tag SCRIPT = new Tag("script");
 812: 
 813:     /**
 814:      * The &lt;select&gt; tag
 815:      */
 816:     public static final Tag SELECT = new Tag("select");
 817: 
 818:     /**
 819:      * The &lt;small&gt; tag
 820:      */
 821:     public static final Tag SMALL = new Tag("small");
 822: 
 823:     /**
 824:      * The &lt;span&gt; tag
 825:      */
 826:     public static final Tag SPAN = new Tag("span");
 827: 
 828:     /**
 829:      * The &lt;strike&gt; tag
 830:      */
 831:     public static final Tag STRIKE = new Tag("strike");
 832: 
 833:     /**
 834:      * The &lt;strong&gt; tag
 835:      */
 836:     public static final Tag STRONG = new Tag("strong");
 837: 
 838:     /**
 839:      * The &lt;style&gt; tag
 840:      */
 841:     public static final Tag STYLE = new Tag("style");
 842: 
 843:     /**
 844:      * The &lt;sub&gt; tag
 845:      */
 846:     public static final Tag SUB = new Tag("sub");
 847: 
 848:     /**
 849:      * The &lt;sup&gt; tag
 850:      */
 851:     public static final Tag SUP = new Tag("sup");
 852: 
 853:     /**
 854:      * The &lt;table&gt; tag , block tag.
 855:      */
 856:     public static final Tag TABLE = new Tag("table", BLOCK);
 857: 
 858:     /**
 859:      * The &lt;td&gt; tag , breaks flow, block tag.
 860:      */
 861:     public static final Tag TD = new Tag("td", BREAKS | BLOCK);
 862: 
 863:     /**
 864:      * The &lt;textarea&gt; tag , preformatted.
 865:      */
 866:     public static final Tag TEXTAREA = new Tag("textarea", PREFORMATTED);
 867: 
 868:     /**
 869:      * The &lt;th&gt; tag , breaks flow, block tag.
 870:      */
 871:     public static final Tag TH = new Tag("th", BREAKS | BLOCK);
 872: 
 873:     /**
 874:      * The &lt;title&gt; tag , breaks flow, block tag.
 875:      */
 876:     public static final Tag TITLE = new Tag("title", BREAKS | BLOCK);
 877: 
 878:     /**
 879:      * The &lt;tr&gt; tag , block tag.
 880:      */
 881:     public static final Tag TR = new Tag("tr", BLOCK);
 882: 
 883:     /**
 884:      * The &lt;tt&gt; tag
 885:      */
 886:     public static final Tag TT = new Tag("tt");
 887: 
 888:     /**
 889:      * The &lt;u&gt; tag
 890:      */
 891:     public static final Tag U = new Tag("u");
 892: 
 893:     /**
 894:      * The &lt;ul&gt; tag , breaks flow, block tag.
 895:      */
 896:     public static final Tag UL = new Tag("ul", BREAKS | BLOCK);
 897: 
 898:     /**
 899:      * The &lt;var&gt; tag
 900:      */
 901:     public static final Tag VAR = new Tag("var");
 902: 
 903:     /* Special tags */
 904: 
 905:     /**
 906:      * Total number of syntetic tags, delared in the Tag class.
 907:      * This must be adjusted if the new synthetic tags are declared.
 908:      * Otherwise the HTML.getAllTags() will not work as expected.
 909:      */
 910:     private static final int TOTAL_SYNTHETIC_TAGS = 3;
 911: 
 912:     /**
 913:      * All comments are labeled with this tag.
 914:      * This tag is not included into the array, returned by getAllTags().
 915:      * toString() returns 'comment'. HTML reader synthesizes this tag.
 916:      */
 917:     public static final Tag COMMENT = new Tag("comment", SYNTHETIC);
 918: 
 919:     /**
 920:      *  All text content is labeled with this tag.
 921:      *  This tag is not included into the array, returned by getAllTags().
 922:      *  toString() returns 'content'. HTML reader synthesizes this tag.
 923:      */
 924:     public static final Tag CONTENT = new Tag("content", SYNTHETIC);
 925: 
 926:     /**
 927:      * All text content must be in a paragraph element.
 928:      * If a paragraph didn't exist when content was encountered,
 929:      * a paragraph is manufactured.
 930:      * toString() returns 'p-implied'. HTML reader synthesizes this tag.
 931:      */
 932:     public static final Tag IMPLIED = new Tag("p-implied", SYNTHETIC);
 933:     final String name;
 934:     final int flags;
 935: 
 936:     /**
 937:      * Create the unitialised instance of HTML.Tag.
 938:      *
 939:      * The {@link #breaksFlow()}, {@link #isBlock()}
 940:      * and {@link #isPreformatted()} will always return false.
 941:      * The {@link #toString()} will return <code>null</code>.
 942:      *
 943:      * @since 1.3
 944:      */
 945:     public Tag()
 946:     {
 947:       name = null;
 948:       flags = 0;
 949:     }
 950: 
 951:     /**
 952:      * Creates a new Tag with the specified id, and with causesBreak
 953:      * and isBlock set to false.
 954:      */
 955:     protected Tag(String id)
 956:     {
 957:       name = id;
 958:       flags = 0;
 959:     }
 960: 
 961:     /**
 962:      * Creates a new Tag with the specified tag name and
 963:      * causesBreak and isBlock properties.
 964:      */
 965:     protected Tag(String id, boolean causesBreak, boolean isBlock)
 966:     {
 967:       int f = 0;
 968: 
 969:       if (causesBreak)
 970:         {
 971:           f |= BREAKS;
 972:         }
 973: 
 974:       if (isBlock)
 975:         {
 976:           f |= BLOCK;
 977:         }
 978: 
 979:       flags = f;
 980:       name = id;
 981:     }
 982: 
 983:     /**
 984:      * Create a tag taking flags.
 985:      */
 986:     Tag(String id, int a_flags)
 987:     {
 988:       name = id;
 989:       flags = a_flags;
 990:     }
 991: 
 992:     /**
 993:      * Returns true if this tag is a block tag, which is a tag used to
 994:      * add structure to a document.
 995:      */
 996:     public boolean isBlock()
 997:     {
 998:       return (flags & BLOCK) != 0;
 999:     }
1000: 
1001:     /**
1002:      * Returns true if this tag is pre-formatted, which is true if
1003:      * the tag is either PRE or TEXTAREA
1004:      */
1005:     public boolean isPreformatted()
1006:     {
1007:       return (flags & PREFORMATTED) != 0;
1008:     }
1009: 
1010:     /**
1011:      * Returns true if this tag causes a line break to the flow of text
1012:      */
1013:     public boolean breaksFlow()
1014:     {
1015:       return (flags & BREAKS) != 0;
1016:     }
1017: 
1018:     /**
1019:      * Returns the tag name. The names of the built-in tags are always
1020:      * returned in lowercase.
1021:      */
1022:     public String toString()
1023:     {
1024:       return name;
1025:     }
1026: 
1027:     /**
1028:      * Return an array of HTML tags, declared in HTML.Tag class.
1029:      * WARNING: This method expects that the Tags are the only
1030:      * public fields declared in the Tag class.
1031:      */
1032:     static Tag[] getAllTags()
1033:     {
1034:       Field[] f = Tag.class.getFields();
1035:       Field x;
1036: 
1037:       // The syntetic tags are not included.
1038:       Tag[] tags = new Tag[ f.length - TOTAL_SYNTHETIC_TAGS ];
1039:       int p = 0;
1040:       Tag t;
1041: 
1042:       for (int i = 0; i < f.length; i++)
1043:         {
1044:           x = f [ i ];
1045: 
1046:           if ((x.getModifiers() & Modifier.STATIC) != 0)
1047:             {
1048:               if (x.getType().equals(Tag.class))
1049:                 {
1050:                   try
1051:                     {
1052:                       t = (Tag) x.get(null);
1053: 
1054:                       if (!t.isSyntetic())
1055:                         {
1056:                           tags [ p++ ] = t;
1057:                         }
1058:                     }
1059:                   catch (IllegalAccessException ex)
1060:                     {
1061:                       unexpected(ex);
1062:                     }
1063:                   catch (IllegalArgumentException ex)
1064:                     {
1065:                       unexpected(ex);
1066:                     }
1067:                 }
1068:             }
1069:         }
1070: 
1071:       return tags;
1072:     }
1073: 
1074:     /**
1075:      * Returns true for tags, generated by the html reader
1076:      * (COMMENT, CONTENT and IMPLIED).
1077:      */
1078:     boolean isSyntetic()
1079:     {
1080:       return (flags & SYNTHETIC) != 0;
1081:     }
1082: 
1083:     private static void unexpected(Exception ex)
1084:                             throws Error
1085:     {
1086:       throw new Error("This should never happen, report a bug", ex);
1087:     }
1088:   }
1089: 
1090:   /**
1091:    * Represents an unknown HTML tag.
1092:    * @author Mark Wielaard (mark@klomp.org)
1093:    */
1094:   public static class UnknownTag
1095:     extends Tag
1096:     implements Serializable
1097:   {
1098:     private static final long serialVersionUID = -1534369342247250625L;
1099: 
1100:     /**
1101:      * Creates a new UnknownTag with the specified name
1102:      * @param name The tag name.
1103:      *
1104:      */
1105:     public UnknownTag(String name)
1106:     {
1107:       super(name);
1108:     }
1109:   }
1110: 
1111:   /**
1112:    * This value is returned for attributes without value that have no
1113:    * default value defined in the DTD.
1114:    */
1115:   public static final String NULL_ATTRIBUTE_VALUE = "#DEFAULT";
1116: 
1117:   /* Package level html tag flags */
1118:   static final int BREAKS = 1;
1119:   static final int BLOCK = 2;
1120:   static final int PREFORMATTED = 4;
1121:   static final int SYNTHETIC = 8;
1122:   private static Map tagMap;
1123:   private static Map attrMap;
1124: 
1125:   /**
1126:    * The public constructor (does nothing). It it seldom required to have
1127:    * an instance of this class, because all public fields and methods
1128:    * are static.
1129:    */
1130:   public HTML()
1131:   {
1132:     // Nothing to do here.
1133:   }
1134: 
1135:   /**
1136:    * Returns the set of the recognized HTML attributes.
1137:    */
1138:   public static HTML.Attribute[] getAllAttributeKeys()
1139:   {
1140:     return Attribute.getAllAttributes();
1141:   }
1142: 
1143:   /**
1144:    * Returns the set of actual HTML tags that are recognized by
1145:    * the default HTML reader. The returned array does not include the
1146:    * COMMENT, CONTENT and IMPLIED tags.
1147:    */
1148:   public static HTML.Tag[] getAllTags()
1149:   {
1150:     return Tag.getAllTags();
1151:   }
1152: 
1153:   /**
1154:    * Returns an htl attribute constant for the given attribute name.
1155:    * @param attName the attribute name, case insensitive
1156:    */
1157:   public static Attribute getAttributeKey(String attName)
1158:   {
1159:     if (attrMap == null)
1160:       {
1161:         // Create the map on demand.
1162:         attrMap = new TreeMap();
1163: 
1164:         Attribute[] attrs = getAllAttributeKeys();
1165: 
1166:         for (int i = 0; i < attrs.length; i++)
1167:           {
1168:             attrMap.put(attrs [ i ].toString(), attrs [ i ]);
1169:           }
1170:       }
1171: 
1172:     return (Attribute) attrMap.get(attName.toLowerCase());
1173:   }
1174: 
1175:   /**
1176:    * Searches the value of given attribute in the provided set.
1177:    * If the value is found (String type expected), tries to parse it as
1178:    * an integer value. If succeded, returns the obtained integer value.
1179:    *
1180:    * For example:<p><code>
1181:    * SimpleAttributeSet ase = new SimpleAttributeSet();
1182:    * ase.addAttribute(HTML.getAttributeKey("size"),"222");
1183:    * System.out.println(
1184:    *  HTML.getIntegerAttributeValue
1185:    *     (ase, HTML.getAttributeKey("size"), 333)); // prints "222"
1186:    * System.out.println(
1187:    *  HTML.getIntegerAttributeValue
1188:    *     (ase, HTML.getAttributeKey("width"), 333)); // prints "333".
1189:    * </code></p>
1190:    *
1191:    *
1192:    * @param set The attribute set to search in. If the set contains the
1193:    * given attribute, it must by a type of String.
1194:    * @param attribute The html attribute to search in
1195:    * @param defaultValue The value that is returned if the attribute is not
1196:    * found in the given set or if the NumberFormatException was thrown
1197:    * during the parsing.
1198:    */
1199:   public static int getIntegerAttributeValue(AttributeSet set,
1200:                                              HTML.Attribute attribute,
1201:                                              int defaultValue
1202:                                             )
1203:   {
1204:     Object v = set.getAttribute(attribute);
1205: 
1206:     if (v == null)
1207:       {
1208:         return defaultValue;
1209:       }
1210: 
1211:     try
1212:       {
1213:         return Integer.parseInt(v.toString().trim());
1214:       }
1215:     catch (Exception ex)
1216:       {
1217:         return defaultValue;
1218:       }
1219:   }
1220: 
1221:   /**
1222:    * Returns a HTML tag constant for the given HTML attribute name.
1223:    * If the tag is unknown, the null is returned.
1224:    * @param tagName the tag name, case insensitive
1225:    */
1226:   public static Tag getTag(String tagName)
1227:   {
1228:     if (tagMap == null)
1229:       {
1230:         // Create the mao on demand.
1231:         tagMap = new TreeMap();
1232: 
1233:         Tag[] tags = getAllTags();
1234: 
1235:         for (int i = 0; i < tags.length; i++)
1236:           {
1237:             tagMap.put(tags [ i ].toString(), tags [ i ]);
1238:           }
1239:       }
1240: 
1241:     return (Tag) tagMap.get(tagName.toLowerCase());
1242:   }
1243: }