| 1 |
package org.freehep.util.io; |
| 2 |
|
| 3 |
import java.io.PrintWriter; |
| 4 |
import java.io.Writer; |
| 5 |
|
| 6 |
/** |
| 7 |
* A PrintWriter that keeps track of an indentation level and indents the output |
| 8 |
* appropriately. |
| 9 |
* |
| 10 |
* @author Tony Johnson |
| 11 |
* @author Mark Donszelmann |
| 12 |
* @version $Id: IndentPrintWriter.java 8584 2006-08-10 23:06:37Z duns $ |
| 13 |
*/ |
| 14 |
public class IndentPrintWriter extends PrintWriter { |
| 15 |
/** |
| 16 |
* Creates an Indent PrintWriter. |
| 17 |
* |
| 18 |
* @param w writer to write to |
| 19 |
* @param level starting indentation level |
| 20 |
*/ |
| 21 |
public IndentPrintWriter(Writer w, int level) { |
| 22 |
super(w); |
| 23 |
setIndent(level); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Creates an Indent PrintWriter with indentation level 0. |
| 28 |
* |
| 29 |
* @param w writer to write to |
| 30 |
*/ |
| 31 |
public IndentPrintWriter(Writer w) { |
| 32 |
this(w, 0); |
| 33 |
} |
| 34 |
|
| 35 |
public void print(boolean s) { |
| 36 |
doIndent(); |
| 37 |
super.print(s); |
| 38 |
} |
| 39 |
|
| 40 |
public void print(char s) { |
| 41 |
doIndent(); |
| 42 |
super.print(s); |
| 43 |
} |
| 44 |
|
| 45 |
public void print(char[] s) { |
| 46 |
doIndent(); |
| 47 |
super.print(s); |
| 48 |
} |
| 49 |
|
| 50 |
public void print(double s) { |
| 51 |
doIndent(); |
| 52 |
super.print(s); |
| 53 |
} |
| 54 |
|
| 55 |
public void print(float s) { |
| 56 |
doIndent(); |
| 57 |
super.print(s); |
| 58 |
} |
| 59 |
|
| 60 |
public void print(int s) { |
| 61 |
doIndent(); |
| 62 |
super.print(s); |
| 63 |
} |
| 64 |
|
| 65 |
public void print(long s) { |
| 66 |
doIndent(); |
| 67 |
super.print(s); |
| 68 |
} |
| 69 |
|
| 70 |
public void print(Object s) { |
| 71 |
doIndent(); |
| 72 |
super.print(s); |
| 73 |
} |
| 74 |
|
| 75 |
public void print(String s) { |
| 76 |
doIndent(); |
| 77 |
super.print(s); |
| 78 |
} |
| 79 |
|
| 80 |
public void println() { |
| 81 |
indented = false; |
| 82 |
super.println(); |
| 83 |
} |
| 84 |
|
| 85 |
// all other println's are implemented by the superclass in terms of print's |
| 86 |
|
| 87 |
private void doIndent() { |
| 88 |
if (indented) |
| 89 |
return; |
| 90 |
indented = true; |
| 91 |
for (int i = 0; i < indent; i++) |
| 92 |
super.print(indentString); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Increase the indentation |
| 97 |
*/ |
| 98 |
public void indent() { |
| 99 |
indent++; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Decrease the indentation |
| 104 |
*/ |
| 105 |
public void outdent() { |
| 106 |
indent--; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Return the current indent count |
| 111 |
* |
| 112 |
* @return current indentation level |
| 113 |
*/ |
| 114 |
public int getIndent() { |
| 115 |
return indent; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Set the current indent count |
| 120 |
* |
| 121 |
* @param level new level |
| 122 |
*/ |
| 123 |
public void setIndent(int level) { |
| 124 |
indent = level; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Return the current indentString |
| 129 |
* |
| 130 |
* @return indent string |
| 131 |
* |
| 132 |
* @see #setIndentString(String) |
| 133 |
*/ |
| 134 |
public String getIndentString() { |
| 135 |
return indentString; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Set the current indentString. Default is a single tab per indent level. |
| 140 |
* |
| 141 |
* @param indentString The characters to prefix each line with (repeated for |
| 142 |
* each indent level) |
| 143 |
*/ |
| 144 |
public void setIndentString(String indentString) { |
| 145 |
this.indentString = indentString; |
| 146 |
} |
| 147 |
|
| 148 |
private int indent = 0; |
| 149 |
|
| 150 |
private boolean indented = false; |
| 151 |
|
| 152 |
private String indentString = " "; |
| 153 |
|
| 154 |
} |