Source code: nice/tools/testsuite/output/AbstractOutput.java
1 /**************************************************************************/
2 /* NICE Testsuite */
3 /* A testsuite for the Nice programming language */
4 /* (c) Alex Greif 2002 */
5 /* */
6 /* This program is free software; you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation; either version 2 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /**************************************************************************/
12
13 package nice.tools.testsuite.output;
14
15
16 import java.io.*;
17 import java.util.*;
18
19
20
21 /**
22 * Abstract representation of the test engine output.
23 * This class holds a reference to the underlying writer.
24 *
25 * @author Alex Greif <a href="mailto:alex.greif@web.de">alex.greif@web.de</a>
26 * @version $Id: AbstractOutput.java,v 1.4 2002/09/07 21:05:04 agreif Exp $
27 */
28 public abstract class AbstractOutput implements Output {
29
30
31 /**
32 * Wrapper around the original writer.
33 *
34 */
35 private OutputWriter _writer;
36
37 /**
38 * Creates an instance of AbstractWriter with the underlying writer.
39 *
40 * @param out TODO
41 */
42 public AbstractOutput(Writer out) {
43 _writer = new OutputWriter(out);
44 }
45
46
47 /**
48 * Writes a string to the writer of this output.
49 *
50 * @param str TODO
51 */
52 private void write(String str) {
53 try {
54 _writer.write(str);
55 } catch(IOException e) {
56 e.printStackTrace();
57 }
58 }
59
60
61 /**
62 * Marks the current position in the buffer of the writer of this output.
63 *
64 */
65 protected void mark() {
66 _writer.mark();
67 }
68
69
70 /**
71 * Resets the buffer to the writer to the marked position.
72 *
73 */
74 protected void reset() {
75 _writer.reset();
76 }
77
78
79 /**
80 * Closes the writer of the writer of this output.
81 *
82 */
83 public void close() {
84 try {
85 _writer.close();
86 } catch(IOException e) {
87 e.printStackTrace();
88 }
89 }
90
91
92 /**
93 * Flushes the writer of this output.
94 *
95 */
96 protected void flush() {
97 try {
98 _writer.flush();
99 } catch(IOException e) {
100 e.printStackTrace();
101 }
102 }
103
104
105
106 /**
107 * Logs a statement to this Output.
108 *
109 * @param statement TODO
110 */
111 public void log(String statement) {
112 log(null, statement);
113 }
114
115
116 /**
117 * Logs a statement with the given prefix in angled braces.
118 *
119 * @param prefix TODO
120 * @param statement TODO
121 */
122 public void log(String prefix, String statement) {
123 if (statement.length() == 0) { // workaround, reader returns null for ""
124 write(getIndent());
125 write((prefix == null ? "" : "["+prefix+"] ") + statement + getLineBreak());
126 return;
127 }
128
129 BufferedReader reader = null;
130 String line = "";
131 try {
132 reader = new BufferedReader(new StringReader(statement));
133 while((line = reader.readLine()) != null) {
134 write(getIndent());
135 write((prefix == null ? "" : "["+prefix+"] ") + line + getLineBreak());
136 }
137 } catch(IOException e) {
138 e.printStackTrace();
139 } finally {
140 if (reader != null)
141 try {
142 reader.close();
143 } catch(IOException e) {
144 e.printStackTrace();
145 }
146 }
147 }
148
149
150 /**
151 * Logs a statement to this output and flushes the writer.
152 *
153 * @param statement TODO
154 */
155 public void logAndFlush(String statement) {
156 log(null, statement);
157 flush();
158 }
159
160
161 /**
162 * Logs a statement with the given prefix in angled braces and flushes the writer.
163 *
164 * @param prefix TODO
165 * @param statement TODO
166 */
167 public void logAndFlush(String prefix, String statement) {
168 log(prefix, statement);
169 flush();
170 }
171
172
173
174
175
176 /**
177 * Returns the line break that is specific to this output.
178 * Default is the unix type linebreak
179 *
180 */
181 protected String getLineBreak() {
182 return "\n";
183 }
184
185 /**
186 * Returns the indentation as string that should be used in this output format.
187 * The daultvalue is an empty string.
188 *
189 */
190 protected String getIndent() {
191 return "";
192 }
193
194
195 }
196
197
198