Class XmlBuilder

java.lang.Object
com.pnfsoftware.jeb.util.format.TextBuilder
com.pnfsoftware.jeb.util.format.XmlBuilder
All Implemented Interfaces:
Appendable

public class XmlBuilder extends TextBuilder
A specialized text builder used to craft XML documents sequentially.
  • Constructor Details

    • XmlBuilder

      public XmlBuilder()
      Create an XML builder with default indentation settings.
    • XmlBuilder

      public XmlBuilder(int indentationSize)
      Create an XML builder.
      Parameters:
      indentationSize - number of spaces per indentation level
    • XmlBuilder

      public XmlBuilder(int indentationSize, int initialCapacity)
      Create an XML builder.
      Parameters:
      indentationSize - number of spaces per indentation level
      initialCapacity - initial capacity of the underlying character buffer
  • Method Details

    • appendStandardHeader

      public void appendStandardHeader()
      Append a standard 1.0 header specifying a UTF-8 encoding and standalone=no.
    • appendHeader

      public void appendHeader(int versionMajor, int versionMinor, String encoding, boolean standalone)
      Append an XML header.
      Parameters:
      versionMajor - XML major version
      versionMinor - XML minor version
      encoding - declared character encoding
      standalone - true to mark the document as standalone
    • openElement

      public void openElement(String name)
      Output an XML element. The closing tag is not output.
      Parameters:
      name - the element name
    • openElement

      public void openElement(String name, Map<String,String> attributes)
      Output an XML element with attributes. The closing tag is not output.
      Parameters:
      name -
      attributes - optional attributes
    • openElement

      public void openElement(String name, Map<String,String> attributes, boolean close)
      Output an XML element with attributes.
      Parameters:
      name - the element name
      attributes - optional map of attributes name->value, which must be escaped (this method will append the attributes' names and values as-is)
      close - if true, the tag is immediately closed using "/>"
    • closeElement

      public void closeElement()
      Close the last opened element.
    • closeElement

      public void closeElement(boolean appendLine)
      Close the last opened element.
      Parameters:
      appendLine - if true, a new line is appended after the closing tag
    • escapeTextData

      public static CharSequence escapeTextData(CharSequence s)
      Escape an XML text data string.
      Parameters:
      s - text data to escape
      Returns:
      the escaped text data
    • escapeAttributeData

      public static CharSequence escapeAttributeData(CharSequence s)
      Escape an XML attribute data string.
      Parameters:
      s - attribute data to escape
      Returns:
      the escaped attribute data
    • escapeAll

      public static CharSequence escapeAll(CharSequence s)
      Escape a string for XML text or attribute contexts.
      Parameters:
      s - string to escape
      Returns:
      the escaped string