+/* $Id: CodePrintStream.java 1 2010-02-24 13:03:05Z haidw $ */
+package dol.util;
+
+import java.io.OutputStream;
+import java.io.PrintStream;
+
+/**
+ * Class to print code that is correctly indented.
+ */
+public class CodePrintStream extends PrintStream {
+
+ /**
+ *
+ */
+ public CodePrintStream(OutputStream out) {
+ super(out);
+ }
+
+ /**
+ *
+ */
+ public CodePrintStream(OutputStream out, boolean autoFlush) {
+ super(out, autoFlush);
+ }
+
+ /**
+ *
+ */
+ public void print(String x) {
+ super.print(x);
+ }
+
+ /**
+ *
+ */
+ public void println(String x) {
+ super.println(x);
+ }
+
+ /**
+ *
+ */
+ public void printPrefix(String x) {
+ super.print(_prefix + x);
+ }
+
+ /**
+ *
+ */
+ public void printPrefix() {
+ super.print(_prefix);
+ }
+
+ /**
+ *
+ */
+ public void printPrefixln(String x) {
+ super.println(_prefix + x);
+ }
+
+ /**
+ *
+ */
+ public void printPrefixln() {
+ super.println();
+ }
+
+ /**
+ * Print an opening curly brace and increase the indentation.
+ */
+ public void printLeftBracket() {
+ printPrefix();
+ println("{");
+ prefixInc();
+ }
+
+ /**
+ * Decrease the indentation and print a closing curly brace.
+ */
+ public void printRightBracket() {
+ prefixDec();
+ printPrefixln("}");
+ }
+
+ /**
+ * Decrement the indentation.
+ */
+ public void prefixDec() {
+ if( _prefix.length() >= _offset.length() ) {
+ _prefix = _prefix.substring(_offset.length());
+ }
+ }
+
+ /**
+ * Increment the indentation.
+ */
+ public void prefixInc() {
+ _prefix += _offset;
+ }
+
+ /**
+ *
+ */
+ public static void main(String[] args) {
+ CodePrintStream ps = new CodePrintStream(System.out);
+ ps.println("aa");
+ ps.print("bb");
+ }
+
+ private static String _offset = " ";
+ private String _prefix = "";
+}
+
+
+