KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > generator > WrappingWriter


1 package de.webman.generator;
2
3 import java.io.*;
4
5 /**
6 Wrappes each line written to a Writer out with a pre- and a postfix.
7 Useful for instance to generate HTML-lized output.
8 Empty lines and a single newline-charcter are ignored.
9  * @author $Author: alex $
10  * @version $Revision: 1.1 $
11 */

12 public class WrappingWriter extends FilterWriter
13 {
14     protected String JavaDoc prefix;
15     protected int prefixLen;
16     
17     protected String JavaDoc postfix;
18     protected int postfixLen;
19     
20     private String JavaDoc newline;
21     
22     /**
23     Contsructor.
24     @param out the writer to wrap.
25     */

26     public WrappingWriter(Writer out)
27     {
28         super(out);
29         newline = System.getProperty("line.separator");
30     }
31    
32     /**
33     @param prefix sets the prefix
34     */

35     public void setPrefix(String JavaDoc prefix)
36     {
37         if (prefix == null) {
38             prefix = "";
39         }
40         
41         this.prefix = prefix;
42         prefixLen = prefix.length();
43     }
44
45        
46     /**
47     @param postfix sets the postfix
48     */

49     public void setPostfix(String JavaDoc postfix)
50     {
51         if (postfix == null) {
52             postfix = "";
53         }
54         
55         this.postfix = postfix;
56         postfixLen = postfix.length();
57     }
58
59     /**
60     Overwrites the filter writers write method to add the pre- and postfix.
61     Note that single newline chacters are ignored!
62     */

63     public void write(String JavaDoc str, int off, int len) throws IOException
64     {
65         if (str == null || str.equals("") || str.equals(newline) )
66             return;
67             
68         super.write(prefix + str + postfix, off, prefixLen + len + postfixLen);
69     }
70 }
71     
72
Popular Tags