📄 html.java
字号:
* 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; } /** * Calls compareTo on the tag names (Strings) */ public int compareTo(Object other) { return name.compareTo(((Tag) other).name); } /** * The tags are equal if the names are equal (ignoring case). */ public boolean equals(Object other) { if (other == this) { return true; } if (!(other instanceof Tag)) { return false; } Tag that = (Tag) other; return that.name.equalsIgnoreCase(name); } /** * Returns the hash code which corresponds to the string for this tag. */ public int hashCode() { return name == null ? 0 : name.hashCode(); } /** * 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -