01: package de.webman.generator;
02:
03: import java.io.*;
04:
05: /**
06: Wrappes each line written to a Writer out with a pre- and a postfix.
07: Useful for instance to generate HTML-lized output.
08: Empty lines and a single newline-charcter are ignored.
09: * @author $Author: alex $
10: * @version $Revision: 1.1 $
11: */
12: public class WrappingWriter extends FilterWriter {
13: protected String prefix;
14: protected int prefixLen;
15:
16: protected String postfix;
17: protected int postfixLen;
18:
19: private String newline;
20:
21: /**
22: Contsructor.
23: @param out the writer to wrap.
24: */
25: public WrappingWriter(Writer out) {
26: super (out);
27: newline = System.getProperty("line.separator");
28: }
29:
30: /**
31: @param prefix sets the prefix
32: */
33: public void setPrefix(String prefix) {
34: if (prefix == null) {
35: prefix = "";
36: }
37:
38: this .prefix = prefix;
39: prefixLen = prefix.length();
40: }
41:
42: /**
43: @param postfix sets the postfix
44: */
45: public void setPostfix(String postfix) {
46: if (postfix == null) {
47: postfix = "";
48: }
49:
50: this .postfix = postfix;
51: postfixLen = postfix.length();
52: }
53:
54: /**
55: Overwrites the filter writers write method to add the pre- and postfix.
56: Note that single newline chacters are ignored!
57: */
58: public void write(String str, int off, int len) throws IOException {
59: if (str == null || str.equals("") || str.equals(newline))
60: return;
61:
62: super.write(prefix + str + postfix, off, prefixLen + len
63: + postfixLen);
64: }
65: }
|