Class AbstractHtmlElement

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

public abstract class AbstractHtmlElement extends Object implements ApiElement<Element>, Html
The root of HTML elements is independent on the Servlet API

Usage

    ServletResponse response = new ServletResponse();
    try (HtmlElement html = HtmlElement.of(response)) {
        html.addBody().addHeading("Hello!");
    }
    assertTrue(response.toString().contains("<h1>Hello!</h1>"));
 
For more information, see the next sample.
  • Constructor Details

    • AbstractHtmlElement

      public AbstractHtmlElement(@NotNull @NotNull HtmlConfig config, @NotNull @NotNull Appendable writer)
      Create new instance with empty HTML headers
    • AbstractHtmlElement

      public AbstractHtmlElement(@NotNull @NotNull ApiElement root, @NotNull @NotNull HtmlConfig config, @NotNull @NotNull Appendable writer)
      Create new instance with empty HTML headers
  • 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

      public Element setAttribute(String name, Object value)
      Description copied from interface: ApiElement
      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
    • addText

      public Element addText(Object value)
      Description copied from interface: ApiElement
      Add a text and escape special character
      Specified by:
      addText in interface ApiElement<Element>
      Parameters:
      value - The null value is allowed. Formatting is performed by the AbstractWriter.writeValue(Object, ApiElement, String) } method, where the default implementation calls a toString() only.
      Returns:
      This instance
    • addTextTemplated

      public Element addTextTemplated(@NotNull @NotNull CharSequence template, @NotNull @NotNull Object... values)
      Description copied from interface: ApiElement
      Message template
      Specified by:
      addTextTemplated in interface ApiElement<Element>
      Parameters:
      template - Message template where parameters are marked by the {} symbol
      values - argument values
      Returns:
      The original builder
    • addRawText

      public Element addRawText(Object value)
      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:
      value - The null value is ignored.
      Returns:
      This instance
    • addComment

      public Element addComment(CharSequence comment)
      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
    • addCDATA

      public Element addCDATA(CharSequence charData)
      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
    • addElement

      @NotNull public final @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
    • getHead

      public Element getHead()
      Returns a head element
    • addHead

      public Element addHead()
      Returns a head element
    • getBody

      @NotNull public @NotNull Element getBody()
      Returns a body element
    • addBody

      @NotNull public @NotNull Element addBody()
      Returns a body element
    • addJavascriptLinks

      public void addJavascriptLinks(boolean defer, @NotNull @NotNull CharSequence... javascriptLinks)
      Create a new Javascript element and return it
      Parameters:
      javascriptLinks - URL list to Javascript
      defer - A script that will not run until after the page has loaded
    • addJavascriptLink

      public Element addJavascriptLink(boolean defer, @NotNull @NotNull CharSequence javascriptLink)
      Create a new Javascript element and return it
      Parameters:
      javascriptLink - URL to Javascript
      defer - A script that will not run until after the page has loaded
      Returns:
    • addJavascriptBody

      public Element addJavascriptBody(@Nullable @Nullable CharSequence... javascript)
      Create a new Javascript element and return it. Each item is separated by a new line.
      Parameters:
      javascript - Add a javascriptLink link
      Returns:
      New CSS element
    • addCssLinks

      public void addCssLinks(@NotNull @NotNull CharSequence... css)
      Create a new CSS element and return it
      Parameters:
      css - Add a CSS link
    • addCssLink

      public Element addCssLink(@NotNull @NotNull CharSequence css)
      Create a new CSS element and return it
      Parameters:
      css - Add a CSS link
      Returns:
      New CSS element
    • addCssBody

      public Element addCssBody(@NotNull @NotNull CharSequence css)
      Create a new CSS element and return it
      Parameters:
      css - CSS content
      Returns:
      New CSS element
    • addCssBodies

      public Element addCssBodies(@NotNull @NotNull CharSequence lineSeparator, @NotNull @NotNull CharSequence... css)
      Create a new CSS element and return it. Each item is separated by a new line.
      Parameters:
      lineSeparator - Row separator
      css - CSS content rows
      Returns:
      New CSS element
    • original

      @NotNull public @NotNull Element original()
      Get an original root element
    • toString

      @NotNull public @NotNull String toString() throws IllegalStateException
      Returns an Render the HTML code including header. Call the close() method before view
      Overrides:
      toString in class Object
      Throws:
      IllegalStateException
    • close

      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
    • getConfig

      @NotNull public @NotNull HtmlConfig getConfig()
      Get config
    • getTitle

      public CharSequence getTitle()
      Get title of configuration
    • then

      @NotNull public final @NotNull ExceptionProvider then(@NotNull @NotNull Consumer<AbstractHtmlElement> 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<AbstractHtmlElement> 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);
            });
       
    • of

      @NotNull public static @NotNull HtmlElement of(@NotNull @NotNull Appendable writer, @NotNull @NotNull HtmlConfig myConfig) throws IllegalStateException
      Create a root element for a required element name. The MAIN factory method.
      Throws:
      IllegalStateException
    • of

      @NotNull public static @NotNull HtmlElement of(@NotNull @NotNull HttpContext context, @NotNull @NotNull HtmlConfig myConfig)
      Create root element for a required element name. The MAIN factory method.
    • ofServlet

      @NotNull public static @NotNull HtmlElement ofServlet(@NotNull @NotNull Object httpServletResponse, @Nullable @Nullable HtmlConfig config)
      Create a new instance with empty HTML headers. The MAIN servlet factory method.
      Throws:
      IllegalStateException - IO exceptions
      See Also:
    • ofServlet

      @NotNull public static @NotNull HtmlElement ofServlet(@NotNull @NotNull String title, @NotNull @NotNull Object httpServletResponse, @NotNull @NotNull CharSequence... cssLinks)
      Create new instance with empty HTML headers
      Throws:
      IllegalStateException - IO exceptions
      See Also:
    • of

      @NotNull public static @NotNull HtmlElement of(@NotNull @NotNull CharSequence title, @NotNull @NotNull Appendable response, @NotNull @NotNull CharSequence... cssLinks)
      Create new instance with empty HTML headers
      Throws:
      IllegalStateException - IO exceptions
      See Also:
    • of

      @NotNull public static @NotNull HtmlElement of(@NotNull @NotNull CharSequence title, @NotNull @NotNull Appendable response, @NotNull @NotNull Charset charset, @NotNull @NotNull CharSequence... cssLinks)
      Create new instance with empty HTML headers
      Throws:
      IllegalStateException - IO exceptions
      See Also:
    • niceOf

      @NotNull public static @NotNull HtmlElement niceOf(@NotNull @NotNull CharSequence title, @NotNull @NotNull Appendable response, @NotNull @NotNull CharSequence... cssLinks)
      Create new instance with empty HTML headers
      Throws:
      IllegalStateException - IO exceptions
      See Also:
    • niceOf

      @NotNull public static @NotNull HtmlElement niceOf(@NotNull @NotNull CharSequence title, @NotNull @NotNull Appendable response, @NotNull @NotNull Charset charset, @NotNull @NotNull CharSequence... cssLinks)
      Create new instance with empty HTML headers
      Throws:
      IllegalStateException - IO exceptions
      See Also:
    • niceOfResponse

      @NotNull public static @NotNull HtmlElement niceOfResponse(@NotNull @NotNull String title, @NotNull @NotNull Object httpServletResponse, @NotNull @NotNull CharSequence... cssLinks)
      Create new instance with empty HTML headers
      Throws:
      IllegalStateException - IO exceptions
      See Also:
    • niceOfResponse

      @NotNull public static @NotNull HtmlElement niceOfResponse(@NotNull @NotNull Object httpServletResponse, @NotNull @NotNull CharSequence... cssLinks)
      Create new instance with empty HTML headers
      Throws:
      IllegalStateException - IO exceptions
      See Also:
    • niceOf

      @NotNull public static @NotNull HtmlElement niceOf(@NotNull @NotNull String title, @NotNull @NotNull HttpContext context, @NotNull @NotNull CharSequence... cssLinks)
      Create new instance with empty HTML headers
      Throws:
      IllegalStateException - IO exceptions
      See Also:
    • niceOf

      @NotNull public static @NotNull HtmlElement niceOf(@NotNull @NotNull String title, @NotNull @NotNull Appendable response, @NotNull @NotNull CharSequence... cssLinks)
      Create new instance with empty HTML headers
      Throws:
      IllegalStateException - IO exceptions
      See Also:
    • of

      @NotNull public static @NotNull HtmlElement of(@Nullable @Nullable HtmlConfig config) throws IllegalStateException
      Create new instance with empty HTML headers
      Parameters:
      config - Html configuration
      Returns:
      An instance of the HtmlPage
      Throws:
      IllegalStateException - IO exceptions