001: /**************************************************************************/
002: /* NICE Testsuite */
003: /* A testsuite for the Nice programming language */
004: /* (c) Alex Greif 2002 */
005: /* */
006: /* This program is free software; you can redistribute it and/or modify */
007: /* it under the terms of the GNU General Public License as published by */
008: /* the Free Software Foundation; either version 2 of the License, or */
009: /* (at your option) any later version. */
010: /* */
011: /**************************************************************************/package nice.tools.testsuite.output;
012:
013: import java.io.*;
014: import java.util.*;
015:
016: /**
017: * Abstract representation of the test engine output.
018: * This class holds a reference to the underlying writer.
019: *
020: * @author Alex Greif <a href="mailto:alex.greif@web.de">alex.greif@web.de</a>
021: * @version $Id: AbstractOutput.java,v 1.4 2002/09/07 21:05:04 agreif Exp $
022: */
023: public abstract class AbstractOutput implements Output {
024:
025: /**
026: * Wrapper around the original writer.
027: *
028: */
029: private OutputWriter _writer;
030:
031: /**
032: * Creates an instance of AbstractWriter with the underlying writer.
033: *
034: * @param out TODO
035: */
036: public AbstractOutput(Writer out) {
037: _writer = new OutputWriter(out);
038: }
039:
040: /**
041: * Writes a string to the writer of this output.
042: *
043: * @param str TODO
044: */
045: private void write(String str) {
046: try {
047: _writer.write(str);
048: } catch (IOException e) {
049: e.printStackTrace();
050: }
051: }
052:
053: /**
054: * Marks the current position in the buffer of the writer of this output.
055: *
056: */
057: protected void mark() {
058: _writer.mark();
059: }
060:
061: /**
062: * Resets the buffer to the writer to the marked position.
063: *
064: */
065: protected void reset() {
066: _writer.reset();
067: }
068:
069: /**
070: * Closes the writer of the writer of this output.
071: *
072: */
073: public void close() {
074: try {
075: _writer.close();
076: } catch (IOException e) {
077: e.printStackTrace();
078: }
079: }
080:
081: /**
082: * Flushes the writer of this output.
083: *
084: */
085: protected void flush() {
086: try {
087: _writer.flush();
088: } catch (IOException e) {
089: e.printStackTrace();
090: }
091: }
092:
093: /**
094: * Logs a statement to this Output.
095: *
096: * @param statement TODO
097: */
098: public void log(String statement) {
099: log(null, statement);
100: }
101:
102: /**
103: * Logs a statement with the given prefix in angled braces.
104: *
105: * @param prefix TODO
106: * @param statement TODO
107: */
108: public void log(String prefix, String statement) {
109: if (statement.length() == 0) { // workaround, reader returns null for ""
110: write(getIndent());
111: write((prefix == null ? "" : "[" + prefix + "] ")
112: + statement + getLineBreak());
113: return;
114: }
115:
116: BufferedReader reader = null;
117: String line = "";
118: try {
119: reader = new BufferedReader(new StringReader(statement));
120: while ((line = reader.readLine()) != null) {
121: write(getIndent());
122: write((prefix == null ? "" : "[" + prefix + "] ")
123: + line + getLineBreak());
124: }
125: } catch (IOException e) {
126: e.printStackTrace();
127: } finally {
128: if (reader != null)
129: try {
130: reader.close();
131: } catch (IOException e) {
132: e.printStackTrace();
133: }
134: }
135: }
136:
137: /**
138: * Logs a statement to this output and flushes the writer.
139: *
140: * @param statement TODO
141: */
142: public void logAndFlush(String statement) {
143: log(null, statement);
144: flush();
145: }
146:
147: /**
148: * Logs a statement with the given prefix in angled braces and flushes the writer.
149: *
150: * @param prefix TODO
151: * @param statement TODO
152: */
153: public void logAndFlush(String prefix, String statement) {
154: log(prefix, statement);
155: flush();
156: }
157:
158: /**
159: * Returns the line break that is specific to this output.
160: * Default is the unix type linebreak
161: *
162: */
163: protected String getLineBreak() {
164: return "\n";
165: }
166:
167: /**
168: * Returns the indentation as string that should be used in this output format.
169: * The daultvalue is an empty string.
170: *
171: */
172: protected String getIndent() {
173: return "";
174: }
175:
176: }
|