src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlTree.java

Fri, 18 Oct 2013 16:34:42 -0700

author
bpatel
date
Fri, 18 Oct 2013 16:34:42 -0700
changeset 2147
130b8c0e570e
parent 2116
bf6b11347b1a
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8026567: Use meaningful style names for strong and italic styles.
Reviewed-by: jjg

bpatel@766 1 /*
jjg@1736 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
bpatel@766 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bpatel@766 4 *
bpatel@766 5 * This code is free software; you can redistribute it and/or modify it
bpatel@766 6 * under the terms of the GNU General Public License version 2 only, as
bpatel@766 7 * published by the Free Software Foundation. Oracle designates this
bpatel@766 8 * particular file as subject to the "Classpath" exception as provided
bpatel@766 9 * by Oracle in the LICENSE file that accompanied this code.
bpatel@766 10 *
bpatel@766 11 * This code is distributed in the hope that it will be useful, but WITHOUT
bpatel@766 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bpatel@766 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
bpatel@766 14 * version 2 for more details (a copy is included in the LICENSE file that
bpatel@766 15 * accompanied this code).
bpatel@766 16 *
bpatel@766 17 * You should have received a copy of the GNU General Public License version
bpatel@766 18 * 2 along with this work; if not, write to the Free Software Foundation,
bpatel@766 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bpatel@766 20 *
bpatel@766 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bpatel@766 22 * or visit www.oracle.com if you need additional information or have any
bpatel@766 23 * questions.
bpatel@766 24 */
bpatel@766 25
bpatel@766 26 package com.sun.tools.doclets.formats.html.markup;
bpatel@766 27
jjg@1364 28 import java.io.IOException;
jjg@1364 29 import java.io.Writer;
bpatel@766 30 import java.util.*;
bpatel@2055 31 import java.nio.charset.*;
jjg@1364 32
bpatel@766 33 import com.sun.tools.doclets.internal.toolkit.Content;
bpatel@766 34 import com.sun.tools.doclets.internal.toolkit.util.*;
bpatel@766 35
bpatel@766 36 /**
bpatel@766 37 * Class for generating HTML tree for javadoc output.
bpatel@766 38 *
jjg@1359 39 * <p><b>This is NOT part of any supported API.
jjg@1359 40 * If you write code that depends on this, you do so at your own risk.
jjg@1359 41 * This code and its internal interfaces are subject to change or
jjg@1359 42 * deletion without notice.</b>
jjg@1359 43 *
bpatel@766 44 * @author Bhavesh Patel
bpatel@766 45 */
bpatel@766 46 public class HtmlTree extends Content {
bpatel@766 47
bpatel@766 48 private HtmlTag htmlTag;
bpatel@766 49 private Map<HtmlAttr,String> attrs = Collections.<HtmlAttr,String>emptyMap();
bpatel@766 50 private List<Content> content = Collections.<Content>emptyList();
bpatel@766 51 public static final Content EMPTY = new StringContent("");
bpatel@766 52
bpatel@766 53 /**
bpatel@766 54 * Constructor to construct HtmlTree object.
bpatel@766 55 *
bpatel@766 56 * @param tag HTML tag for the HtmlTree object
bpatel@766 57 */
bpatel@766 58 public HtmlTree(HtmlTag tag) {
bpatel@766 59 htmlTag = nullCheck(tag);
bpatel@766 60 }
bpatel@766 61
bpatel@766 62 /**
bpatel@766 63 * Constructor to construct HtmlTree object.
bpatel@766 64 *
bpatel@766 65 * @param tag HTML tag for the HtmlTree object
bpatel@766 66 * @param contents contents to be added to the tree
bpatel@766 67 */
bpatel@766 68 public HtmlTree(HtmlTag tag, Content... contents) {
bpatel@766 69 this(tag);
bpatel@766 70 for (Content content: contents)
bpatel@766 71 addContent(content);
bpatel@766 72 }
bpatel@766 73
bpatel@766 74 /**
bpatel@766 75 * Adds an attribute for the HTML tag.
bpatel@766 76 *
bpatel@766 77 * @param attrName name of the attribute
bpatel@766 78 * @param attrValue value of the attribute
bpatel@766 79 */
bpatel@766 80 public void addAttr(HtmlAttr attrName, String attrValue) {
bpatel@766 81 if (attrs.isEmpty())
jjg@1746 82 attrs = new LinkedHashMap<HtmlAttr,String>(3);
jjg@1746 83 attrs.put(nullCheck(attrName), escapeHtmlChars(attrValue));
bpatel@766 84 }
bpatel@766 85
jjg@1747 86 public void setTitle(Content body) {
jjg@1747 87 addAttr(HtmlAttr.TITLE, stripHtml(body));
jjg@1747 88 }
jjg@1747 89
bpatel@766 90 /**
bpatel@766 91 * Adds a style for the HTML tag.
bpatel@766 92 *
bpatel@766 93 * @param style style to be added
bpatel@766 94 */
bpatel@766 95 public void addStyle(HtmlStyle style) {
bpatel@766 96 addAttr(HtmlAttr.CLASS, style.toString());
bpatel@766 97 }
bpatel@766 98
bpatel@766 99 /**
bpatel@766 100 * Adds content for the HTML tag.
bpatel@766 101 *
bpatel@766 102 * @param tagContent tag content to be added
bpatel@766 103 */
bpatel@766 104 public void addContent(Content tagContent) {
bpatel@2116 105 if (tagContent instanceof ContentBuilder) {
bpatel@2116 106 for (Content content: ((ContentBuilder)tagContent).contents) {
bpatel@2116 107 addContent(content);
bpatel@2116 108 }
bpatel@2116 109 }
bpatel@2116 110 else if (tagContent == HtmlTree.EMPTY || tagContent.isValid()) {
bpatel@766 111 if (content.isEmpty())
bpatel@766 112 content = new ArrayList<Content>();
bpatel@766 113 content.add(tagContent);
bpatel@766 114 }
bpatel@766 115 }
bpatel@766 116
bpatel@766 117 /**
bpatel@766 118 * This method adds a string content to the htmltree. If the last content member
bpatel@766 119 * added is a StringContent, append the string to that StringContent or else
bpatel@766 120 * create a new StringContent and add it to the html tree.
bpatel@766 121 *
bpatel@766 122 * @param stringContent string content that needs to be added
bpatel@766 123 */
bpatel@766 124 public void addContent(String stringContent) {
bpatel@766 125 if (!content.isEmpty()) {
bpatel@766 126 Content lastContent = content.get(content.size() - 1);
bpatel@766 127 if (lastContent instanceof StringContent)
bpatel@766 128 lastContent.addContent(stringContent);
bpatel@766 129 else
bpatel@766 130 addContent(new StringContent(stringContent));
bpatel@766 131 }
bpatel@766 132 else
bpatel@766 133 addContent(new StringContent(stringContent));
bpatel@766 134 }
bpatel@766 135
jjg@1737 136 public int charCount() {
jjg@1737 137 int n = 0;
jjg@1737 138 for (Content c : content)
jjg@1737 139 n += c.charCount();
jjg@1737 140 return n;
jjg@1737 141 }
jjg@1737 142
bpatel@766 143 /**
jjg@1746 144 * Given a string, escape all special html characters and
jjg@1746 145 * return the result.
jjg@1746 146 *
jjg@1746 147 * @param s The string to check.
jjg@1746 148 * @return the original string with all of the HTML characters escaped.
jjg@1746 149 */
jjg@1746 150 private static String escapeHtmlChars(String s) {
jjg@1746 151 for (int i = 0; i < s.length(); i++) {
jjg@1746 152 char ch = s.charAt(i);
jjg@1746 153 switch (ch) {
jjg@1746 154 // only start building a new string if we need to
jjg@1746 155 case '<': case '>': case '&':
jjg@1746 156 StringBuilder sb = new StringBuilder(s.substring(0, i));
jjg@1746 157 for ( ; i < s.length(); i++) {
jjg@1746 158 ch = s.charAt(i);
jjg@1746 159 switch (ch) {
jjg@1746 160 case '<': sb.append("&lt;"); break;
jjg@1746 161 case '>': sb.append("&gt;"); break;
jjg@1746 162 case '&': sb.append("&amp;"); break;
jjg@1746 163 default: sb.append(ch); break;
jjg@1746 164 }
jjg@1746 165 }
jjg@1746 166 return sb.toString();
jjg@1746 167 }
jjg@1746 168 }
jjg@1746 169 return s;
jjg@1746 170 }
jjg@1746 171
jjg@1746 172 /**
bpatel@2055 173 * A set of ASCII URI characters to be left unencoded.
bpatel@2055 174 */
jlahoda@2063 175 public static final BitSet NONENCODING_CHARS = new BitSet(256);
bpatel@2055 176
bpatel@2055 177 static {
bpatel@2055 178 // alphabetic characters
bpatel@2055 179 for (int i = 'a'; i <= 'z'; i++) {
bpatel@2055 180 NONENCODING_CHARS.set(i);
bpatel@2055 181 }
bpatel@2055 182 for (int i = 'A'; i <= 'Z'; i++) {
bpatel@2055 183 NONENCODING_CHARS.set(i);
bpatel@2055 184 }
bpatel@2055 185 // numeric characters
bpatel@2055 186 for (int i = '0'; i <= '9'; i++) {
bpatel@2055 187 NONENCODING_CHARS.set(i);
bpatel@2055 188 }
bpatel@2055 189 // Reserved characters as per RFC 3986. These are set of delimiting characters.
bpatel@2055 190 String noEnc = ":/?#[]@!$&'()*+,;=";
bpatel@2055 191 // Unreserved characters as per RFC 3986 which should not be percent encoded.
bpatel@2055 192 noEnc += "-._~";
bpatel@2055 193 for (int i = 0; i < noEnc.length(); i++) {
bpatel@2055 194 NONENCODING_CHARS.set(noEnc.charAt(i));
bpatel@2055 195 }
bpatel@2055 196 }
bpatel@2055 197
bpatel@2055 198 private static String encodeURL(String url) {
bpatel@2055 199 byte[] urlBytes = url.getBytes(Charset.forName("UTF-8"));
bpatel@2055 200 StringBuilder sb = new StringBuilder();
bpatel@2055 201 for (int i = 0; i < urlBytes.length; i++) {
bpatel@2055 202 int c = urlBytes[i];
bpatel@2055 203 if (NONENCODING_CHARS.get(c & 0xFF)) {
bpatel@2055 204 sb.append((char) c);
bpatel@2055 205 } else {
bpatel@2055 206 sb.append(String.format("%%%02X", c & 0xFF));
bpatel@2055 207 }
bpatel@2055 208 }
bpatel@2055 209 return sb.toString();
bpatel@2055 210 }
bpatel@2055 211
bpatel@2055 212 /**
bpatel@766 213 * Generates an HTML anchor tag.
bpatel@766 214 *
bpatel@766 215 * @param ref reference url for the anchor tag
bpatel@766 216 * @param body content for the anchor tag
bpatel@766 217 * @return an HtmlTree object
bpatel@766 218 */
bpatel@766 219 public static HtmlTree A(String ref, Content body) {
bpatel@766 220 HtmlTree htmltree = new HtmlTree(HtmlTag.A, nullCheck(body));
bpatel@2055 221 htmltree.addAttr(HtmlAttr.HREF, encodeURL(ref));
bpatel@766 222 return htmltree;
bpatel@766 223 }
bpatel@766 224
bpatel@766 225 /**
bpatel@766 226 * Generates an HTML anchor tag with name attribute and content.
bpatel@766 227 *
bpatel@766 228 * @param name name for the anchor tag
bpatel@766 229 * @param body content for the anchor tag
bpatel@766 230 * @return an HtmlTree object
bpatel@766 231 */
bpatel@766 232 public static HtmlTree A_NAME(String name, Content body) {
bpatel@766 233 HtmlTree htmltree = HtmlTree.A_NAME(name);
bpatel@766 234 htmltree.addContent(nullCheck(body));
bpatel@766 235 return htmltree;
bpatel@766 236 }
bpatel@766 237
bpatel@766 238 /**
bpatel@766 239 * Generates an HTML anchor tag with name attribute.
bpatel@766 240 *
bpatel@766 241 * @param name name for the anchor tag
bpatel@766 242 * @return an HtmlTree object
bpatel@766 243 */
bpatel@766 244 public static HtmlTree A_NAME(String name) {
bpatel@766 245 HtmlTree htmltree = new HtmlTree(HtmlTag.A);
bpatel@766 246 htmltree.addAttr(HtmlAttr.NAME, nullCheck(name));
bpatel@766 247 return htmltree;
bpatel@766 248 }
bpatel@766 249
bpatel@766 250 /**
bpatel@766 251 * Generates a CAPTION tag with some content.
bpatel@766 252 *
bpatel@766 253 * @param body content for the tag
bpatel@766 254 * @return an HtmlTree object for the CAPTION tag
bpatel@766 255 */
bpatel@766 256 public static HtmlTree CAPTION(Content body) {
bpatel@766 257 HtmlTree htmltree = new HtmlTree(HtmlTag.CAPTION, nullCheck(body));
bpatel@766 258 return htmltree;
bpatel@766 259 }
bpatel@766 260
bpatel@766 261 /**
bpatel@766 262 * Generates a CODE tag with some content.
bpatel@766 263 *
bpatel@766 264 * @param body content for the tag
bpatel@766 265 * @return an HtmlTree object for the CODE tag
bpatel@766 266 */
bpatel@766 267 public static HtmlTree CODE(Content body) {
bpatel@766 268 HtmlTree htmltree = new HtmlTree(HtmlTag.CODE, nullCheck(body));
bpatel@766 269 return htmltree;
bpatel@766 270 }
bpatel@766 271
bpatel@766 272 /**
bpatel@766 273 * Generates a DD tag with some content.
bpatel@766 274 *
bpatel@766 275 * @param body content for the tag
bpatel@766 276 * @return an HtmlTree object for the DD tag
bpatel@766 277 */
bpatel@766 278 public static HtmlTree DD(Content body) {
bpatel@766 279 HtmlTree htmltree = new HtmlTree(HtmlTag.DD, nullCheck(body));
bpatel@766 280 return htmltree;
bpatel@766 281 }
bpatel@766 282
bpatel@766 283 /**
bpatel@766 284 * Generates a DL tag with some content.
bpatel@766 285 *
bpatel@766 286 * @param body content for the tag
bpatel@766 287 * @return an HtmlTree object for the DL tag
bpatel@766 288 */
bpatel@766 289 public static HtmlTree DL(Content body) {
bpatel@766 290 HtmlTree htmltree = new HtmlTree(HtmlTag.DL, nullCheck(body));
bpatel@766 291 return htmltree;
bpatel@766 292 }
bpatel@766 293
bpatel@766 294 /**
bpatel@766 295 * Generates a DIV tag with the style class attributes. It also encloses
bpatel@766 296 * a content.
bpatel@766 297 *
bpatel@766 298 * @param styleClass stylesheet class for the tag
bpatel@766 299 * @param body content for the tag
bpatel@766 300 * @return an HtmlTree object for the DIV tag
bpatel@766 301 */
bpatel@766 302 public static HtmlTree DIV(HtmlStyle styleClass, Content body) {
bpatel@766 303 HtmlTree htmltree = new HtmlTree(HtmlTag.DIV, nullCheck(body));
bpatel@766 304 if (styleClass != null)
bpatel@766 305 htmltree.addStyle(styleClass);
bpatel@766 306 return htmltree;
bpatel@766 307 }
bpatel@766 308
bpatel@766 309 /**
bpatel@766 310 * Generates a DIV tag with some content.
bpatel@766 311 *
bpatel@766 312 * @param body content for the tag
bpatel@766 313 * @return an HtmlTree object for the DIV tag
bpatel@766 314 */
bpatel@766 315 public static HtmlTree DIV(Content body) {
bpatel@766 316 return DIV(null, body);
bpatel@766 317 }
bpatel@766 318
bpatel@766 319 /**
bpatel@766 320 * Generates a DT tag with some content.
bpatel@766 321 *
bpatel@766 322 * @param body content for the tag
bpatel@766 323 * @return an HtmlTree object for the DT tag
bpatel@766 324 */
bpatel@766 325 public static HtmlTree DT(Content body) {
bpatel@766 326 HtmlTree htmltree = new HtmlTree(HtmlTag.DT, nullCheck(body));
bpatel@766 327 return htmltree;
bpatel@766 328 }
bpatel@766 329
bpatel@766 330 /**
bpatel@766 331 * Generates a FRAME tag.
bpatel@766 332 *
bpatel@766 333 * @param src the url of the document to be shown in the frame
bpatel@766 334 * @param name specifies the name of the frame
bpatel@766 335 * @param title the title for the frame
bpatel@766 336 * @param scrolling specifies whether to display scrollbars in the frame
bpatel@766 337 * @return an HtmlTree object for the FRAME tag
bpatel@766 338 */
bpatel@766 339 public static HtmlTree FRAME(String src, String name, String title, String scrolling) {
bpatel@766 340 HtmlTree htmltree = new HtmlTree(HtmlTag.FRAME);
bpatel@766 341 htmltree.addAttr(HtmlAttr.SRC, nullCheck(src));
bpatel@766 342 htmltree.addAttr(HtmlAttr.NAME, nullCheck(name));
bpatel@766 343 htmltree.addAttr(HtmlAttr.TITLE, nullCheck(title));
bpatel@766 344 if (scrolling != null)
bpatel@766 345 htmltree.addAttr(HtmlAttr.SCROLLING, scrolling);
bpatel@766 346 return htmltree;
bpatel@766 347 }
bpatel@766 348
bpatel@766 349 /**
bpatel@766 350 * Generates a Frame tag.
bpatel@766 351 *
bpatel@766 352 * @param src the url of the document to be shown in the frame
bpatel@766 353 * @param name specifies the name of the frame
bpatel@766 354 * @param title the title for the frame
bpatel@766 355 * @return an HtmlTree object for the SPAN tag
bpatel@766 356 */
bpatel@766 357 public static HtmlTree FRAME(String src, String name, String title) {
bpatel@766 358 return FRAME(src, name, title, null);
bpatel@766 359 }
bpatel@766 360
bpatel@766 361 /**
bpatel@766 362 * Generates a FRAMESET tag.
bpatel@766 363 *
bpatel@766 364 * @param cols the size of columns in the frameset
bpatel@766 365 * @param rows the size of rows in the frameset
bpatel@766 366 * @param title the title for the frameset
bpatel@766 367 * @param onload the script to run when the document loads
bpatel@766 368 * @return an HtmlTree object for the FRAMESET tag
bpatel@766 369 */
bpatel@766 370 public static HtmlTree FRAMESET(String cols, String rows, String title, String onload) {
bpatel@766 371 HtmlTree htmltree = new HtmlTree(HtmlTag.FRAMESET);
bpatel@766 372 if (cols != null)
bpatel@766 373 htmltree.addAttr(HtmlAttr.COLS, cols);
bpatel@766 374 if (rows != null)
bpatel@766 375 htmltree.addAttr(HtmlAttr.ROWS, rows);
bpatel@766 376 htmltree.addAttr(HtmlAttr.TITLE, nullCheck(title));
bpatel@766 377 htmltree.addAttr(HtmlAttr.ONLOAD, nullCheck(onload));
bpatel@766 378 return htmltree;
bpatel@766 379 }
bpatel@766 380
bpatel@766 381 /**
bpatel@766 382 * Generates a heading tag (h1 to h6) with the title and style class attributes. It also encloses
bpatel@766 383 * a content.
bpatel@766 384 *
bpatel@766 385 * @param headingTag the heading tag to be generated
bpatel@766 386 * @param printTitle true if title for the tag needs to be printed else false
bpatel@766 387 * @param styleClass stylesheet class for the tag
bpatel@766 388 * @param body content for the tag
bpatel@766 389 * @return an HtmlTree object for the tag
bpatel@766 390 */
bpatel@766 391 public static HtmlTree HEADING(HtmlTag headingTag, boolean printTitle,
bpatel@766 392 HtmlStyle styleClass, Content body) {
bpatel@766 393 HtmlTree htmltree = new HtmlTree(headingTag, nullCheck(body));
bpatel@766 394 if (printTitle)
jjg@1747 395 htmltree.setTitle(body);
bpatel@766 396 if (styleClass != null)
bpatel@766 397 htmltree.addStyle(styleClass);
bpatel@766 398 return htmltree;
bpatel@766 399 }
bpatel@766 400
bpatel@766 401 /**
bpatel@766 402 * Generates a heading tag (h1 to h6) with style class attribute. It also encloses
bpatel@766 403 * a content.
bpatel@766 404 *
bpatel@766 405 * @param headingTag the heading tag to be generated
bpatel@766 406 * @param styleClass stylesheet class for the tag
bpatel@766 407 * @param body content for the tag
bpatel@766 408 * @return an HtmlTree object for the tag
bpatel@766 409 */
bpatel@766 410 public static HtmlTree HEADING(HtmlTag headingTag, HtmlStyle styleClass, Content body) {
bpatel@766 411 return HEADING(headingTag, false, styleClass, body);
bpatel@766 412 }
bpatel@766 413
bpatel@766 414 /**
bpatel@766 415 * Generates a heading tag (h1 to h6) with the title attribute. It also encloses
bpatel@766 416 * a content.
bpatel@766 417 *
bpatel@766 418 * @param headingTag the heading tag to be generated
bpatel@766 419 * @param printTitle true if the title for the tag needs to be printed else false
bpatel@766 420 * @param body content for the tag
bpatel@766 421 * @return an HtmlTree object for the tag
bpatel@766 422 */
bpatel@766 423 public static HtmlTree HEADING(HtmlTag headingTag, boolean printTitle, Content body) {
bpatel@766 424 return HEADING(headingTag, printTitle, null, body);
bpatel@766 425 }
bpatel@766 426
bpatel@766 427 /**
bpatel@766 428 * Generates a heading tag (h1 to h6) with some content.
bpatel@766 429 *
bpatel@766 430 * @param headingTag the heading tag to be generated
bpatel@766 431 * @param body content for the tag
bpatel@766 432 * @return an HtmlTree object for the tag
bpatel@766 433 */
bpatel@766 434 public static HtmlTree HEADING(HtmlTag headingTag, Content body) {
bpatel@766 435 return HEADING(headingTag, false, null, body);
bpatel@766 436 }
bpatel@766 437
bpatel@766 438 /**
bpatel@766 439 * Generates an HTML tag with lang attribute. It also adds head and body
bpatel@766 440 * content to the HTML tree.
bpatel@766 441 *
bpatel@766 442 * @param lang language for the HTML document
bpatel@766 443 * @param head head for the HTML tag
bpatel@766 444 * @param body body for the HTML tag
bpatel@766 445 * @return an HtmlTree object for the HTML tag
bpatel@766 446 */
bpatel@766 447 public static HtmlTree HTML(String lang, Content head, Content body) {
bpatel@766 448 HtmlTree htmltree = new HtmlTree(HtmlTag.HTML, nullCheck(head), nullCheck(body));
bpatel@766 449 htmltree.addAttr(HtmlAttr.LANG, nullCheck(lang));
bpatel@766 450 return htmltree;
bpatel@766 451 }
bpatel@766 452
bpatel@766 453 /**
bpatel@766 454 * Generates a LI tag with some content.
bpatel@766 455 *
bpatel@766 456 * @param body content for the tag
bpatel@766 457 * @return an HtmlTree object for the LI tag
bpatel@766 458 */
bpatel@766 459 public static HtmlTree LI(Content body) {
bpatel@766 460 return LI(null, body);
bpatel@766 461 }
bpatel@766 462
bpatel@766 463 /**
bpatel@766 464 * Generates a LI tag with some content.
bpatel@766 465 *
bpatel@766 466 * @param styleClass style for the tag
bpatel@766 467 * @param body content for the tag
bpatel@766 468 * @return an HtmlTree object for the LI tag
bpatel@766 469 */
bpatel@766 470 public static HtmlTree LI(HtmlStyle styleClass, Content body) {
bpatel@766 471 HtmlTree htmltree = new HtmlTree(HtmlTag.LI, nullCheck(body));
bpatel@766 472 if (styleClass != null)
bpatel@766 473 htmltree.addStyle(styleClass);
bpatel@766 474 return htmltree;
bpatel@766 475 }
bpatel@766 476
bpatel@766 477 /**
bpatel@766 478 * Generates a LINK tag with the rel, type, href and title attributes.
bpatel@766 479 *
bpatel@766 480 * @param rel relevance of the link
bpatel@766 481 * @param type type of link
bpatel@766 482 * @param href the path for the link
bpatel@766 483 * @param title title for the link
bpatel@766 484 * @return an HtmlTree object for the LINK tag
bpatel@766 485 */
bpatel@766 486 public static HtmlTree LINK(String rel, String type, String href, String title) {
bpatel@766 487 HtmlTree htmltree = new HtmlTree(HtmlTag.LINK);
bpatel@766 488 htmltree.addAttr(HtmlAttr.REL, nullCheck(rel));
bpatel@766 489 htmltree.addAttr(HtmlAttr.TYPE, nullCheck(type));
bpatel@766 490 htmltree.addAttr(HtmlAttr.HREF, nullCheck(href));
bpatel@766 491 htmltree.addAttr(HtmlAttr.TITLE, nullCheck(title));
bpatel@766 492 return htmltree;
bpatel@766 493 }
bpatel@766 494
bpatel@766 495 /**
bpatel@766 496 * Generates a META tag with the http-equiv, content and charset attributes.
bpatel@766 497 *
jjg@1358 498 * @param httpEquiv http equiv attribute for the META tag
bpatel@766 499 * @param content type of content
jjg@1358 500 * @param charSet character set used
bpatel@766 501 * @return an HtmlTree object for the META tag
bpatel@766 502 */
bpatel@766 503 public static HtmlTree META(String httpEquiv, String content, String charSet) {
bpatel@766 504 HtmlTree htmltree = new HtmlTree(HtmlTag.META);
bpatel@1981 505 String contentCharset = content + "; charset=" + charSet;
bpatel@766 506 htmltree.addAttr(HtmlAttr.HTTP_EQUIV, nullCheck(httpEquiv));
bpatel@1981 507 htmltree.addAttr(HtmlAttr.CONTENT, contentCharset);
bpatel@766 508 return htmltree;
bpatel@766 509 }
bpatel@766 510
bpatel@766 511 /**
bpatel@766 512 * Generates a META tag with the name and content attributes.
bpatel@766 513 *
bpatel@766 514 * @param name name attribute
bpatel@766 515 * @param content type of content
bpatel@766 516 * @return an HtmlTree object for the META tag
bpatel@766 517 */
bpatel@766 518 public static HtmlTree META(String name, String content) {
bpatel@766 519 HtmlTree htmltree = new HtmlTree(HtmlTag.META);
bpatel@766 520 htmltree.addAttr(HtmlAttr.NAME, nullCheck(name));
bpatel@766 521 htmltree.addAttr(HtmlAttr.CONTENT, nullCheck(content));
bpatel@766 522 return htmltree;
bpatel@766 523 }
bpatel@766 524
bpatel@766 525 /**
bpatel@766 526 * Generates a NOSCRIPT tag with some content.
bpatel@766 527 *
bpatel@766 528 * @param body content of the noscript tag
bpatel@766 529 * @return an HtmlTree object for the NOSCRIPT tag
bpatel@766 530 */
bpatel@766 531 public static HtmlTree NOSCRIPT(Content body) {
bpatel@766 532 HtmlTree htmltree = new HtmlTree(HtmlTag.NOSCRIPT, nullCheck(body));
bpatel@766 533 return htmltree;
bpatel@766 534 }
bpatel@766 535
bpatel@766 536 /**
bpatel@766 537 * Generates a P tag with some content.
bpatel@766 538 *
bpatel@766 539 * @param body content of the Paragraph tag
bpatel@766 540 * @return an HtmlTree object for the P tag
bpatel@766 541 */
bpatel@766 542 public static HtmlTree P(Content body) {
bpatel@766 543 return P(null, body);
bpatel@766 544 }
bpatel@766 545
bpatel@766 546 /**
bpatel@766 547 * Generates a P tag with some content.
bpatel@766 548 *
bpatel@766 549 * @param styleClass style of the Paragraph tag
bpatel@766 550 * @param body content of the Paragraph tag
bpatel@766 551 * @return an HtmlTree object for the P tag
bpatel@766 552 */
bpatel@766 553 public static HtmlTree P(HtmlStyle styleClass, Content body) {
bpatel@766 554 HtmlTree htmltree = new HtmlTree(HtmlTag.P, nullCheck(body));
bpatel@766 555 if (styleClass != null)
bpatel@766 556 htmltree.addStyle(styleClass);
bpatel@766 557 return htmltree;
bpatel@766 558 }
bpatel@766 559
bpatel@766 560 /**
bpatel@1417 561 * Generates a SCRIPT tag with the type and src attributes.
bpatel@1417 562 *
bpatel@1417 563 * @param type type of link
bpatel@1417 564 * @param src the path for the script
bpatel@1417 565 * @return an HtmlTree object for the SCRIPT tag
bpatel@1417 566 */
bpatel@1417 567 public static HtmlTree SCRIPT(String type, String src) {
bpatel@1417 568 HtmlTree htmltree = new HtmlTree(HtmlTag.SCRIPT);
bpatel@1417 569 htmltree.addAttr(HtmlAttr.TYPE, nullCheck(type));
bpatel@1417 570 htmltree.addAttr(HtmlAttr.SRC, nullCheck(src));
bpatel@1417 571 return htmltree;
bpatel@1417 572 }
bpatel@1417 573
bpatel@1417 574 /**
bpatel@766 575 * Generates a SMALL tag with some content.
bpatel@766 576 *
bpatel@766 577 * @param body content for the tag
bpatel@766 578 * @return an HtmlTree object for the SMALL tag
bpatel@766 579 */
bpatel@766 580 public static HtmlTree SMALL(Content body) {
bpatel@766 581 HtmlTree htmltree = new HtmlTree(HtmlTag.SMALL, nullCheck(body));
bpatel@766 582 return htmltree;
bpatel@766 583 }
bpatel@766 584
bpatel@766 585 /**
bpatel@766 586 * Generates a SPAN tag with some content.
bpatel@766 587 *
bpatel@766 588 * @param body content for the tag
bpatel@766 589 * @return an HtmlTree object for the SPAN tag
bpatel@766 590 */
bpatel@766 591 public static HtmlTree SPAN(Content body) {
bpatel@766 592 return SPAN(null, body);
bpatel@766 593 }
bpatel@766 594
bpatel@766 595 /**
bpatel@766 596 * Generates a SPAN tag with style class attribute and some content.
bpatel@766 597 *
bpatel@766 598 * @param styleClass style class for the tag
bpatel@766 599 * @param body content for the tag
bpatel@766 600 * @return an HtmlTree object for the SPAN tag
bpatel@766 601 */
bpatel@766 602 public static HtmlTree SPAN(HtmlStyle styleClass, Content body) {
bpatel@766 603 HtmlTree htmltree = new HtmlTree(HtmlTag.SPAN, nullCheck(body));
bpatel@766 604 if (styleClass != null)
bpatel@766 605 htmltree.addStyle(styleClass);
bpatel@766 606 return htmltree;
bpatel@766 607 }
bpatel@766 608
bpatel@766 609 /**
bpatel@1417 610 * Generates a SPAN tag with id and style class attributes. It also encloses
bpatel@1417 611 * a content.
bpatel@1417 612 *
bpatel@1417 613 * @param id the id for the tag
bpatel@1417 614 * @param styleClass stylesheet class for the tag
bpatel@1417 615 * @param body content for the tag
bpatel@1417 616 * @return an HtmlTree object for the SPAN tag
bpatel@1417 617 */
bpatel@1417 618 public static HtmlTree SPAN(String id, HtmlStyle styleClass, Content body) {
bpatel@1417 619 HtmlTree htmltree = new HtmlTree(HtmlTag.SPAN, nullCheck(body));
bpatel@1417 620 htmltree.addAttr(HtmlAttr.ID, nullCheck(id));
bpatel@1417 621 if (styleClass != null)
bpatel@1417 622 htmltree.addStyle(styleClass);
bpatel@1417 623 return htmltree;
bpatel@1417 624 }
bpatel@1417 625
bpatel@1417 626 /**
bpatel@766 627 * Generates a Table tag with style class, border, cell padding,
bpatel@766 628 * cellspacing and summary attributes and some content.
bpatel@766 629 *
bpatel@766 630 * @param styleClass style of the table
bpatel@766 631 * @param border border for the table
bpatel@766 632 * @param cellPadding cell padding for the table
bpatel@766 633 * @param cellSpacing cell spacing for the table
bpatel@766 634 * @param summary summary for the table
bpatel@766 635 * @param body content for the table
bpatel@766 636 * @return an HtmlTree object for the TABLE tag
bpatel@766 637 */
bpatel@766 638 public static HtmlTree TABLE(HtmlStyle styleClass, int border, int cellPadding,
bpatel@766 639 int cellSpacing, String summary, Content body) {
bpatel@766 640 HtmlTree htmltree = new HtmlTree(HtmlTag.TABLE, nullCheck(body));
bpatel@766 641 if (styleClass != null)
bpatel@766 642 htmltree.addStyle(styleClass);
bpatel@766 643 htmltree.addAttr(HtmlAttr.BORDER, Integer.toString(border));
bpatel@766 644 htmltree.addAttr(HtmlAttr.CELLPADDING, Integer.toString(cellPadding));
bpatel@766 645 htmltree.addAttr(HtmlAttr.CELLSPACING, Integer.toString(cellSpacing));
bpatel@766 646 htmltree.addAttr(HtmlAttr.SUMMARY, nullCheck(summary));
bpatel@766 647 return htmltree;
bpatel@766 648 }
bpatel@766 649
bpatel@766 650 /**
bpatel@766 651 * Generates a TD tag with style class attribute and some content.
bpatel@766 652 *
bpatel@766 653 * @param styleClass style for the tag
bpatel@766 654 * @param body content for the tag
bpatel@766 655 * @return an HtmlTree object for the TD tag
bpatel@766 656 */
bpatel@766 657 public static HtmlTree TD(HtmlStyle styleClass, Content body) {
bpatel@766 658 HtmlTree htmltree = new HtmlTree(HtmlTag.TD, nullCheck(body));
bpatel@766 659 if (styleClass != null)
bpatel@766 660 htmltree.addStyle(styleClass);
bpatel@766 661 return htmltree;
bpatel@766 662 }
bpatel@766 663
bpatel@766 664 /**
bpatel@766 665 * Generates a TD tag for an HTML table with some content.
bpatel@766 666 *
bpatel@766 667 * @param body content for the tag
bpatel@766 668 * @return an HtmlTree object for the TD tag
bpatel@766 669 */
bpatel@766 670 public static HtmlTree TD(Content body) {
bpatel@766 671 return TD(null, body);
bpatel@766 672 }
bpatel@766 673
bpatel@766 674 /**
bpatel@766 675 * Generates a TH tag with style class and scope attributes and some content.
bpatel@766 676 *
bpatel@766 677 * @param styleClass style for the tag
bpatel@766 678 * @param scope scope of the tag
bpatel@766 679 * @param body content for the tag
bpatel@766 680 * @return an HtmlTree object for the TH tag
bpatel@766 681 */
bpatel@766 682 public static HtmlTree TH(HtmlStyle styleClass, String scope, Content body) {
bpatel@766 683 HtmlTree htmltree = new HtmlTree(HtmlTag.TH, nullCheck(body));
bpatel@766 684 if (styleClass != null)
bpatel@766 685 htmltree.addStyle(styleClass);
bpatel@766 686 htmltree.addAttr(HtmlAttr.SCOPE, nullCheck(scope));
bpatel@766 687 return htmltree;
bpatel@766 688 }
bpatel@766 689
bpatel@766 690 /**
bpatel@766 691 * Generates a TH tag with scope attribute and some content.
bpatel@766 692 *
bpatel@766 693 * @param scope scope of the tag
bpatel@766 694 * @param body content for the tag
bpatel@766 695 * @return an HtmlTree object for the TH tag
bpatel@766 696 */
bpatel@766 697 public static HtmlTree TH(String scope, Content body) {
bpatel@766 698 return TH(null, scope, body);
bpatel@766 699 }
bpatel@766 700
bpatel@766 701 /**
bpatel@766 702 * Generates a TITLE tag with some content.
bpatel@766 703 *
bpatel@766 704 * @param body content for the tag
bpatel@766 705 * @return an HtmlTree object for the TITLE tag
bpatel@766 706 */
bpatel@766 707 public static HtmlTree TITLE(Content body) {
bpatel@766 708 HtmlTree htmltree = new HtmlTree(HtmlTag.TITLE, nullCheck(body));
bpatel@766 709 return htmltree;
bpatel@766 710 }
bpatel@766 711
bpatel@766 712 /**
bpatel@766 713 * Generates a TR tag for an HTML table with some content.
bpatel@766 714 *
bpatel@766 715 * @param body content for the tag
bpatel@766 716 * @return an HtmlTree object for the TR tag
bpatel@766 717 */
bpatel@766 718 public static HtmlTree TR(Content body) {
bpatel@766 719 HtmlTree htmltree = new HtmlTree(HtmlTag.TR, nullCheck(body));
bpatel@766 720 return htmltree;
bpatel@766 721 }
bpatel@766 722
bpatel@766 723 /**
bpatel@766 724 * Generates a UL tag with the style class attribute and some content.
bpatel@766 725 *
bpatel@766 726 * @param styleClass style for the tag
bpatel@766 727 * @param body content for the tag
bpatel@766 728 * @return an HtmlTree object for the UL tag
bpatel@766 729 */
bpatel@766 730 public static HtmlTree UL(HtmlStyle styleClass, Content body) {
bpatel@766 731 HtmlTree htmltree = new HtmlTree(HtmlTag.UL, nullCheck(body));
bpatel@766 732 htmltree.addStyle(nullCheck(styleClass));
bpatel@766 733 return htmltree;
bpatel@766 734 }
bpatel@766 735
bpatel@766 736 /**
bpatel@766 737 * {@inheritDoc}
bpatel@766 738 */
bpatel@766 739 public boolean isEmpty() {
bpatel@766 740 return (!hasContent() && !hasAttrs());
bpatel@766 741 }
bpatel@766 742
bpatel@766 743 /**
bpatel@766 744 * Returns true if the HTML tree has content.
bpatel@766 745 *
bpatel@766 746 * @return true if the HTML tree has content else return false
bpatel@766 747 */
bpatel@766 748 public boolean hasContent() {
bpatel@766 749 return (!content.isEmpty());
bpatel@766 750 }
bpatel@766 751
bpatel@766 752 /**
bpatel@766 753 * Returns true if the HTML tree has attributes.
bpatel@766 754 *
bpatel@766 755 * @return true if the HTML tree has attributes else return false
bpatel@766 756 */
bpatel@766 757 public boolean hasAttrs() {
bpatel@766 758 return (!attrs.isEmpty());
bpatel@766 759 }
bpatel@766 760
bpatel@766 761 /**
bpatel@766 762 * Returns true if the HTML tree has a specific attribute.
bpatel@766 763 *
bpatel@766 764 * @param attrName name of the attribute to check within the HTML tree
bpatel@766 765 * @return true if the HTML tree has the specified attribute else return false
bpatel@766 766 */
bpatel@766 767 public boolean hasAttr(HtmlAttr attrName) {
bpatel@766 768 return (attrs.containsKey(attrName));
bpatel@766 769 }
bpatel@766 770
bpatel@766 771 /**
bpatel@766 772 * Returns true if the HTML tree is valid. This check is more specific to
bpatel@766 773 * standard doclet and not exactly similar to W3C specifications. But it
bpatel@766 774 * ensures HTML validation.
bpatel@766 775 *
bpatel@766 776 * @return true if the HTML tree is valid
bpatel@766 777 */
bpatel@766 778 public boolean isValid() {
bpatel@766 779 switch (htmlTag) {
bpatel@766 780 case A :
bpatel@766 781 return (hasAttr(HtmlAttr.NAME) || (hasAttr(HtmlAttr.HREF) && hasContent()));
bpatel@766 782 case BR :
bpatel@766 783 return (!hasContent() && (!hasAttrs() || hasAttr(HtmlAttr.CLEAR)));
bpatel@766 784 case FRAME :
bpatel@766 785 return (hasAttr(HtmlAttr.SRC) && !hasContent());
bpatel@766 786 case HR :
bpatel@766 787 return (!hasContent());
bpatel@766 788 case IMG :
bpatel@766 789 return (hasAttr(HtmlAttr.SRC) && hasAttr(HtmlAttr.ALT) && !hasContent());
bpatel@766 790 case LINK :
bpatel@766 791 return (hasAttr(HtmlAttr.HREF) && !hasContent());
bpatel@766 792 case META :
bpatel@766 793 return (hasAttr(HtmlAttr.CONTENT) && !hasContent());
bpatel@1417 794 case SCRIPT :
bpatel@1417 795 return ((hasAttr(HtmlAttr.TYPE) && hasAttr(HtmlAttr.SRC) && !hasContent()) ||
bpatel@1417 796 (hasAttr(HtmlAttr.TYPE) && hasContent()));
bpatel@766 797 default :
bpatel@766 798 return hasContent();
bpatel@766 799 }
bpatel@766 800 }
bpatel@766 801
bpatel@766 802 /**
bpatel@766 803 * Returns true if the element is an inline element.
bpatel@766 804 *
bpatel@766 805 * @return true if the HTML tag is an inline element
bpatel@766 806 */
bpatel@766 807 public boolean isInline() {
bpatel@766 808 return (htmlTag.blockType == HtmlTag.BlockType.INLINE);
bpatel@766 809 }
bpatel@766 810
bpatel@766 811 /**
bpatel@766 812 * {@inheritDoc}
bpatel@766 813 */
jjg@1364 814 @Override
jjg@1364 815 public boolean write(Writer out, boolean atNewline) throws IOException {
jjg@1364 816 if (!isInline() && !atNewline)
jjg@1364 817 out.write(DocletConstants.NL);
bpatel@766 818 String tagString = htmlTag.toString();
jjg@1364 819 out.write("<");
jjg@1364 820 out.write(tagString);
bpatel@766 821 Iterator<HtmlAttr> iterator = attrs.keySet().iterator();
bpatel@766 822 HtmlAttr key;
jjg@1747 823 String value;
bpatel@766 824 while (iterator.hasNext()) {
bpatel@766 825 key = iterator.next();
bpatel@766 826 value = attrs.get(key);
jjg@1364 827 out.write(" ");
jjg@1364 828 out.write(key.toString());
bpatel@819 829 if (!value.isEmpty()) {
jjg@1364 830 out.write("=\"");
jjg@1364 831 out.write(value);
jjg@1364 832 out.write("\"");
bpatel@819 833 }
bpatel@766 834 }
jjg@1364 835 out.write(">");
jjg@1364 836 boolean nl = false;
bpatel@766 837 for (Content c : content)
jjg@1364 838 nl = c.write(out, nl);
bpatel@819 839 if (htmlTag.endTagRequired()) {
jjg@1364 840 out.write("</");
jjg@1364 841 out.write(tagString);
jjg@1364 842 out.write(">");
bpatel@819 843 }
jjg@1364 844 if (!isInline()) {
jjg@1364 845 out.write(DocletConstants.NL);
jjg@1364 846 return true;
jjg@1364 847 } else {
jjg@1364 848 return false;
jjg@1364 849 }
bpatel@766 850 }
jjg@1746 851
jjg@1746 852 /**
jjg@1746 853 * Given a Content node, strips all html characters and
jjg@1746 854 * return the result.
jjg@1746 855 *
jjg@1746 856 * @param body The content node to check.
jjg@1746 857 * @return the plain text from the content node
jjg@1746 858 *
jjg@1746 859 */
jjg@1746 860 private static String stripHtml(Content body) {
jjg@1746 861 String rawString = body.toString();
jjg@1746 862 // remove HTML tags
jjg@1746 863 rawString = rawString.replaceAll("\\<.*?>", " ");
jjg@1746 864 // consolidate multiple spaces between a word to a single space
jjg@1746 865 rawString = rawString.replaceAll("\\b\\s{2,}\\b", " ");
jjg@1746 866 // remove extra whitespaces
jjg@1746 867 return rawString.trim();
jjg@1746 868 }
bpatel@766 869 }

mercurial