Class SerializerWriter<SELF extends SerializerWriter<SELF>>

java.lang.Object
java.io.Writer
org.apache.juneau.marshall.serializer.SerializerWriter<SELF>
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable
Direct Known Subclasses:
CsvWriter, HjsonWriter, HoconWriter, IniWriter, JsonWriter, MarkdownWriter, TomlWriter, UonWriter, XmlWriter, YamlWriter

public abstract class SerializerWriter<SELF extends SerializerWriter<SELF>> extends Writer
Simple wrapper around a standard Writer with additional methods.

Modeled after the Java ProcessBuilder class so that you can chain commands to reduce the need for string concatenation for performance reasons.

Example:

writer.append("foo").nl().i(5).append("bar");

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final int
    Max indentation levels.
    protected final Writer
    The underlying writer.
    protected final char
    The quote character being used by this writer.
    protected final boolean
    Trim strings flag.
    protected final UriResolver
    The URI resolver of the request.
    protected final boolean
    Use-whitespace flag.

    Fields inherited from class java.io.Writer

    lock
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    SerializerWriter(Writer out, boolean useWhitespace, int maxIndent, boolean trimStrings, char quoteChar, UriResolver uriResolver)
     
    protected
    Copy Constructor
  • Method Summary

    Modifier and Type
    Method
    Description
    append(char c)
     
    append(char[] value)
    Appends the specified characters to this writer.
    append(int indent, char value)
    Writes an indent (if the useWhitespace setting is enabled), followed by text.
    append(int indent, String value)
    Writes an indent (if the useWhitespace setting is enabled), followed by text.
    append(Object value)
    Writes the string representation of the specified object to the writer.
    append(String value)
    Writes the specified text to the writer if it isn't null.
    appendIf(boolean flag, char value)
    Writes the specified text to the writer if b is true.
    appendIf(boolean flag, String value)
    Writes the specified text to the writer if b is true.
    appendln(int indent, String value)
    Writes an indent (if the useWhitespace setting is enabled), followed by text, followed by a newline (if the useWhitespace setting is enabled).
    Writes the specified text followed by a newline (if the useWhitespace setting is enabled).
    Appends the specified object as a URI.
    void
     
    cr(int depth)
    Performs a carriage return.
    cre(int depth)
    Performs a carriage return at the end of a line.
    void
     
    i(int indent)
    Writes an indent to the writer if the useWhitespace setting is enabled.
    ie(int indent)
    Writes an end-of-line indent to the writer if the useWhitespace setting is enabled.
    nl(int indent)
    Writes a newline to the writer if the useWhitespace setting is enabled.
    nlIf(boolean flag, int indent)
    Writes a newline to the writer if the useWhitespace setting is enabled and the boolean flag is true.
    q()
    Adds the quote character specified by the quoteChar setting to the output.
    s()
    Adds a whitespace character to the output if the useWhitespace setting is enabled.
    protected final SELF
     
    sIf(boolean flag)
    Writes a space if the boolean expression is true and useWhitespace is false.
    w(char value)
    Writes the specified character to the writer.
    w(String value)
    Writes the specified string to the writer.
    void
    write(char[] cbuf, int off, int len)
     

    Methods inherited from class java.io.Writer

    append, append, nullWriter, write, write, write, write

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • out

      protected final Writer out
      The underlying writer.
    • useWhitespace

      protected final boolean useWhitespace
      Use-whitespace flag.
    • maxIndent

      protected final int maxIndent
      Max indentation levels.
    • trimStrings

      protected final boolean trimStrings
      Trim strings flag.
    • quoteChar

      protected final char quoteChar
      The quote character being used by this writer.
    • uriResolver

      protected final UriResolver uriResolver
      The URI resolver of the request.
  • Constructor Details

    • SerializerWriter

      Copy Constructor
      Parameters:
      w - Writer being copied.
      Must not be null.
    • SerializerWriter

      protected SerializerWriter(Writer out, boolean useWhitespace, int maxIndent, boolean trimStrings, char quoteChar, UriResolver uriResolver)
      Parameters:
      out - The writer being wrapped.
      Must not be null.
      useWhitespace - If true, calling cr(int) will create an indentation and calling s() will write a space character.
      maxIndent - The maximum indentation level.
      trimStrings - If true, strings should be trimmed before they're serialized.
      quoteChar - The character to write when q() is called.
      uriResolver - The URI resolver for resolving URIs to absolute or root-relative form.
  • Method Details

    • self

      protected final SELF self()
    • append

      public SELF append(char c)
      Specified by:
      append in interface Appendable
      Overrides:
      append in class Writer
    • append

      public SELF append(char[] value)
      Appends the specified characters to this writer.
      Parameters:
      value - The characters to append to this writer.
      Must not be null.
      Returns:
      This object.
    • append

      public SELF append(int indent, char value)
      Writes an indent (if the useWhitespace setting is enabled), followed by text.
      Parameters:
      indent - The number of tabs to indent.
      value - The character to write.
      Returns:
      This object.
    • append

      public SELF append(int indent, String value)
      Writes an indent (if the useWhitespace setting is enabled), followed by text.
      Parameters:
      indent - The number of tabs to indent.
      value - The text to write.
      Returns:
      This object.
    • append

      public SELF append(Object value)
      Writes the string representation of the specified object to the writer.
      Parameters:
      value - The object to write.
      Must not be null.
      Returns:
      This object.
    • append

      public SELF append(String value)
      Writes the specified text to the writer if it isn't null.
      Parameters:
      value - The text to write.
      Can be null (ignored).
      Returns:
      This object.
    • appendIf

      public SELF appendIf(boolean flag, char value)
      Writes the specified text to the writer if b is true.
      Parameters:
      flag - Boolean flag.
      value - The text to write.
      Returns:
      This object.
    • appendIf

      public SELF appendIf(boolean flag, String value)
      Writes the specified text to the writer if b is true.
      Parameters:
      flag - Boolean flag.
      value - The text to write.
      Returns:
      This object.
    • appendln

      public SELF appendln(int indent, String value)
      Writes an indent (if the useWhitespace setting is enabled), followed by text, followed by a newline (if the useWhitespace setting is enabled).
      Parameters:
      indent - The number of tabs to indent.
      value - The text to write.
      Returns:
      This object.
    • appendln

      public SELF appendln(String value)
      Writes the specified text followed by a newline (if the useWhitespace setting is enabled).
      Parameters:
      value - The text to write.
      Returns:
      This object.
    • appendUri

      public SELF appendUri(Object value)
      Appends the specified object as a URI.

      Object is converted to a String using toString(), so this will work on URL or URI objects, or any other type that returns a URI via it's toString() method.

      The URI is resolved based on the Serializer.Builder.uriRelativity(UriRelativity) and Serializer.Builder.uriResolution(UriResolution) settings and the UriContext that's part of the session.

      Parameters:
      value - The URI to serialize.
      Can be null. The effect is implementation-dependent — see UriResolver.append(Appendable, Object).
      Returns:
      This object.
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in class Writer
      Throws:
      IOException
    • cr

      public SELF cr(int depth)
      Performs a carriage return.

      Adds a newline and the specified number of tabs (if the useWhitespace setting is enabled) to the output.

      Parameters:
      depth - The indentation.
      Returns:
      This object.
    • cre

      public SELF cre(int depth)
      Performs a carriage return at the end of a line.

      Adds a newline and the specified number of tabs (if the useWhitespace setting is enabled) to the output.

      Parameters:
      depth - The indentation.
      Returns:
      This object.
    • flush

      public void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in class Writer
      Throws:
      IOException
    • i

      public SELF i(int indent)
      Writes an indent to the writer if the useWhitespace setting is enabled.
      Parameters:
      indent - The number of tabs to indent.
      Returns:
      This object.
    • ie

      public SELF ie(int indent)
      Writes an end-of-line indent to the writer if the useWhitespace setting is enabled.
      Parameters:
      indent - The number of tabs to indent.
      Returns:
      This object.
    • nl

      public SELF nl(int indent)
      Writes a newline to the writer if the useWhitespace setting is enabled.
      Parameters:
      indent - The current indentation level.
      Returns:
      This object.
    • nlIf

      public SELF nlIf(boolean flag, int indent)
      Writes a newline to the writer if the useWhitespace setting is enabled and the boolean flag is true.
      Parameters:
      flag - The boolean flag.
      indent - The current indentation level.
      Returns:
      This object.
    • q

      public SELF q()
      Adds the quote character specified by the quoteChar setting to the output.
      Returns:
      This object.
    • s

      public SELF s()
      Adds a whitespace character to the output if the useWhitespace setting is enabled.
      Returns:
      This object.
    • sIf

      public SELF sIf(boolean flag)
      Writes a space if the boolean expression is true and useWhitespace is false.

      Intended for cases in XML where text should be separated by either a space or newline. This ensures the text is separated by a space if whitespace is disabled.

      Parameters:
      flag - The boolean flag.
      Returns:
      This object.
    • w

      public SELF w(char value)
      Writes the specified character to the writer.
      Parameters:
      value - The character to write.
      Returns:
      This object.
    • w

      public SELF w(String value)
      Writes the specified string to the writer.
      Parameters:
      value - The string to write.
      Must not be null.
      Returns:
      This object.
    • write

      public void write(char[] cbuf, int off, int len)
      Specified by:
      write in class Writer