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

Wed, 06 Apr 2011 20:33:44 -0700

author
ohair
date
Wed, 06 Apr 2011 20:33:44 -0700
changeset 962
0ff2bbd38f10
parent 819
a466f00c5cd2
child 1358
fc123bdeddb8
permissions
-rw-r--r--

7033660: Update copyright year to 2011 on any files changed in 2011
Reviewed-by: dholmes

bpatel@766 1 /*
ohair@962 2 * Copyright (c) 2010, 2011, 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
bpatel@766 28 import java.util.*;
bpatel@766 29 import com.sun.tools.doclets.internal.toolkit.Content;
bpatel@766 30 import com.sun.tools.doclets.internal.toolkit.util.*;
bpatel@766 31
bpatel@766 32 /**
bpatel@766 33 * Class for generating HTML tree for javadoc output.
bpatel@766 34 *
bpatel@766 35 * @author Bhavesh Patel
bpatel@766 36 */
bpatel@766 37 public class HtmlTree extends Content {
bpatel@766 38
bpatel@766 39 private HtmlTag htmlTag;
bpatel@766 40 private Map<HtmlAttr,String> attrs = Collections.<HtmlAttr,String>emptyMap();
bpatel@766 41 private List<Content> content = Collections.<Content>emptyList();
bpatel@766 42 public static final Content EMPTY = new StringContent("");
bpatel@766 43
bpatel@766 44 /**
bpatel@766 45 * Constructor to construct HtmlTree object.
bpatel@766 46 *
bpatel@766 47 * @param tag HTML tag for the HtmlTree object
bpatel@766 48 */
bpatel@766 49 public HtmlTree(HtmlTag tag) {
bpatel@766 50 htmlTag = nullCheck(tag);
bpatel@766 51 }
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 * @param contents contents to be added to the tree
bpatel@766 58 */
bpatel@766 59 public HtmlTree(HtmlTag tag, Content... contents) {
bpatel@766 60 this(tag);
bpatel@766 61 for (Content content: contents)
bpatel@766 62 addContent(content);
bpatel@766 63 }
bpatel@766 64
bpatel@766 65 /**
bpatel@766 66 * Adds an attribute for the HTML tag.
bpatel@766 67 *
bpatel@766 68 * @param attrName name of the attribute
bpatel@766 69 * @param attrValue value of the attribute
bpatel@766 70 */
bpatel@766 71 public void addAttr(HtmlAttr attrName, String attrValue) {
bpatel@766 72 if (attrs.isEmpty())
bpatel@766 73 attrs = new LinkedHashMap<HtmlAttr,String>();
bpatel@766 74 attrs.put(nullCheck(attrName), nullCheck(attrValue));
bpatel@766 75 }
bpatel@766 76
bpatel@766 77 /**
bpatel@766 78 * Adds a style for the HTML tag.
bpatel@766 79 *
bpatel@766 80 * @param style style to be added
bpatel@766 81 */
bpatel@766 82 public void addStyle(HtmlStyle style) {
bpatel@766 83 addAttr(HtmlAttr.CLASS, style.toString());
bpatel@766 84 }
bpatel@766 85
bpatel@766 86 /**
bpatel@766 87 * Adds content for the HTML tag.
bpatel@766 88 *
bpatel@766 89 * @param tagContent tag content to be added
bpatel@766 90 */
bpatel@766 91 public void addContent(Content tagContent) {
bpatel@766 92 if (tagContent == HtmlTree.EMPTY || tagContent.isValid()) {
bpatel@766 93 if (content.isEmpty())
bpatel@766 94 content = new ArrayList<Content>();
bpatel@766 95 content.add(tagContent);
bpatel@766 96 }
bpatel@766 97 }
bpatel@766 98
bpatel@766 99 /**
bpatel@766 100 * This method adds a string content to the htmltree. If the last content member
bpatel@766 101 * added is a StringContent, append the string to that StringContent or else
bpatel@766 102 * create a new StringContent and add it to the html tree.
bpatel@766 103 *
bpatel@766 104 * @param stringContent string content that needs to be added
bpatel@766 105 */
bpatel@766 106 public void addContent(String stringContent) {
bpatel@766 107 if (!content.isEmpty()) {
bpatel@766 108 Content lastContent = content.get(content.size() - 1);
bpatel@766 109 if (lastContent instanceof StringContent)
bpatel@766 110 lastContent.addContent(stringContent);
bpatel@766 111 else
bpatel@766 112 addContent(new StringContent(stringContent));
bpatel@766 113 }
bpatel@766 114 else
bpatel@766 115 addContent(new StringContent(stringContent));
bpatel@766 116 }
bpatel@766 117
bpatel@766 118 /**
bpatel@766 119 * Generates an HTML anchor tag.
bpatel@766 120 *
bpatel@766 121 * @param ref reference url for the anchor tag
bpatel@766 122 * @param body content for the anchor tag
bpatel@766 123 * @return an HtmlTree object
bpatel@766 124 */
bpatel@766 125 public static HtmlTree A(String ref, Content body) {
bpatel@766 126 HtmlTree htmltree = new HtmlTree(HtmlTag.A, nullCheck(body));
bpatel@766 127 htmltree.addAttr(HtmlAttr.HREF, nullCheck(ref));
bpatel@766 128 return htmltree;
bpatel@766 129 }
bpatel@766 130
bpatel@766 131 /**
bpatel@766 132 * Generates an HTML anchor tag with name attribute and content.
bpatel@766 133 *
bpatel@766 134 * @param name name for the anchor tag
bpatel@766 135 * @param body content for the anchor tag
bpatel@766 136 * @return an HtmlTree object
bpatel@766 137 */
bpatel@766 138 public static HtmlTree A_NAME(String name, Content body) {
bpatel@766 139 HtmlTree htmltree = HtmlTree.A_NAME(name);
bpatel@766 140 htmltree.addContent(nullCheck(body));
bpatel@766 141 return htmltree;
bpatel@766 142 }
bpatel@766 143
bpatel@766 144 /**
bpatel@766 145 * Generates an HTML anchor tag with name attribute.
bpatel@766 146 *
bpatel@766 147 * @param name name for the anchor tag
bpatel@766 148 * @return an HtmlTree object
bpatel@766 149 */
bpatel@766 150 public static HtmlTree A_NAME(String name) {
bpatel@766 151 HtmlTree htmltree = new HtmlTree(HtmlTag.A);
bpatel@766 152 htmltree.addAttr(HtmlAttr.NAME, nullCheck(name));
bpatel@766 153 return htmltree;
bpatel@766 154 }
bpatel@766 155
bpatel@766 156 /**
bpatel@766 157 * Generates a CAPTION tag with some content.
bpatel@766 158 *
bpatel@766 159 * @param body content for the tag
bpatel@766 160 * @return an HtmlTree object for the CAPTION tag
bpatel@766 161 */
bpatel@766 162 public static HtmlTree CAPTION(Content body) {
bpatel@766 163 HtmlTree htmltree = new HtmlTree(HtmlTag.CAPTION, nullCheck(body));
bpatel@766 164 return htmltree;
bpatel@766 165 }
bpatel@766 166
bpatel@766 167 /**
bpatel@766 168 * Generates a CODE tag with some content.
bpatel@766 169 *
bpatel@766 170 * @param body content for the tag
bpatel@766 171 * @return an HtmlTree object for the CODE tag
bpatel@766 172 */
bpatel@766 173 public static HtmlTree CODE(Content body) {
bpatel@766 174 HtmlTree htmltree = new HtmlTree(HtmlTag.CODE, nullCheck(body));
bpatel@766 175 return htmltree;
bpatel@766 176 }
bpatel@766 177
bpatel@766 178 /**
bpatel@766 179 * Generates a DD tag with some content.
bpatel@766 180 *
bpatel@766 181 * @param body content for the tag
bpatel@766 182 * @return an HtmlTree object for the DD tag
bpatel@766 183 */
bpatel@766 184 public static HtmlTree DD(Content body) {
bpatel@766 185 HtmlTree htmltree = new HtmlTree(HtmlTag.DD, nullCheck(body));
bpatel@766 186 return htmltree;
bpatel@766 187 }
bpatel@766 188
bpatel@766 189 /**
bpatel@766 190 * Generates a DL tag with some content.
bpatel@766 191 *
bpatel@766 192 * @param body content for the tag
bpatel@766 193 * @return an HtmlTree object for the DL tag
bpatel@766 194 */
bpatel@766 195 public static HtmlTree DL(Content body) {
bpatel@766 196 HtmlTree htmltree = new HtmlTree(HtmlTag.DL, nullCheck(body));
bpatel@766 197 return htmltree;
bpatel@766 198 }
bpatel@766 199
bpatel@766 200 /**
bpatel@766 201 * Generates a DIV tag with the style class attributes. It also encloses
bpatel@766 202 * a content.
bpatel@766 203 *
bpatel@766 204 * @param styleClass stylesheet class for the tag
bpatel@766 205 * @param body content for the tag
bpatel@766 206 * @return an HtmlTree object for the DIV tag
bpatel@766 207 */
bpatel@766 208 public static HtmlTree DIV(HtmlStyle styleClass, Content body) {
bpatel@766 209 HtmlTree htmltree = new HtmlTree(HtmlTag.DIV, nullCheck(body));
bpatel@766 210 if (styleClass != null)
bpatel@766 211 htmltree.addStyle(styleClass);
bpatel@766 212 return htmltree;
bpatel@766 213 }
bpatel@766 214
bpatel@766 215 /**
bpatel@766 216 * Generates a DIV tag with some content.
bpatel@766 217 *
bpatel@766 218 * @param body content for the tag
bpatel@766 219 * @return an HtmlTree object for the DIV tag
bpatel@766 220 */
bpatel@766 221 public static HtmlTree DIV(Content body) {
bpatel@766 222 return DIV(null, body);
bpatel@766 223 }
bpatel@766 224
bpatel@766 225 /**
bpatel@766 226 * Generates a DT tag with some content.
bpatel@766 227 *
bpatel@766 228 * @param body content for the tag
bpatel@766 229 * @return an HtmlTree object for the DT tag
bpatel@766 230 */
bpatel@766 231 public static HtmlTree DT(Content body) {
bpatel@766 232 HtmlTree htmltree = new HtmlTree(HtmlTag.DT, nullCheck(body));
bpatel@766 233 return htmltree;
bpatel@766 234 }
bpatel@766 235
bpatel@766 236 /**
bpatel@766 237 * Generates a EM tag with some content.
bpatel@766 238 *
bpatel@766 239 * @param body content to be added to the tag
bpatel@766 240 * @return an HtmlTree object for the EM tag
bpatel@766 241 */
bpatel@766 242 public static HtmlTree EM(Content body) {
bpatel@766 243 HtmlTree htmltree = new HtmlTree(HtmlTag.EM, nullCheck(body));
bpatel@766 244 return htmltree;
bpatel@766 245 }
bpatel@766 246
bpatel@766 247 /**
bpatel@766 248 * Generates a FRAME tag.
bpatel@766 249 *
bpatel@766 250 * @param src the url of the document to be shown in the frame
bpatel@766 251 * @param name specifies the name of the frame
bpatel@766 252 * @param title the title for the frame
bpatel@766 253 * @param scrolling specifies whether to display scrollbars in the frame
bpatel@766 254 * @return an HtmlTree object for the FRAME tag
bpatel@766 255 */
bpatel@766 256 public static HtmlTree FRAME(String src, String name, String title, String scrolling) {
bpatel@766 257 HtmlTree htmltree = new HtmlTree(HtmlTag.FRAME);
bpatel@766 258 htmltree.addAttr(HtmlAttr.SRC, nullCheck(src));
bpatel@766 259 htmltree.addAttr(HtmlAttr.NAME, nullCheck(name));
bpatel@766 260 htmltree.addAttr(HtmlAttr.TITLE, nullCheck(title));
bpatel@766 261 if (scrolling != null)
bpatel@766 262 htmltree.addAttr(HtmlAttr.SCROLLING, scrolling);
bpatel@766 263 return htmltree;
bpatel@766 264 }
bpatel@766 265
bpatel@766 266 /**
bpatel@766 267 * Generates a Frame tag.
bpatel@766 268 *
bpatel@766 269 * @param src the url of the document to be shown in the frame
bpatel@766 270 * @param name specifies the name of the frame
bpatel@766 271 * @param title the title for the frame
bpatel@766 272 * @return an HtmlTree object for the SPAN tag
bpatel@766 273 */
bpatel@766 274 public static HtmlTree FRAME(String src, String name, String title) {
bpatel@766 275 return FRAME(src, name, title, null);
bpatel@766 276 }
bpatel@766 277
bpatel@766 278 /**
bpatel@766 279 * Generates a FRAMESET tag.
bpatel@766 280 *
bpatel@766 281 * @param cols the size of columns in the frameset
bpatel@766 282 * @param rows the size of rows in the frameset
bpatel@766 283 * @param title the title for the frameset
bpatel@766 284 * @param onload the script to run when the document loads
bpatel@766 285 * @return an HtmlTree object for the FRAMESET tag
bpatel@766 286 */
bpatel@766 287 public static HtmlTree FRAMESET(String cols, String rows, String title, String onload) {
bpatel@766 288 HtmlTree htmltree = new HtmlTree(HtmlTag.FRAMESET);
bpatel@766 289 if (cols != null)
bpatel@766 290 htmltree.addAttr(HtmlAttr.COLS, cols);
bpatel@766 291 if (rows != null)
bpatel@766 292 htmltree.addAttr(HtmlAttr.ROWS, rows);
bpatel@766 293 htmltree.addAttr(HtmlAttr.TITLE, nullCheck(title));
bpatel@766 294 htmltree.addAttr(HtmlAttr.ONLOAD, nullCheck(onload));
bpatel@766 295 return htmltree;
bpatel@766 296 }
bpatel@766 297
bpatel@766 298 /**
bpatel@766 299 * Generates a heading tag (h1 to h6) with the title and style class attributes. It also encloses
bpatel@766 300 * a content.
bpatel@766 301 *
bpatel@766 302 * @param headingTag the heading tag to be generated
bpatel@766 303 * @param printTitle true if title for the tag needs to be printed else false
bpatel@766 304 * @param styleClass stylesheet class for the tag
bpatel@766 305 * @param body content for the tag
bpatel@766 306 * @return an HtmlTree object for the tag
bpatel@766 307 */
bpatel@766 308 public static HtmlTree HEADING(HtmlTag headingTag, boolean printTitle,
bpatel@766 309 HtmlStyle styleClass, Content body) {
bpatel@766 310 HtmlTree htmltree = new HtmlTree(headingTag, nullCheck(body));
bpatel@766 311 if (printTitle)
bpatel@766 312 htmltree.addAttr(HtmlAttr.TITLE, Util.stripHtml(body.toString()));
bpatel@766 313 if (styleClass != null)
bpatel@766 314 htmltree.addStyle(styleClass);
bpatel@766 315 return htmltree;
bpatel@766 316 }
bpatel@766 317
bpatel@766 318 /**
bpatel@766 319 * Generates a heading tag (h1 to h6) with style class attribute. It also encloses
bpatel@766 320 * a content.
bpatel@766 321 *
bpatel@766 322 * @param headingTag the heading tag to be generated
bpatel@766 323 * @param styleClass stylesheet class for the tag
bpatel@766 324 * @param body content for the tag
bpatel@766 325 * @return an HtmlTree object for the tag
bpatel@766 326 */
bpatel@766 327 public static HtmlTree HEADING(HtmlTag headingTag, HtmlStyle styleClass, Content body) {
bpatel@766 328 return HEADING(headingTag, false, styleClass, body);
bpatel@766 329 }
bpatel@766 330
bpatel@766 331 /**
bpatel@766 332 * Generates a heading tag (h1 to h6) with the title attribute. It also encloses
bpatel@766 333 * a content.
bpatel@766 334 *
bpatel@766 335 * @param headingTag the heading tag to be generated
bpatel@766 336 * @param printTitle true if the title for the tag needs to be printed else false
bpatel@766 337 * @param body content for the tag
bpatel@766 338 * @return an HtmlTree object for the tag
bpatel@766 339 */
bpatel@766 340 public static HtmlTree HEADING(HtmlTag headingTag, boolean printTitle, Content body) {
bpatel@766 341 return HEADING(headingTag, printTitle, null, body);
bpatel@766 342 }
bpatel@766 343
bpatel@766 344 /**
bpatel@766 345 * Generates a heading tag (h1 to h6) with some content.
bpatel@766 346 *
bpatel@766 347 * @param headingTag the heading tag to be generated
bpatel@766 348 * @param body content for the tag
bpatel@766 349 * @return an HtmlTree object for the tag
bpatel@766 350 */
bpatel@766 351 public static HtmlTree HEADING(HtmlTag headingTag, Content body) {
bpatel@766 352 return HEADING(headingTag, false, null, body);
bpatel@766 353 }
bpatel@766 354
bpatel@766 355 /**
bpatel@766 356 * Generates an HTML tag with lang attribute. It also adds head and body
bpatel@766 357 * content to the HTML tree.
bpatel@766 358 *
bpatel@766 359 * @param lang language for the HTML document
bpatel@766 360 * @param head head for the HTML tag
bpatel@766 361 * @param body body for the HTML tag
bpatel@766 362 * @return an HtmlTree object for the HTML tag
bpatel@766 363 */
bpatel@766 364 public static HtmlTree HTML(String lang, Content head, Content body) {
bpatel@766 365 HtmlTree htmltree = new HtmlTree(HtmlTag.HTML, nullCheck(head), nullCheck(body));
bpatel@766 366 htmltree.addAttr(HtmlAttr.LANG, nullCheck(lang));
bpatel@766 367 return htmltree;
bpatel@766 368 }
bpatel@766 369
bpatel@766 370 /**
bpatel@766 371 * Generates a I tag with some content.
bpatel@766 372 *
bpatel@766 373 * @param body content for the tag
bpatel@766 374 * @return an HtmlTree object for the I tag
bpatel@766 375 */
bpatel@766 376 public static HtmlTree I(Content body) {
bpatel@766 377 HtmlTree htmltree = new HtmlTree(HtmlTag.I, nullCheck(body));
bpatel@766 378 return htmltree;
bpatel@766 379 }
bpatel@766 380
bpatel@766 381 /**
bpatel@766 382 * Generates a LI tag with some content.
bpatel@766 383 *
bpatel@766 384 * @param body content for the tag
bpatel@766 385 * @return an HtmlTree object for the LI tag
bpatel@766 386 */
bpatel@766 387 public static HtmlTree LI(Content body) {
bpatel@766 388 return LI(null, body);
bpatel@766 389 }
bpatel@766 390
bpatel@766 391 /**
bpatel@766 392 * Generates a LI tag with some content.
bpatel@766 393 *
bpatel@766 394 * @param styleClass style for the tag
bpatel@766 395 * @param body content for the tag
bpatel@766 396 * @return an HtmlTree object for the LI tag
bpatel@766 397 */
bpatel@766 398 public static HtmlTree LI(HtmlStyle styleClass, Content body) {
bpatel@766 399 HtmlTree htmltree = new HtmlTree(HtmlTag.LI, nullCheck(body));
bpatel@766 400 if (styleClass != null)
bpatel@766 401 htmltree.addStyle(styleClass);
bpatel@766 402 return htmltree;
bpatel@766 403 }
bpatel@766 404
bpatel@766 405 /**
bpatel@766 406 * Generates a LINK tag with the rel, type, href and title attributes.
bpatel@766 407 *
bpatel@766 408 * @param rel relevance of the link
bpatel@766 409 * @param type type of link
bpatel@766 410 * @param href the path for the link
bpatel@766 411 * @param title title for the link
bpatel@766 412 * @return an HtmlTree object for the LINK tag
bpatel@766 413 */
bpatel@766 414 public static HtmlTree LINK(String rel, String type, String href, String title) {
bpatel@766 415 HtmlTree htmltree = new HtmlTree(HtmlTag.LINK);
bpatel@766 416 htmltree.addAttr(HtmlAttr.REL, nullCheck(rel));
bpatel@766 417 htmltree.addAttr(HtmlAttr.TYPE, nullCheck(type));
bpatel@766 418 htmltree.addAttr(HtmlAttr.HREF, nullCheck(href));
bpatel@766 419 htmltree.addAttr(HtmlAttr.TITLE, nullCheck(title));
bpatel@766 420 return htmltree;
bpatel@766 421 }
bpatel@766 422
bpatel@766 423 /**
bpatel@766 424 * Generates a META tag with the http-equiv, content and charset attributes.
bpatel@766 425 *
bpatel@766 426 * @param http-equiv http equiv attribute for the META tag
bpatel@766 427 * @param content type of content
bpatel@766 428 * @param charset character set used
bpatel@766 429 * @return an HtmlTree object for the META tag
bpatel@766 430 */
bpatel@766 431 public static HtmlTree META(String httpEquiv, String content, String charSet) {
bpatel@766 432 HtmlTree htmltree = new HtmlTree(HtmlTag.META);
bpatel@766 433 htmltree.addAttr(HtmlAttr.HTTP_EQUIV, nullCheck(httpEquiv));
bpatel@766 434 htmltree.addAttr(HtmlAttr.CONTENT, nullCheck(content));
bpatel@766 435 htmltree.addAttr(HtmlAttr.CHARSET, nullCheck(charSet));
bpatel@766 436 return htmltree;
bpatel@766 437 }
bpatel@766 438
bpatel@766 439 /**
bpatel@766 440 * Generates a META tag with the name and content attributes.
bpatel@766 441 *
bpatel@766 442 * @param name name attribute
bpatel@766 443 * @param content type of content
bpatel@766 444 * @return an HtmlTree object for the META tag
bpatel@766 445 */
bpatel@766 446 public static HtmlTree META(String name, String content) {
bpatel@766 447 HtmlTree htmltree = new HtmlTree(HtmlTag.META);
bpatel@766 448 htmltree.addAttr(HtmlAttr.NAME, nullCheck(name));
bpatel@766 449 htmltree.addAttr(HtmlAttr.CONTENT, nullCheck(content));
bpatel@766 450 return htmltree;
bpatel@766 451 }
bpatel@766 452
bpatel@766 453 /**
bpatel@766 454 * Generates a NOSCRIPT tag with some content.
bpatel@766 455 *
bpatel@766 456 * @param body content of the noscript tag
bpatel@766 457 * @return an HtmlTree object for the NOSCRIPT tag
bpatel@766 458 */
bpatel@766 459 public static HtmlTree NOSCRIPT(Content body) {
bpatel@766 460 HtmlTree htmltree = new HtmlTree(HtmlTag.NOSCRIPT, nullCheck(body));
bpatel@766 461 return htmltree;
bpatel@766 462 }
bpatel@766 463
bpatel@766 464 /**
bpatel@766 465 * Generates a P tag with some content.
bpatel@766 466 *
bpatel@766 467 * @param body content of the Paragraph tag
bpatel@766 468 * @return an HtmlTree object for the P tag
bpatel@766 469 */
bpatel@766 470 public static HtmlTree P(Content body) {
bpatel@766 471 return P(null, body);
bpatel@766 472 }
bpatel@766 473
bpatel@766 474 /**
bpatel@766 475 * Generates a P tag with some content.
bpatel@766 476 *
bpatel@766 477 * @param styleClass style of the Paragraph tag
bpatel@766 478 * @param body content of the Paragraph tag
bpatel@766 479 * @return an HtmlTree object for the P tag
bpatel@766 480 */
bpatel@766 481 public static HtmlTree P(HtmlStyle styleClass, Content body) {
bpatel@766 482 HtmlTree htmltree = new HtmlTree(HtmlTag.P, nullCheck(body));
bpatel@766 483 if (styleClass != null)
bpatel@766 484 htmltree.addStyle(styleClass);
bpatel@766 485 return htmltree;
bpatel@766 486 }
bpatel@766 487
bpatel@766 488 /**
bpatel@766 489 * Generates a SMALL tag with some content.
bpatel@766 490 *
bpatel@766 491 * @param body content for the tag
bpatel@766 492 * @return an HtmlTree object for the SMALL tag
bpatel@766 493 */
bpatel@766 494 public static HtmlTree SMALL(Content body) {
bpatel@766 495 HtmlTree htmltree = new HtmlTree(HtmlTag.SMALL, nullCheck(body));
bpatel@766 496 return htmltree;
bpatel@766 497 }
bpatel@766 498
bpatel@766 499 /**
bpatel@766 500 * Generates a STRONG tag with some content.
bpatel@766 501 *
bpatel@766 502 * @param body content for the tag
bpatel@766 503 * @return an HtmlTree object for the STRONG tag
bpatel@766 504 */
bpatel@766 505 public static HtmlTree STRONG(Content body) {
bpatel@766 506 HtmlTree htmltree = new HtmlTree(HtmlTag.STRONG, nullCheck(body));
bpatel@766 507 return htmltree;
bpatel@766 508 }
bpatel@766 509
bpatel@766 510 /**
bpatel@766 511 * Generates a SPAN tag with some content.
bpatel@766 512 *
bpatel@766 513 * @param body content for the tag
bpatel@766 514 * @return an HtmlTree object for the SPAN tag
bpatel@766 515 */
bpatel@766 516 public static HtmlTree SPAN(Content body) {
bpatel@766 517 return SPAN(null, body);
bpatel@766 518 }
bpatel@766 519
bpatel@766 520 /**
bpatel@766 521 * Generates a SPAN tag with style class attribute and some content.
bpatel@766 522 *
bpatel@766 523 * @param styleClass style class for the tag
bpatel@766 524 * @param body content for the tag
bpatel@766 525 * @return an HtmlTree object for the SPAN tag
bpatel@766 526 */
bpatel@766 527 public static HtmlTree SPAN(HtmlStyle styleClass, Content body) {
bpatel@766 528 HtmlTree htmltree = new HtmlTree(HtmlTag.SPAN, nullCheck(body));
bpatel@766 529 if (styleClass != null)
bpatel@766 530 htmltree.addStyle(styleClass);
bpatel@766 531 return htmltree;
bpatel@766 532 }
bpatel@766 533
bpatel@766 534 /**
bpatel@766 535 * Generates a Table tag with border, width and summary attributes and
bpatel@766 536 * some content.
bpatel@766 537 *
bpatel@766 538 * @param border border for the table
bpatel@766 539 * @param width width of the table
bpatel@766 540 * @param summary summary for the table
bpatel@766 541 * @param body content for the table
bpatel@766 542 * @return an HtmlTree object for the TABLE tag
bpatel@766 543 */
bpatel@766 544 public static HtmlTree TABLE(int border, int width, String summary,
bpatel@766 545 Content body) {
bpatel@766 546 HtmlTree htmltree = new HtmlTree(HtmlTag.TABLE, nullCheck(body));
bpatel@766 547 htmltree.addAttr(HtmlAttr.BORDER, Integer.toString(border));
bpatel@766 548 htmltree.addAttr(HtmlAttr.WIDTH, Integer.toString(width));
bpatel@766 549 htmltree.addAttr(HtmlAttr.SUMMARY, nullCheck(summary));
bpatel@766 550 return htmltree;
bpatel@766 551 }
bpatel@766 552
bpatel@766 553 /**
bpatel@766 554 * Generates a Table tag with style class, border, cell padding,
bpatel@766 555 * cellspacing and summary attributes and some content.
bpatel@766 556 *
bpatel@766 557 * @param styleClass style of the table
bpatel@766 558 * @param border border for the table
bpatel@766 559 * @param cellPadding cell padding for the table
bpatel@766 560 * @param cellSpacing cell spacing for the table
bpatel@766 561 * @param summary summary for the table
bpatel@766 562 * @param body content for the table
bpatel@766 563 * @return an HtmlTree object for the TABLE tag
bpatel@766 564 */
bpatel@766 565 public static HtmlTree TABLE(HtmlStyle styleClass, int border, int cellPadding,
bpatel@766 566 int cellSpacing, String summary, Content body) {
bpatel@766 567 HtmlTree htmltree = new HtmlTree(HtmlTag.TABLE, nullCheck(body));
bpatel@766 568 if (styleClass != null)
bpatel@766 569 htmltree.addStyle(styleClass);
bpatel@766 570 htmltree.addAttr(HtmlAttr.BORDER, Integer.toString(border));
bpatel@766 571 htmltree.addAttr(HtmlAttr.CELLPADDING, Integer.toString(cellPadding));
bpatel@766 572 htmltree.addAttr(HtmlAttr.CELLSPACING, Integer.toString(cellSpacing));
bpatel@766 573 htmltree.addAttr(HtmlAttr.SUMMARY, nullCheck(summary));
bpatel@766 574 return htmltree;
bpatel@766 575 }
bpatel@766 576
bpatel@766 577 /**
bpatel@766 578 * Generates a Table tag with border, cell padding,
bpatel@766 579 * cellspacing and summary attributes and some content.
bpatel@766 580 *
bpatel@766 581 * @param border border for the table
bpatel@766 582 * @param cellPadding cell padding for the table
bpatel@766 583 * @param cellSpacing cell spacing for the table
bpatel@766 584 * @param summary summary for the table
bpatel@766 585 * @param body content for the table
bpatel@766 586 * @return an HtmlTree object for the TABLE tag
bpatel@766 587 */
bpatel@766 588 public static HtmlTree TABLE(int border, int cellPadding,
bpatel@766 589 int cellSpacing, String summary, Content body) {
bpatel@766 590 return TABLE(null, border, cellPadding, cellSpacing, summary, body);
bpatel@766 591 }
bpatel@766 592
bpatel@766 593 /**
bpatel@766 594 * Generates a TD tag with style class attribute and some content.
bpatel@766 595 *
bpatel@766 596 * @param styleClass style for the tag
bpatel@766 597 * @param body content for the tag
bpatel@766 598 * @return an HtmlTree object for the TD tag
bpatel@766 599 */
bpatel@766 600 public static HtmlTree TD(HtmlStyle styleClass, Content body) {
bpatel@766 601 HtmlTree htmltree = new HtmlTree(HtmlTag.TD, nullCheck(body));
bpatel@766 602 if (styleClass != null)
bpatel@766 603 htmltree.addStyle(styleClass);
bpatel@766 604 return htmltree;
bpatel@766 605 }
bpatel@766 606
bpatel@766 607 /**
bpatel@766 608 * Generates a TD tag for an HTML table with some content.
bpatel@766 609 *
bpatel@766 610 * @param body content for the tag
bpatel@766 611 * @return an HtmlTree object for the TD tag
bpatel@766 612 */
bpatel@766 613 public static HtmlTree TD(Content body) {
bpatel@766 614 return TD(null, body);
bpatel@766 615 }
bpatel@766 616
bpatel@766 617 /**
bpatel@766 618 * Generates a TH tag with style class and scope attributes and some content.
bpatel@766 619 *
bpatel@766 620 * @param styleClass style for the tag
bpatel@766 621 * @param scope scope of the tag
bpatel@766 622 * @param body content for the tag
bpatel@766 623 * @return an HtmlTree object for the TH tag
bpatel@766 624 */
bpatel@766 625 public static HtmlTree TH(HtmlStyle styleClass, String scope, Content body) {
bpatel@766 626 HtmlTree htmltree = new HtmlTree(HtmlTag.TH, nullCheck(body));
bpatel@766 627 if (styleClass != null)
bpatel@766 628 htmltree.addStyle(styleClass);
bpatel@766 629 htmltree.addAttr(HtmlAttr.SCOPE, nullCheck(scope));
bpatel@766 630 return htmltree;
bpatel@766 631 }
bpatel@766 632
bpatel@766 633 /**
bpatel@766 634 * Generates a TH tag with scope attribute and some content.
bpatel@766 635 *
bpatel@766 636 * @param scope scope of the tag
bpatel@766 637 * @param body content for the tag
bpatel@766 638 * @return an HtmlTree object for the TH tag
bpatel@766 639 */
bpatel@766 640 public static HtmlTree TH(String scope, Content body) {
bpatel@766 641 return TH(null, scope, body);
bpatel@766 642 }
bpatel@766 643
bpatel@766 644 /**
bpatel@766 645 * Generates a TITLE tag with some content.
bpatel@766 646 *
bpatel@766 647 * @param body content for the tag
bpatel@766 648 * @return an HtmlTree object for the TITLE tag
bpatel@766 649 */
bpatel@766 650 public static HtmlTree TITLE(Content body) {
bpatel@766 651 HtmlTree htmltree = new HtmlTree(HtmlTag.TITLE, nullCheck(body));
bpatel@766 652 return htmltree;
bpatel@766 653 }
bpatel@766 654
bpatel@766 655 /**
bpatel@766 656 * Generates a TR tag for an HTML table with some content.
bpatel@766 657 *
bpatel@766 658 * @param body content for the tag
bpatel@766 659 * @return an HtmlTree object for the TR tag
bpatel@766 660 */
bpatel@766 661 public static HtmlTree TR(Content body) {
bpatel@766 662 HtmlTree htmltree = new HtmlTree(HtmlTag.TR, nullCheck(body));
bpatel@766 663 return htmltree;
bpatel@766 664 }
bpatel@766 665
bpatel@766 666 /**
bpatel@766 667 * Generates a UL tag with the style class attribute and some content.
bpatel@766 668 *
bpatel@766 669 * @param styleClass style for the tag
bpatel@766 670 * @param body content for the tag
bpatel@766 671 * @return an HtmlTree object for the UL tag
bpatel@766 672 */
bpatel@766 673 public static HtmlTree UL(HtmlStyle styleClass, Content body) {
bpatel@766 674 HtmlTree htmltree = new HtmlTree(HtmlTag.UL, nullCheck(body));
bpatel@766 675 htmltree.addStyle(nullCheck(styleClass));
bpatel@766 676 return htmltree;
bpatel@766 677 }
bpatel@766 678
bpatel@766 679 /**
bpatel@766 680 * {@inheritDoc}
bpatel@766 681 */
bpatel@766 682 public boolean isEmpty() {
bpatel@766 683 return (!hasContent() && !hasAttrs());
bpatel@766 684 }
bpatel@766 685
bpatel@766 686 /**
bpatel@766 687 * Returns true if the HTML tree has content.
bpatel@766 688 *
bpatel@766 689 * @return true if the HTML tree has content else return false
bpatel@766 690 */
bpatel@766 691 public boolean hasContent() {
bpatel@766 692 return (!content.isEmpty());
bpatel@766 693 }
bpatel@766 694
bpatel@766 695 /**
bpatel@766 696 * Returns true if the HTML tree has attributes.
bpatel@766 697 *
bpatel@766 698 * @return true if the HTML tree has attributes else return false
bpatel@766 699 */
bpatel@766 700 public boolean hasAttrs() {
bpatel@766 701 return (!attrs.isEmpty());
bpatel@766 702 }
bpatel@766 703
bpatel@766 704 /**
bpatel@766 705 * Returns true if the HTML tree has a specific attribute.
bpatel@766 706 *
bpatel@766 707 * @param attrName name of the attribute to check within the HTML tree
bpatel@766 708 * @return true if the HTML tree has the specified attribute else return false
bpatel@766 709 */
bpatel@766 710 public boolean hasAttr(HtmlAttr attrName) {
bpatel@766 711 return (attrs.containsKey(attrName));
bpatel@766 712 }
bpatel@766 713
bpatel@766 714 /**
bpatel@766 715 * Returns true if the HTML tree is valid. This check is more specific to
bpatel@766 716 * standard doclet and not exactly similar to W3C specifications. But it
bpatel@766 717 * ensures HTML validation.
bpatel@766 718 *
bpatel@766 719 * @return true if the HTML tree is valid
bpatel@766 720 */
bpatel@766 721 public boolean isValid() {
bpatel@766 722 switch (htmlTag) {
bpatel@766 723 case A :
bpatel@766 724 return (hasAttr(HtmlAttr.NAME) || (hasAttr(HtmlAttr.HREF) && hasContent()));
bpatel@766 725 case BR :
bpatel@766 726 return (!hasContent() && (!hasAttrs() || hasAttr(HtmlAttr.CLEAR)));
bpatel@766 727 case FRAME :
bpatel@766 728 return (hasAttr(HtmlAttr.SRC) && !hasContent());
bpatel@766 729 case HR :
bpatel@766 730 return (!hasContent());
bpatel@766 731 case IMG :
bpatel@766 732 return (hasAttr(HtmlAttr.SRC) && hasAttr(HtmlAttr.ALT) && !hasContent());
bpatel@766 733 case LINK :
bpatel@766 734 return (hasAttr(HtmlAttr.HREF) && !hasContent());
bpatel@766 735 case META :
bpatel@766 736 return (hasAttr(HtmlAttr.CONTENT) && !hasContent());
bpatel@766 737 default :
bpatel@766 738 return hasContent();
bpatel@766 739 }
bpatel@766 740 }
bpatel@766 741
bpatel@766 742 /**
bpatel@766 743 * Returns true if the element is an inline element.
bpatel@766 744 *
bpatel@766 745 * @return true if the HTML tag is an inline element
bpatel@766 746 */
bpatel@766 747 public boolean isInline() {
bpatel@766 748 return (htmlTag.blockType == HtmlTag.BlockType.INLINE);
bpatel@766 749 }
bpatel@766 750
bpatel@766 751 /**
bpatel@766 752 * {@inheritDoc}
bpatel@766 753 */
bpatel@766 754 public void write(StringBuilder contentBuilder) {
bpatel@766 755 if (!isInline() && !endsWithNewLine(contentBuilder))
bpatel@793 756 contentBuilder.append(DocletConstants.NL);
bpatel@766 757 String tagString = htmlTag.toString();
bpatel@819 758 contentBuilder.append("<");
bpatel@819 759 contentBuilder.append(tagString);
bpatel@766 760 Iterator<HtmlAttr> iterator = attrs.keySet().iterator();
bpatel@766 761 HtmlAttr key;
bpatel@766 762 String value = "";
bpatel@766 763 while (iterator.hasNext()) {
bpatel@766 764 key = iterator.next();
bpatel@766 765 value = attrs.get(key);
bpatel@819 766 contentBuilder.append(" ");
bpatel@819 767 contentBuilder.append(key.toString());
bpatel@819 768 if (!value.isEmpty()) {
bpatel@819 769 contentBuilder.append("=\"");
bpatel@819 770 contentBuilder.append(value);
bpatel@819 771 contentBuilder.append("\"");
bpatel@819 772 }
bpatel@766 773 }
bpatel@766 774 contentBuilder.append(">");
bpatel@766 775 for (Content c : content)
bpatel@766 776 c.write(contentBuilder);
bpatel@819 777 if (htmlTag.endTagRequired()) {
bpatel@819 778 contentBuilder.append("</");
bpatel@819 779 contentBuilder.append(tagString);
bpatel@819 780 contentBuilder.append(">");
bpatel@819 781 }
bpatel@766 782 if (!isInline())
bpatel@793 783 contentBuilder.append(DocletConstants.NL);
bpatel@766 784 }
bpatel@766 785 }

mercurial