14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing.text.html; 26 27 import java.io.*; 28 import java.util.Hashtable; 29 import javax.swing.text.AttributeSet; 30 import javax.swing.text.StyleConstants; 31 import javax.swing.text.StyleContext; 32 33 /** 34 * Constants used in the <code>HTMLDocument</code>. These 35 * are basically tag and attribute definitions. 36 * 37 * @author Timothy Prinzing 38 * @author Sunita Mani 39 * 40 */ 41 public class HTML { 42 43 /** 44 * Typesafe enumeration for an HTML tag. Although the 45 * set of HTML tags is a closed set, we have left the 46 * set open so that people can add their own tag types 47 * to their custom parser and still communicate to the 48 * reader. 49 */ 50 public static class Tag { 51 52 /** @since 1.3 */ 53 public Tag() {} 54 55 /** 56 * Creates a new <code>Tag</code> with the specified <code>id</code>, 57 * and with <code>causesBreak</code> and <code>isBlock</code> 58 * set to <code>false</code>. 59 * 60 * @param id the id of the new tag 61 */ 62 protected Tag(String id) { 63 this(id, false, false); 64 } 65 66 /** 67 * Creates a new <code>Tag</code> with the specified <code>id</code>; 68 * <code>causesBreak</code> and <code>isBlock</code> are defined 69 * by the user. 70 * 71 * @param id the id of the new tag 72 * @param causesBreak <code>true</code> if this tag 73 * causes a break to the flow of data 74 * @param isBlock <code>true</code> if the tag is used 75 * to add structure to a document 76 */ 77 protected Tag(String id, boolean causesBreak, boolean isBlock) { 78 name = id; 79 this.breakTag = causesBreak; 80 this.blockTag = isBlock; 81 } 82 83 /** 84 * Returns <code>true</code> if this tag is a block 85 * tag, which is a tag used to add structure to a 86 * document. 87 * 88 * @return <code>true</code> if this tag is a block 89 * tag, otherwise returns <code>false</code> 90 */ 91 public boolean isBlock() { 92 return blockTag; 93 } 94 95 /** 96 * Returns <code>true</code> if this tag causes a 97 * line break to the flow of data, otherwise returns 98 * <code>false</code>. 99 * 100 * @return <code>true</code> if this tag causes a 101 * line break to the flow of data, otherwise returns 102 * <code>false</code> 103 */ 104 public boolean breaksFlow() { 105 return breakTag; 106 } 107 108 /** 109 * Returns <code>true</code> if this tag is pre-formatted, 110 * which is true if the tag is either <code>PRE</code> or 111 * <code>TEXTAREA</code>. 112 * 113 * @return <code>true</code> if this tag is pre-formatted, 114 * otherwise returns <code>false</code> 115 */ 116 public boolean isPreformatted() { 117 return (this == PRE || this == TEXTAREA); 118 } 119 120 /** 121 * Returns the string representation of the 122 * tag. 123 * 124 * @return the <code>String</code> representation of the tag 125 */ 126 public String toString() { 127 return name; 128 } 129 130 /** 131 * Returns <code>true</code> if this tag is considered to be a paragraph 132 * in the internal HTML model. <code>false</code> - otherwise. 133 * 134 * @return <code>true</code> if this tag is considered to be a paragraph 135 * in the internal HTML model. <code>false</code> - otherwise. 136 * @see HTMLDocument.HTMLReader.ParagraphAction 137 */ 138 boolean isParagraph() { 139 return ( 140 this == P 141 || this == IMPLIED 142 || this == DT 143 || this == H1 144 || this == H2 145 || this == H3 146 || this == H4 147 || this == H5 148 || this == H6 149 ); 150 } 151 152 boolean blockTag; 153 boolean breakTag; 154 String name; 155 boolean unknown; 563 PRE, SAMP, SCRIPT, SELECT, SMALL, SPAN, STRIKE, S, 564 STRONG, STYLE, SUB, SUP, TABLE, TD, TEXTAREA, 565 TH, TITLE, TR, TT, U, UL, VAR 566 }; 567 568 static { 569 // Force HTMLs static initialize to be loaded. 570 getTag("html"); 571 } 572 } 573 574 /** 575 * Class represents unknown HTML tag. 576 */ 577 // There is no unique instance of UnknownTag, so we allow it to be 578 // Serializable. 579 @SuppressWarnings("serial") // Same-version serialization only 580 public static class UnknownTag extends Tag implements Serializable { 581 582 /** 583 * Creates a new <code>UnknownTag</code> with the specified 584 * <code>id</code>. 585 * @param id the id of the new tag 586 */ 587 public UnknownTag(String id) { 588 super(id); 589 } 590 591 /** 592 * Returns the hash code which corresponds to the string 593 * for this tag. 594 */ 595 public int hashCode() { 596 return toString().hashCode(); 597 } 598 599 /** 600 * Compares this object to the specified object. 601 * The result is <code>true</code> if and only if the argument is not 602 * <code>null</code> and is an <code>UnknownTag</code> object 603 * with the same name. 604 * 605 * @param obj the object to compare this tag with 606 * @return <code>true</code> if the objects are equal; 607 * <code>false</code> otherwise 608 */ 609 public boolean equals(Object obj) { 610 if (obj instanceof UnknownTag) { 611 return toString().equals(obj.toString()); 612 } 613 return false; 614 } 615 616 private void writeObject(java.io.ObjectOutputStream s) 617 throws IOException { 618 s.defaultWriteObject(); 619 s.writeBoolean(blockTag); 620 s.writeBoolean(breakTag); 621 s.writeBoolean(unknown); 622 s.writeObject(name); 623 } 624 625 private void readObject(ObjectInputStream s) 626 throws ClassNotFoundException, IOException { 627 s.defaultReadObject(); 628 blockTag = s.readBoolean(); 629 breakTag = s.readBoolean(); 630 unknown = s.readBoolean(); 631 name = (String)s.readObject(); 632 } 633 } 634 635 /** 636 * Typesafe enumeration representing an HTML 637 * attribute. 638 */ 639 public static final class Attribute { 640 641 /** 642 * Creates a new <code>Attribute</code> with the specified 643 * <code>id</code>. 644 * 645 * @param id the id of the new <code>Attribute</code> 646 */ 647 Attribute(String id) { 648 name = id; 649 } 650 651 /** 652 * Returns the string representation of this attribute. 653 * @return the string representation of this attribute 654 */ 655 public String toString() { 656 return name; 657 } 658 659 private String name; 660 661 662 /** 663 * Attribute "size" 664 */ 665 public static final Attribute SIZE = new Attribute("size"); 1190 */ 1191 public static Tag[] getAllTags() { 1192 Tag[] tags = new Tag[Tag.allTags.length]; 1193 System.arraycopy(Tag.allTags, 0, tags, 0, Tag.allTags.length); 1194 return tags; 1195 } 1196 1197 /** 1198 * Fetches a tag constant for a well-known tag name (i.e. one of 1199 * the tags in the set {A, ADDRESS, APPLET, AREA, B, 1200 * BASE, BASEFONT, BIG, 1201 * BLOCKQUOTE, BODY, BR, CAPTION, CENTER, CITE, CODE, 1202 * DD, DFN, DIR, DIV, DL, DT, EM, FONT, FORM, FRAME, 1203 * FRAMESET, H1, H2, H3, H4, H5, H6, HEAD, HR, HTML, 1204 * I, IMG, INPUT, ISINDEX, KBD, LI, LINK, MAP, MENU, 1205 * META, NOBR, NOFRAMES, OBJECT, OL, OPTION, P, PARAM, 1206 * PRE, SAMP, SCRIPT, SELECT, SMALL, SPAN, STRIKE, S, 1207 * STRONG, STYLE, SUB, SUP, TABLE, TD, TEXTAREA, 1208 * TH, TITLE, TR, TT, U, UL, VAR}. If the given 1209 * name does not represent one of the well-known tags, then 1210 * <code>null</code> will be returned. 1211 * 1212 * @param tagName the <code>String</code> name requested 1213 * @return a tag constant corresponding to the <code>tagName</code>, 1214 * or <code>null</code> if not found 1215 */ 1216 public static Tag getTag(String tagName) { 1217 1218 Tag t = tagHashtable.get(tagName); 1219 return (t == null ? null : t); 1220 } 1221 1222 /** 1223 * Returns the HTML <code>Tag</code> associated with the 1224 * <code>StyleConstants</code> key <code>sc</code>. 1225 * If no matching <code>Tag</code> is found, returns 1226 * <code>null</code>. 1227 * 1228 * @param sc the <code>StyleConstants</code> key 1229 * @return tag which corresponds to <code>sc</code>, or 1230 * <code>null</code> if not found 1231 */ 1232 static Tag getTagForStyleConstantsKey(StyleConstants sc) { 1233 return scMapping.get(sc); 1234 } 1235 1236 /** 1237 * Fetches an integer attribute value. Attribute values 1238 * are stored as a string, and this is a convenience method 1239 * to convert to an actual integer. 1240 * 1241 * @param attr the set of attributes to use to try to fetch a value 1242 * @param key the key to use to fetch the value 1243 * @param def the default value to use if the attribute isn't 1244 * defined or there is an error converting to an integer 1245 * @return an attribute value 1246 */ 1247 public static int getIntegerAttributeValue(AttributeSet attr, 1248 Attribute key, int def) { 1249 int value = def; 1250 String istr = (String) attr.getAttribute(key); 1283 System.arraycopy(Attribute.allAttributes, 0, 1284 attributes, 0, Attribute.allAttributes.length); 1285 return attributes; 1286 } 1287 1288 /** 1289 * Fetches an attribute constant for a well-known attribute name 1290 * (i.e. one of the attributes in the set {FACE, COMMENT, SIZE, 1291 * COLOR, CLEAR, BACKGROUND, BGCOLOR, TEXT, LINK, VLINK, ALINK, 1292 * WIDTH, HEIGHT, ALIGN, NAME, HREF, REL, REV, TITLE, TARGET, 1293 * SHAPE, COORDS, ISMAP, NOHREF, ALT, ID, SRC, HSPACE, VSPACE, 1294 * USEMAP, LOWSRC, CODEBASE, CODE, ARCHIVE, VALUE, VALUETYPE, 1295 * TYPE, CLASS, STYLE, LANG, DIR, DECLARE, CLASSID, DATA, CODETYPE, 1296 * STANDBY, BORDER, SHAPES, NOSHADE, COMPACT, START, ACTION, METHOD, 1297 * ENCTYPE, CHECKED, MAXLENGTH, MULTIPLE, SELECTED, ROWS, COLS, 1298 * DUMMY, CELLSPACING, CELLPADDING, VALIGN, HALIGN, NOWRAP, ROWSPAN, 1299 * COLSPAN, PROMPT, HTTPEQUIV, CONTENT, LANGUAGE, VERSION, N, 1300 * FRAMEBORDER, MARGINWIDTH, MARGINHEIGHT, SCROLLING, NORESIZE, 1301 * MEDIA, ENDTAG}). 1302 * If the given name does not represent one of the well-known attributes, 1303 * then <code>null</code> will be returned. 1304 * 1305 * @param attName the <code>String</code> requested 1306 * @return the <code>Attribute</code> corresponding to <code>attName</code> 1307 */ 1308 public static Attribute getAttributeKey(String attName) { 1309 Attribute a = attHashtable.get(attName); 1310 if (a == null) { 1311 return null; 1312 } 1313 return a; 1314 } 1315 1316 } | 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing.text.html; 26 27 import java.io.*; 28 import java.util.Hashtable; 29 import javax.swing.text.AttributeSet; 30 import javax.swing.text.StyleConstants; 31 import javax.swing.text.StyleContext; 32 33 /** 34 * Constants used in the {@code HTMLDocument}. These 35 * are basically tag and attribute definitions. 36 * 37 * @author Timothy Prinzing 38 * @author Sunita Mani 39 * 40 */ 41 public class HTML { 42 43 /** 44 * Typesafe enumeration for an HTML tag. Although the 45 * set of HTML tags is a closed set, we have left the 46 * set open so that people can add their own tag types 47 * to their custom parser and still communicate to the 48 * reader. 49 */ 50 public static class Tag { 51 52 /** @since 1.3 */ 53 public Tag() {} 54 55 /** 56 * Creates a new {@code Tag} with the specified {@code id}, 57 * and with {@code causesBreak} and {@code isBlock} 58 * set to {@code false}. 59 * 60 * @param id the id of the new tag 61 */ 62 protected Tag(String id) { 63 this(id, false, false); 64 } 65 66 /** 67 * Creates a new {@code Tag} with the specified {@code id}; 68 * {@code causesBreak} and {@code isBlock} are defined 69 * by the user. 70 * 71 * @param id the id of the new tag 72 * @param causesBreak {@code true} if this tag 73 * causes a break to the flow of data 74 * @param isBlock {@code true} if the tag is used 75 * to add structure to a document 76 */ 77 protected Tag(String id, boolean causesBreak, boolean isBlock) { 78 name = id; 79 this.breakTag = causesBreak; 80 this.blockTag = isBlock; 81 } 82 83 /** 84 * Returns {@code true} if this tag is a block 85 * tag, which is a tag used to add structure to a 86 * document. 87 * 88 * @return {@code true} if this tag is a block 89 * tag, otherwise returns {@code false} 90 */ 91 public boolean isBlock() { 92 return blockTag; 93 } 94 95 /** 96 * Returns {@code true} if this tag causes a 97 * line break to the flow of data, otherwise returns 98 * {@code false}. 99 * 100 * @return {@code true} if this tag causes a 101 * line break to the flow of data, otherwise returns 102 * {@code false} 103 */ 104 public boolean breaksFlow() { 105 return breakTag; 106 } 107 108 /** 109 * Returns {@code true} if this tag is pre-formatted, 110 * which is true if the tag is either {@code PRE} or 111 * {@code TEXTAREA}. 112 * 113 * @return {@code true} if this tag is pre-formatted, 114 * otherwise returns {@code false} 115 */ 116 public boolean isPreformatted() { 117 return (this == PRE || this == TEXTAREA); 118 } 119 120 /** 121 * Returns the string representation of the 122 * tag. 123 * 124 * @return the {@code String} representation of the tag 125 */ 126 public String toString() { 127 return name; 128 } 129 130 /** 131 * Returns {@code true} if this tag is considered to be a paragraph 132 * in the internal HTML model. {@code false} - otherwise. 133 * 134 * @return {@code true} if this tag is considered to be a paragraph 135 * in the internal HTML model. {@code false} - otherwise. 136 * @see HTMLDocument.HTMLReader.ParagraphAction 137 */ 138 boolean isParagraph() { 139 return ( 140 this == P 141 || this == IMPLIED 142 || this == DT 143 || this == H1 144 || this == H2 145 || this == H3 146 || this == H4 147 || this == H5 148 || this == H6 149 ); 150 } 151 152 boolean blockTag; 153 boolean breakTag; 154 String name; 155 boolean unknown; 563 PRE, SAMP, SCRIPT, SELECT, SMALL, SPAN, STRIKE, S, 564 STRONG, STYLE, SUB, SUP, TABLE, TD, TEXTAREA, 565 TH, TITLE, TR, TT, U, UL, VAR 566 }; 567 568 static { 569 // Force HTMLs static initialize to be loaded. 570 getTag("html"); 571 } 572 } 573 574 /** 575 * Class represents unknown HTML tag. 576 */ 577 // There is no unique instance of UnknownTag, so we allow it to be 578 // Serializable. 579 @SuppressWarnings("serial") // Same-version serialization only 580 public static class UnknownTag extends Tag implements Serializable { 581 582 /** 583 * Creates a new {@code UnknownTag} with the specified 584 * {@code id}. 585 * @param id the id of the new tag 586 */ 587 public UnknownTag(String id) { 588 super(id); 589 } 590 591 /** 592 * Returns the hash code which corresponds to the string 593 * for this tag. 594 */ 595 public int hashCode() { 596 return toString().hashCode(); 597 } 598 599 /** 600 * Compares this object to the specified object. 601 * The result is {@code true} if and only if the argument is not 602 * {@code null} and is an {@code UnknownTag} object 603 * with the same name. 604 * 605 * @param obj the object to compare this tag with 606 * @return {@code true} if the objects are equal; 607 * {@code false} otherwise 608 */ 609 public boolean equals(Object obj) { 610 if (obj instanceof UnknownTag) { 611 return toString().equals(obj.toString()); 612 } 613 return false; 614 } 615 616 private void writeObject(java.io.ObjectOutputStream s) 617 throws IOException { 618 s.defaultWriteObject(); 619 s.writeBoolean(blockTag); 620 s.writeBoolean(breakTag); 621 s.writeBoolean(unknown); 622 s.writeObject(name); 623 } 624 625 private void readObject(ObjectInputStream s) 626 throws ClassNotFoundException, IOException { 627 s.defaultReadObject(); 628 blockTag = s.readBoolean(); 629 breakTag = s.readBoolean(); 630 unknown = s.readBoolean(); 631 name = (String)s.readObject(); 632 } 633 } 634 635 /** 636 * Typesafe enumeration representing an HTML 637 * attribute. 638 */ 639 public static final class Attribute { 640 641 /** 642 * Creates a new {@code Attribute} with the specified 643 * {@code id}. 644 * 645 * @param id the id of the new {@code Attribute} 646 */ 647 Attribute(String id) { 648 name = id; 649 } 650 651 /** 652 * Returns the string representation of this attribute. 653 * @return the string representation of this attribute 654 */ 655 public String toString() { 656 return name; 657 } 658 659 private String name; 660 661 662 /** 663 * Attribute "size" 664 */ 665 public static final Attribute SIZE = new Attribute("size"); 1190 */ 1191 public static Tag[] getAllTags() { 1192 Tag[] tags = new Tag[Tag.allTags.length]; 1193 System.arraycopy(Tag.allTags, 0, tags, 0, Tag.allTags.length); 1194 return tags; 1195 } 1196 1197 /** 1198 * Fetches a tag constant for a well-known tag name (i.e. one of 1199 * the tags in the set {A, ADDRESS, APPLET, AREA, B, 1200 * BASE, BASEFONT, BIG, 1201 * BLOCKQUOTE, BODY, BR, CAPTION, CENTER, CITE, CODE, 1202 * DD, DFN, DIR, DIV, DL, DT, EM, FONT, FORM, FRAME, 1203 * FRAMESET, H1, H2, H3, H4, H5, H6, HEAD, HR, HTML, 1204 * I, IMG, INPUT, ISINDEX, KBD, LI, LINK, MAP, MENU, 1205 * META, NOBR, NOFRAMES, OBJECT, OL, OPTION, P, PARAM, 1206 * PRE, SAMP, SCRIPT, SELECT, SMALL, SPAN, STRIKE, S, 1207 * STRONG, STYLE, SUB, SUP, TABLE, TD, TEXTAREA, 1208 * TH, TITLE, TR, TT, U, UL, VAR}. If the given 1209 * name does not represent one of the well-known tags, then 1210 * {@code null} will be returned. 1211 * 1212 * @param tagName the {@code String} name requested 1213 * @return a tag constant corresponding to the {@code tagName}, 1214 * or {@code null} if not found 1215 */ 1216 public static Tag getTag(String tagName) { 1217 1218 Tag t = tagHashtable.get(tagName); 1219 return (t == null ? null : t); 1220 } 1221 1222 /** 1223 * Returns the HTML {@code Tag} associated with the 1224 * {@code StyleConstants} key {@code sc}. 1225 * If no matching {@code Tag} is found, returns 1226 * {@code null}. 1227 * 1228 * @param sc the {@code StyleConstants} key 1229 * @return tag which corresponds to {@code sc}, or 1230 * {@code null} if not found 1231 */ 1232 static Tag getTagForStyleConstantsKey(StyleConstants sc) { 1233 return scMapping.get(sc); 1234 } 1235 1236 /** 1237 * Fetches an integer attribute value. Attribute values 1238 * are stored as a string, and this is a convenience method 1239 * to convert to an actual integer. 1240 * 1241 * @param attr the set of attributes to use to try to fetch a value 1242 * @param key the key to use to fetch the value 1243 * @param def the default value to use if the attribute isn't 1244 * defined or there is an error converting to an integer 1245 * @return an attribute value 1246 */ 1247 public static int getIntegerAttributeValue(AttributeSet attr, 1248 Attribute key, int def) { 1249 int value = def; 1250 String istr = (String) attr.getAttribute(key); 1283 System.arraycopy(Attribute.allAttributes, 0, 1284 attributes, 0, Attribute.allAttributes.length); 1285 return attributes; 1286 } 1287 1288 /** 1289 * Fetches an attribute constant for a well-known attribute name 1290 * (i.e. one of the attributes in the set {FACE, COMMENT, SIZE, 1291 * COLOR, CLEAR, BACKGROUND, BGCOLOR, TEXT, LINK, VLINK, ALINK, 1292 * WIDTH, HEIGHT, ALIGN, NAME, HREF, REL, REV, TITLE, TARGET, 1293 * SHAPE, COORDS, ISMAP, NOHREF, ALT, ID, SRC, HSPACE, VSPACE, 1294 * USEMAP, LOWSRC, CODEBASE, CODE, ARCHIVE, VALUE, VALUETYPE, 1295 * TYPE, CLASS, STYLE, LANG, DIR, DECLARE, CLASSID, DATA, CODETYPE, 1296 * STANDBY, BORDER, SHAPES, NOSHADE, COMPACT, START, ACTION, METHOD, 1297 * ENCTYPE, CHECKED, MAXLENGTH, MULTIPLE, SELECTED, ROWS, COLS, 1298 * DUMMY, CELLSPACING, CELLPADDING, VALIGN, HALIGN, NOWRAP, ROWSPAN, 1299 * COLSPAN, PROMPT, HTTPEQUIV, CONTENT, LANGUAGE, VERSION, N, 1300 * FRAMEBORDER, MARGINWIDTH, MARGINHEIGHT, SCROLLING, NORESIZE, 1301 * MEDIA, ENDTAG}). 1302 * If the given name does not represent one of the well-known attributes, 1303 * then {@code null} will be returned. 1304 * 1305 * @param attName the {@code String} requested 1306 * @return the {@code Attribute} corresponding to {@code attName} 1307 */ 1308 public static Attribute getAttributeKey(String attName) { 1309 Attribute a = attHashtable.get(attName); 1310 if (a == null) { 1311 return null; 1312 } 1313 return a; 1314 } 1315 1316 } |