KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > cachius > support > TokenFilterWriter


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.cachius.support;
25
26 import java.io.BufferedWriter JavaDoc;
27 import java.io.FilterWriter JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.Writer JavaDoc;
30
31 /**
32  * A FilterWriter that replaces all occurences of a given token with a
33  * replacement string.
34  *
35  * @author Felix Gnass
36  */

37 public class TokenFilterWriter extends FilterWriter JavaDoc {
38
39     private final int writeBufferSize = 1024;
40     
41     private char[] writeBuffer;
42     
43     private char[] pattern;
44
45     private String JavaDoc replacement;
46     
47     private char[] buffer;
48     
49     private int index = 0;
50     
51         
52     public TokenFilterWriter(String JavaDoc token, String JavaDoc replacement, Writer JavaDoc out) {
53         this(token, replacement, new BufferedWriter JavaDoc(out));
54     }
55     
56     public TokenFilterWriter(String JavaDoc token, String JavaDoc replacement,
57             BufferedWriter JavaDoc out) {
58             
59         super(out);
60         this.pattern = token.toCharArray();
61         this.replacement = replacement;
62         this.buffer = new char[pattern.length];
63     }
64     
65     public void write(char[] buf, int offset, int len) throws IOException JavaDoc {
66         for (int i = offset; i < len; i++) {
67             write(buf[i]);
68         }
69     }
70     
71     public synchronized void write(String JavaDoc str, int off, int len)
72             throws IOException JavaDoc {
73         
74         char cbuf[];
75         if (len <= writeBufferSize) {
76             if (writeBuffer == null) {
77                 writeBuffer = new char[writeBufferSize];
78             }
79             cbuf = writeBuffer;
80         }
81         else {
82             cbuf = new char[len];
83         }
84         str.getChars(off, (off + len), cbuf, 0);
85         write(cbuf, 0, len);
86     }
87         
88     public void write(int c) throws IOException JavaDoc {
89         if (c == pattern[index]) {
90             buffer[index] = (char) c;
91             index++;
92             if (index == pattern.length) {
93                 out.write(replacement);
94                 index = 0;
95             }
96         }
97         else {
98             if (index > 0) {
99                 out.write(buffer, 0, index);
100                 index = 0;
101             }
102             out.write(c);
103         }
104     }
105     
106 }
107
Popular Tags