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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial