Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 15 | arune | 1 | /** |
| 2 | * File created by Jimmy |
||
| 3 | * at 2004-jan-04 19:26:58 |
||
| 4 | */ |
||
| 5 | package Macbeth.Utilities; |
||
| 6 | |||
| 7 | import java.io.PrintStream; |
||
| 8 | import java.io.PipedOutputStream; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * This stream will format all text written to it, and then |
||
| 12 | * write the formatted text to the underlying PrintStream. |
||
| 13 | * The formatting that will be performed can be defined by using |
||
| 14 | * methods setPreString, setPostString and setLineBreakString. |
||
| 15 | * Per default, no formatting is performed at all. |
||
| 16 | * @author Jimmy |
||
| 17 | */ |
||
| 18 | public class TextFormatterStream extends PrintStream { |
||
| 19 | //the underlying stream |
||
| 20 | private PrintStream stream; |
||
| 21 | //a pre-string that will be appended _before_ everything that is written to this stream |
||
| 22 | private String preString; |
||
| 23 | //a post-string that will appended _after_ everything that is written to this stream |
||
| 24 | private String postString; |
||
| 25 | //any additional line break string (like <br> for instance) |
||
| 26 | private String lineBreakString; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Creates a new instance of TextFormatterStream. |
||
| 30 | * @param stream The underlying stream to which the |
||
| 31 | * formatted text should be written. |
||
| 32 | */ |
||
| 33 | public TextFormatterStream(PrintStream stream) { |
||
| 34 | //construct PrintWriter with non-used PipedOutputStream |
||
| 35 | super(new PipedOutputStream()); |
||
| 36 | //save reference to underlying stream |
||
| 37 | this.stream = stream; |
||
| 38 | //no pre- or post-strings per default |
||
| 39 | preString = ""; |
||
| 40 | postString = ""; |
||
| 41 | //use system line break per default |
||
| 42 | lineBreakString = MySystem.lineBreak; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Sets the pre-string that should be appended <b>before</b> |
||
| 47 | * everything else that is written to this stream. |
||
| 48 | * @param preString The pre-string. If you don't want any |
||
| 49 | * pre-string, set this to null. |
||
| 50 | */ |
||
| 51 | public void setPreString(String preString) { |
||
| 52 | this.preString = preString; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Sets the post-string that should be appended <b>after</b> |
||
| 57 | * everything else that is written to this stream. |
||
| 58 | * @param postString The post-string. If you don't want any |
||
| 59 | * post-string, set this to null. |
||
| 60 | */ |
||
| 61 | public void setPostString(String postString) { |
||
| 62 | this.postString = postString; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Sets the line break-string. Everytime you write a line |
||
| 67 | * to this stream, this line break string will also |
||
| 68 | * be written to the underlying stream. Per default this is |
||
| 69 | * the system line separator, like '\n' or '\n\r' (depending |
||
| 70 | * on the OS). |
||
| 71 | * @param lineBreakString The line break-string. |
||
| 72 | */ |
||
| 73 | public void setLineBreakString(String lineBreakString) { |
||
| 74 | this.lineBreakString = lineBreakString; |
||
| 75 | } |
||
| 76 | |||
| 77 | /* Override all standard print-methods: */ |
||
| 78 | |||
| 79 | public void println(char c[]) { |
||
| 80 | String str = preString + new String(c,0,c.length) + postString + lineBreakString; |
||
| 81 | if(c!=null) stream.print(str); |
||
| 82 | } |
||
| 83 | |||
| 84 | public void print(char c[]) { |
||
| 85 | String str = preString + new String(c,0,c.length) + postString; |
||
| 86 | if(c!=null) stream.print(str); |
||
| 87 | } |
||
| 88 | |||
| 89 | public void println(double d) { |
||
| 90 | String str = preString + d + postString + lineBreakString; |
||
| 91 | stream.print(str); |
||
| 92 | } |
||
| 93 | |||
| 94 | public void print(double d) { |
||
| 95 | String str = preString + d + postString; |
||
| 96 | stream.print(str); |
||
| 97 | } |
||
| 98 | |||
| 99 | public void println(float f) { |
||
| 100 | String str = preString + f + postString + lineBreakString; |
||
| 101 | stream.print(str); |
||
| 102 | } |
||
| 103 | |||
| 104 | public void print(float f) { |
||
| 105 | String str = preString + f + postString; |
||
| 106 | stream.print(str); |
||
| 107 | } |
||
| 108 | |||
| 109 | public void println(char c) { |
||
| 110 | String str = preString + c + postString + lineBreakString; |
||
| 111 | stream.print(str); |
||
| 112 | } |
||
| 113 | |||
| 114 | public void print(char c) { |
||
| 115 | String str = preString + c + postString; |
||
| 116 | stream.print(str); |
||
| 117 | } |
||
| 118 | |||
| 119 | public void println(boolean b) { |
||
| 120 | String str = preString + b + postString + lineBreakString; |
||
| 121 | stream.print(str); |
||
| 122 | } |
||
| 123 | |||
| 124 | public void print(boolean b) { |
||
| 125 | String str = preString + b + postString; |
||
| 126 | stream.print(str); |
||
| 127 | } |
||
| 128 | |||
| 129 | public void println(int i) { |
||
| 130 | String str = preString + i + postString + lineBreakString; |
||
| 131 | stream.print(str); |
||
| 132 | } |
||
| 133 | |||
| 134 | public void print(int i) { |
||
| 135 | String str = preString + i + postString; |
||
| 136 | stream.print(str); |
||
| 137 | } |
||
| 138 | |||
| 139 | public void println(String str) { |
||
| 140 | String string = preString + (str!=null ? str : "null") + postString + lineBreakString; |
||
| 141 | stream.print(string); |
||
| 142 | } |
||
| 143 | |||
| 144 | public void print(String str) { |
||
| 145 | String string = preString + (str!=null ? str : "null") + postString; |
||
| 146 | stream.print(string); |
||
| 147 | } |
||
| 148 | |||
| 149 | public void println(Object obj) { |
||
| 150 | String str = preString + (obj!=null ? obj.toString() : "null") + postString + lineBreakString; |
||
| 151 | stream.print(str); |
||
| 152 | } |
||
| 153 | |||
| 154 | public void print(Object obj) { |
||
| 155 | String str = preString + (obj!=null ? obj.toString() : "null") + postString; |
||
| 156 | stream.print(str); |
||
| 157 | } |
||
| 158 | |||
| 159 | } |