html.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 1,244 行 · 第 1/2 页

JAVA
1,244
字号
    /**     * The &lt;div&gt; tag , breaks flow, block tag.     */    public static final Tag DIV = new Tag("div", BREAKS | BLOCK);    /**     * The &lt;dl&gt; tag , breaks flow, block tag.     */    public static final Tag DL = new Tag("dl", BREAKS | BLOCK);    /**     * The &lt;dt&gt; tag , breaks flow, block tag.     */    public static final Tag DT = new Tag("dt", BREAKS | BLOCK);    /**     * The &lt;em&gt; tag     */    public static final Tag EM = new Tag("em");    /**     * The &lt;font&gt; tag     */    public static final Tag FONT = new Tag("font");    /**     * The &lt;form&gt; tag , breaks flow.     */    public static final Tag FORM = new Tag("form", BREAKS);    /**     * The &lt;frame&gt; tag     */    public static final Tag FRAME = new Tag("frame");    /**     * The &lt;frameset&gt; tag     */    public static final Tag FRAMESET = new Tag("frameset");    /**     * The &lt;h1&gt; tag , breaks flow, block tag.     */    public static final Tag H1 = new Tag("h1", BREAKS | BLOCK);    /**     * The &lt;h2&gt; tag , breaks flow, block tag.     */    public static final Tag H2 = new Tag("h2", BREAKS | BLOCK);    /**     * The &lt;h3&gt; tag , breaks flow, block tag.     */    public static final Tag H3 = new Tag("h3", BREAKS | BLOCK);    /**     * The &lt;h4&gt; tag , breaks flow, block tag.     */    public static final Tag H4 = new Tag("h4", BREAKS | BLOCK);    /**     * The &lt;h5&gt; tag , breaks flow, block tag.     */    public static final Tag H5 = new Tag("h5", BREAKS | BLOCK);    /**     * The &lt;h6&gt; tag , breaks flow, block tag.     */    public static final Tag H6 = new Tag("h6", BREAKS | BLOCK);    /**     * The &lt;head&gt; tag , breaks flow, block tag.     */    public static final Tag HEAD = new Tag("head", BREAKS | BLOCK);    /**     * The &lt;hr&gt; tag , breaks flow.     */    public static final Tag HR = new Tag("hr", BREAKS);    /**     * The &lt;html&gt; tag , breaks flow.     */    public static final Tag HTML = new Tag("html", BREAKS);    /**     * The &lt;i&gt; tag     */    public static final Tag I = new Tag("i");    /**     * The &lt;img&gt; tag     */    public static final Tag IMG = new Tag("img");    /**     * The &lt;input&gt; tag     */    public static final Tag INPUT = new Tag("input");    /**     * The &lt;isindex&gt; tag , breaks flow.     */    public static final Tag ISINDEX = new Tag("isindex", BREAKS);    /**     * The &lt;kbd&gt; tag     */    public static final Tag KBD = new Tag("kbd");    /**     * The &lt;li&gt; tag , breaks flow, block tag.     */    public static final Tag LI = new Tag("li", BREAKS | BLOCK);    /**     * The &lt;link&gt; tag     */    public static final Tag LINK = new Tag("link");    /**     * The &lt;map&gt; tag     */    public static final Tag MAP = new Tag("map");    /**     * The &lt;menu&gt; tag , breaks flow, block tag.     */    public static final Tag MENU = new Tag("menu", BREAKS | BLOCK);    /**     * The &lt;meta&gt; tag     */    public static final Tag META = new Tag("meta");    /**     * The &lt;nobr&gt; tag     */    public static final Tag NOBR = new Tag("nobr");    /**     * The &lt;noframes&gt; tag , breaks flow, block tag.     */    public static final Tag NOFRAMES = new Tag("noframes", BREAKS | BLOCK);    /**     * The &lt;object&gt; tag     */    public static final Tag OBJECT = new Tag("object");    /**     * The &lt;ol&gt; tag , breaks flow, block tag.     */    public static final Tag OL = new Tag("ol", BREAKS | BLOCK);    /**     * The &lt;option&gt; tag     */    public static final Tag OPTION = new Tag("option");    /**     * The &lt;p&gt; tag , breaks flow, block tag.     */    public static final Tag P = new Tag("p", BREAKS | BLOCK);    /**     * The &lt;param&gt; tag     */    public static final Tag PARAM = new Tag("param");    /**     * The &lt;pre&gt; tag , breaks flow, block tag, preformatted.     */    public static final Tag PRE = new Tag("pre", BREAKS | BLOCK | PREFORMATTED);    /**     * The &lt;s&gt; tag     */    public static final Tag S = new Tag("s");    /**     * The &lt;samp&gt; tag     */    public static final Tag SAMP = new Tag("samp");    /**     * The &lt;script&gt; tag     */    public static final Tag SCRIPT = new Tag("script");    /**     * The &lt;select&gt; tag     */    public static final Tag SELECT = new Tag("select");    /**     * The &lt;small&gt; tag     */    public static final Tag SMALL = new Tag("small");    /**     * The &lt;span&gt; tag     */    public static final Tag SPAN = new Tag("span");    /**     * The &lt;strike&gt; tag     */    public static final Tag STRIKE = new Tag("strike");    /**     * The &lt;strong&gt; tag     */    public static final Tag STRONG = new Tag("strong");    /**     * The &lt;style&gt; tag     */    public static final Tag STYLE = new Tag("style");    /**     * The &lt;sub&gt; tag     */    public static final Tag SUB = new Tag("sub");    /**     * The &lt;sup&gt; tag     */    public static final Tag SUP = new Tag("sup");    /**     * The &lt;table&gt; tag , block tag.     */    public static final Tag TABLE = new Tag("table", BLOCK);    /**     * The &lt;td&gt; tag , breaks flow, block tag.     */    public static final Tag TD = new Tag("td", BREAKS | BLOCK);    /**     * The &lt;textarea&gt; tag , preformatted.     */    public static final Tag TEXTAREA = new Tag("textarea", PREFORMATTED);    /**     * The &lt;th&gt; tag , breaks flow, block tag.     */    public static final Tag TH = new Tag("th", BREAKS | BLOCK);    /**     * The &lt;title&gt; tag , breaks flow, block tag.     */    public static final Tag TITLE = new Tag("title", BREAKS | BLOCK);    /**     * The &lt;tr&gt; tag , block tag.     */    public static final Tag TR = new Tag("tr", BLOCK);    /**     * The &lt;tt&gt; tag     */    public static final Tag TT = new Tag("tt");    /**     * The &lt;u&gt; tag     */    public static final Tag U = new Tag("u");    /**     * The &lt;ul&gt; tag , breaks flow, block tag.     */    public static final Tag UL = new Tag("ul", BREAKS | BLOCK);    /**     * The &lt;var&gt; tag     */    public static final Tag VAR = new Tag("var");    /* Special tags */    /**     * Total number of syntetic tags, delared in the Tag class.     * This must be adjusted if the new synthetic tags are declared.     * Otherwise the HTML.getAllTags() will not work as expected.     */    private static final int TOTAL_SYNTHETIC_TAGS = 3;    /**     * All comments are labeled with this tag.     * This tag is not included into the array, returned by getAllTags().     * toString() returns 'comment'. HTML reader synthesizes this tag.     */    public static final Tag COMMENT = new Tag("comment", SYNTHETIC);    /**     *  All text content is labeled with this tag.     *  This tag is not included into the array, returned by getAllTags().     *  toString() returns 'content'. HTML reader synthesizes this tag.     */    public static final Tag CONTENT = new Tag("content", SYNTHETIC);    /**     * All text content must be in a paragraph element.     * If a paragraph didn't exist when content was encountered,     * a paragraph is manufactured.     * toString() returns 'p-implied'. HTML reader synthesizes this tag.     */    public static final Tag IMPLIED = new Tag("p-implied", SYNTHETIC);    final String name;    final int flags;    /**     * Create the unitialised instance of HTML.Tag.     *     * The {@link #breaksFlow()}, {@link #isBlock()}     * and {@link #isPreformatted()} will always return false.     * The {@link #toString()} will return <code>null</code>.     *     * @since 1.3     */    public Tag()    {      name = null;      flags = 0;    }    /**     * Creates a new Tag with the specified id, and with causesBreak     * and isBlock set to false.     */    protected Tag(String id)    {      name = id;      flags = 0;    }    /**     * Creates a new Tag with the specified tag name and     * causesBreak and isBlock properties.     */    protected Tag(String id, boolean causesBreak, boolean isBlock)    {      int f = 0;      if (causesBreak)        {          f |= BREAKS;        }      if (isBlock)        {          f |= BLOCK;        }      flags = f;      name = id;    }    /**     * Create a tag taking flags.     */    Tag(String id, int a_flags)    {      name = id;      flags = a_flags;    }    /**     * Returns true if this tag is a block tag, which is a tag used to     * add structure to a document.     */    public boolean isBlock()    {      return (flags & BLOCK) != 0;    }    /**     * Returns true if this tag is pre-formatted, which is true if     * the tag is either PRE or TEXTAREA     */    public boolean isPreformatted()    {      return (flags & PREFORMATTED) != 0;    }    /**     * Returns true if this tag causes a line break to the flow of text     */    public boolean breaksFlow()    {      return (flags & BREAKS) != 0;    }    /**     * Returns the tag name. The names of the built-in tags are always     * returned in lowercase.     */    public String toString()    {      return name;    }    /**     * Return an array of HTML tags, declared in HTML.Tag class.     * WARNING: This method expects that the Tags are the only     * public fields declared in the Tag class.     */    static Tag[] getAllTags()    {      Field[] f = Tag.class.getFields();      Field x;      // The syntetic tags are not included.      Tag[] tags = new Tag[ f.length - TOTAL_SYNTHETIC_TAGS ];      int p = 0;      Tag t;      for (int i = 0; i < f.length; i++)        {          x = f [ i ];          if ((x.getModifiers() & Modifier.STATIC) != 0)            {              if (x.getType().equals(Tag.class))                {                  try                    {                      t = (Tag) x.get(null);                      if (!t.isSyntetic())                        {                          tags [ p++ ] = t;                        }                    }                  catch (IllegalAccessException ex)                    {                      unexpected(ex);                    }                  catch (IllegalArgumentException ex)                    {                      unexpected(ex);                    }                }            }        }      return tags;    }    /**     * Returns true for tags, generated by the html reader     * (COMMENT, CONTENT and IMPLIED).     */    boolean isSyntetic()    {      return (flags & SYNTHETIC) != 0;    }    private static void unexpected(Exception ex)                            throws Error    {      throw new Error("This should never happen, report a bug", ex);    }  }  /**   * Represents an unknown HTML tag.   * @author Mark Wielaard (mark@klomp.org)   */  public static class UnknownTag    extends Tag    implements Serializable  {    private static final long serialVersionUID = -1534369342247250625L;    /**     * Creates a new UnknownTag with the specified name     * @param name The tag name.     *     */    public UnknownTag(String name)    {      super(name);    }  }  /**   * This value is returned for attributes without value that have no   * default value defined in the DTD.   */  public static final String NULL_ATTRIBUTE_VALUE = "#DEFAULT";  /* Package level html tag flags */  static final int BREAKS = 1;  static final int BLOCK = 2;  static final int PREFORMATTED = 4;  static final int SYNTHETIC = 8;  private static Map tagMap;  private static Map attrMap;  /**   * The public constructor (does nothing). It it seldom required to have   * an instance of this class, because all public fields and methods   * are static.   */  public HTML()  {    // Nothing to do here.  }  /**   * Returns the set of the recognized HTML attributes.   */  public static HTML.Attribute[] getAllAttributeKeys()  {    return Attribute.getAllAttributes();  }  /**   * Returns the set of actual HTML tags that are recognized by   * the default HTML reader. The returned array does not include the   * COMMENT, CONTENT and IMPLIED tags.   */  public static HTML.Tag[] getAllTags()  {    return Tag.getAllTags();  }  /**   * Returns an htl attribute constant for the given attribute name.   * @param attName the attribute name, case insensitive   */  public static Attribute getAttributeKey(String attName)  {    if (attrMap == null)      {        // Create the map on demand.        attrMap = new TreeMap();        Attribute[] attrs = getAllAttributeKeys();        for (int i = 0; i < attrs.length; i++)          {            attrMap.put(attrs [ i ].toString(), attrs [ i ]);          }      }    return (Attribute) attrMap.get(attName.toLowerCase());  }  /**   * Searches the value of given attribute in the provided set.   * If the value is found (String type expected), tries to parse it as   * an integer value. If succeded, returns the obtained integer value.   *   * For example:<p><code>   * SimpleAttributeSet ase = new SimpleAttributeSet();   * ase.addAttribute(HTML.getAttributeKey("size"),"222");   * System.out.println(   *  HTML.getIntegerAttributeValue   *     (ase, HTML.getAttributeKey("size"), 333)); // prints "222"   * System.out.println(   *  HTML.getIntegerAttributeValue   *     (ase, HTML.getAttributeKey("width"), 333)); // prints "333".   * </code></p>   *   *   * @param set The attribute set to search in. If the set contains the   * given attribute, it must by a type of String.   * @param attribute The html attribute to search in   * @param defaultValue The value that is returned if the attribute is not   * found in the given set or if the NumberFormatException was thrown   * during the parsing.   */  public static int getIntegerAttributeValue(AttributeSet set,                                             HTML.Attribute attribute,                                             int defaultValue                                            )  {    Object v = set.getAttribute(attribute);    if (v == null)      {        return defaultValue;      }    try      {        return Integer.parseInt(v.toString().trim());      }    catch (Exception ex)      {        return defaultValue;      }  }  /**   * Returns a HTML tag constant for the given HTML attribute name.   * If the tag is unknown, the null is returned.   * @param tagName the tag name, case insensitive   */  public static Tag getTag(String tagName)  {    if (tagMap == null)      {        // Create the mao on demand.        tagMap = new TreeMap();        Tag[] tags = getAllTags();        for (int i = 0; i < tags.length; i++)          {            tagMap.put(tags [ i ].toString(), tags [ i ]);          }      }    return (Tag) tagMap.get(tagName.toLowerCase());  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?