1 /*
   2  * Copyright (c) 2003, 2009, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package build.tools.logutil;
  27 
  28 import java.io.PrintWriter ;
  29 import java.io.Writer ;
  30 import java.io.OutputStream ;
  31 import java.io.BufferedWriter ;
  32 import java.io.OutputStreamWriter ;
  33 import java.util.StringTokenizer ;
  34 
  35 public class IndentingPrintWriter extends PrintWriter {
  36     private int level = 0 ;
  37     private int indentWidth = 4 ;
  38     private String indentString = "" ;
  39 
  40     public void printMsg( String msg, Object... data )
  41     {
  42         // System.out.println( "printMsg called with msg=" + msg + " data=" + data ) ;
  43         StringTokenizer st = new StringTokenizer( msg, "@", true ) ;
  44         StringBuffer result = new StringBuffer() ;
  45         String token = null ;
  46         int pos = 0;
  47 
  48         while (st.hasMoreTokens()) {
  49             token = st.nextToken() ;
  50             if (token.equals("@")) {
  51                 if (pos < data.length) {
  52                     result.append( data[pos] );
  53                     ++pos;
  54                 } else {
  55                     throw new Error( "List too short for message" ) ;
  56                 }
  57             } else {
  58                 result.append( token ) ;
  59             }
  60         }
  61 
  62         // System.out.println( "Printing result " + result + " to file" ) ;
  63         print( result ) ;
  64         println() ;
  65     }
  66 
  67     public IndentingPrintWriter (Writer out) {
  68         super( out, true ) ;
  69         // System.out.println( "Constructing a new IndentingPrintWriter with Writer " + out ) ;
  70     }
  71 
  72     public IndentingPrintWriter(Writer out, boolean autoFlush) {
  73         super( out, autoFlush ) ;
  74         // System.out.println( "Constructing a new IndentingPrintWriter with Writer " + out ) ;
  75     }
  76 
  77     public IndentingPrintWriter(OutputStream out) {
  78         super(out, true);
  79         // System.out.println( "Constructing a new IndentingPrintWriter with OutputStream " + out ) ;
  80     }
  81 
  82     public IndentingPrintWriter(OutputStream out, boolean autoFlush) {
  83         super(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
  84         // System.out.println( "Constructing a new IndentingPrintWriter with OutputStream " + out ) ;
  85     }
  86 
  87     public void setIndentWidth( int indentWidth )
  88     {
  89         this.indentWidth = indentWidth ;
  90         updateIndentString() ;
  91     }
  92 
  93     public void indent()
  94     {
  95         level++ ;
  96         updateIndentString() ;
  97     }
  98 
  99     public void undent()
 100     {
 101         if (level > 0) {
 102             level-- ;
 103             updateIndentString() ;
 104         }
 105     }
 106 
 107     private void updateIndentString()
 108     {
 109         int size = level * indentWidth ;
 110         StringBuffer sbuf = new StringBuffer( size ) ;
 111         for (int ctr = 0; ctr<size; ctr++ )
 112             sbuf.append( " " ) ;
 113         indentString = sbuf.toString() ;
 114     }
 115 
 116     // overridden from PrintWriter
 117     public void println()
 118     {
 119         super.println() ;
 120 
 121         print( indentString ) ;
 122     }
 123 }