2a3a80b2f17dd08ef200ee1c0b622c3712217f54
[jump.git] / dol / src / dol / util / CodePrintStream.java
1 /* $Id: CodePrintStream.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.util;
3
4 import java.io.OutputStream;
5 import java.io.PrintStream;
6
7 /**
8  * Class to print code that is correctly indented.
9  */
10 public class CodePrintStream extends PrintStream {
11
12     /**
13      *
14      */
15     public CodePrintStream(OutputStream out) {
16         super(out);
17     }
18
19     /**
20      *
21      */
22     public CodePrintStream(OutputStream out, boolean autoFlush) {
23         super(out, autoFlush);
24     }
25
26     /**
27     *
28     */
29    public void print(String x) {
30        super.print(x);
31    }
32    
33     /**
34     *
35     */
36     public void println(String x) {
37         super.println(x);
38     }
39   
40     /**
41      *
42      */
43     public void printPrefix(String x) {
44         super.print(_prefix + x);
45     }
46     
47     /**
48      *
49      */
50     public void printPrefix() {
51         super.print(_prefix);
52     }
53
54     /**
55      *
56      */
57     public void printPrefixln(String x) {
58         super.println(_prefix + x);
59     }
60
61     /**
62      *
63      */
64     public void printPrefixln() {
65         super.println();
66     }
67
68     /**
69      * Print an opening curly brace and increase the indentation.
70      */
71     public void printLeftBracket() {
72         printPrefix();
73         println("{");
74         prefixInc();
75     }
76
77     /**
78      * Decrease the indentation and print a closing curly brace.
79      */
80     public void printRightBracket() {
81         prefixDec();
82         printPrefixln("}");
83     }
84
85     /**
86      *  Decrement the indentation.
87      */
88     public void prefixDec() {
89         if( _prefix.length() >= _offset.length() ) {
90             _prefix = _prefix.substring(_offset.length());
91         }
92     }
93
94     /**
95      *  Increment the indentation.
96      */
97     public void prefixInc() {
98         _prefix += _offset;
99     }
100
101     /**
102      *
103      */
104     public static void main(String[] args) {
105         CodePrintStream ps = new CodePrintStream(System.out);
106         ps.println("aa");
107         ps.print("bb");
108     }
109
110     private static String _offset = "    ";
111     private String _prefix = "";
112 }
113
114
115