Package org.apache.calcite.util
Class StackWriter
- java.lang.Object
-
- java.io.Writer
-
- java.io.FilterWriter
-
- org.apache.calcite.util.StackWriter
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.Appendable
,java.lang.AutoCloseable
public class StackWriter extends java.io.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 Summary
Fields Modifier and Type Field Description static int
CLOSE_SQL_IDENTIFIER
directive for ending an SQL identifierstatic int
CLOSE_SQL_STRING_LITERAL
directive for ending an SQL string literalprivate static java.lang.Character
DOUBLE_QUOTE
static int
INDENT
directive for increasing the indentation levelstatic java.lang.String
INDENT_SPACE4
four-space indentationstatic java.lang.String
INDENT_TAB
tab indentationprivate java.lang.String
indentation
private int
indentationDepth
private boolean
needIndent
static int
OPEN_SQL_IDENTIFIER
directive for beginning an SQL identifierstatic int
OPEN_SQL_STRING_LITERAL
directive for beginning an SQL string literalstatic int
OUTDENT
directive for decreasing the indentation levelprivate java.util.Deque<java.lang.Character>
quoteStack
private static java.lang.Character
SINGLE_QUOTE
-
Constructor Summary
Constructors Constructor Description StackWriter(java.io.Writer writer, java.lang.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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
indentIfNeeded()
private void
popQuote(java.lang.Character quoteChar)
static void
printSqlIdentifier(java.io.PrintWriter pw, java.lang.String s)
Writes an SQL identifier.static void
printSqlStringLiteral(java.io.PrintWriter pw, java.lang.String s)
Writes an SQL string literal.private void
pushQuote(java.lang.Character quoteChar)
void
write(char[] cbuf, int off, int len)
void
write(int c)
void
write(java.lang.String str, int off, int len)
private void
writeQuote(java.lang.Character quoteChar)
-
-
-
Field Detail
-
INDENT
public static final int INDENT
directive for increasing the indentation level- See Also:
- Constant Field Values
-
OUTDENT
public static final int OUTDENT
directive for decreasing the indentation level- See Also:
- Constant Field Values
-
OPEN_SQL_STRING_LITERAL
public static final int OPEN_SQL_STRING_LITERAL
directive for beginning an SQL string literal- See Also:
- Constant Field Values
-
CLOSE_SQL_STRING_LITERAL
public static final int CLOSE_SQL_STRING_LITERAL
directive for ending an SQL string literal- See Also:
- Constant Field Values
-
OPEN_SQL_IDENTIFIER
public static final int OPEN_SQL_IDENTIFIER
directive for beginning an SQL identifier- See Also:
- Constant Field Values
-
CLOSE_SQL_IDENTIFIER
public static final int CLOSE_SQL_IDENTIFIER
directive for ending an SQL identifier- See Also:
- Constant Field Values
-
INDENT_TAB
public static final java.lang.String INDENT_TAB
tab indentation- See Also:
- Constant Field Values
-
INDENT_SPACE4
public static final java.lang.String INDENT_SPACE4
four-space indentation- See Also:
- Constant Field Values
-
SINGLE_QUOTE
private static final java.lang.Character SINGLE_QUOTE
-
DOUBLE_QUOTE
private static final java.lang.Character DOUBLE_QUOTE
-
indentationDepth
private int indentationDepth
-
indentation
private java.lang.String indentation
-
needIndent
private boolean needIndent
-
quoteStack
private final java.util.Deque<java.lang.Character> quoteStack
-
-
Constructor Detail
-
StackWriter
public StackWriter(java.io.Writer writer, java.lang.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 writerindentation
- indentation unit such asINDENT_TAB
orINDENT_SPACE4
-
-
Method Detail
-
indentIfNeeded
private void indentIfNeeded() throws java.io.IOException
- Throws:
java.io.IOException
-
writeQuote
private void writeQuote(java.lang.Character quoteChar) throws java.io.IOException
- Throws:
java.io.IOException
-
pushQuote
private void pushQuote(java.lang.Character quoteChar) throws java.io.IOException
- Throws:
java.io.IOException
-
popQuote
private void popQuote(java.lang.Character quoteChar) throws java.io.IOException
- Throws:
java.io.IOException
-
write
public void write(int c) throws java.io.IOException
- Overrides:
write
in classjava.io.FilterWriter
- Throws:
java.io.IOException
-
write
public void write(char[] cbuf, int off, int len) throws java.io.IOException
- Overrides:
write
in classjava.io.FilterWriter
- Throws:
java.io.IOException
-
write
public void write(java.lang.String str, int off, int len) throws java.io.IOException
- Overrides:
write
in classjava.io.FilterWriter
- Throws:
java.io.IOException
-
printSqlStringLiteral
public static void printSqlStringLiteral(java.io.PrintWriter pw, java.lang.String s)
Writes an SQL string literal.- Parameters:
pw
- PrintWriter on which to writes
- text of literal
-
printSqlIdentifier
public static void printSqlIdentifier(java.io.PrintWriter pw, java.lang.String s)
Writes an SQL identifier.- Parameters:
pw
- PrintWriter on which to writes
- identifier
-
-