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

changeset 766
90af8d87741f
child 793
ffbf2b2a8611
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/doclets/formats/html/markup/HtmlTree.java	Wed Dec 01 11:02:38 2010 -0800
     1.3 @@ -0,0 +1,777 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.doclets.formats.html.markup;
    1.30 +
    1.31 +import java.util.*;
    1.32 +import com.sun.tools.doclets.internal.toolkit.Content;
    1.33 +import com.sun.tools.doclets.internal.toolkit.util.*;
    1.34 +
    1.35 +/**
    1.36 + * Class for generating HTML tree for javadoc output.
    1.37 + *
    1.38 + * @author Bhavesh Patel
    1.39 + */
    1.40 +public class HtmlTree extends Content {
    1.41 +
    1.42 +    private HtmlTag htmlTag;
    1.43 +    private Map<HtmlAttr,String> attrs = Collections.<HtmlAttr,String>emptyMap();
    1.44 +    private List<Content> content = Collections.<Content>emptyList();
    1.45 +    public static final Content EMPTY = new StringContent("");
    1.46 +
    1.47 +    /**
    1.48 +     * Constructor to construct HtmlTree object.
    1.49 +     *
    1.50 +     * @param tag HTML tag for the HtmlTree object
    1.51 +     */
    1.52 +    public HtmlTree(HtmlTag tag) {
    1.53 +        htmlTag = nullCheck(tag);
    1.54 +    }
    1.55 +
    1.56 +    /**
    1.57 +     * Constructor to construct HtmlTree object.
    1.58 +     *
    1.59 +     * @param tag HTML tag for the HtmlTree object
    1.60 +     * @param contents contents to be added to the tree
    1.61 +     */
    1.62 +    public HtmlTree(HtmlTag tag, Content... contents) {
    1.63 +        this(tag);
    1.64 +        for (Content content: contents)
    1.65 +            addContent(content);
    1.66 +    }
    1.67 +
    1.68 +    /**
    1.69 +     * Adds an attribute for the HTML tag.
    1.70 +     *
    1.71 +     * @param attrName name of the attribute
    1.72 +     * @param attrValue value of the attribute
    1.73 +     */
    1.74 +    public void addAttr(HtmlAttr attrName, String attrValue) {
    1.75 +        if (attrs.isEmpty())
    1.76 +            attrs = new LinkedHashMap<HtmlAttr,String>();
    1.77 +        attrs.put(nullCheck(attrName), nullCheck(attrValue));
    1.78 +    }
    1.79 +
    1.80 +    /**
    1.81 +     * Adds a style for the HTML tag.
    1.82 +     *
    1.83 +     * @param style style to be added
    1.84 +     */
    1.85 +    public void addStyle(HtmlStyle style) {
    1.86 +        addAttr(HtmlAttr.CLASS, style.toString());
    1.87 +    }
    1.88 +
    1.89 +    /**
    1.90 +     * Adds content for the HTML tag.
    1.91 +     *
    1.92 +     * @param tagContent tag content to be added
    1.93 +     */
    1.94 +    public void addContent(Content tagContent) {
    1.95 +        if (tagContent == HtmlTree.EMPTY || tagContent.isValid()) {
    1.96 +            if (content.isEmpty())
    1.97 +                content = new ArrayList<Content>();
    1.98 +            content.add(tagContent);
    1.99 +        }
   1.100 +    }
   1.101 +
   1.102 +    /**
   1.103 +     * This method adds a string content to the htmltree. If the last content member
   1.104 +     * added is a StringContent, append the string to that StringContent or else
   1.105 +     * create a new StringContent and add it to the html tree.
   1.106 +     *
   1.107 +     * @param stringContent string content that needs to be added
   1.108 +     */
   1.109 +    public void addContent(String stringContent) {
   1.110 +        if (!content.isEmpty()) {
   1.111 +            Content lastContent = content.get(content.size() - 1);
   1.112 +            if (lastContent instanceof StringContent)
   1.113 +                lastContent.addContent(stringContent);
   1.114 +            else
   1.115 +                addContent(new StringContent(stringContent));
   1.116 +        }
   1.117 +        else
   1.118 +            addContent(new StringContent(stringContent));
   1.119 +    }
   1.120 +
   1.121 +    /**
   1.122 +     * Generates an HTML anchor tag.
   1.123 +     *
   1.124 +     * @param ref reference url for the anchor tag
   1.125 +     * @param body content for the anchor tag
   1.126 +     * @return an HtmlTree object
   1.127 +     */
   1.128 +    public static HtmlTree A(String ref, Content body) {
   1.129 +        HtmlTree htmltree = new HtmlTree(HtmlTag.A, nullCheck(body));
   1.130 +        htmltree.addAttr(HtmlAttr.HREF, nullCheck(ref));
   1.131 +        return htmltree;
   1.132 +    }
   1.133 +
   1.134 +    /**
   1.135 +     * Generates an HTML anchor tag with name attribute and content.
   1.136 +     *
   1.137 +     * @param name name for the anchor tag
   1.138 +     * @param body content for the anchor tag
   1.139 +     * @return an HtmlTree object
   1.140 +     */
   1.141 +    public static HtmlTree A_NAME(String name, Content body) {
   1.142 +        HtmlTree htmltree = HtmlTree.A_NAME(name);
   1.143 +        htmltree.addContent(nullCheck(body));
   1.144 +        return htmltree;
   1.145 +    }
   1.146 +
   1.147 +    /**
   1.148 +     * Generates an HTML anchor tag with name attribute.
   1.149 +     *
   1.150 +     * @param name name for the anchor tag
   1.151 +     * @return an HtmlTree object
   1.152 +     */
   1.153 +    public static HtmlTree A_NAME(String name) {
   1.154 +        HtmlTree htmltree = new HtmlTree(HtmlTag.A);
   1.155 +        htmltree.addAttr(HtmlAttr.NAME, nullCheck(name));
   1.156 +        return htmltree;
   1.157 +    }
   1.158 +
   1.159 +    /**
   1.160 +     * Generates a CAPTION tag with some content.
   1.161 +     *
   1.162 +     * @param body content for the tag
   1.163 +     * @return an HtmlTree object for the CAPTION tag
   1.164 +     */
   1.165 +    public static HtmlTree CAPTION(Content body) {
   1.166 +        HtmlTree htmltree = new HtmlTree(HtmlTag.CAPTION, nullCheck(body));
   1.167 +        return htmltree;
   1.168 +    }
   1.169 +
   1.170 +    /**
   1.171 +     * Generates a CODE tag with some content.
   1.172 +     *
   1.173 +     * @param body content for the tag
   1.174 +     * @return an HtmlTree object for the CODE tag
   1.175 +     */
   1.176 +    public static HtmlTree CODE(Content body) {
   1.177 +        HtmlTree htmltree = new HtmlTree(HtmlTag.CODE, nullCheck(body));
   1.178 +        return htmltree;
   1.179 +    }
   1.180 +
   1.181 +    /**
   1.182 +     * Generates a DD tag with some content.
   1.183 +     *
   1.184 +     * @param body content for the tag
   1.185 +     * @return an HtmlTree object for the DD tag
   1.186 +     */
   1.187 +    public static HtmlTree DD(Content body) {
   1.188 +        HtmlTree htmltree = new HtmlTree(HtmlTag.DD, nullCheck(body));
   1.189 +        return htmltree;
   1.190 +    }
   1.191 +
   1.192 +    /**
   1.193 +     * Generates a DL tag with some content.
   1.194 +     *
   1.195 +     * @param body content for the tag
   1.196 +     * @return an HtmlTree object for the DL tag
   1.197 +     */
   1.198 +    public static HtmlTree DL(Content body) {
   1.199 +        HtmlTree htmltree = new HtmlTree(HtmlTag.DL, nullCheck(body));
   1.200 +        return htmltree;
   1.201 +    }
   1.202 +
   1.203 +    /**
   1.204 +     * Generates a DIV tag with the style class attributes. It also encloses
   1.205 +     * a content.
   1.206 +     *
   1.207 +     * @param styleClass stylesheet class for the tag
   1.208 +     * @param body content for the tag
   1.209 +     * @return an HtmlTree object for the DIV tag
   1.210 +     */
   1.211 +    public static HtmlTree DIV(HtmlStyle styleClass, Content body) {
   1.212 +        HtmlTree htmltree = new HtmlTree(HtmlTag.DIV, nullCheck(body));
   1.213 +        if (styleClass != null)
   1.214 +            htmltree.addStyle(styleClass);
   1.215 +        return htmltree;
   1.216 +    }
   1.217 +
   1.218 +    /**
   1.219 +     * Generates a DIV tag with some content.
   1.220 +     *
   1.221 +     * @param body content for the tag
   1.222 +     * @return an HtmlTree object for the DIV tag
   1.223 +     */
   1.224 +    public static HtmlTree DIV(Content body) {
   1.225 +        return DIV(null, body);
   1.226 +    }
   1.227 +
   1.228 +    /**
   1.229 +     * Generates a DT tag with some content.
   1.230 +     *
   1.231 +     * @param body content for the tag
   1.232 +     * @return an HtmlTree object for the DT tag
   1.233 +     */
   1.234 +    public static HtmlTree DT(Content body) {
   1.235 +        HtmlTree htmltree = new HtmlTree(HtmlTag.DT, nullCheck(body));
   1.236 +        return htmltree;
   1.237 +    }
   1.238 +
   1.239 +    /**
   1.240 +     * Generates a EM tag with some content.
   1.241 +     *
   1.242 +     * @param body content to be added to the tag
   1.243 +     * @return an HtmlTree object for the EM tag
   1.244 +     */
   1.245 +    public static HtmlTree EM(Content body) {
   1.246 +        HtmlTree htmltree = new HtmlTree(HtmlTag.EM, nullCheck(body));
   1.247 +        return htmltree;
   1.248 +    }
   1.249 +
   1.250 +    /**
   1.251 +     * Generates a FRAME tag.
   1.252 +     *
   1.253 +     * @param src the url of the document to be shown in the frame
   1.254 +     * @param name specifies the name of the frame
   1.255 +     * @param title the title for the frame
   1.256 +     * @param scrolling specifies whether to display scrollbars in the frame
   1.257 +     * @return an HtmlTree object for the FRAME tag
   1.258 +     */
   1.259 +    public static HtmlTree FRAME(String src, String name, String title, String scrolling) {
   1.260 +        HtmlTree htmltree = new HtmlTree(HtmlTag.FRAME);
   1.261 +        htmltree.addAttr(HtmlAttr.SRC, nullCheck(src));
   1.262 +        htmltree.addAttr(HtmlAttr.NAME, nullCheck(name));
   1.263 +        htmltree.addAttr(HtmlAttr.TITLE, nullCheck(title));
   1.264 +        if (scrolling != null)
   1.265 +            htmltree.addAttr(HtmlAttr.SCROLLING, scrolling);
   1.266 +        return htmltree;
   1.267 +    }
   1.268 +
   1.269 +    /**
   1.270 +     * Generates a Frame tag.
   1.271 +     *
   1.272 +     * @param src the url of the document to be shown in the frame
   1.273 +     * @param name specifies the name of the frame
   1.274 +     * @param title the title for the frame
   1.275 +     * @return an HtmlTree object for the SPAN tag
   1.276 +     */
   1.277 +    public static HtmlTree FRAME(String src, String name, String title) {
   1.278 +        return FRAME(src, name, title, null);
   1.279 +    }
   1.280 +
   1.281 +    /**
   1.282 +     * Generates a FRAMESET tag.
   1.283 +     *
   1.284 +     * @param cols the size of columns in the frameset
   1.285 +     * @param rows the size of rows in the frameset
   1.286 +     * @param title the title for the frameset
   1.287 +     * @param onload the script to run when the document loads
   1.288 +     * @return an HtmlTree object for the FRAMESET tag
   1.289 +     */
   1.290 +    public static HtmlTree FRAMESET(String cols, String rows, String title, String onload) {
   1.291 +        HtmlTree htmltree = new HtmlTree(HtmlTag.FRAMESET);
   1.292 +        if (cols != null)
   1.293 +            htmltree.addAttr(HtmlAttr.COLS, cols);
   1.294 +        if (rows != null)
   1.295 +            htmltree.addAttr(HtmlAttr.ROWS, rows);
   1.296 +        htmltree.addAttr(HtmlAttr.TITLE, nullCheck(title));
   1.297 +        htmltree.addAttr(HtmlAttr.ONLOAD, nullCheck(onload));
   1.298 +        return htmltree;
   1.299 +    }
   1.300 +
   1.301 +    /**
   1.302 +     * Generates a heading tag (h1 to h6) with the title and style class attributes. It also encloses
   1.303 +     * a content.
   1.304 +     *
   1.305 +     * @param headingTag the heading tag to be generated
   1.306 +     * @param printTitle true if title for the tag needs to be printed else false
   1.307 +     * @param styleClass stylesheet class for the tag
   1.308 +     * @param body content for the tag
   1.309 +     * @return an HtmlTree object for the tag
   1.310 +     */
   1.311 +    public static HtmlTree HEADING(HtmlTag headingTag, boolean printTitle,
   1.312 +            HtmlStyle styleClass, Content body) {
   1.313 +        HtmlTree htmltree = new HtmlTree(headingTag, nullCheck(body));
   1.314 +        if (printTitle)
   1.315 +            htmltree.addAttr(HtmlAttr.TITLE, Util.stripHtml(body.toString()));
   1.316 +        if (styleClass != null)
   1.317 +            htmltree.addStyle(styleClass);
   1.318 +        return htmltree;
   1.319 +    }
   1.320 +
   1.321 +    /**
   1.322 +     * Generates a heading tag (h1 to h6) with style class attribute. It also encloses
   1.323 +     * a content.
   1.324 +     *
   1.325 +     * @param headingTag the heading tag to be generated
   1.326 +     * @param styleClass stylesheet class for the tag
   1.327 +     * @param body content for the tag
   1.328 +     * @return an HtmlTree object for the tag
   1.329 +     */
   1.330 +    public static HtmlTree HEADING(HtmlTag headingTag, HtmlStyle styleClass, Content body) {
   1.331 +        return HEADING(headingTag, false, styleClass, body);
   1.332 +    }
   1.333 +
   1.334 +    /**
   1.335 +     * Generates a heading tag (h1 to h6) with the title attribute. It also encloses
   1.336 +     * a content.
   1.337 +     *
   1.338 +     * @param headingTag the heading tag to be generated
   1.339 +     * @param printTitle true if the title for the tag needs to be printed else false
   1.340 +     * @param body content for the tag
   1.341 +     * @return an HtmlTree object for the tag
   1.342 +     */
   1.343 +    public static HtmlTree HEADING(HtmlTag headingTag, boolean printTitle, Content body) {
   1.344 +        return HEADING(headingTag, printTitle, null, body);
   1.345 +    }
   1.346 +
   1.347 +    /**
   1.348 +     * Generates a heading tag (h1 to h6)  with some content.
   1.349 +     *
   1.350 +     * @param headingTag the heading tag to be generated
   1.351 +     * @param body content for the tag
   1.352 +     * @return an HtmlTree object for the tag
   1.353 +     */
   1.354 +    public static HtmlTree HEADING(HtmlTag headingTag, Content body) {
   1.355 +        return HEADING(headingTag, false, null, body);
   1.356 +    }
   1.357 +
   1.358 +    /**
   1.359 +     * Generates an HTML tag with lang attribute. It also adds head and body
   1.360 +     * content to the HTML tree.
   1.361 +     *
   1.362 +     * @param lang language for the HTML document
   1.363 +     * @param head head for the HTML tag
   1.364 +     * @param body body for the HTML tag
   1.365 +     * @return an HtmlTree object for the HTML tag
   1.366 +     */
   1.367 +    public static HtmlTree HTML(String lang, Content head, Content body) {
   1.368 +        HtmlTree htmltree = new HtmlTree(HtmlTag.HTML, nullCheck(head), nullCheck(body));
   1.369 +        htmltree.addAttr(HtmlAttr.LANG, nullCheck(lang));
   1.370 +        return htmltree;
   1.371 +    }
   1.372 +
   1.373 +    /**
   1.374 +     * Generates a I tag with some content.
   1.375 +     *
   1.376 +     * @param body content for the tag
   1.377 +     * @return an HtmlTree object for the I tag
   1.378 +     */
   1.379 +    public static HtmlTree I(Content body) {
   1.380 +        HtmlTree htmltree = new HtmlTree(HtmlTag.I, nullCheck(body));
   1.381 +        return htmltree;
   1.382 +    }
   1.383 +
   1.384 +    /**
   1.385 +     * Generates a LI tag with some content.
   1.386 +     *
   1.387 +     * @param body content for the tag
   1.388 +     * @return an HtmlTree object for the LI tag
   1.389 +     */
   1.390 +    public static HtmlTree LI(Content body) {
   1.391 +        return LI(null, body);
   1.392 +    }
   1.393 +
   1.394 +    /**
   1.395 +     * Generates a LI tag with some content.
   1.396 +     *
   1.397 +     * @param styleClass style for the tag
   1.398 +     * @param body content for the tag
   1.399 +     * @return an HtmlTree object for the LI tag
   1.400 +     */
   1.401 +    public static HtmlTree LI(HtmlStyle styleClass, Content body) {
   1.402 +        HtmlTree htmltree = new HtmlTree(HtmlTag.LI, nullCheck(body));
   1.403 +        if (styleClass != null)
   1.404 +            htmltree.addStyle(styleClass);
   1.405 +        return htmltree;
   1.406 +    }
   1.407 +
   1.408 +    /**
   1.409 +     * Generates a LINK tag with the rel, type, href and title attributes.
   1.410 +     *
   1.411 +     * @param rel relevance of the link
   1.412 +     * @param type type of link
   1.413 +     * @param href the path for the link
   1.414 +     * @param title title for the link
   1.415 +     * @return an HtmlTree object for the LINK tag
   1.416 +     */
   1.417 +    public static HtmlTree LINK(String rel, String type, String href, String title) {
   1.418 +        HtmlTree htmltree = new HtmlTree(HtmlTag.LINK);
   1.419 +        htmltree.addAttr(HtmlAttr.REL, nullCheck(rel));
   1.420 +        htmltree.addAttr(HtmlAttr.TYPE, nullCheck(type));
   1.421 +        htmltree.addAttr(HtmlAttr.HREF, nullCheck(href));
   1.422 +        htmltree.addAttr(HtmlAttr.TITLE, nullCheck(title));
   1.423 +        return htmltree;
   1.424 +    }
   1.425 +
   1.426 +    /**
   1.427 +     * Generates a META tag with the http-equiv, content and charset attributes.
   1.428 +     *
   1.429 +     * @param http-equiv http equiv attribute for the META tag
   1.430 +     * @param content type of content
   1.431 +     * @param charset character set used
   1.432 +     * @return an HtmlTree object for the META tag
   1.433 +     */
   1.434 +    public static HtmlTree META(String httpEquiv, String content, String charSet) {
   1.435 +        HtmlTree htmltree = new HtmlTree(HtmlTag.META);
   1.436 +        htmltree.addAttr(HtmlAttr.HTTP_EQUIV, nullCheck(httpEquiv));
   1.437 +        htmltree.addAttr(HtmlAttr.CONTENT, nullCheck(content));
   1.438 +        htmltree.addAttr(HtmlAttr.CHARSET, nullCheck(charSet));
   1.439 +        return htmltree;
   1.440 +    }
   1.441 +
   1.442 +    /**
   1.443 +     * Generates a META tag with the name and content attributes.
   1.444 +     *
   1.445 +     * @param name name attribute
   1.446 +     * @param content type of content
   1.447 +     * @return an HtmlTree object for the META tag
   1.448 +     */
   1.449 +    public static HtmlTree META(String name, String content) {
   1.450 +        HtmlTree htmltree = new HtmlTree(HtmlTag.META);
   1.451 +        htmltree.addAttr(HtmlAttr.NAME, nullCheck(name));
   1.452 +        htmltree.addAttr(HtmlAttr.CONTENT, nullCheck(content));
   1.453 +        return htmltree;
   1.454 +    }
   1.455 +
   1.456 +    /**
   1.457 +     * Generates a NOSCRIPT tag with some content.
   1.458 +     *
   1.459 +     * @param body content of the noscript tag
   1.460 +     * @return an HtmlTree object for the NOSCRIPT tag
   1.461 +     */
   1.462 +    public static HtmlTree NOSCRIPT(Content body) {
   1.463 +        HtmlTree htmltree = new HtmlTree(HtmlTag.NOSCRIPT, nullCheck(body));
   1.464 +        return htmltree;
   1.465 +    }
   1.466 +
   1.467 +    /**
   1.468 +     * Generates a P tag with some content.
   1.469 +     *
   1.470 +     * @param body content of the Paragraph tag
   1.471 +     * @return an HtmlTree object for the P tag
   1.472 +     */
   1.473 +    public static HtmlTree P(Content body) {
   1.474 +        return P(null, body);
   1.475 +    }
   1.476 +
   1.477 +    /**
   1.478 +     * Generates a P tag with some content.
   1.479 +     *
   1.480 +     * @param styleClass style of the Paragraph tag
   1.481 +     * @param body content of the Paragraph tag
   1.482 +     * @return an HtmlTree object for the P tag
   1.483 +     */
   1.484 +    public static HtmlTree P(HtmlStyle styleClass, Content body) {
   1.485 +        HtmlTree htmltree = new HtmlTree(HtmlTag.P, nullCheck(body));
   1.486 +        if (styleClass != null)
   1.487 +            htmltree.addStyle(styleClass);
   1.488 +        return htmltree;
   1.489 +    }
   1.490 +
   1.491 +    /**
   1.492 +     * Generates a SMALL tag with some content.
   1.493 +     *
   1.494 +     * @param body content for the tag
   1.495 +     * @return an HtmlTree object for the SMALL tag
   1.496 +     */
   1.497 +    public static HtmlTree SMALL(Content body) {
   1.498 +        HtmlTree htmltree = new HtmlTree(HtmlTag.SMALL, nullCheck(body));
   1.499 +        return htmltree;
   1.500 +    }
   1.501 +
   1.502 +    /**
   1.503 +     * Generates a STRONG tag with some content.
   1.504 +     *
   1.505 +     * @param body content for the tag
   1.506 +     * @return an HtmlTree object for the STRONG tag
   1.507 +     */
   1.508 +    public static HtmlTree STRONG(Content body) {
   1.509 +        HtmlTree htmltree = new HtmlTree(HtmlTag.STRONG, nullCheck(body));
   1.510 +        return htmltree;
   1.511 +    }
   1.512 +
   1.513 +    /**
   1.514 +     * Generates a SPAN tag with some content.
   1.515 +     *
   1.516 +     * @param body content for the tag
   1.517 +     * @return an HtmlTree object for the SPAN tag
   1.518 +     */
   1.519 +    public static HtmlTree SPAN(Content body) {
   1.520 +        return SPAN(null, body);
   1.521 +    }
   1.522 +
   1.523 +    /**
   1.524 +     * Generates a SPAN tag with style class attribute and some content.
   1.525 +     *
   1.526 +     * @param styleClass style class for the tag
   1.527 +     * @param body content for the tag
   1.528 +     * @return an HtmlTree object for the SPAN tag
   1.529 +     */
   1.530 +    public static HtmlTree SPAN(HtmlStyle styleClass, Content body) {
   1.531 +        HtmlTree htmltree = new HtmlTree(HtmlTag.SPAN, nullCheck(body));
   1.532 +        if (styleClass != null)
   1.533 +            htmltree.addStyle(styleClass);
   1.534 +        return htmltree;
   1.535 +    }
   1.536 +
   1.537 +    /**
   1.538 +     * Generates a Table tag with border, width and summary attributes and
   1.539 +     * some content.
   1.540 +     *
   1.541 +     * @param border border for the table
   1.542 +     * @param width width of the table
   1.543 +     * @param summary summary for the table
   1.544 +     * @param body content for the table
   1.545 +     * @return an HtmlTree object for the TABLE tag
   1.546 +     */
   1.547 +    public static HtmlTree TABLE(int border, int width, String summary,
   1.548 +            Content body) {
   1.549 +        HtmlTree htmltree = new HtmlTree(HtmlTag.TABLE, nullCheck(body));
   1.550 +        htmltree.addAttr(HtmlAttr.BORDER, Integer.toString(border));
   1.551 +        htmltree.addAttr(HtmlAttr.WIDTH, Integer.toString(width));
   1.552 +        htmltree.addAttr(HtmlAttr.SUMMARY, nullCheck(summary));
   1.553 +        return htmltree;
   1.554 +    }
   1.555 +
   1.556 +    /**
   1.557 +     * Generates a Table tag with style class, border, cell padding,
   1.558 +     * cellspacing and summary attributes and some content.
   1.559 +     *
   1.560 +     * @param styleClass style of the table
   1.561 +     * @param border border for the table
   1.562 +     * @param cellPadding cell padding for the table
   1.563 +     * @param cellSpacing cell spacing for the table
   1.564 +     * @param summary summary for the table
   1.565 +     * @param body content for the table
   1.566 +     * @return an HtmlTree object for the TABLE tag
   1.567 +     */
   1.568 +    public static HtmlTree TABLE(HtmlStyle styleClass, int border, int cellPadding,
   1.569 +            int cellSpacing, String summary, Content body) {
   1.570 +        HtmlTree htmltree = new HtmlTree(HtmlTag.TABLE, nullCheck(body));
   1.571 +        if (styleClass != null)
   1.572 +            htmltree.addStyle(styleClass);
   1.573 +        htmltree.addAttr(HtmlAttr.BORDER, Integer.toString(border));
   1.574 +        htmltree.addAttr(HtmlAttr.CELLPADDING, Integer.toString(cellPadding));
   1.575 +        htmltree.addAttr(HtmlAttr.CELLSPACING, Integer.toString(cellSpacing));
   1.576 +        htmltree.addAttr(HtmlAttr.SUMMARY, nullCheck(summary));
   1.577 +        return htmltree;
   1.578 +    }
   1.579 +
   1.580 +    /**
   1.581 +     * Generates a Table tag with border, cell padding,
   1.582 +     * cellspacing and summary attributes and some content.
   1.583 +     *
   1.584 +     * @param border border for the table
   1.585 +     * @param cellPadding cell padding for the table
   1.586 +     * @param cellSpacing cell spacing for the table
   1.587 +     * @param summary summary for the table
   1.588 +     * @param body content for the table
   1.589 +     * @return an HtmlTree object for the TABLE tag
   1.590 +     */
   1.591 +    public static HtmlTree TABLE(int border, int cellPadding,
   1.592 +            int cellSpacing, String summary, Content body) {
   1.593 +        return TABLE(null, border, cellPadding, cellSpacing, summary, body);
   1.594 +    }
   1.595 +
   1.596 +    /**
   1.597 +     * Generates a TD tag with style class attribute and some content.
   1.598 +     *
   1.599 +     * @param styleClass style for the tag
   1.600 +     * @param body content for the tag
   1.601 +     * @return an HtmlTree object for the TD tag
   1.602 +     */
   1.603 +    public static HtmlTree TD(HtmlStyle styleClass, Content body) {
   1.604 +        HtmlTree htmltree = new HtmlTree(HtmlTag.TD, nullCheck(body));
   1.605 +        if (styleClass != null)
   1.606 +            htmltree.addStyle(styleClass);
   1.607 +        return htmltree;
   1.608 +    }
   1.609 +
   1.610 +    /**
   1.611 +     * Generates a TD tag for an HTML table with some content.
   1.612 +     *
   1.613 +     * @param body content for the tag
   1.614 +     * @return an HtmlTree object for the TD tag
   1.615 +     */
   1.616 +    public static HtmlTree TD(Content body) {
   1.617 +        return TD(null, body);
   1.618 +    }
   1.619 +
   1.620 +    /**
   1.621 +     * Generates a TH tag with style class and scope attributes and some content.
   1.622 +     *
   1.623 +     * @param styleClass style for the tag
   1.624 +     * @param scope scope of the tag
   1.625 +     * @param body content for the tag
   1.626 +     * @return an HtmlTree object for the TH tag
   1.627 +     */
   1.628 +    public static HtmlTree TH(HtmlStyle styleClass, String scope, Content body) {
   1.629 +        HtmlTree htmltree = new HtmlTree(HtmlTag.TH, nullCheck(body));
   1.630 +        if (styleClass != null)
   1.631 +            htmltree.addStyle(styleClass);
   1.632 +        htmltree.addAttr(HtmlAttr.SCOPE, nullCheck(scope));
   1.633 +        return htmltree;
   1.634 +    }
   1.635 +
   1.636 +    /**
   1.637 +     * Generates a TH tag with scope attribute and some content.
   1.638 +     *
   1.639 +     * @param scope scope of the tag
   1.640 +     * @param body content for the tag
   1.641 +     * @return an HtmlTree object for the TH tag
   1.642 +     */
   1.643 +    public static HtmlTree TH(String scope, Content body) {
   1.644 +        return TH(null, scope, body);
   1.645 +    }
   1.646 +
   1.647 +    /**
   1.648 +     * Generates a TITLE tag with some content.
   1.649 +     *
   1.650 +     * @param body content for the tag
   1.651 +     * @return an HtmlTree object for the TITLE tag
   1.652 +     */
   1.653 +    public static HtmlTree TITLE(Content body) {
   1.654 +        HtmlTree htmltree = new HtmlTree(HtmlTag.TITLE, nullCheck(body));
   1.655 +        return htmltree;
   1.656 +    }
   1.657 +
   1.658 +    /**
   1.659 +     * Generates a TR tag for an HTML table with some content.
   1.660 +     *
   1.661 +     * @param body content for the tag
   1.662 +     * @return an HtmlTree object for the TR tag
   1.663 +     */
   1.664 +    public static HtmlTree TR(Content body) {
   1.665 +        HtmlTree htmltree = new HtmlTree(HtmlTag.TR, nullCheck(body));
   1.666 +        return htmltree;
   1.667 +    }
   1.668 +
   1.669 +    /**
   1.670 +     * Generates a UL tag with the style class attribute and some content.
   1.671 +     *
   1.672 +     * @param styleClass style for the tag
   1.673 +     * @param body content for the tag
   1.674 +     * @return an HtmlTree object for the UL tag
   1.675 +     */
   1.676 +    public static HtmlTree UL(HtmlStyle styleClass, Content body) {
   1.677 +        HtmlTree htmltree = new HtmlTree(HtmlTag.UL, nullCheck(body));
   1.678 +        htmltree.addStyle(nullCheck(styleClass));
   1.679 +        return htmltree;
   1.680 +    }
   1.681 +
   1.682 +    /**
   1.683 +     * {@inheritDoc}
   1.684 +     */
   1.685 +    public boolean isEmpty() {
   1.686 +        return (!hasContent() && !hasAttrs());
   1.687 +    }
   1.688 +
   1.689 +    /**
   1.690 +     * Returns true if the HTML tree has content.
   1.691 +     *
   1.692 +     * @return true if the HTML tree has content else return false
   1.693 +     */
   1.694 +    public boolean hasContent() {
   1.695 +        return (!content.isEmpty());
   1.696 +    }
   1.697 +
   1.698 +    /**
   1.699 +     * Returns true if the HTML tree has attributes.
   1.700 +     *
   1.701 +     * @return true if the HTML tree has attributes else return false
   1.702 +     */
   1.703 +    public boolean hasAttrs() {
   1.704 +        return (!attrs.isEmpty());
   1.705 +    }
   1.706 +
   1.707 +    /**
   1.708 +     * Returns true if the HTML tree has a specific attribute.
   1.709 +     *
   1.710 +     * @param attrName name of the attribute to check within the HTML tree
   1.711 +     * @return true if the HTML tree has the specified attribute else return false
   1.712 +     */
   1.713 +    public boolean hasAttr(HtmlAttr attrName) {
   1.714 +        return (attrs.containsKey(attrName));
   1.715 +    }
   1.716 +
   1.717 +    /**
   1.718 +     * Returns true if the HTML tree is valid. This check is more specific to
   1.719 +     * standard doclet and not exactly similar to W3C specifications. But it
   1.720 +     * ensures HTML validation.
   1.721 +     *
   1.722 +     * @return true if the HTML tree is valid
   1.723 +     */
   1.724 +    public boolean isValid() {
   1.725 +        switch (htmlTag) {
   1.726 +            case A :
   1.727 +                return (hasAttr(HtmlAttr.NAME) || (hasAttr(HtmlAttr.HREF) && hasContent()));
   1.728 +            case BR :
   1.729 +                return (!hasContent() && (!hasAttrs() || hasAttr(HtmlAttr.CLEAR)));
   1.730 +            case FRAME :
   1.731 +                return (hasAttr(HtmlAttr.SRC) && !hasContent());
   1.732 +            case HR :
   1.733 +                return (!hasContent());
   1.734 +            case IMG :
   1.735 +                return (hasAttr(HtmlAttr.SRC) && hasAttr(HtmlAttr.ALT) && !hasContent());
   1.736 +            case LINK :
   1.737 +                return (hasAttr(HtmlAttr.HREF) && !hasContent());
   1.738 +            case META :
   1.739 +                return (hasAttr(HtmlAttr.CONTENT) && !hasContent());
   1.740 +            default :
   1.741 +                return hasContent();
   1.742 +        }
   1.743 +    }
   1.744 +
   1.745 +    /**
   1.746 +     * Returns true if the element is an inline element.
   1.747 +     *
   1.748 +     * @return true if the HTML tag is an inline element
   1.749 +     */
   1.750 +    public boolean isInline() {
   1.751 +        return (htmlTag.blockType == HtmlTag.BlockType.INLINE);
   1.752 +    }
   1.753 +
   1.754 +    /**
   1.755 +     * {@inheritDoc}
   1.756 +     */
   1.757 +    public void write(StringBuilder contentBuilder) {
   1.758 +        if (!isInline() && !endsWithNewLine(contentBuilder))
   1.759 +            contentBuilder.append("\n");
   1.760 +        String tagString = htmlTag.toString();
   1.761 +        contentBuilder.append("<" + tagString);
   1.762 +        Iterator<HtmlAttr> iterator = attrs.keySet().iterator();
   1.763 +        HtmlAttr key;
   1.764 +        String value = "";
   1.765 +        while (iterator.hasNext()) {
   1.766 +            key = iterator.next();
   1.767 +            value = attrs.get(key);
   1.768 +            contentBuilder.append(" " + key.toString());
   1.769 +            if (!value.isEmpty())
   1.770 +                contentBuilder.append("=\"" + value + "\"");
   1.771 +        }
   1.772 +        contentBuilder.append(">");
   1.773 +        for (Content c : content)
   1.774 +            c.write(contentBuilder);
   1.775 +        if (htmlTag.endTagRequired())
   1.776 +            contentBuilder.append("</" + tagString + ">");
   1.777 +        if (!isInline())
   1.778 +            contentBuilder.append("\n");
   1.779 +    }
   1.780 +}

mercurial