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

Tue, 14 May 2013 10:14:55 -0700

author
jjg
date
Tue, 14 May 2013 10:14:55 -0700
changeset 1746
bd51ca92c013
parent 1737
7a9ef837e57f
child 1747
df4f44800923
permissions
-rw-r--r--

8012178: Cleanup use of Util.escapeHtmlChars
Reviewed-by: darcy

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

mercurial