public class StackWriter extends FilterWriter
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'' ');
| Modifier and Type | Field and Description |
|---|---|
static int |
CLOSE_SQL_IDENTIFIER
directive for ending an SQL identifier
|
static int |
CLOSE_SQL_STRING_LITERAL
directive for ending an SQL string literal
|
private static Character |
DOUBLE_QUOTE |
static int |
INDENT
directive for increasing the indentation level
|
static String |
INDENT_SPACE4
four-space indentation
|
static String |
INDENT_TAB
tab indentation
|
private String |
indentation |
private int |
indentationDepth |
private boolean |
needIndent |
static int |
OPEN_SQL_IDENTIFIER
directive for beginning an SQL identifier
|
static int |
OPEN_SQL_STRING_LITERAL
directive for beginning an SQL string literal
|
static int |
OUTDENT
directive for decreasing the indentation level
|
private Deque<Character> |
quoteStack |
private static Character |
SINGLE_QUOTE |
out| Constructor and Description |
|---|
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.
|
| Modifier and Type | Method and Description |
|---|---|
private void |
indentIfNeeded() |
private void |
popQuote(Character quoteChar) |
static void |
printSqlIdentifier(PrintWriter pw,
String s)
Writes an SQL identifier.
|
static void |
printSqlStringLiteral(PrintWriter pw,
String s)
Writes an SQL string literal.
|
private void |
pushQuote(Character quoteChar) |
void |
write(char[] cbuf,
int off,
int len) |
void |
write(int c) |
void |
write(String str,
int off,
int len) |
private void |
writeQuote(Character quoteChar) |
close, flushpublic static final int INDENT
public static final int OUTDENT
public static final int OPEN_SQL_STRING_LITERAL
public static final int CLOSE_SQL_STRING_LITERAL
public static final int OPEN_SQL_IDENTIFIER
public static final int CLOSE_SQL_IDENTIFIER
public static final String INDENT_TAB
public static final String INDENT_SPACE4
private static final Character SINGLE_QUOTE
private static final Character DOUBLE_QUOTE
private int indentationDepth
private String indentation
private boolean needIndent
public StackWriter(Writer writer, String indentation)
writer - underlying writerindentation - indentation unit such as INDENT_TAB or
INDENT_SPACE4private void indentIfNeeded()
throws IOException
IOExceptionprivate void writeQuote(Character quoteChar) throws IOException
IOExceptionprivate void pushQuote(Character quoteChar) throws IOException
IOExceptionprivate void popQuote(Character quoteChar) throws IOException
IOExceptionpublic void write(int c)
throws IOException
write in class FilterWriterIOExceptionpublic void write(char[] cbuf,
int off,
int len)
throws IOException
write in class FilterWriterIOExceptionpublic void write(String str, int off, int len) throws IOException
write in class FilterWriterIOExceptionpublic static void printSqlStringLiteral(PrintWriter pw, String s)
pw - PrintWriter on which to writes - text of literalpublic static void printSqlIdentifier(PrintWriter pw, String s)
pw - PrintWriter on which to writes - identifierCopyright © 2012–2018 The Apache Software Foundation. All rights reserved.