HTMLElement.java

    1/*******************************************************************************
    2 * Copyright (c) 2009, 2010 Mountainminds GmbH & Co. KG and Contributors
    3 * All rights reserved. This program and the accompanying materials
    4 * are made available under the terms of the Eclipse Public License v1.0
    5 * which accompanies this distribution, and is available at
    6 * http://www.eclipse.org/legal/epl-v10.html
    7 *
    8 * Contributors:
    9 *    Marc R. Hoffmann - initial API and implementation
   10 *    
   11 *******************************************************************************/
   12package org.jacoco.report.html;
   13
   14import java.io.IOException;
   15import java.io.Writer;
   16
   17import org.jacoco.report.ReportOutputFolder;
   18import org.jacoco.report.xml.XMLElement;
   19
   20/**
   21 * A {@link XMLElement} with utility methods to create XHTML documents. It
   22 * provides methods of HTML tags to avoid magic strings in the generators.
   23 * 
   24 * @author Marc R. Hoffmann
   25 * @version 0.4.1.20101007204400
   26 */
   27public class HTMLElement extends XMLElement {
   28
   29    /**
   30     * Creates a new element for a HTML document.
   31     * 
   32     * @param writer
   33     *            all output will be written directly to this
   34     * @param name
   35     *            element name
   36     */
   37    protected HTMLElement(final Writer writer, final String name) {
   38        super(writer, name);
   39    }
   40
   41    @Override
   42    public HTMLElement element(final String name) throws IOException {
   43        final HTMLElement element = new HTMLElement(writer, name);
   44        addChildElement(element);
   45        return element;
   46    }
   47
   48    /**
   49     * Creates a 'meta' element.
   50     * 
   51     * @param httpequivattr
   52     *            value of the http-equiv attribute
   53     * @param contentattr
   54     *            value for the content attribute
   55     * @return 'meta' element
   56     * @throws IOException
   57     *             in case of problems with the writer
   58     */
   59    public HTMLElement meta(final String httpequivattr, final String contentattr)
   60            throws IOException {
   61        final HTMLElement meta = element("meta");
   62        meta.attr("http-equiv", httpequivattr);
   63        meta.attr("content", contentattr);
   64        return meta;
   65    }
   66
   67    /**
   68     * Creates a 'link' element.
   69     * 
   70     * @param relattr
   71     *            value of the rel attribute
   72     * @param hrefattr
   73     *            value for the href attribute
   74     * @param typeattr
   75     *            value for the type attribute
   76     * @return 'link' element
   77     * @throws IOException
   78     *             in case of problems with the writer
   79     */
   80    public HTMLElement link(final String relattr, final String hrefattr,
   81            final String typeattr) throws IOException {
   82        final HTMLElement link = element("link");
   83        link.attr("rel", relattr);
   84        link.attr("href", hrefattr);
   85        link.attr("type", typeattr);
   86        return link;
   87    }
   88
   89    /**
   90     * Creates a 'title' element.
   91     * 
   92     * @return 'title' element
   93     * @throws IOException
   94     *             in case of problems with the writer
   95     */
   96    public HTMLElement title() throws IOException {
   97        return element("title");
   98    }
   99
  100    /**
  101     * Creates a 'h1' element.
  102     * 
  103     * @return 'h1' element
  104     * @throws IOException
  105     *             in case of problems with the writer
  106     */
  107    public HTMLElement h1() throws IOException {
  108        return element("h1");
  109    }
  110
  111    /**
  112     * Creates a 'p' element.
  113     * 
  114     * @return 'p' element
  115     * @throws IOException
  116     *             in case of problems with the writer
  117     */
  118    public HTMLElement p() throws IOException {
  119        return element("p");
  120    }
  121
  122    /**
  123     * Creates a 'span' element.
  124     * 
  125     * @return 'span' element
  126     * @throws IOException
  127     *             in case of problems with the writer
  128     */
  129    public HTMLElement span() throws IOException {
  130        return element("span");
  131    }
  132
  133    /**
  134     * Creates a 'span' element.
  135     * 
  136     * @param classattr
  137     *            value of the class attribute
  138     * @return 'span' element
  139     * @throws IOException
  140     *             in case of problems with the writer
  141     */
  142    public HTMLElement span(final String classattr) throws IOException {
  143        final HTMLElement span = span();
  144        span.attr("class", classattr);
  145        return span;
  146    }
  147
  148    /**
  149     * Creates a 'span' element.
  150     * 
  151     * @param classattr
  152     *            value of the class attribute
  153     * @param idattr
  154     *            value of the id attribute
  155     * @return 'span' element
  156     * @throws IOException
  157     *             in case of problems with the writer
  158     */
  159    public HTMLElement span(final String classattr, final String idattr)
  160            throws IOException {
  161        final HTMLElement span = span(classattr);
  162        span.attr("id", idattr);
  163        return span;
  164    }
  165
  166    /**
  167     * Creates a 'div' element.
  168     * 
  169     * @param classattr
  170     *            value of the class attribute
  171     * @return 'div' element
  172     * @throws IOException
  173     *             in case of problems with the writer
  174     */
  175    public HTMLElement div(final String classattr) throws IOException {
  176        final HTMLElement div = element("div");
  177        div.attr("class", classattr);
  178        return div;
  179    }
  180
  181    /**
  182     * Creates a 'code' element.
  183     * 
  184     * @return 'code' element
  185     * @throws IOException
  186     *             in case of problems with the writer
  187     */
  188    public HTMLElement code() throws IOException {
  189        return element("code");
  190    }
  191
  192    /**
  193     * Creates a 'pre' element.
  194     * 
  195     * @param classattr
  196     *            value of the class attribute
  197     * @return 'pre' element
  198     * @throws IOException
  199     *             in case of problems with the writer
  200     */
  201    public HTMLElement pre(final String classattr) throws IOException {
  202        final HTMLElement pre = element("pre");
  203        pre.attr("class", classattr);
  204        return pre;
  205    }
  206
  207    /**
  208     * Creates a 'a' element.
  209     * 
  210     * @param hrefattr
  211     *            value of the href attribute
  212     * @return 'a' element
  213     * @throws IOException
  214     *             in case of problems with the writer
  215     */
  216    public HTMLElement a(final String hrefattr) throws IOException {
  217        final HTMLElement a = element("a");
  218        a.attr("href", hrefattr);
  219        return a;
  220    }
  221
  222    /**
  223     * Creates a 'a' element.
  224     * 
  225     * @param hrefattr
  226     *            value of the href attribute
  227     * @param classattr
  228     *            value of the class attribute
  229     * @return 'a' element
  230     * @throws IOException
  231     *             in case of problems with the writer
  232     */
  233    public HTMLElement a(final String hrefattr, final String classattr)
  234            throws IOException {
  235        final HTMLElement a = a(hrefattr);
  236        a.attr("class", classattr);
  237        return a;
  238    }
  239
  240    /**
  241     * Creates a link to the given {@link ILinkable}.
  242     * 
  243     * @param linkable
  244     *            object to link to
  245     * @param base
  246     *            base folder where the link should be placed
  247     * @return 'a' element or 'span' element, if the link target does not exist
  248     * @throws IOException
  249     *             in case of problems with the writer
  250     */
  251    public HTMLElement a(final ILinkable linkable, final ReportOutputFolder base)
  252            throws IOException {
  253        final HTMLElement a;
  254        final String link = linkable.getLink(base);
  255        if (link == null) {
  256            a = span(linkable.getLinkStyle());
  257        } else {
  258            a = a(link, linkable.getLinkStyle());
  259        }
  260        a.text(linkable.getLinkLabel());
  261        return a;
  262    }
  263
  264    /**
  265     * Creates a 'table' element.
  266     * 
  267     * @param classattr
  268     *            value of the class attribute
  269     * @return 'table' element
  270     * @throws IOException
  271     *             in case of problems with the writer
  272     */
  273    public HTMLElement table(final String classattr) throws IOException {
  274        final HTMLElement table = element("table");
  275        table.attr("class", classattr);
  276        table.attr("cellspacing", "0");
  277        return table;
  278    }
  279
  280    /**
  281     * Creates a 'thead' element.
  282     * 
  283     * @return 'thead' element
  284     * @throws IOException
  285     *             in case of problems with the writer
  286     */
  287    public HTMLElement thead() throws IOException {
  288        return element("thead");
  289    }
  290
  291    /**
  292     * Creates a 'tfoot' element.
  293     * 
  294     * @return 'tfoot' element
  295     * @throws IOException
  296     *             in case of problems with the writer
  297     */
  298    public HTMLElement tfoot() throws IOException {
  299        return element("tfoot");
  300    }
  301
  302    /**
  303     * Creates a 'tbody' element.
  304     * 
  305     * @return 'tbody' element
  306     * @throws IOException
  307     *             in case of problems with the writer
  308     */
  309    public HTMLElement tbody() throws IOException {
  310        return element("tbody");
  311    }
  312
  313    /**
  314     * Creates a 'tr' element.
  315     * 
  316     * @return 'tr' element
  317     * @throws IOException
  318     *             in case of problems with the writer
  319     */
  320    public HTMLElement tr() throws IOException {
  321        return element("tr");
  322    }
  323
  324    /**
  325     * Creates a 'td' element.
  326     * 
  327     * @return 'td' element
  328     * @throws IOException
  329     *             in case of problems with the writer
  330     */
  331    public HTMLElement td() throws IOException {
  332        return element("td");
  333    }
  334
  335    /**
  336     * Creates a 'td' element.
  337     * 
  338     * @param classattr
  339     *            value of the class attribute
  340     * @return 'td' element
  341     * @throws IOException
  342     *             in case of problems with the writer
  343     */
  344    public HTMLElement td(final String classattr) throws IOException {
  345        final HTMLElement td = td();
  346        td.attr("class", classattr);
  347        return td;
  348    }
  349
  350    /**
  351     * Creates a 'img' element.
  352     * 
  353     * @param srcattr
  354     *            value of the src attribute
  355     * @param widthattr
  356     *            value of the width attribute
  357     * @param heightattr
  358     *            value of the height attribute
  359     * @param titleattr
  360     *            value of the title and alt attribute
  361     * @throws IOException
  362     *             in case of problems with the writer
  363     */
  364    public void img(final String srcattr, final int widthattr,
  365            final int heightattr, final String titleattr) throws IOException {
  366        final HTMLElement img = element("img");
  367        img.attr("src", srcattr);
  368        img.attr("width", widthattr);
  369        img.attr("height", heightattr);
  370        img.attr("title", titleattr);
  371        img.attr("alt", titleattr);
  372        img.close();
  373    }
  374
  375    /**
  376     * Creates a 'script' element.
  377     * 
  378     * @param typeattr
  379     *            value of the type attribute
  380     * @param srcattr
  381     *            value of the src attribute
  382     * @throws IOException
  383     *             in case of problems with the writer
  384     */
  385    public void script(final String typeattr, final String srcattr)
  386            throws IOException {
  387        final HTMLElement script = element("script");
  388        script.attr("type", typeattr);
  389        script.attr("src", srcattr);
  390        // Enforce open and closing tag otherwise it won't work in browsers:
  391        script.text("");
  392        script.close();
  393    }
  394
  395}