Package org.apache.calcite.util
Class StackWriter
java.lang.Object
java.io.Writer
java.io.FilterWriter
org.apache.calcite.util.StackWriter
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
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 Summary
Modifier and TypeFieldDescriptionstatic final int
Directive for ending an SQL identifier.static final int
Directive for ending an SQL string literal.static final int
Directive for increasing the indentation level.static final String
Four-space indentation.static final String
Tab indentation.static final int
Directive for beginning an SQL identifier.static final int
Directive for beginning an SQL string literal.static final int
Directive for decreasing the indentation level.Fields inherited from class java.io.FilterWriter
out
-
Constructor Summary
ConstructorDescriptionStackWriter
(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. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
printSqlIdentifier
(PrintWriter pw, String s) Writes an SQL identifier.static void
Writes an SQL string literal.void
write
(char[] cbuf, int off, int len) void
write
(int c) void
Methods inherited from class java.io.FilterWriter
close, flush
-
Field Details
-
INDENT
public static final int INDENTDirective for increasing the indentation level.- See Also:
-
OUTDENT
public static final int OUTDENTDirective for decreasing the indentation level.- See Also:
-
OPEN_SQL_STRING_LITERAL
public static final int OPEN_SQL_STRING_LITERALDirective for beginning an SQL string literal.- See Also:
-
CLOSE_SQL_STRING_LITERAL
public static final int CLOSE_SQL_STRING_LITERALDirective for ending an SQL string literal.- See Also:
-
OPEN_SQL_IDENTIFIER
public static final int OPEN_SQL_IDENTIFIERDirective for beginning an SQL identifier.- See Also:
-
CLOSE_SQL_IDENTIFIER
public static final int CLOSE_SQL_IDENTIFIERDirective for ending an SQL identifier.- See Also:
-
INDENT_TAB
Tab indentation.- See Also:
-
INDENT_SPACE4
Four-space indentation.- See Also:
-
-
Constructor Details
-
StackWriter
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 writerindentation
- indentation unit such asINDENT_TAB
orINDENT_SPACE4
-
-
Method Details
-
write
- Overrides:
write
in classFilterWriter
- Throws:
IOException
-
write
- Overrides:
write
in classFilterWriter
- Throws:
IOException
-
write
- Overrides:
write
in classFilterWriter
- Throws:
IOException
-
printSqlStringLiteral
Writes an SQL string literal.- Parameters:
pw
- PrintWriter on which to writes
- text of literal
-
printSqlIdentifier
Writes an SQL identifier.- Parameters:
pw
- PrintWriter on which to writes
- identifier
-