view src/kryshen/tema/io/ReplaceWriter.java @ 15:e9d13c7ffeb1

Update header comment.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 24 Mar 2009 18:51:47 +0300
parents 6c41a0b43e58
children
line source
1 /*
2 * Copyright 2006-2009 Mikhail Kryshen
3 *
4 * This file is part of Tema.
5 *
6 * Tema is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * Tema is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the
17 * GNU Lesser General Public License along with Tema.
18 * If not, see <http://www.gnu.org/licenses/>.
19 */
21 package kryshen.tema.io;
23 import java.io.Writer;
24 import java.io.FilterWriter;
25 import java.io.IOException;
27 /**
28 * FilterWriter which replaces characters with escape strings.
29 *
30 * @author Mikhail Kryshen
31 */
32 public class ReplaceWriter extends FilterWriter {
33 private String[] patterns;
34 private String[] replaces;
36 private String prefix;
37 private String postfix;
39 private int maxPatternLength = 0;
41 /** Have this Writer processed any data? */
42 private boolean processedData = false;
44 private StringBuffer buffer = new StringBuffer();
46 public ReplaceWriter(Writer w, String pattern, String replace) {
47 this(w, new String[]{pattern}, new String[]{replace});
48 }
50 public ReplaceWriter(Writer w, String pattern, String replace,
51 String prefix, String postfix) {
53 this(w, new String[]{pattern}, new String[]{replace},
54 prefix, postfix);
55 }
57 public ReplaceWriter(Writer w, String[] patterns, String[] replaces) {
58 this(w, patterns, replaces, null, null);
59 }
61 public ReplaceWriter(Writer w, String[] patterns, String[] replaces,
62 String prefix, String postfix) {
63 super(w);
65 this.patterns = patterns;
66 this.replaces = replaces;
68 this.prefix = prefix;
69 this.postfix = postfix;
71 for (int i = 0; i < patterns.length; i++) {
72 int length = patterns[i].length();
73 if (length > maxPatternLength)
74 maxPatternLength = length;
75 }
76 }
78 private boolean processBuffer(int minLength) throws IOException {
79 if (buffer.length() == 0) return false;
81 if (!processedData) {
82 if (prefix != null)
83 super.write(prefix, 0, prefix.length());
84 processedData = true;
85 }
87 int k;
88 for (k = 0; minLength + k < buffer.length(); k++) {
89 for (int i = 0; i < patterns.length; i++) {
90 String pattern = patterns[i];
91 int length = pattern.length();
93 if (length + k > buffer.length())
94 continue;
96 boolean match = true;
98 for (int j = 0; j < length; j++) {
99 if (pattern.charAt(j) != buffer.charAt(j + k)) {
100 match = false;
101 break;
102 }
103 }
105 if (match) {
106 super.write(buffer.substring(0, k), 0, k);
107 buffer.delete(0, k + length);
108 super.write(replaces[i], 0, replaces[i].length());
109 return true;
110 }
111 }
112 }
114 super.write(buffer.substring(0, k), 0, k);
115 buffer.delete(0, k);
117 return false;
118 }
120 public void write(int c) throws IOException {
121 buffer.append((char)c);
122 processBuffer(maxPatternLength);
123 }
125 public void write(char[] cbuf, int off, int len) throws IOException {
126 for (int i = off; i < off + len; i++) {
127 buffer.append(cbuf[i]);
128 }
129 processBuffer(maxPatternLength);
130 }
132 public void write(String str, int off, int len) throws IOException {
133 buffer.append(str.substring(off, off + len));
134 processBuffer(maxPatternLength);
135 }
137 public void finish() throws IOException {
138 while (processBuffer(0));
140 // super.write(buffer.toString(), 0, buffer.length());
141 // buffer.delete(0, buffer.length());
143 if (processedData && postfix != null)
144 super.write(postfix, 0, postfix.length());
145 }
147 public void close() throws IOException {
148 finish();
149 super.close();
150 }
151 }