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

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

author
jjg
date
Tue, 14 May 2013 10:14:54 -0700
changeset 1742
7af0fa419a2b
parent 1737
7a9ef837e57f
child 1746
bd51ca92c013
permissions
-rw-r--r--

8012174: {@literal} and {@code} should use \"new\" Taglet, not old.
Reviewed-by: darcy

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

mercurial