Class XmlBuilder

java.lang.Object
org.ujorm.tools.xml.builder.XmlBuilder
All Implemented Interfaces:
Closeable, AutoCloseable, ApiElement<XmlBuilder>

public class XmlBuilder extends Object implements ApiElement<XmlBuilder>
A XML builder. The main benefits are:
  • secure building well-formed XML documents by the Java code
  • a simple API built on a single XmlElement class
  • creating XML components by a subclass is possible
  • great performance and small memory footprint

How to use the class:

  XmlPriter writer = XmlPriter.forXml();
  try (XmlBuilder html = new XmlBuilder(Html.HTML, writer)) {
      try (XmlBuilder head = html.addElement(Html.HEAD)) {
          head.addElement(Html.META, Html.A_CHARSET, UTF_8);
          head.addElement(Html.TITLE).addText("Test");
      }
      ry (XmlBuilder body = html.addElement(Html.BODY)) {
          body.addElement(Html.H1).addText("Hello word!");
          body.addElement(Html.DIV).addText(null);
      }
  };
  String result = writer.toString();
 
The XmlElement class implements the Closeable implementation for an optional highlighting the tree structure in the source code.
Since:
1.86
Author:
Pavel Ponec
  • Field Details

    • HIDDEN_NAME

      @Nullable public static @Nullable String HIDDEN_NAME
      A name of a hidden element must be a unique instance
    • HTML

      public static final String HTML
      The HTML tag name
      See Also:
    • REQUIRED_MSG

      protected static final String REQUIRED_MSG
      Assertion message template
      See Also:
    • name

      @NotNull protected final @NotNull String name
      Element name
  • Constructor Details

    • XmlBuilder

      public XmlBuilder(@NotNull @NotNull String name, @NotNull @NotNull XmlPrinter writer, int level)
      The new element constructor
      Parameters:
      name - The element name must not be special HTML characters. The null value is intended to build a root of AJAX queries.
    • XmlBuilder

      protected XmlBuilder(@NotNull @NotNull String name, @NotNull @NotNull XmlPrinter writer, int level, boolean printName)
      The new element constructor
      Parameters:
      name - The element name must not be special HTML characters. The null value is intended to build a root of AJAX queries.
      writer - A XmlPrinter
      level - Level of the Element
      printName - Print the element name immediately.
    • XmlBuilder

      public XmlBuilder(@NotNull @NotNull String name, @NotNull @NotNull XmlPrinter writer)
      New element with a parent
  • Method Details

    • getName

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

      @Nullable protected @Nullable XmlBuilder nextChild(@Nullable @Nullable XmlBuilder element)
      Setup states
      Parameters:
      element - A child Node or null value for a text data
    • addElement

      @NotNull public final @NotNull XmlBuilder addElement(@NotNull @NotNull String name)
      Create a new XmlBuilder for a required name and add it to children.
      Specified by:
      addElement in interface ApiElement<XmlBuilder>
      Parameters:
      name - A name of the new XmlElement is required.
      Returns:
      The new XmlElement!
    • setAttribute

      @NotNull public final @NotNull XmlBuilder setAttribute(@Nullable @Nullable String name, @Nullable @Nullable Object value)
      Add an attribute
      Specified by:
      setAttribute in interface ApiElement<XmlBuilder>
      Parameters:
      name - Required element name
      value - The null value is 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 final @NotNull XmlBuilder addText(@Nullable @Nullable Object value)
      Add a text and escape special character
      Specified by:
      addText in interface ApiElement<XmlBuilder>
      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

      @NotNull public final @NotNull XmlBuilder addTextTemplated(@Nullable @Nullable CharSequence template, @NotNull @NotNull Object... values)
      Message template with hight performance.
      Specified by:
      addTextTemplated in interface ApiElement<XmlBuilder>
      Parameters:
      template - Message template where parameters are marked by the {} symbol
      values - argument values
      Returns:
      The original builder
    • addRawText

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

      @NotNull @Deprecated public final @NotNull XmlBuilder addComment(@Nullable @Nullable CharSequence comment)
      Deprecated.
      Add a comment text. The CDATA structure isn't really for HTML at all.
      Specified by:
      addComment in interface ApiElement<XmlBuilder>
      Parameters:
      comment - A comment text must not contain a string --> .
      Returns:
      This instance
    • addCDATA

      @NotNull @Deprecated public final @NotNull XmlBuilder addCDATA(@Nullable @Nullable CharSequence charData)
      Deprecated.
      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<XmlBuilder>
      Parameters:
      charData - A text including the final DATA sequence. An empty argument is ignored.
      Returns:
      This instance
    • close

      public final void close()
      Close the Node
      Specified by:
      close in interface ApiElement<XmlBuilder>
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • isClosed

      public boolean isClosed()
      Is the node closed?
    • getLevel

      public int getLevel()
    • isFilled

      public boolean isFilled()
    • isLastText

      public boolean isLastText()
      The last child was a text
    • getWriter

      public XmlPrinter getWriter()
      Writer
    • toString

      @NotNull public @NotNull String toString()
      Render the XML code including header
      Overrides:
      toString in class Object
    • forHtml

      @NotNull public static @NotNull XmlBuilder forHtml(@NotNull @NotNull Appendable response)
      Create builder for HTML
    • forNiceHtml

      @NotNull public static @NotNull XmlBuilder forNiceHtml(@NotNull @NotNull Appendable response)