Class StackWriter

java.lang.Object
java.io.Writer
java.io.FilterWriter
org.apache.calcite.util.StackWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

public class StackWriter extends FilterWriter
A helper class for generating formatted text. StackWriter keeps track of nested formatting state like indentation level and quote escaping. Typically, it is inserted between a PrintWriter and the real Writer; directives are passed straight through the PrintWriter via the write method, as in the following example:

    StringWriter sw = new StringWriter();
    StackWriter stackw = new StackWriter(sw, StackWriter.INDENT_SPACE4);
    PrintWriter pw = new PrintWriter(stackw);
    pw.write(StackWriter.INDENT);
    pw.print("execute remote(link_name,");
    pw.write(StackWriter.OPEN_SQL_STRING_LITERAL);
    pw.println();
    pw.write(StackWriter.INDENT);
    pw.println("select * from t where c > 'alabama'");
    pw.write(StackWriter.OUTDENT);
    pw.write(StackWriter.CLOSE_SQL_STRING_LITERAL);
    pw.println(");");
    pw.write(StackWriter.OUTDENT);
    pw.close();
    System.out.println(sw.toString());
 

which produces the following output:


      execute remote(link_name,'
          select * from t where c > ''alabama''
      ');
 
  • Field Details

    • INDENT

      public static final int INDENT
      Directive for increasing the indentation level.
      See Also:
    • OUTDENT

      public static final int OUTDENT
      Directive for decreasing the indentation level.
      See Also:
    • OPEN_SQL_STRING_LITERAL

      public static final int OPEN_SQL_STRING_LITERAL
      Directive for beginning an SQL string literal.
      See Also:
    • CLOSE_SQL_STRING_LITERAL

      public static final int CLOSE_SQL_STRING_LITERAL
      Directive for ending an SQL string literal.
      See Also:
    • OPEN_SQL_IDENTIFIER

      public static final int OPEN_SQL_IDENTIFIER
      Directive for beginning an SQL identifier.
      See Also:
    • CLOSE_SQL_IDENTIFIER

      public static final int CLOSE_SQL_IDENTIFIER
      Directive for ending an SQL identifier.
      See Also:
    • INDENT_TAB

      public static final String INDENT_TAB
      Tab indentation.
      See Also:
    • INDENT_SPACE4

      public static final String INDENT_SPACE4
      Four-space indentation.
      See Also:
  • Constructor Details

    • StackWriter

      public StackWriter(Writer writer, String indentation)
      Creates a new StackWriter on top of an existing Writer, with the specified string to be used for each level of indentation.
      Parameters:
      writer - underlying writer
      indentation - indentation unit such as INDENT_TAB or INDENT_SPACE4
  • Method Details