view src/kryshen/tema/io/ReplaceWriter.java @ 2:6c41a0b43e58

Tema 0.3 (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 19 Feb 2008 20:32:17 +0300
parents
children e9d13c7ffeb1
line source
1 /*
2 * Copyright 2006-2008 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 *
20 * $Id: ReplaceWriter.java,v 1.8 2008/02/19 16:21:00 mikhail Exp $
21 */
23 package kryshen.tema.io;
25 import java.io.Writer;
26 import java.io.FilterWriter;
27 import java.io.IOException;
29 /**
30 * FilterWriter which replaces characters with escape strings.
31 *
32 * @author Mikhail Kryshen
33 */
34 public class ReplaceWriter extends FilterWriter {
35 private String[] patterns;
36 private String[] replaces;
38 private String prefix;
39 private String postfix;
41 private int maxPatternLength = 0;
43 /** Have this Writer processed any data? */
44 private boolean processedData = false;
46 private StringBuffer buffer = new StringBuffer();
48 public ReplaceWriter(Writer w, String pattern, String replace) {
49 this(w, new String[]{pattern}, new String[]{replace});
50 }
52 public ReplaceWriter(Writer w, String pattern, String replace,
53 String prefix, String postfix) {
55 this(w, new String[]{pattern}, new String[]{replace},
56 prefix, postfix);
57 }
59 public ReplaceWriter(Writer w, String[] patterns, String[] replaces) {
60 this(w, patterns, replaces, null, null);
61 }
63 public ReplaceWriter(Writer w, String[] patterns, String[] replaces,
64 String prefix, String postfix) {
65 super(w);
67 this.patterns = patterns;
68 this.replaces = replaces;
70 this.prefix = prefix;
71 this.postfix = postfix;
73 for (int i = 0; i < patterns.length; i++) {
74 int length = patterns[i].length();
75 if (length > maxPatternLength)
76 maxPatternLength = length;
77 }
78 }
80 private boolean processBuffer(int minLength) throws IOException {
81 if (buffer.length() == 0) return false;
83 if (!processedData) {
84 if (prefix != null)
85 super.write(prefix, 0, prefix.length());
86 processedData = true;
87 }
89 int k;
90 for (k = 0; minLength + k < buffer.length(); k++) {
91 for (int i = 0; i < patterns.length; i++) {
92 String pattern = patterns[i];
93 int length = pattern.length();
95 if (length + k > buffer.length())
96 continue;
98 boolean match = true;
100 for (int j = 0; j < length; j++) {
101 if (pattern.charAt(j) != buffer.charAt(j + k)) {
102 match = false;
103 break;
104 }
105 }
107 if (match) {
108 super.write(buffer.substring(0, k), 0, k);
109 buffer.delete(0, k + length);
110 super.write(replaces[i], 0, replaces[i].length());
111 return true;
112 }
113 }
114 }
116 super.write(buffer.substring(0, k), 0, k);
117 buffer.delete(0, k);
119 return false;
120 }
122 public void write(int c) throws IOException {
123 buffer.append((char)c);
124 processBuffer(maxPatternLength);
125 }
127 public void write(char[] cbuf, int off, int len) throws IOException {
128 for (int i = off; i < off + len; i++) {
129 buffer.append(cbuf[i]);
130 }
131 processBuffer(maxPatternLength);
132 }
134 public void write(String str, int off, int len) throws IOException {
135 buffer.append(str.substring(off, off + len));
136 processBuffer(maxPatternLength);
137 }
139 public void finish() throws IOException {
140 while (processBuffer(0));
142 // super.write(buffer.toString(), 0, buffer.length());
143 // buffer.delete(0, buffer.length());
145 if (processedData && postfix != null)
146 super.write(postfix, 0, postfix.length());
147 }
149 public void close() throws IOException {
150 finish();
151 super.close();
152 }
153 }