html.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 1,244 行 · 第 1/2 页
JAVA
1,244 行
/** * The <div> tag , breaks flow, block tag. */ public static final Tag DIV = new Tag("div", BREAKS | BLOCK); /** * The <dl> tag , breaks flow, block tag. */ public static final Tag DL = new Tag("dl", BREAKS | BLOCK); /** * The <dt> tag , breaks flow, block tag. */ public static final Tag DT = new Tag("dt", BREAKS | BLOCK); /** * The <em> tag */ public static final Tag EM = new Tag("em"); /** * The <font> tag */ public static final Tag FONT = new Tag("font"); /** * The <form> tag , breaks flow. */ public static final Tag FORM = new Tag("form", BREAKS); /** * The <frame> tag */ public static final Tag FRAME = new Tag("frame"); /** * The <frameset> tag */ public static final Tag FRAMESET = new Tag("frameset"); /** * The <h1> tag , breaks flow, block tag. */ public static final Tag H1 = new Tag("h1", BREAKS | BLOCK); /** * The <h2> tag , breaks flow, block tag. */ public static final Tag H2 = new Tag("h2", BREAKS | BLOCK); /** * The <h3> tag , breaks flow, block tag. */ public static final Tag H3 = new Tag("h3", BREAKS | BLOCK); /** * The <h4> tag , breaks flow, block tag. */ public static final Tag H4 = new Tag("h4", BREAKS | BLOCK); /** * The <h5> tag , breaks flow, block tag. */ public static final Tag H5 = new Tag("h5", BREAKS | BLOCK); /** * The <h6> tag , breaks flow, block tag. */ public static final Tag H6 = new Tag("h6", BREAKS | BLOCK); /** * The <head> tag , breaks flow, block tag. */ public static final Tag HEAD = new Tag("head", BREAKS | BLOCK); /** * The <hr> tag , breaks flow. */ public static final Tag HR = new Tag("hr", BREAKS); /** * The <html> tag , breaks flow. */ public static final Tag HTML = new Tag("html", BREAKS); /** * The <i> tag */ public static final Tag I = new Tag("i"); /** * The <img> tag */ public static final Tag IMG = new Tag("img"); /** * The <input> tag */ public static final Tag INPUT = new Tag("input"); /** * The <isindex> tag , breaks flow. */ public static final Tag ISINDEX = new Tag("isindex", BREAKS); /** * The <kbd> tag */ public static final Tag KBD = new Tag("kbd"); /** * The <li> tag , breaks flow, block tag. */ public static final Tag LI = new Tag("li", BREAKS | BLOCK); /** * The <link> tag */ public static final Tag LINK = new Tag("link"); /** * The <map> tag */ public static final Tag MAP = new Tag("map"); /** * The <menu> tag , breaks flow, block tag. */ public static final Tag MENU = new Tag("menu", BREAKS | BLOCK); /** * The <meta> tag */ public static final Tag META = new Tag("meta"); /** * The <nobr> tag */ public static final Tag NOBR = new Tag("nobr"); /** * The <noframes> tag , breaks flow, block tag. */ public static final Tag NOFRAMES = new Tag("noframes", BREAKS | BLOCK); /** * The <object> tag */ public static final Tag OBJECT = new Tag("object"); /** * The <ol> tag , breaks flow, block tag. */ public static final Tag OL = new Tag("ol", BREAKS | BLOCK); /** * The <option> tag */ public static final Tag OPTION = new Tag("option"); /** * The <p> tag , breaks flow, block tag. */ public static final Tag P = new Tag("p", BREAKS | BLOCK); /** * The <param> tag */ public static final Tag PARAM = new Tag("param"); /** * The <pre> tag , breaks flow, block tag, preformatted. */ public static final Tag PRE = new Tag("pre", BREAKS | BLOCK | PREFORMATTED); /** * The <s> tag */ public static final Tag S = new Tag("s"); /** * The <samp> tag */ public static final Tag SAMP = new Tag("samp"); /** * The <script> tag */ public static final Tag SCRIPT = new Tag("script"); /** * The <select> tag */ public static final Tag SELECT = new Tag("select"); /** * The <small> tag */ public static final Tag SMALL = new Tag("small"); /** * The <span> tag */ public static final Tag SPAN = new Tag("span"); /** * The <strike> tag */ public static final Tag STRIKE = new Tag("strike"); /** * The <strong> tag */ public static final Tag STRONG = new Tag("strong"); /** * The <style> tag */ public static final Tag STYLE = new Tag("style"); /** * The <sub> tag */ public static final Tag SUB = new Tag("sub"); /** * The <sup> tag */ public static final Tag SUP = new Tag("sup"); /** * The <table> tag , block tag. */ public static final Tag TABLE = new Tag("table", BLOCK); /** * The <td> tag , breaks flow, block tag. */ public static final Tag TD = new Tag("td", BREAKS | BLOCK); /** * The <textarea> tag , preformatted. */ public static final Tag TEXTAREA = new Tag("textarea", PREFORMATTED); /** * The <th> tag , breaks flow, block tag. */ public static final Tag TH = new Tag("th", BREAKS | BLOCK); /** * The <title> tag , breaks flow, block tag. */ public static final Tag TITLE = new Tag("title", BREAKS | BLOCK); /** * The <tr> tag , block tag. */ public static final Tag TR = new Tag("tr", BLOCK); /** * The <tt> tag */ public static final Tag TT = new Tag("tt"); /** * The <u> tag */ public static final Tag U = new Tag("u"); /** * The <ul> tag , breaks flow, block tag. */ public static final Tag UL = new Tag("ul", BREAKS | BLOCK); /** * The <var> 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 + -
显示快捷键?