org.eigenbase.util
Class StackWriter

java.lang.Object
  extended by java.io.Writer
      extended by java.io.FilterWriter
          extended by org.eigenbase.util.StackWriter
All Implemented Interfaces:
Closeable, Flushable, Appendable

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''
      ');

Version:
$Id: //open/dev/farrago/src/org/eigenbase/util/StackWriter.java#8 $
Author:
John V. Sichi

Field Summary
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 doubleQuote
           
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  LinkedList quoteStack
           
private static Character singleQuote
           
 
Fields inherited from class java.io.FilterWriter
out
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
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.
 
Method Summary
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)
           
 
Methods inherited from class java.io.FilterWriter
close, flush
 
Methods inherited from class java.io.Writer
append, append, append, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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 String INDENT_TAB
tab indentation

See Also:
Constant Field Values

INDENT_SPACE4

public static final String INDENT_SPACE4
four-space indentation

See Also:
Constant Field Values

singleQuote

private static final Character singleQuote

doubleQuote

private static final Character doubleQuote

indentationDepth

private int indentationDepth

indentation

private String indentation

needIndent

private boolean needIndent

quoteStack

private LinkedList quoteStack
Constructor Detail

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 - underyling writer
indentation - indentation unit such as INDENT_TAB or INDENT_SPACE4
Method Detail

indentIfNeeded

private void indentIfNeeded()
                     throws IOException
Throws:
IOException

writeQuote

private void writeQuote(Character quoteChar)
                 throws IOException
Throws:
IOException

pushQuote

private void pushQuote(Character quoteChar)
                throws IOException
Throws:
IOException

popQuote

private void popQuote(Character quoteChar)
               throws IOException
Throws:
IOException

write

public void write(int c)
           throws IOException
Overrides:
write in class FilterWriter
Throws:
IOException

write

public void write(char[] cbuf,
                  int off,
                  int len)
           throws IOException
Overrides:
write in class FilterWriter
Throws:
IOException

write

public void write(String str,
                  int off,
                  int len)
           throws IOException
Overrides:
write in class FilterWriter
Throws:
IOException

printSqlStringLiteral

public static void printSqlStringLiteral(PrintWriter pw,
                                         String s)
Writes an SQL string literal.

Parameters:
pw - PrintWriter on which to write
s - text of literal

printSqlIdentifier

public static void printSqlIdentifier(PrintWriter pw,
                                      String s)
Writes an SQL identifier.

Parameters:
pw - PrintWriter on which to write
s - identifier