01: // This file is part of KeY - Integrated Deductive Software Design
02: // Copyright (C) 2001-2007 Universitaet Karlsruhe, Germany
03: // Universitaet Koblenz-Landau, Germany
04: // Chalmers University of Technology, Sweden
05: //
06: // The KeY system is protected by the GNU General Public License.
07: // See LICENSE.TXT for details.
08: //
09: //
10: package de.uka.ilkd.key.util.pp;
11:
12: /** A {@link Backend} which appends all output to a StringBuffer.
13: * The {@link #mark(Object o)} method does nothing in this
14: * implementation. There is a method {@link #count()} which returns
15: * the number of characters written by this so far.
16: * The method {@link #getString()} gets the output written so far.
17: */
18: public class StringBackend implements Backend {
19: protected StringBuffer out;
20: protected int initOutLength;
21: protected int lineWidth;
22:
23: /** Create a new StringBackend. This will append all output to
24: * the given StringBuffer <code>sb</code>. */
25: public StringBackend(StringBuffer sb, int lineWidth) {
26: this .lineWidth = lineWidth;
27: this .out = sb;
28: this .initOutLength = sb.length();
29: }
30:
31: /** Create a new StringBackend. This will accumulate output in
32: * a fresh, private StringBuffer. */
33: public StringBackend(int lineWidth) {
34: this (new StringBuffer(lineWidth), lineWidth);
35: }
36:
37: /** Append a String <code>s</code> to the output. <code>s</code>
38: * contains no newlines. */
39: public void print(String s) throws java.io.IOException {
40: out.append(s);
41: }
42:
43: /** Start a new line. */
44: public void newLine() throws java.io.IOException {
45: out.append('\n');
46: }
47:
48: /** Closes this backend */
49: public void close() throws java.io.IOException {
50: return;
51: }
52:
53: /** Flushes any buffered output */
54: public void flush() throws java.io.IOException {
55: return;
56: }
57:
58: /** Gets called to record a <code>mark()</code> call in the input. */
59: public void mark(Object o) {
60: return;
61: }
62:
63: /** Returns the number of characters written through this backend.*/
64: public int count() {
65: return out.length() - initOutLength;
66: }
67:
68: /** Returns the available space per line */
69: public int lineWidth() {
70: return lineWidth;
71: }
72:
73: /** Returns the space required to print the String <code>s</code> */
74: public int measure(String s) {
75: return s.length();
76: }
77:
78: /** Returns the accumulated output */
79: public String getString() {
80: return out.toString();
81: }
82: }
|