Class XmlModel

java.lang.Object
org.ujorm.tools.xml.model.XmlModel
All Implemented Interfaces:
Closeable, Serializable, AutoCloseable, ApiElement<XmlModel>
Direct Known Subclasses:
XmlElement

public class XmlModel extends Object implements ApiElement<XmlModel>, Serializable
XML element model to rendering a XML file. 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:

  XmlElement root = new XmlElement("root");
  root.addElement("childA")
          .setAttrib("x", 1)
          .setAttrib("y", 2);
  root.addElement("childB")
          .setAttrib("x", 3)
          .setAttrib("y", 4)
          .addText("A text message.");
  root.addRawText("\n<rawXml/>\n");
  root.addCDATA("A character data <&\">");
  String result = root.toString();
 
Since:
2.03
Author:
Pavel Ponec
See Also:
  • Field Details

    • name

      @NotNull protected final @NotNull String name
      Element name
    • attributes

      @Nullable protected @Nullable Map<String,Object> attributes
      Attributes
    • children

      @Nullable protected @Nullable List<Object> children
      Child elements with a null items
  • Constructor Details

    • XmlModel

      public XmlModel(@NotNull @NotNull String name)
      Parameters:
      name - The element name must not be special HTML characters. The null value is intended to build a root of AJAX queries.
    • XmlModel

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

    • getName

      @Nullable public @Nullable String getName()
      Description copied from interface: ApiElement
      Get an element name
      Specified by:
      getName in interface ApiElement<XmlModel>
    • getAttribs

      @NotNull protected @NotNull Map<String,Object> getAttribs()
      Return attributes
    • addChild

      @NotNull protected void addChild(@Nullable @Nullable Object child)
      Add a child entity
    • addElement

      @NotNull public final @NotNull XmlModel addElement(@NotNull @NotNull XmlModel element)
      Add a child element
      Parameters:
      element - Add a child element is required. An undefined argument is ignored.
      Returns:
      The argument type of XmlElement!
    • addElement

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

      @NotNull public final @NotNull XmlModel setAttribute(@NotNull @NotNull String name, @Nullable @Nullable Object value)
      Set one attribute
      Specified by:
      setAttribute in interface ApiElement<XmlModel>
      Parameters:
      name - Required element name
      value - The null value is ignored. Formatting is performed by the XmlWriter#writeValue(java.lang.Object, org.ujorm.tools.dom.XmlElement, java.lang.String, java.io.Writer) method, where the default implementation calls a toString() only.
      Returns:
      The original element
    • addText

      @NotNull public final @NotNull XmlModel addText(@Nullable @Nullable Object value)
      Add a text and escape special character
      Specified by:
      addText in interface ApiElement<XmlModel>
      Parameters:
      value - The null value is allowed. Formatting is performed by the XmlWriter#writeValue(java.lang.Object, org.ujorm.tools.dom.XmlElement, java.lang.String, java.io.Writer) method, where the default implementation calls a toString() only.
      Returns:
      This instance
    • addTextTemplated

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

      @NotNull public final @NotNull XmlModel 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<XmlModel>
      Parameters:
      value - The null value is ignored.
      Returns:
      This instance
    • addComment

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

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

      @NotNull public @NotNull Map<String,Object> getAttributes()
      Get an unmodifiable map of attributes
    • getChildren

      @NotNull public @NotNull List<Object> getChildren()
      Get an unmodifiable list of children
    • close

      public final void close()
      An empty method
      Specified by:
      close in interface ApiElement<XmlModel>
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • toString

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

      @NotNull public @NotNull XmlWriter toWriter(int level, @NotNull @NotNull XmlWriter out) throws IOException
      Render the XML code without header
      Throws:
      IOException