Class 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''
          ');
     
    • 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)  
      • Methods inherited from class java.io.FilterWriter

        close, flush
      • Methods inherited from class java.io.Writer

        append, append, append, nullWriter, 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 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 writer
        indentation - indentation unit such as INDENT_TAB or INDENT_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 class java.io.FilterWriter
        Throws:
        java.io.IOException
      • write

        public void write​(char[] cbuf,
                          int off,
                          int len)
                   throws java.io.IOException
        Overrides:
        write in class java.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 class java.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 write
        s - 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 write
        s - identifier