bpatel@766: /* jjg@1736: * Copyright (c) 2010, 2013, 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: jjg@1364: import java.io.IOException; jjg@1364: import java.io.Writer; bpatel@766: import java.util.*; jjg@1364: 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: * jjg@1359: *

This is NOT part of any supported API. jjg@1359: * If you write code that depends on this, you do so at your own risk. jjg@1359: * This code and its internal interfaces are subject to change or jjg@1359: * deletion without notice. jjg@1359: * 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()) jjg@1746: attrs = new LinkedHashMap(3); jjg@1746: attrs.put(nullCheck(attrName), escapeHtmlChars(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: jjg@1737: public int charCount() { jjg@1737: int n = 0; jjg@1737: for (Content c : content) jjg@1737: n += c.charCount(); jjg@1737: return n; jjg@1737: } jjg@1737: bpatel@766: /** jjg@1746: * Given a string, escape all special html characters and jjg@1746: * return the result. jjg@1746: * jjg@1746: * @param s The string to check. jjg@1746: * @return the original string with all of the HTML characters escaped. jjg@1746: */ jjg@1746: private static String escapeHtmlChars(String s) { jjg@1746: for (int i = 0; i < s.length(); i++) { jjg@1746: char ch = s.charAt(i); jjg@1746: switch (ch) { jjg@1746: // only start building a new string if we need to jjg@1746: case '<': case '>': case '&': jjg@1746: StringBuilder sb = new StringBuilder(s.substring(0, i)); jjg@1746: for ( ; i < s.length(); i++) { jjg@1746: ch = s.charAt(i); jjg@1746: switch (ch) { jjg@1746: case '<': sb.append("<"); break; jjg@1746: case '>': sb.append(">"); break; jjg@1746: case '&': sb.append("&"); break; jjg@1746: default: sb.append(ch); break; jjg@1746: } jjg@1746: } jjg@1746: return sb.toString(); jjg@1746: } jjg@1746: } jjg@1746: return s; jjg@1746: } jjg@1746: jjg@1746: /** 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)); jjg@1746: htmltree.addAttr(HtmlAttr.HREF, 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) jjg@1746: htmltree.addAttr(HtmlAttr.TITLE, stripHtml(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 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: * jjg@1358: * @param httpEquiv http equiv attribute for the META tag bpatel@766: * @param content type of content jjg@1358: * @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@1417: * Generates a SCRIPT tag with the type and src attributes. bpatel@1417: * bpatel@1417: * @param type type of link bpatel@1417: * @param src the path for the script bpatel@1417: * @return an HtmlTree object for the SCRIPT tag bpatel@1417: */ bpatel@1417: public static HtmlTree SCRIPT(String type, String src) { bpatel@1417: HtmlTree htmltree = new HtmlTree(HtmlTag.SCRIPT); bpatel@1417: htmltree.addAttr(HtmlAttr.TYPE, nullCheck(type)); bpatel@1417: htmltree.addAttr(HtmlAttr.SRC, nullCheck(src)); bpatel@1417: return htmltree; bpatel@1417: } bpatel@1417: bpatel@1417: /** 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@1417: * Generates a SPAN tag with id and style class attributes. It also encloses bpatel@1417: * a content. bpatel@1417: * bpatel@1417: * @param id the id for the tag bpatel@1417: * @param styleClass stylesheet class for the tag bpatel@1417: * @param body content for the tag bpatel@1417: * @return an HtmlTree object for the SPAN tag bpatel@1417: */ bpatel@1417: public static HtmlTree SPAN(String id, HtmlStyle styleClass, Content body) { bpatel@1417: HtmlTree htmltree = new HtmlTree(HtmlTag.SPAN, nullCheck(body)); bpatel@1417: htmltree.addAttr(HtmlAttr.ID, nullCheck(id)); bpatel@1417: if (styleClass != null) bpatel@1417: htmltree.addStyle(styleClass); bpatel@1417: return htmltree; bpatel@1417: } bpatel@1417: bpatel@1417: /** 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@1417: case SCRIPT : bpatel@1417: return ((hasAttr(HtmlAttr.TYPE) && hasAttr(HtmlAttr.SRC) && !hasContent()) || bpatel@1417: (hasAttr(HtmlAttr.TYPE) && 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: */ jjg@1364: @Override jjg@1364: public boolean write(Writer out, boolean atNewline) throws IOException { jjg@1364: if (!isInline() && !atNewline) jjg@1364: out.write(DocletConstants.NL); bpatel@766: String tagString = htmlTag.toString(); jjg@1364: out.write("<"); jjg@1364: out.write(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); jjg@1364: out.write(" "); jjg@1364: out.write(key.toString()); bpatel@819: if (!value.isEmpty()) { jjg@1364: out.write("=\""); jjg@1364: out.write(value); jjg@1364: out.write("\""); bpatel@819: } bpatel@766: } jjg@1364: out.write(">"); jjg@1364: boolean nl = false; bpatel@766: for (Content c : content) jjg@1364: nl = c.write(out, nl); bpatel@819: if (htmlTag.endTagRequired()) { jjg@1364: out.write(""); bpatel@819: } jjg@1364: if (!isInline()) { jjg@1364: out.write(DocletConstants.NL); jjg@1364: return true; jjg@1364: } else { jjg@1364: return false; jjg@1364: } bpatel@766: } jjg@1746: jjg@1746: /** jjg@1746: * Given a Content node, strips all html characters and jjg@1746: * return the result. jjg@1746: * jjg@1746: * @param body The content node to check. jjg@1746: * @return the plain text from the content node jjg@1746: * jjg@1746: */ jjg@1746: private static String stripHtml(Content body) { jjg@1746: String rawString = body.toString(); jjg@1746: // remove HTML tags jjg@1746: rawString = rawString.replaceAll("\\<.*?>", " "); jjg@1746: // consolidate multiple spaces between a word to a single space jjg@1746: rawString = rawString.replaceAll("\\b\\s{2,}\\b", " "); jjg@1746: // remove extra whitespaces jjg@1746: return rawString.trim(); jjg@1746: } bpatel@766: }