bpatel@766: /* bpatel@766: * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. bpatel@766: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. bpatel@766: * bpatel@766: * This code is free software; you can redistribute it and/or modify it bpatel@766: * under the terms of the GNU General Public License version 2 only, as bpatel@766: * published by the Free Software Foundation. Oracle designates this bpatel@766: * particular file as subject to the "Classpath" exception as provided bpatel@766: * by Oracle in the LICENSE file that accompanied this code. bpatel@766: * bpatel@766: * This code is distributed in the hope that it will be useful, but WITHOUT bpatel@766: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or bpatel@766: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License bpatel@766: * version 2 for more details (a copy is included in the LICENSE file that bpatel@766: * accompanied this code). bpatel@766: * bpatel@766: * You should have received a copy of the GNU General Public License version bpatel@766: * 2 along with this work; if not, write to the Free Software Foundation, bpatel@766: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. bpatel@766: * bpatel@766: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA bpatel@766: * or visit www.oracle.com if you need additional information or have any bpatel@766: * questions. bpatel@766: */ bpatel@766: bpatel@766: package com.sun.tools.doclets.formats.html.markup; bpatel@766: bpatel@766: import java.util.*; bpatel@766: import com.sun.tools.doclets.internal.toolkit.Content; bpatel@766: import com.sun.tools.doclets.internal.toolkit.util.*; bpatel@766: bpatel@766: /** bpatel@766: * Class for generating HTML tree for javadoc output. bpatel@766: * bpatel@766: * @author Bhavesh Patel bpatel@766: */ bpatel@766: public class HtmlTree extends Content { bpatel@766: bpatel@766: private HtmlTag htmlTag; bpatel@766: private Map attrs = Collections.emptyMap(); bpatel@766: private List content = Collections.emptyList(); bpatel@766: public static final Content EMPTY = new StringContent(""); bpatel@766: bpatel@766: /** bpatel@766: * Constructor to construct HtmlTree object. bpatel@766: * bpatel@766: * @param tag HTML tag for the HtmlTree object bpatel@766: */ bpatel@766: public HtmlTree(HtmlTag tag) { bpatel@766: htmlTag = nullCheck(tag); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Constructor to construct HtmlTree object. bpatel@766: * bpatel@766: * @param tag HTML tag for the HtmlTree object bpatel@766: * @param contents contents to be added to the tree bpatel@766: */ bpatel@766: public HtmlTree(HtmlTag tag, Content... contents) { bpatel@766: this(tag); bpatel@766: for (Content content: contents) bpatel@766: addContent(content); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Adds an attribute for the HTML tag. bpatel@766: * bpatel@766: * @param attrName name of the attribute bpatel@766: * @param attrValue value of the attribute bpatel@766: */ bpatel@766: public void addAttr(HtmlAttr attrName, String attrValue) { bpatel@766: if (attrs.isEmpty()) bpatel@766: attrs = new LinkedHashMap(); bpatel@766: attrs.put(nullCheck(attrName), nullCheck(attrValue)); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Adds a style for the HTML tag. bpatel@766: * bpatel@766: * @param style style to be added bpatel@766: */ bpatel@766: public void addStyle(HtmlStyle style) { bpatel@766: addAttr(HtmlAttr.CLASS, style.toString()); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Adds content for the HTML tag. bpatel@766: * bpatel@766: * @param tagContent tag content to be added bpatel@766: */ bpatel@766: public void addContent(Content tagContent) { bpatel@766: if (tagContent == HtmlTree.EMPTY || tagContent.isValid()) { bpatel@766: if (content.isEmpty()) bpatel@766: content = new ArrayList(); bpatel@766: content.add(tagContent); bpatel@766: } bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * This method adds a string content to the htmltree. If the last content member bpatel@766: * added is a StringContent, append the string to that StringContent or else bpatel@766: * create a new StringContent and add it to the html tree. bpatel@766: * bpatel@766: * @param stringContent string content that needs to be added bpatel@766: */ bpatel@766: public void addContent(String stringContent) { bpatel@766: if (!content.isEmpty()) { bpatel@766: Content lastContent = content.get(content.size() - 1); bpatel@766: if (lastContent instanceof StringContent) bpatel@766: lastContent.addContent(stringContent); bpatel@766: else bpatel@766: addContent(new StringContent(stringContent)); bpatel@766: } bpatel@766: else bpatel@766: addContent(new StringContent(stringContent)); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates an HTML anchor tag. bpatel@766: * bpatel@766: * @param ref reference url for the anchor tag bpatel@766: * @param body content for the anchor tag bpatel@766: * @return an HtmlTree object bpatel@766: */ bpatel@766: public static HtmlTree A(String ref, Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.A, nullCheck(body)); bpatel@766: htmltree.addAttr(HtmlAttr.HREF, nullCheck(ref)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates an HTML anchor tag with name attribute and content. bpatel@766: * bpatel@766: * @param name name for the anchor tag bpatel@766: * @param body content for the anchor tag bpatel@766: * @return an HtmlTree object bpatel@766: */ bpatel@766: public static HtmlTree A_NAME(String name, Content body) { bpatel@766: HtmlTree htmltree = HtmlTree.A_NAME(name); bpatel@766: htmltree.addContent(nullCheck(body)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates an HTML anchor tag with name attribute. bpatel@766: * bpatel@766: * @param name name for the anchor tag bpatel@766: * @return an HtmlTree object bpatel@766: */ bpatel@766: public static HtmlTree A_NAME(String name) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.A); bpatel@766: htmltree.addAttr(HtmlAttr.NAME, nullCheck(name)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a CAPTION tag with some content. bpatel@766: * bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the CAPTION tag bpatel@766: */ bpatel@766: public static HtmlTree CAPTION(Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.CAPTION, nullCheck(body)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a CODE tag with some content. bpatel@766: * bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the CODE tag bpatel@766: */ bpatel@766: public static HtmlTree CODE(Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.CODE, nullCheck(body)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a DD tag with some content. bpatel@766: * bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the DD tag bpatel@766: */ bpatel@766: public static HtmlTree DD(Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.DD, nullCheck(body)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a DL tag with some content. bpatel@766: * bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the DL tag bpatel@766: */ bpatel@766: public static HtmlTree DL(Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.DL, nullCheck(body)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a DIV tag with the style class attributes. It also encloses bpatel@766: * a content. bpatel@766: * bpatel@766: * @param styleClass stylesheet class for the tag bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the DIV tag bpatel@766: */ bpatel@766: public static HtmlTree DIV(HtmlStyle styleClass, Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.DIV, nullCheck(body)); bpatel@766: if (styleClass != null) bpatel@766: htmltree.addStyle(styleClass); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a DIV tag with some content. bpatel@766: * bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the DIV tag bpatel@766: */ bpatel@766: public static HtmlTree DIV(Content body) { bpatel@766: return DIV(null, body); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a DT tag with some content. bpatel@766: * bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the DT tag bpatel@766: */ bpatel@766: public static HtmlTree DT(Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.DT, nullCheck(body)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a EM tag with some content. bpatel@766: * bpatel@766: * @param body content to be added to the tag bpatel@766: * @return an HtmlTree object for the EM tag bpatel@766: */ bpatel@766: public static HtmlTree EM(Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.EM, nullCheck(body)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a FRAME tag. bpatel@766: * bpatel@766: * @param src the url of the document to be shown in the frame bpatel@766: * @param name specifies the name of the frame bpatel@766: * @param title the title for the frame bpatel@766: * @param scrolling specifies whether to display scrollbars in the frame bpatel@766: * @return an HtmlTree object for the FRAME tag bpatel@766: */ bpatel@766: public static HtmlTree FRAME(String src, String name, String title, String scrolling) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.FRAME); bpatel@766: htmltree.addAttr(HtmlAttr.SRC, nullCheck(src)); bpatel@766: htmltree.addAttr(HtmlAttr.NAME, nullCheck(name)); bpatel@766: htmltree.addAttr(HtmlAttr.TITLE, nullCheck(title)); bpatel@766: if (scrolling != null) bpatel@766: htmltree.addAttr(HtmlAttr.SCROLLING, scrolling); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a Frame tag. bpatel@766: * bpatel@766: * @param src the url of the document to be shown in the frame bpatel@766: * @param name specifies the name of the frame bpatel@766: * @param title the title for the frame bpatel@766: * @return an HtmlTree object for the SPAN tag bpatel@766: */ bpatel@766: public static HtmlTree FRAME(String src, String name, String title) { bpatel@766: return FRAME(src, name, title, null); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a FRAMESET tag. bpatel@766: * bpatel@766: * @param cols the size of columns in the frameset bpatel@766: * @param rows the size of rows in the frameset bpatel@766: * @param title the title for the frameset bpatel@766: * @param onload the script to run when the document loads bpatel@766: * @return an HtmlTree object for the FRAMESET tag bpatel@766: */ bpatel@766: public static HtmlTree FRAMESET(String cols, String rows, String title, String onload) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.FRAMESET); bpatel@766: if (cols != null) bpatel@766: htmltree.addAttr(HtmlAttr.COLS, cols); bpatel@766: if (rows != null) bpatel@766: htmltree.addAttr(HtmlAttr.ROWS, rows); bpatel@766: htmltree.addAttr(HtmlAttr.TITLE, nullCheck(title)); bpatel@766: htmltree.addAttr(HtmlAttr.ONLOAD, nullCheck(onload)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a heading tag (h1 to h6) with the title and style class attributes. It also encloses bpatel@766: * a content. bpatel@766: * bpatel@766: * @param headingTag the heading tag to be generated bpatel@766: * @param printTitle true if title for the tag needs to be printed else false bpatel@766: * @param styleClass stylesheet class for the tag bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the tag bpatel@766: */ bpatel@766: public static HtmlTree HEADING(HtmlTag headingTag, boolean printTitle, bpatel@766: HtmlStyle styleClass, Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(headingTag, nullCheck(body)); bpatel@766: if (printTitle) bpatel@766: htmltree.addAttr(HtmlAttr.TITLE, Util.stripHtml(body.toString())); bpatel@766: if (styleClass != null) bpatel@766: htmltree.addStyle(styleClass); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a heading tag (h1 to h6) with style class attribute. It also encloses bpatel@766: * a content. bpatel@766: * bpatel@766: * @param headingTag the heading tag to be generated bpatel@766: * @param styleClass stylesheet class for the tag bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the tag bpatel@766: */ bpatel@766: public static HtmlTree HEADING(HtmlTag headingTag, HtmlStyle styleClass, Content body) { bpatel@766: return HEADING(headingTag, false, styleClass, body); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a heading tag (h1 to h6) with the title attribute. It also encloses bpatel@766: * a content. bpatel@766: * bpatel@766: * @param headingTag the heading tag to be generated bpatel@766: * @param printTitle true if the title for the tag needs to be printed else false bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the tag bpatel@766: */ bpatel@766: public static HtmlTree HEADING(HtmlTag headingTag, boolean printTitle, Content body) { bpatel@766: return HEADING(headingTag, printTitle, null, body); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a heading tag (h1 to h6) with some content. bpatel@766: * bpatel@766: * @param headingTag the heading tag to be generated bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the tag bpatel@766: */ bpatel@766: public static HtmlTree HEADING(HtmlTag headingTag, Content body) { bpatel@766: return HEADING(headingTag, false, null, body); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates an HTML tag with lang attribute. It also adds head and body bpatel@766: * content to the HTML tree. bpatel@766: * bpatel@766: * @param lang language for the HTML document bpatel@766: * @param head head for the HTML tag bpatel@766: * @param body body for the HTML tag bpatel@766: * @return an HtmlTree object for the HTML tag bpatel@766: */ bpatel@766: public static HtmlTree HTML(String lang, Content head, Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.HTML, nullCheck(head), nullCheck(body)); bpatel@766: htmltree.addAttr(HtmlAttr.LANG, nullCheck(lang)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a I tag with some content. bpatel@766: * bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the I tag bpatel@766: */ bpatel@766: public static HtmlTree I(Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.I, nullCheck(body)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a LI tag with some content. bpatel@766: * bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the LI tag bpatel@766: */ bpatel@766: public static HtmlTree LI(Content body) { bpatel@766: return LI(null, body); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a LI tag with some content. bpatel@766: * bpatel@766: * @param styleClass style for the tag bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the LI tag bpatel@766: */ bpatel@766: public static HtmlTree LI(HtmlStyle styleClass, Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.LI, nullCheck(body)); bpatel@766: if (styleClass != null) bpatel@766: htmltree.addStyle(styleClass); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a LINK tag with the rel, type, href and title attributes. bpatel@766: * bpatel@766: * @param rel relevance of the link bpatel@766: * @param type type of link bpatel@766: * @param href the path for the link bpatel@766: * @param title title for the link bpatel@766: * @return an HtmlTree object for the LINK tag bpatel@766: */ bpatel@766: public static HtmlTree LINK(String rel, String type, String href, String title) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.LINK); bpatel@766: htmltree.addAttr(HtmlAttr.REL, nullCheck(rel)); bpatel@766: htmltree.addAttr(HtmlAttr.TYPE, nullCheck(type)); bpatel@766: htmltree.addAttr(HtmlAttr.HREF, nullCheck(href)); bpatel@766: htmltree.addAttr(HtmlAttr.TITLE, nullCheck(title)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a META tag with the http-equiv, content and charset attributes. bpatel@766: * bpatel@766: * @param http-equiv http equiv attribute for the META tag bpatel@766: * @param content type of content bpatel@766: * @param charset character set used bpatel@766: * @return an HtmlTree object for the META tag bpatel@766: */ bpatel@766: public static HtmlTree META(String httpEquiv, String content, String charSet) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.META); bpatel@766: htmltree.addAttr(HtmlAttr.HTTP_EQUIV, nullCheck(httpEquiv)); bpatel@766: htmltree.addAttr(HtmlAttr.CONTENT, nullCheck(content)); bpatel@766: htmltree.addAttr(HtmlAttr.CHARSET, nullCheck(charSet)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a META tag with the name and content attributes. bpatel@766: * bpatel@766: * @param name name attribute bpatel@766: * @param content type of content bpatel@766: * @return an HtmlTree object for the META tag bpatel@766: */ bpatel@766: public static HtmlTree META(String name, String content) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.META); bpatel@766: htmltree.addAttr(HtmlAttr.NAME, nullCheck(name)); bpatel@766: htmltree.addAttr(HtmlAttr.CONTENT, nullCheck(content)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a NOSCRIPT tag with some content. bpatel@766: * bpatel@766: * @param body content of the noscript tag bpatel@766: * @return an HtmlTree object for the NOSCRIPT tag bpatel@766: */ bpatel@766: public static HtmlTree NOSCRIPT(Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.NOSCRIPT, nullCheck(body)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a P tag with some content. bpatel@766: * bpatel@766: * @param body content of the Paragraph tag bpatel@766: * @return an HtmlTree object for the P tag bpatel@766: */ bpatel@766: public static HtmlTree P(Content body) { bpatel@766: return P(null, body); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a P tag with some content. bpatel@766: * bpatel@766: * @param styleClass style of the Paragraph tag bpatel@766: * @param body content of the Paragraph tag bpatel@766: * @return an HtmlTree object for the P tag bpatel@766: */ bpatel@766: public static HtmlTree P(HtmlStyle styleClass, Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.P, nullCheck(body)); bpatel@766: if (styleClass != null) bpatel@766: htmltree.addStyle(styleClass); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a SMALL tag with some content. bpatel@766: * bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the SMALL tag bpatel@766: */ bpatel@766: public static HtmlTree SMALL(Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.SMALL, nullCheck(body)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a STRONG tag with some content. bpatel@766: * bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the STRONG tag bpatel@766: */ bpatel@766: public static HtmlTree STRONG(Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.STRONG, nullCheck(body)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a SPAN tag with some content. bpatel@766: * bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the SPAN tag bpatel@766: */ bpatel@766: public static HtmlTree SPAN(Content body) { bpatel@766: return SPAN(null, body); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a SPAN tag with style class attribute and some content. bpatel@766: * bpatel@766: * @param styleClass style class for the tag bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the SPAN tag bpatel@766: */ bpatel@766: public static HtmlTree SPAN(HtmlStyle styleClass, Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.SPAN, nullCheck(body)); bpatel@766: if (styleClass != null) bpatel@766: htmltree.addStyle(styleClass); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a Table tag with border, width and summary attributes and bpatel@766: * some content. bpatel@766: * bpatel@766: * @param border border for the table bpatel@766: * @param width width of the table bpatel@766: * @param summary summary for the table bpatel@766: * @param body content for the table bpatel@766: * @return an HtmlTree object for the TABLE tag bpatel@766: */ bpatel@766: public static HtmlTree TABLE(int border, int width, String summary, bpatel@766: Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.TABLE, nullCheck(body)); bpatel@766: htmltree.addAttr(HtmlAttr.BORDER, Integer.toString(border)); bpatel@766: htmltree.addAttr(HtmlAttr.WIDTH, Integer.toString(width)); bpatel@766: htmltree.addAttr(HtmlAttr.SUMMARY, nullCheck(summary)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a Table tag with style class, border, cell padding, bpatel@766: * cellspacing and summary attributes and some content. bpatel@766: * bpatel@766: * @param styleClass style of the table bpatel@766: * @param border border for the table bpatel@766: * @param cellPadding cell padding for the table bpatel@766: * @param cellSpacing cell spacing for the table bpatel@766: * @param summary summary for the table bpatel@766: * @param body content for the table bpatel@766: * @return an HtmlTree object for the TABLE tag bpatel@766: */ bpatel@766: public static HtmlTree TABLE(HtmlStyle styleClass, int border, int cellPadding, bpatel@766: int cellSpacing, String summary, Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.TABLE, nullCheck(body)); bpatel@766: if (styleClass != null) bpatel@766: htmltree.addStyle(styleClass); bpatel@766: htmltree.addAttr(HtmlAttr.BORDER, Integer.toString(border)); bpatel@766: htmltree.addAttr(HtmlAttr.CELLPADDING, Integer.toString(cellPadding)); bpatel@766: htmltree.addAttr(HtmlAttr.CELLSPACING, Integer.toString(cellSpacing)); bpatel@766: htmltree.addAttr(HtmlAttr.SUMMARY, nullCheck(summary)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a Table tag with border, cell padding, bpatel@766: * cellspacing and summary attributes and some content. bpatel@766: * bpatel@766: * @param border border for the table bpatel@766: * @param cellPadding cell padding for the table bpatel@766: * @param cellSpacing cell spacing for the table bpatel@766: * @param summary summary for the table bpatel@766: * @param body content for the table bpatel@766: * @return an HtmlTree object for the TABLE tag bpatel@766: */ bpatel@766: public static HtmlTree TABLE(int border, int cellPadding, bpatel@766: int cellSpacing, String summary, Content body) { bpatel@766: return TABLE(null, border, cellPadding, cellSpacing, summary, body); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a TD tag with style class attribute and some content. bpatel@766: * bpatel@766: * @param styleClass style for the tag bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the TD tag bpatel@766: */ bpatel@766: public static HtmlTree TD(HtmlStyle styleClass, Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.TD, nullCheck(body)); bpatel@766: if (styleClass != null) bpatel@766: htmltree.addStyle(styleClass); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a TD tag for an HTML table with some content. bpatel@766: * bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the TD tag bpatel@766: */ bpatel@766: public static HtmlTree TD(Content body) { bpatel@766: return TD(null, body); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a TH tag with style class and scope attributes and some content. bpatel@766: * bpatel@766: * @param styleClass style for the tag bpatel@766: * @param scope scope of the tag bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the TH tag bpatel@766: */ bpatel@766: public static HtmlTree TH(HtmlStyle styleClass, String scope, Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.TH, nullCheck(body)); bpatel@766: if (styleClass != null) bpatel@766: htmltree.addStyle(styleClass); bpatel@766: htmltree.addAttr(HtmlAttr.SCOPE, nullCheck(scope)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a TH tag with scope attribute and some content. bpatel@766: * bpatel@766: * @param scope scope of the tag bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the TH tag bpatel@766: */ bpatel@766: public static HtmlTree TH(String scope, Content body) { bpatel@766: return TH(null, scope, body); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a TITLE tag with some content. bpatel@766: * bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the TITLE tag bpatel@766: */ bpatel@766: public static HtmlTree TITLE(Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.TITLE, nullCheck(body)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a TR tag for an HTML table with some content. bpatel@766: * bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the TR tag bpatel@766: */ bpatel@766: public static HtmlTree TR(Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.TR, nullCheck(body)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Generates a UL tag with the style class attribute and some content. bpatel@766: * bpatel@766: * @param styleClass style for the tag bpatel@766: * @param body content for the tag bpatel@766: * @return an HtmlTree object for the UL tag bpatel@766: */ bpatel@766: public static HtmlTree UL(HtmlStyle styleClass, Content body) { bpatel@766: HtmlTree htmltree = new HtmlTree(HtmlTag.UL, nullCheck(body)); bpatel@766: htmltree.addStyle(nullCheck(styleClass)); bpatel@766: return htmltree; bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * {@inheritDoc} bpatel@766: */ bpatel@766: public boolean isEmpty() { bpatel@766: return (!hasContent() && !hasAttrs()); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Returns true if the HTML tree has content. bpatel@766: * bpatel@766: * @return true if the HTML tree has content else return false bpatel@766: */ bpatel@766: public boolean hasContent() { bpatel@766: return (!content.isEmpty()); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Returns true if the HTML tree has attributes. bpatel@766: * bpatel@766: * @return true if the HTML tree has attributes else return false bpatel@766: */ bpatel@766: public boolean hasAttrs() { bpatel@766: return (!attrs.isEmpty()); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Returns true if the HTML tree has a specific attribute. bpatel@766: * bpatel@766: * @param attrName name of the attribute to check within the HTML tree bpatel@766: * @return true if the HTML tree has the specified attribute else return false bpatel@766: */ bpatel@766: public boolean hasAttr(HtmlAttr attrName) { bpatel@766: return (attrs.containsKey(attrName)); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Returns true if the HTML tree is valid. This check is more specific to bpatel@766: * standard doclet and not exactly similar to W3C specifications. But it bpatel@766: * ensures HTML validation. bpatel@766: * bpatel@766: * @return true if the HTML tree is valid bpatel@766: */ bpatel@766: public boolean isValid() { bpatel@766: switch (htmlTag) { bpatel@766: case A : bpatel@766: return (hasAttr(HtmlAttr.NAME) || (hasAttr(HtmlAttr.HREF) && hasContent())); bpatel@766: case BR : bpatel@766: return (!hasContent() && (!hasAttrs() || hasAttr(HtmlAttr.CLEAR))); bpatel@766: case FRAME : bpatel@766: return (hasAttr(HtmlAttr.SRC) && !hasContent()); bpatel@766: case HR : bpatel@766: return (!hasContent()); bpatel@766: case IMG : bpatel@766: return (hasAttr(HtmlAttr.SRC) && hasAttr(HtmlAttr.ALT) && !hasContent()); bpatel@766: case LINK : bpatel@766: return (hasAttr(HtmlAttr.HREF) && !hasContent()); bpatel@766: case META : bpatel@766: return (hasAttr(HtmlAttr.CONTENT) && !hasContent()); bpatel@766: default : bpatel@766: return hasContent(); bpatel@766: } bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * Returns true if the element is an inline element. bpatel@766: * bpatel@766: * @return true if the HTML tag is an inline element bpatel@766: */ bpatel@766: public boolean isInline() { bpatel@766: return (htmlTag.blockType == HtmlTag.BlockType.INLINE); bpatel@766: } bpatel@766: bpatel@766: /** bpatel@766: * {@inheritDoc} bpatel@766: */ bpatel@766: public void write(StringBuilder contentBuilder) { bpatel@766: if (!isInline() && !endsWithNewLine(contentBuilder)) bpatel@793: contentBuilder.append(DocletConstants.NL); bpatel@766: String tagString = htmlTag.toString(); bpatel@819: contentBuilder.append("<"); bpatel@819: contentBuilder.append(tagString); bpatel@766: Iterator iterator = attrs.keySet().iterator(); bpatel@766: HtmlAttr key; bpatel@766: String value = ""; bpatel@766: while (iterator.hasNext()) { bpatel@766: key = iterator.next(); bpatel@766: value = attrs.get(key); bpatel@819: contentBuilder.append(" "); bpatel@819: contentBuilder.append(key.toString()); bpatel@819: if (!value.isEmpty()) { bpatel@819: contentBuilder.append("=\""); bpatel@819: contentBuilder.append(value); bpatel@819: contentBuilder.append("\""); bpatel@819: } bpatel@766: } bpatel@766: contentBuilder.append(">"); bpatel@766: for (Content c : content) bpatel@766: c.write(contentBuilder); bpatel@819: if (htmlTag.endTagRequired()) { bpatel@819: contentBuilder.append(""); bpatel@819: } bpatel@766: if (!isInline()) bpatel@793: contentBuilder.append(DocletConstants.NL); bpatel@766: } bpatel@766: }