Class SerializerWriter

java.lang.Object
java.io.Writer
org.apache.juneau.serializer.SerializerWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable
Direct Known Subclasses:
CsvWriter, JsonWriter, UonWriter, XmlWriter

public class SerializerWriter 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 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

      public SerializerWriter(Writer out, boolean useWhitespace, int maxIndent, boolean trimStrings, char quoteChar, UriResolver uriResolver)
      Parameters:
      out - The writer being wrapped.
      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

    • cr

      public SerializerWriter 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 SerializerWriter 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.
    • appendln

      public SerializerWriter 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

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

      public SerializerWriter 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 SerializerWriter 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.
    • appendUri

      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.
      Returns:
      This object.
    • append

      public SerializerWriter append(char[] value)
      Appends the specified characters to this writer.
      Parameters:
      value - The characters to append to this writer.
      Returns:
      This object.
    • s

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

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

      public SerializerWriter 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 SerializerWriter 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 SerializerWriter nl(int indent)
      Writes a newline to the writer if the useWhitespace setting is enabled.
      Parameters:
      indent - The current indentation level.
      Returns:
      This object.
    • sIf

      public SerializerWriter 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.
    • nlIf

      public SerializerWriter 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.
    • append

      public SerializerWriter append(Object value)
      Writes the specified text to the writer if it isn't null.
      Parameters:
      value - The text to write.
      Returns:
      This object.
    • append

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

      public SerializerWriter 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.
    • appendIf

      public SerializerWriter 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.
    • w

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

      public SerializerWriter w(String value)
      Writes the specified string to the writer.
      Parameters:
      value - The string to write.
      Returns:
      This object.
    • append

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

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

      public void flush() throws IOException
      Specified by:
      flush in interface Flushable
      Specified by:
      flush in class Writer
      Throws:
      IOException
    • 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