com.h3c.util

## Class WebUtils.Tag

- java.lang.Object
- - com.h3c.util.WebUtils.Tag

- Direct Known Subclasses:
    HTMLUtils.HTMLTag, HTMLUtils.Page
  Enclosing class:
    WebUtils

      public static class WebUtils.Tag
      extends Object

  A simple class to facilitate building XML or HTML constructs. For example, typical usage might be:

       WebUtils.Tag results = new WebUtils.Tag("results");
       for (Record r: someResultsSet()) {
           WebUtils.Tag result = new WebUtils.Tag("result")
                            .attr("id", r.getId())
                            .attr("status", r.getStatus());
           results.addContent(result);
        }
        ...

  Invoking `results.toString()` might produce something like...

       <results>
         <result id="11" status="normal" />
         <result id="12" status="normal" />
         <result id="13" status="unreachable" />
       </results>

  Note that this class is not intended as a replacement for more complete XML utilities such as JAXB.

  Another example, this time HTML...

       WebUtils.Tag table = new WebUtils.Tag("table").attr("width", "100%");
       WebUtils.Tag tr = new WebUtils.Tag("tr");
       table.addContent(tr);
       WebUtils.Tag td = new WebUtils.Tag("td").addContent("First Cell");
       tr.addContent(td);
       td = new WebUtils.Tag("td").addContent("Second Cell");
       tr.addContent(td);

  Invoking `table.toString()` will produce...

       <table width="100%">
         <tr>
           <td>
             First Cell
           </td>
           <td>
             Second Cell
           </td>
         </tr>
       </table>

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `WebUtils.Tag(String id)` Constructs a tag with the given id.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `WebUtils.Tag` | `addCData(String data)` Adds the specified string as content of this tag, but wrapped in a CDATA construct.
    `WebUtils.Tag` | `addContent(String s)` Adds the specified string as verbatim content of this tag.
    `WebUtils.Tag` | `addContent(String s, boolean textInlining)` Adds the specified string as content of this tag.
    `WebUtils.Tag` | `addContent(String s, boolean textInlining, boolean verbatim)` Adds the specified string as content of this tag.
    `WebUtils.Tag` | `addContent(WebUtils.Tag tag)` Adds the specified tag as a child to this tag.
    `WebUtils.Tag` | `attr(String name, Object value)` Adds an attribute with the given name and value.
    `boolean` | `hasContent()` Returns an indication whether or not the given tag has any content.
    `WebUtils.Tag` | `noShorthand()`
    `String` | `toString()`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### WebUtils.Tag

          public WebUtils.Tag(String id)

      Constructs a tag with the given id.
      Parameters:
        `id` - the id

  - ### Method Detail

    - #### attr

          public WebUtils.Tag attr(String name,
                          Object value)

      Adds an attribute with the given name and value.
      Parameters:
        `name` - the attribute name
        `value` - the attribute value
      Returns:
        self

    - #### noShorthand

          public WebUtils.Tag noShorthand()

    - #### addContent

          public WebUtils.Tag addContent(WebUtils.Tag tag)

      Adds the specified tag as a child to this tag. Note that the child tag inherits the `shorthand` setting of this tag.
      Parameters:
        `tag` - the child tag to be added as content
      Returns:
        self

    - #### addContent

          public WebUtils.Tag addContent(String s,
                                boolean textInlining)

      Adds the specified string as content of this tag. If the textInlining parameter is set true, the textual content will be rendered on the same line as the tag/endtag, for example:

           <tag>content</tag>

      Parameters:
        `s` - the string to be added as content.
        `textInlining` - if true, the text content will be rendered on the same line as the tag/endtag
      Returns:
        self

    - #### addContent

          public WebUtils.Tag addContent(String s,
                                boolean textInlining,
                                boolean verbatim)

      Adds the specified string as content of this tag. If the textInlining parameter is set true, the textual content will be rendered on the same line as the tag/endtag, for example:

           <tag>content</tag>

      Parameters:
        `s` - the string to be added as content.
        `textInlining` - if true, the text content will be rendered on the same line as the tag/endtag
        `verbatim` - if true, the text content will be inserted without any protective encoding
      Returns:
        self

    - #### addContent

          public WebUtils.Tag addContent(String s)

      Adds the specified string as verbatim content of this tag.
      Parameters:
        `s` - the string to be added as content.
      Returns:
        self

    - #### addCData

          public WebUtils.Tag addCData(String data)

      Adds the specified string as content of this tag, but wrapped in a CDATA construct. For example:

           WebUtils.Tag tag = new WebUtils.Tag("record")
                                    .addCData("Some arbitrary data");

      This will result in the following...

           <record>
             <![CDATA[Some arbitrary data]]>
           </record>

      Parameters:
        `data` - the data to be inserted into a CDATA construct
      Returns:
        self

    - #### hasContent

          public boolean hasContent()

      Returns an indication whether or not the given tag has any content.
      Returns:
        true if content has been added; false otherwise

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

Copyright © 2014. All Rights Reserved.