< prev index next >

src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/markup/HtmlTree.java

Print this page




  83      *
  84      * @param tag HTML tag for the HtmlTree object
  85      * @param contents contents to be added to the tree
  86      */
  87     public HtmlTree(HtmlTag tag, List<Content> contents) {
  88         this(tag);
  89         for (Content c: contents)
  90             add(c);
  91     }
  92 
  93     /**
  94      * Adds an attribute for the HTML tag.
  95      *
  96      * @param attrName name of the attribute
  97      * @param attrValue value of the attribute
  98      * @return this object
  99      */
 100     public HtmlTree put(HtmlAttr attrName, String attrValue) {
 101         if (attrs.isEmpty())
 102             attrs = new LinkedHashMap<>(3);
 103         attrs.put(nullCheck(attrName), Entity.escapeHtmlChars(attrValue));
 104         return this;
 105     }
 106 
 107     /**
 108      * Sets the "title" attribute for this tag.
 109      * Any HTML tags in the content will be removed.
 110      *
 111      * @param body the content for the title attribute
 112      * @return this object
 113      */
 114     public HtmlTree setTitle(Content body) {
 115         put(HtmlAttr.TITLE, stripHtml(body));
 116         return this;
 117     }
 118 
 119     /**
 120      * Sets the "role" attribute for this tag.
 121      *
 122      * @param role the role
 123      * @return this object


 171             if (lastContent instanceof StringContent)
 172                 lastContent.add(stringContent);
 173             else
 174                 add(new StringContent(stringContent));
 175         }
 176         else
 177             add(new StringContent(stringContent));
 178     }
 179 
 180     /**
 181      * {@inheritDoc}
 182      */
 183     @Override
 184     public int charCount() {
 185         int n = 0;
 186         for (Content c : content)
 187             n += c.charCount();
 188         return n;
 189     }
 190 





























 191     /*
 192      * The sets of ASCII URI characters to be left unencoded.
 193      * See "Uniform Resource Identifier (URI): Generic Syntax"
 194      * IETF RFC 3986. https://tools.ietf.org/html/rfc3986
 195      */
 196     public static final BitSet MAIN_CHARS;
 197     public static final BitSet QUERY_FRAGMENT_CHARS;
 198 
 199     static {
 200         BitSet alphaDigit = bitSet(bitSet('A', 'Z'), bitSet('a', 'z'), bitSet('0', '9'));
 201         BitSet unreserved = bitSet(alphaDigit, bitSet("-._~"));
 202         BitSet genDelims = bitSet(":/?#[]@");
 203         BitSet subDelims = bitSet("!$&'()*+,;=");
 204         MAIN_CHARS = bitSet(unreserved, genDelims, subDelims);
 205         BitSet pchar = bitSet(unreserved, subDelims, bitSet(":@"));
 206         QUERY_FRAGMENT_CHARS = bitSet(pchar, bitSet("/?"));
 207     }
 208 
 209     private static BitSet bitSet(String s) {
 210         BitSet result = new BitSet();


 671         HtmlTree htmltree = new HtmlTree(HtmlTag.P, nullCheck(body));
 672         if (styleClass != null)
 673             htmltree.setStyle(styleClass);
 674         return htmltree;
 675     }
 676 
 677     /**
 678      * Generates a SCRIPT tag with the type and src attributes.
 679      *
 680      * @param src the path for the script
 681      * @return an HtmlTree object for the SCRIPT tag
 682      */
 683     public static HtmlTree SCRIPT(String src) {
 684         HtmlTree htmltree = new HtmlTree(HtmlTag.SCRIPT);
 685         htmltree.put(HtmlAttr.TYPE, "text/javascript");
 686         htmltree.put(HtmlAttr.SRC, nullCheck(src));
 687         return htmltree;
 688     }
 689 
 690     /**
 691      * Generates a SECTION tag with style class attribute.
 692      *
 693      * @param styleClass the style class for the tag
 694      * @return an HtmlTree object for the SECTION tag
 695      */
 696     public static HtmlTree SECTION(HtmlStyle styleClass) {
 697         HtmlTree htmlTree = new HtmlTree(HtmlTag.SECTION);
 698         htmlTree.setStyle(styleClass);
 699         return htmlTree;
 700     }
 701 
 702     /**
 703      * Generates a SECTION tag with style class attribute and some content.
 704      *
 705      * @param styleClass the style class for the tag
 706      * @param body content of the section tag
 707      * @return an HtmlTree object for the SECTION tag
 708      */
 709     public static HtmlTree SECTION(HtmlStyle styleClass, Content body) {
 710         HtmlTree htmlTree = new HtmlTree(HtmlTag.SECTION, nullCheck(body));
 711         if (styleClass != null) {
 712             htmlTree.setStyle(styleClass);
 713         }
 714         return htmlTree;
 715     }
 716 
 717     /**
 718      * Generates a SMALL tag with some content.
 719      *
 720      * @param body content for the tag
 721      * @return an HtmlTree object for the SMALL tag
 722      */
 723     public static HtmlTree SMALL(Content body) {
 724         HtmlTree htmltree = new HtmlTree(HtmlTag.SMALL, nullCheck(body));
 725         return htmltree;
 726     }
 727 
 728     /**
 729      * Generates a SPAN tag with some content.
 730      *
 731      * @param body content for the tag
 732      * @return an HtmlTree object for the SPAN tag
 733      */
 734     public static HtmlTree SPAN(Content body) {




  83      *
  84      * @param tag HTML tag for the HtmlTree object
  85      * @param contents contents to be added to the tree
  86      */
  87     public HtmlTree(HtmlTag tag, List<Content> contents) {
  88         this(tag);
  89         for (Content c: contents)
  90             add(c);
  91     }
  92 
  93     /**
  94      * Adds an attribute for the HTML tag.
  95      *
  96      * @param attrName name of the attribute
  97      * @param attrValue value of the attribute
  98      * @return this object
  99      */
 100     public HtmlTree put(HtmlAttr attrName, String attrValue) {
 101         if (attrs.isEmpty())
 102             attrs = new LinkedHashMap<>(3);
 103         attrs.put(nullCheck(attrName), escapeHtmlChars(attrValue));
 104         return this;
 105     }
 106 
 107     /**
 108      * Sets the "title" attribute for this tag.
 109      * Any HTML tags in the content will be removed.
 110      *
 111      * @param body the content for the title attribute
 112      * @return this object
 113      */
 114     public HtmlTree setTitle(Content body) {
 115         put(HtmlAttr.TITLE, stripHtml(body));
 116         return this;
 117     }
 118 
 119     /**
 120      * Sets the "role" attribute for this tag.
 121      *
 122      * @param role the role
 123      * @return this object


 171             if (lastContent instanceof StringContent)
 172                 lastContent.add(stringContent);
 173             else
 174                 add(new StringContent(stringContent));
 175         }
 176         else
 177             add(new StringContent(stringContent));
 178     }
 179 
 180     /**
 181      * {@inheritDoc}
 182      */
 183     @Override
 184     public int charCount() {
 185         int n = 0;
 186         for (Content c : content)
 187             n += c.charCount();
 188         return n;
 189     }
 190 
 191     /**
 192      * Given a string, escape all special HTML characters and
 193      * return the result.
 194      *
 195      * @param s The string to check.
 196      * @return the original string with all of the HTML characters escaped.
 197      */
 198     private static String escapeHtmlChars(String s) {
 199         for (int i = 0; i < s.length(); i++) {
 200             char ch = s.charAt(i);
 201             switch (ch) {
 202                 // only start building a new string if we need to
 203                 case '<': case '>': case '&':
 204                     StringBuilder sb = new StringBuilder(s.substring(0, i));
 205                     for ( ; i < s.length(); i++) {
 206                         ch = s.charAt(i);
 207                         switch (ch) {
 208                             case '<': sb.append("&lt;");  break;
 209                             case '>': sb.append("&gt;");  break;
 210                             case '&': sb.append("&amp;"); break;
 211                             default:  sb.append(ch);      break;
 212                         }
 213                     }
 214                     return sb.toString();
 215             }
 216         }
 217         return s;
 218     }
 219 
 220     /*
 221      * The sets of ASCII URI characters to be left unencoded.
 222      * See "Uniform Resource Identifier (URI): Generic Syntax"
 223      * IETF RFC 3986. https://tools.ietf.org/html/rfc3986
 224      */
 225     public static final BitSet MAIN_CHARS;
 226     public static final BitSet QUERY_FRAGMENT_CHARS;
 227 
 228     static {
 229         BitSet alphaDigit = bitSet(bitSet('A', 'Z'), bitSet('a', 'z'), bitSet('0', '9'));
 230         BitSet unreserved = bitSet(alphaDigit, bitSet("-._~"));
 231         BitSet genDelims = bitSet(":/?#[]@");
 232         BitSet subDelims = bitSet("!$&'()*+,;=");
 233         MAIN_CHARS = bitSet(unreserved, genDelims, subDelims);
 234         BitSet pchar = bitSet(unreserved, subDelims, bitSet(":@"));
 235         QUERY_FRAGMENT_CHARS = bitSet(pchar, bitSet("/?"));
 236     }
 237 
 238     private static BitSet bitSet(String s) {
 239         BitSet result = new BitSet();


 700         HtmlTree htmltree = new HtmlTree(HtmlTag.P, nullCheck(body));
 701         if (styleClass != null)
 702             htmltree.setStyle(styleClass);
 703         return htmltree;
 704     }
 705 
 706     /**
 707      * Generates a SCRIPT tag with the type and src attributes.
 708      *
 709      * @param src the path for the script
 710      * @return an HtmlTree object for the SCRIPT tag
 711      */
 712     public static HtmlTree SCRIPT(String src) {
 713         HtmlTree htmltree = new HtmlTree(HtmlTag.SCRIPT);
 714         htmltree.put(HtmlAttr.TYPE, "text/javascript");
 715         htmltree.put(HtmlAttr.SRC, nullCheck(src));
 716         return htmltree;
 717     }
 718 
 719     /**
 720      * Generates a SECTION tag with role attribute.
 721      *

 722      * @return an HtmlTree object for the SECTION tag
 723      */
 724     public static HtmlTree SECTION() {
 725         HtmlTree htmltree = new HtmlTree(HtmlTag.SECTION);
 726         htmltree.setRole(Role.REGION);
 727         return htmltree;
 728     }
 729 
 730     /**
 731      * Generates a SECTION tag with role attribute and some content.
 732      *

 733      * @param body content of the section tag
 734      * @return an HtmlTree object for the SECTION tag
 735      */
 736     public static HtmlTree SECTION(Content body) {
 737         HtmlTree htmltree = new HtmlTree(HtmlTag.SECTION, nullCheck(body));
 738         htmltree.setRole(Role.REGION);
 739         return htmltree;


 740     }
 741 
 742     /**
 743      * Generates a SMALL tag with some content.
 744      *
 745      * @param body content for the tag
 746      * @return an HtmlTree object for the SMALL tag
 747      */
 748     public static HtmlTree SMALL(Content body) {
 749         HtmlTree htmltree = new HtmlTree(HtmlTag.SMALL, nullCheck(body));
 750         return htmltree;
 751     }
 752 
 753     /**
 754      * Generates a SPAN tag with some content.
 755      *
 756      * @param body content for the tag
 757      * @return an HtmlTree object for the SPAN tag
 758      */
 759     public static HtmlTree SPAN(Content body) {


< prev index next >