Class Element

java.lang.Object
org.ujorm.tools.web.Element
All Implemented Interfaces:
Closeable, AutoCloseable, Html, ApiElement<Element>

public final class Element extends Object implements ApiElement<Element>, Html
A HTML Element implements some methods for frequently used elements and attributes

Usage

    ServletResponse response = new ServletResponse();
    try (HtmlElement html = HtmlElement.of(response)) {
        try (Element body = html.getBody()) {
            body.addHeading("Hello!");
        }
    }
    assertTrue(response.toString().contains("<h1>Hello!</h1>"));
 
See Also:
  • Method Details

    • getName

      @NotNull public @NotNull String getName()
      Description copied from interface: ApiElement
      Get an element name
      Specified by:
      getName in interface ApiElement<Element>
    • setAttribute

      @NotNull public @NotNull Element setAttribute(@NotNull @NotNull String name, @Nullable @Nullable Object value)
      Set an attribute
      Specified by:
      setAttribute in interface ApiElement<Element>
      Parameters:
      name - Required element name
      value - The null value is silently ignored. Formatting is performed by the AbstractWriter.writeValue(Object, ApiElement, String) method, where the default implementation calls a toString() only.
      Returns:
      The original element
    • setAttributes

      @NotNull public @NotNull Element setAttributes(@NotNull @NotNull String name, @NotNull @NotNull CharSequence separator, @NotNull @NotNull Object... value)
      Set an attribute
      Parameters:
      name - Required element name
      value - The null value is silently ignored. Formatting is performed by the AbstractWriter.writeValue(Object, ApiElement, String) method, where the default implementation calls a toString() only.
      Returns:
      The original element
    • setAttribute

      @NotNull public @NotNull Element setAttribute(@NotNull @NotNull String name)
      Set an attribute with no value
      Parameters:
      name - Required element name
      Returns:
      The original element
    • setAttr

      @NotNull public @NotNull Element setAttr(@NotNull @NotNull String name, @Nullable @Nullable Object value)
      Parameters:
      name - Required element name
      value - The null value is silently ignored. Formatting is performed by the AbstractWriter.writeValue(Object, ApiElement, String) method, where the default implementation calls a toString() only.
      Returns:
      The original element
    • addText

      @NotNull public @NotNull Element addText(Object data) throws IllegalStateException
      Add simple text
      Specified by:
      addText in interface ApiElement<Element>
      Parameters:
      data - Text item
      Returns:
      A parent element.
      Throws:
      IllegalStateException
      See Also:
    • addText

      @NotNull public @NotNull Element addText(@NotNull @NotNull Object... data) throws IllegalStateException
      Add many texts with no separator
      Parameters:
      data - Text items
      Returns:
      A parent element.
      Throws:
      IllegalStateException
      See Also:
    • addTextTemplated

      @NotNull public @NotNull Element addTextTemplated(CharSequence template, Object... values)
      Add a template based text with parameters with hight performance.
      Specified by:
      addTextTemplated in interface ApiElement<Element>
      Parameters:
      template - A message template with an ENGLISH locale. See String.format(String, Object...)) for more parameters.
      values - A template parameters
      Returns:
      A parent element.
    • addTexts

      public Element addTexts(@NotNull @NotNull CharSequence separator, @NotNull @NotNull Object... data) throws IllegalStateException
      Add many words separated by a delimeter
      Parameters:
      separator - The delimiter must contain no special HTML character.
      data - Data to print
      Returns:
      The current element
      Throws:
      IllegalStateException
    • addRawText

      @NotNull public @NotNull Element addRawText(@Nullable @Nullable Object data) throws IllegalStateException
      Description copied from interface: ApiElement
      Add an native text with no escaped characters, for example: XML code, JavaScript, CSS styles
      Specified by:
      addRawText in interface ApiElement<Element>
      Parameters:
      data - The null value is ignored.
      Returns:
      This instance
      Throws:
      IllegalStateException
    • addRawText

      @NotNull public @NotNull Element addRawText(@NotNull @NotNull Object... data) throws IllegalStateException
      Throws:
      IllegalStateException
    • addRawTexts

      public Element addRawTexts(@NotNull @NotNull CharSequence separator, @NotNull @NotNull Object... data) throws IllegalStateException
      Add many words separated by a delimeter
      Parameters:
      separator - The delimiter must contain no special HTML character.
      data - Data to print
      Returns:
      The current element
      Throws:
      IllegalStateException
    • addComment

      @NotNull public @NotNull Element addComment(CharSequence comment) throws IllegalStateException
      Description copied from interface: ApiElement
      Add a comment text. The CDATA structure isn't really for HTML at all.
      Specified by:
      addComment in interface ApiElement<Element>
      Parameters:
      comment - A comment text must not contain a string --> .
      Returns:
      This instance
      Throws:
      IllegalStateException
    • addCDATA

      @NotNull public @NotNull Element addCDATA(CharSequence charData) throws IllegalStateException
      Description copied from interface: ApiElement
      Add a character data in CDATA format to XML only. The CDATA structure isn't really for HTML at all.
      Specified by:
      addCDATA in interface ApiElement<Element>
      Parameters:
      charData - A text including the final DATA sequence. An empty argument is ignored.
      Returns:
      This instance
      Throws:
      IllegalStateException
    • close

      @NotNull public void close() throws IllegalStateException
      Description copied from interface: ApiElement
      Close the element
      Specified by:
      close in interface ApiElement<Element>
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IllegalStateException
    • addElement

      @NotNull public @NotNull Element addElement(@NotNull @NotNull String name) throws IllegalStateException
      Create new Element
      Specified by:
      addElement in interface ApiElement<Element>
      Parameters:
      name - The element name
      Returns:
      New instance of the Element
      Throws:
      IllegalStateException - An envelope for IO exceptions
    • addElement

      @NotNull public @NotNull Element addElement(@NotNull @NotNull String name, @NotNull @NotNull CharSequence... cssClasses)
      Add a new Element with optional CSS classes
      Parameters:
      name - A required name of the element
      cssClasses - Optional CSS classes.
      Returns:
      New instance of the Element
    • addElementIf

      @NotNull public @NotNull Element addElementIf(boolean enabled, @NotNull @NotNull String name, @NotNull @NotNull CharSequence... cssClasses)
      Add an element according to a condition.
      Parameters:
      enabled - A condition for rendering the element.
      name - An element name
      cssClasses - CSS classes
      Returns:
      New instance of the Element
    • addTable

      @NotNull public @NotNull Element addTable(@NotNull @NotNull CharSequence... cssClasses)
      Add new Table
    • addTable

      @NotNull public @NotNull Element addTable(@NotNull @NotNull Object[][] data, @NotNull @NotNull CharSequence... cssClass)
      Create a HTML table according to data
    • addTable

      @NotNull public @NotNull Element addTable(@NotNull @NotNull Collection<Object[]> data, @NotNull @NotNull CharSequence... cssClass)
      Create a HTML table according to data
    • addTable

      @NotNull public <D, V> @NotNull Element addTable(@NotNull @NotNull Stream<D> domains, @Nullable @Nullable CharSequence[] cssClass, @Nullable @Nullable Object[] headers, @NotNull @NotNull Function<D,V>... attributes)
      Create a HTML table according to data

      Usage

       element.addTable(getCars(), cssClasses, titles,
               Car::getId,
               Car::getName,
               Car::getEnabled);
       
    • addImage

      @NotNull public @NotNull Element addImage(@NotNull @NotNull CharSequence imageLink, @NotNull @NotNull CharSequence alt, @NotNull @NotNull CharSequence... cssClasses) throws IllegalStateException
      Add a link to an image
      Parameters:
      imageLink - A link to image
      alt - An alternate text
      cssClasses - Optional CSS classes
      Returns:
      Throws:
      IllegalStateException
    • addImage

      @NotNull public @NotNull Element addImage(@NotNull @NotNull InputStream imageStream, @NotNull @NotNull CharSequence alt, @NotNull @NotNull CharSequence... cssClasses) throws IllegalStateException
      Add an embeded image
      Parameters:
      imageStream - Stream provides a PNG image and it will be closed after reading.
      alt - An alternate text
      cssClasses - Optional CSS classes
      Returns:
      Throws:
      IllegalStateException
    • addBody

      @NotNull public @NotNull Element addBody(@NotNull @NotNull CharSequence... cssClasses)
      Add new body element
    • addTitle

      @NotNull public @NotNull Element addTitle(@NotNull @NotNull CharSequence... cssClasses)
      Add new title element
    • addLink

      @NotNull public @NotNull Element addLink(@NotNull @NotNull CharSequence... cssClasses)
      Add new link element
    • addStyle

      @NotNull public @NotNull Element addStyle(@NotNull @NotNull CharSequence... cssClasses)
      Add new style element
    • addScript

      @NotNull public @NotNull Element addScript(@NotNull @NotNull CharSequence... cssClasses)
      Add new script element
    • addDiv

      @NotNull public @NotNull Element addDiv(@NotNull @NotNull CharSequence... cssClasses)
      Add new div element
    • addFieldset

      @NotNull public @NotNull Element addFieldset(@Nullable @Nullable String title, @NotNull @NotNull CharSequence... cssClasses)
      Add new fieldset element including a title
      Parameters:
      title - An optional title
      cssClasses - CSS classes
      Returns:
      An instance of FieldSet
      See Also:
    • addPreformatted

      @NotNull public @NotNull Element addPreformatted(@NotNull @NotNull CharSequence... cssClasses)
      Add new pre element
    • addSpan

      @NotNull public @NotNull Element addSpan(@NotNull @NotNull CharSequence... cssClasses)
      Add new span element
    • addParagraph

      @NotNull public @NotNull Element addParagraph(@NotNull @NotNull CharSequence... cssClasses)
      Add new paragram element
    • addForm

      @NotNull public @NotNull Element addForm(@NotNull @NotNull CharSequence... cssClasses)
      Add new form element
    • addHeading

      @NotNull public @NotNull Element addHeading(@NotNull @NotNull CharSequence title, @NotNull @NotNull CharSequence... cssClasses)
      Add a top heading (level one)
    • addHeading

      @NotNull public @NotNull Element addHeading(int level, @NotNull @NotNull CharSequence title, @NotNull @NotNull CharSequence... cssClasses)
      Add new heading with the required level where the first level is the one,
    • addTableHead

      @NotNull public @NotNull Element addTableHead(@NotNull @NotNull CharSequence... cssClasses)
      Add new head of table element
    • addTableRow

      @NotNull public @NotNull Element addTableRow(@NotNull @NotNull CharSequence... cssClasses)
      Add new table row element
    • addTableDetail

      @NotNull public @NotNull Element addTableDetail(@NotNull @NotNull CharSequence... cssClasses)
      Add new detail of table element
    • addLabel

      @NotNull public @NotNull Element addLabel(@NotNull @NotNull CharSequence... cssClasses)
      Add new label element
    • addInput

      @NotNull public @NotNull Element addInput(@NotNull @NotNull CharSequence... cssClasses)
      Add new input element
    • addTextInput

      @NotNull public @NotNull Element addTextInput(@NotNull @NotNull CharSequence... cssClasses)
      Add new input element type of text
    • addTextInp

      @NotNull public <V> @NotNull Element addTextInp(@NotNull @NotNull HttpParameter param, @Nullable V value, @NotNull @NotNull CharSequence title, @NotNull @NotNull CharSequence... cssClasses)
      Add new input element type of text including attributes: name, value, placeholder and title
    • addPasswordInput

      @NotNull public @NotNull Element addPasswordInput(@NotNull @NotNull CharSequence... cssClasses)
      Add a new password input element
    • addHiddenInput

      @NotNull public @NotNull Element addHiddenInput(@Nullable @Nullable CharSequence name, @Nullable @Nullable Object value)
      Add a new hidden input element with a name & value
    • addTextArea

      @NotNull public @NotNull Element addTextArea(@NotNull @NotNull CharSequence... cssClasses)
      Add new text area element
    • addSelect

      @NotNull public @NotNull Element addSelect(@NotNull @NotNull CharSequence... cssClasses)
      Add new select element
      See Also:
    • addSelectOptions

      @NotNull public @NotNull Element addSelectOptions(@NotNull @NotNull Object value, @NotNull @NotNull Map<?,?> options, @NotNull @NotNull CharSequence... cssClasses)
      Add options from map to current select element
      Parameters:
      value - Value of a select element
      options - Consider an instance of the LinkedHashMap class predictable iteration order of options.
      cssClasses -
      Returns:
      Return this
      See Also:
    • addOption

      @NotNull public @NotNull Element addOption(@NotNull @NotNull CharSequence... cssClasses)
      Add new option element
    • addButton

      @NotNull public @NotNull Element addButton(@NotNull @NotNull CharSequence... cssClasses)
      Add new button element
    • addSubmitButton

      @NotNull public @NotNull Element addSubmitButton(@NotNull @NotNull CharSequence... cssClasses)
      Add a submit button
    • addAnchor

      @NotNull public @NotNull Element addAnchor(@NotNull @NotNull String url, @NotNull @NotNull CharSequence... cssClasses)
      Add an anchor element with URL and CSS classes
    • addLinkedText

      @NotNull public @NotNull Element addLinkedText(@NotNull @NotNull String url, @NotNull @NotNull Object... text)
      Add a
      Parameters:
      url -
      text -
      Returns:
      The original element!
    • addUnorderedlist

      @NotNull public @NotNull Element addUnorderedlist(@NotNull @NotNull CharSequence... cssClasses)
      Add new unordered list element
    • addOrderedList

      @NotNull public @NotNull Element addOrderedList(@NotNull @NotNull CharSequence... cssClasses)
      Add new ordered list element
    • addListItem

      @NotNull public @NotNull Element addListItem(@NotNull @NotNull CharSequence... cssClasses)
    • setClass

      @NotNull public @NotNull Element setClass(@NotNull @NotNull CharSequence... cssClasses)
      Set a CSS class attribute optionally, the empty attribute is ignored.
      Parameters:
      cssClasses - Optional CSS classes. The css item is ignored when the value is empty or null.
      Returns:
      The current instanlce
    • addBreak

      @NotNull public @NotNull Element addBreak(@NotNull @NotNull CharSequence... cssClasses)
      Add a line break
    • createHtmlRoot

      @NotNull public static @NotNull Element createHtmlRoot(@NotNull @NotNull Object title, @Nullable @Nullable CharSequence... cssLinks)
      Crate a root element
      Parameters:
      cssLinks - Nullable CSS link array
    • createHtmlRoot

      @NotNull public static @NotNull Element createHtmlRoot(@NotNull @NotNull Object title, @Nullable @Nullable Charset charset, @Nullable @Nullable CharSequence... cssLinks)
      Crate a root element
      Parameters:
      title - A HTML title
      charset - A charset
      cssLinks - Nullable CSS link array
    • setId

      @NotNull public @NotNull Element setId(@Nullable @Nullable CharSequence value)
      Set an identifier of the element
    • setMethod

      @NotNull public @NotNull Element setMethod(@Nullable @Nullable Object value)
      Set a method of form
    • setAction

      @NotNull public @NotNull Element setAction(@Nullable @Nullable Object value)
      Set an action type of from
    • setType

      @NotNull public @NotNull Element setType(@Nullable @Nullable Object value)
      Set a type of input element
    • setName

      @NotNull public @NotNull Element setName(@Nullable @Nullable CharSequence value)
      Set an name of input element
    • setValue

      @NotNull public @NotNull Element setValue(@Nullable @Nullable Object value)
      Set an value of input element
    • setNameValue

      @NotNull public @NotNull Element setNameValue(@Nullable @Nullable CharSequence name, @Nullable @Nullable Object value)
      Set name & value to the input element
    • setFor

      @NotNull public @NotNull Element setFor(@Nullable @Nullable CharSequence value)
      Set an value of input element
    • setRows

      @NotNull public @NotNull Element setRows(@Nullable @org.jetbrains.annotations.Nullable int value)
      Row count of a text area
    • setCols

      @NotNull public @NotNull Element setCols(@Nullable @Nullable Object value)
      Column count of a text area
    • setColSpan

      @NotNull public @NotNull Element setColSpan(@Nullable @org.jetbrains.annotations.Nullable int value)
      Column span inside the table
    • setRowSpan

      @NotNull public @NotNull Element setRowSpan(@Nullable @org.jetbrains.annotations.Nullable int value)
      Row span inside the table
    • setHref

      @NotNull public @NotNull Element setHref(@Nullable @Nullable CharSequence value)
      Set hyperlink reference
    • setHint

      @NotNull public @NotNull Element setHint(@Nullable @Nullable CharSequence value)
      Set a placeholder name
    • then

      @NotNull public @NotNull ExceptionProvider then(@NotNull @NotNull Consumer<Element> builder)
      Deprecated.
      Use the method next(Consumer) rather.
      Apply body of element by a lambda expression.
    • next

      @NotNull public @NotNull ExceptionProvider next(@NotNull @NotNull Consumer<Element> builder)
      Add nested elements to the element.

      Usage

        HtmlElement.of(config, writer).addBody()
            .next(body -> {
               body.addHeading(config.getTitle());
            })
            .catche(e -> {
                logger.log(Level.SEVERE, "An error", e);
            });
       
    • toString

      @NotNull public @NotNull String toString()
      String value
      Overrides:
      toString in class Object
    • of

      @NotNull public static @NotNull Element of(@NotNull @NotNull ApiElement original)
      New element for an API element