1 22 23 24 package com.mchange.v2.io; 25 26 import java.io.*; 27 28 public class IndentedWriter extends FilterWriter 29 { 30 final static String EOL; 31 32 static 33 { 34 String eol = System.getProperty( "line.separator" ); 35 EOL = ( eol != null ? eol : "\r\n" ); 36 } 37 38 int indent_level = 0; 39 boolean at_line_start = true; 40 41 public IndentedWriter( Writer out ) 42 { super( out ); } 43 44 private boolean isEol( char c ) 45 { return ( c == '\r' || c == '\n' ); } 46 47 public void upIndent() 48 { ++indent_level; } 49 50 public void downIndent() 51 { --indent_level; } 52 53 public void write( int c ) throws IOException 54 { 55 out.write( c ); 56 at_line_start = isEol( (char) c ); 57 } 58 59 public void write( char[] chars, int off, int len ) throws IOException 60 { 61 out.write( chars, off, len ); 62 at_line_start = isEol( chars[ off + len - 1] ); 63 } 64 65 public void write( String s, int off, int len ) throws IOException 66 { 67 if (len > 0) 68 { 69 out.write( s, off, len ); 70 at_line_start = isEol( s.charAt( off + len - 1) ); 71 } 72 } 73 74 private void printIndent() throws IOException 75 { 76 for (int i = 0; i < indent_level; ++i) 77 out.write( '\t' ); 78 } 79 80 public void print( String s ) throws IOException 81 { 82 if ( at_line_start ) 83 printIndent(); 84 out.write(s); 85 char last = s.charAt( s.length() - 1 ); 86 at_line_start = isEol( last ); 87 } 88 89 public void println( String s ) throws IOException 90 { 91 if ( at_line_start ) 92 printIndent(); 93 out.write(s); 94 out.write( EOL ); 95 at_line_start = true; 96 } 97 98 public void print( boolean x ) throws IOException 99 { print( String.valueOf(x) ); } 100 101 public void print( byte x ) throws IOException 102 { print( String.valueOf(x) ); } 103 104 public void print( char x ) throws IOException 105 { print( String.valueOf(x) ); } 106 107 public void print( short x ) throws IOException 108 { print( String.valueOf(x) ); } 109 110 public void print( int x ) throws IOException 111 { print( String.valueOf(x) ); } 112 113 public void print( long x ) throws IOException 114 { print( String.valueOf(x) ); } 115 116 public void print( float x ) throws IOException 117 { print( String.valueOf(x) ); } 118 119 public void print( double x ) throws IOException 120 { print( String.valueOf(x) ); } 121 122 public void print( Object x ) throws IOException 123 { print( String.valueOf(x) ); } 124 125 public void println( boolean x ) throws IOException 126 { println( String.valueOf(x) ); } 127 128 public void println( byte x ) throws IOException 129 { println( String.valueOf(x) ); } 130 131 public void println( char x ) throws IOException 132 { println( String.valueOf(x) ); } 133 134 public void println( short x ) throws IOException 135 { println( String.valueOf(x) ); } 136 137 public void println( int x ) throws IOException 138 { println( String.valueOf(x) ); } 139 140 public void println( long x ) throws IOException 141 { println( String.valueOf(x) ); } 142 143 public void println( float x ) throws IOException 144 { println( String.valueOf(x) ); } 145 146 public void println( double x ) throws IOException 147 { println( String.valueOf(x) ); } 148 149 public void println( Object x ) throws IOException 150 { println( String.valueOf(x) ); } 151 152 public void println() throws IOException 153 { println( "" ); } 154 } 155 | Popular Tags |