Class XmlStringBuilder

  • All Implemented Interfaces:
    Closeable, Flushable, Appendable, AutoCloseable

    public final class XmlStringBuilder
    extends PrintWriter

    This class is used by the Layout classes to assist with common but primitive html/xml string construction.

    It extends PrintWriter to allow the same instance to be re-used by the old layout classes which use PrintWriters.

    Since:
    1.0.0
    Author:
    Martin Shevchenko
    • Constructor Detail

      • XmlStringBuilder

        public XmlStringBuilder​(Writer writer)
        Creates a XmlStringBuilder with the default capacity and no explicit locale.
        Parameters:
        writer - the backing writer.
      • XmlStringBuilder

        public XmlStringBuilder​(Writer writer,
                                Locale locale)
        Creates a XmlStringBuilder with the default capacity and the given Locale.
        Parameters:
        writer - the backing writer.
        locale - the locale to use for translating messages.
    • Method Detail

      • appendTag

        public void appendTag​(String name)

        Adds a starting xml tag to the end of this XmlStringBuilder.

        Eg. <name>

        Parameters:
        name - the name of the tag to be added.
      • appendEndTag

        public void appendEndTag​(String name)

        Adds an ending xml tag to the end of this XmlStringBuilder.

        Eg. </name>

        Parameters:
        name - the name of the ending tag to be added.
      • appendTagOpen

        public void appendTagOpen​(String name)

        Adds a starting xml tag (but without the closing angle bracket) to the end of this XmlStringBuilder.

        Eg. <name

        Parameters:
        name - the name of the tag to be added.
      • appendClose

        public void appendClose()

        Adds a closing angle bracket to the end of this XmlStringBuilder.

        Eg. >

      • appendEnd

        public void appendEnd()

        Adds a tag end character plus a closing angle bracket to the end of this XmlStringBuilder.

        Eg. />

      • turnIndentingOff

        @Deprecated
        public void turnIndentingOff()
        Deprecated.
        1.4.13 No longer used: no replacement, will be removed in v2.0.0.
        Turns indenting off for further content written to this XmlStringBuilder.
      • turnIndentingOn

        @Deprecated
        public void turnIndentingOn()
        Deprecated.
        1.4.13 No longer used: no replacement, will be removed in v2.0.0.
        Turns indenting on for further content written to this XmlStringBuilder.
      • appendUrlAttribute

        public void appendUrlAttribute​(String name,
                                       String value)

        Adds an xml URL attribute name+value pair to the end of this XmlStringBuilder. All attribute values are URL encoded to prevent malformed XML and XSS attacks.

        If the value is null an empty string "" is output.

        Eg. name="value"

        Parameters:
        name - the name of the attribute to be added.
        value - the URL value of the attribute to be added.
      • appendOptionalUrlAttribute

        public void appendOptionalUrlAttribute​(String name,
                                               String value)

        If the value is not null, add an xml attribute name+value pair to the end of this XmlStringBuilder

        Eg. name="value"

        Parameters:
        name - the name of the attribute to be added.
        value - the value of the attribute.
      • appendAttribute

        public void appendAttribute​(String name,
                                    Object value)

        Adds an xml attribute name+value pair to the end of this XmlStringBuilder. All attribute values are escaped to prevent malformed XML and XSS attacks.

        If the value is null an empty string "" is output.

        Eg. name="value"

        Parameters:
        name - the name of the attribute to be added.
        value - the value of the attribute to be added.
      • appendAttribute

        public void appendAttribute​(String name,
                                    int value)

        Adds an xml attribute name+value pair to the end of this XmlStringBuilder.

        Eg. name="1"

        Parameters:
        name - the name of the attribute to be added.
        value - the value of the attribute to be added.
      • appendOptionalAttribute

        public void appendOptionalAttribute​(String name,
                                            Object value)

        If the value is not null, add an xml attribute name+value pair to the end of this XmlStringBuilder

        Eg. name="value"

        Parameters:
        name - the name of the attribute to be added.
        value - the value of the attribute.
      • appendOptionalAttribute

        public void appendOptionalAttribute​(String name,
                                            boolean flag,
                                            int value)

        If the flag is true, add an xml attribute name+value pair to the end of this XmlStringBuilder

        Eg. name="1"

        Parameters:
        name - the name of the boolean attribute to be added.
        flag - should this attribute be set.
        value - the value of the attribute.
      • appendOptionalAttribute

        public void appendOptionalAttribute​(String name,
                                            boolean flag,
                                            Object value)

        If the flag is true, add an xml attribute name+value pair to the end of this XmlStringBuilder

        Eg. name="value"

        Parameters:
        name - the name of the boolean attribute to be added.
        flag - should this attribute be set.
        value - the value of the attribute.
      • appendOptional

        public void appendOptional​(String value)
        If the value in not null, add it to the end of this XmlStringBuilder.
        Parameters:
        value - the value
      • append

        public void append​(String string)
        Appends the string to this XmlStringBuilder. XML values are not escaped.
        Parameters:
        string - the String to append.
      • append

        public void append​(Object text)
        Appends the given text to this XmlStringBuilder. XML values are escaped.
        Parameters:
        text - the message to append.
      • append

        public void append​(String string,
                           boolean encode)
        Appends the string to this XmlStringBuilder. XML values are not escaped.
        Parameters:
        string - the String to append.
        encode - true to encode the string before output, false to output as is
      • append

        public void append​(int value)
        Appends the integer value to this XmlStringBuilder.
        Parameters:
        value - the value to append.
      • append

        public PrintWriter append​(char chr)
        Appends the given character to this XmlStringBuilder. XML values are not escaped.
        Specified by:
        append in interface Appendable
        Overrides:
        append in class PrintWriter
        Parameters:
        chr - the character to append.
        Returns:
        this XmlStringBuilder
      • appendEscaped

        public void appendEscaped​(String string)
        Appends an XML-escaped copy of the string to this XmlStringBuffer. If the string is null then no text will be appended.
        Parameters:
        string - the String to append.