view src/kryshen/tema/functions/ReplaceWriter.java @ 1:548a93c24e55

Tema 0.1jk - Javakonkurs edition (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Thu, 14 Dec 2006 23:22:05 +0300
parents
children
line source
1 /*
2 * Copyright (C) 2006 Mikhail A. Kryshen
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * $Id: ReplaceWriter.java,v 1.2 2006/12/14 14:39:26 mikhail Exp $
19 */
21 package kryshen.tema.functions;
23 import java.io.Writer;
24 import java.io.FilterWriter;
25 import java.io.IOException;
27 import java.util.LinkedList;
28 import java.util.Iterator;
30 /**
31 * FilterWriter which replaces characters with escape strings.
32 *
33 * @author Mikhail A. Kryshen
34 */
35 class ReplaceWriter extends FilterWriter {
36 private String[] patterns;
37 private String[] replaces;
39 private String prefix;
40 private String postfix;
42 private int maxPatternLength = 0;
44 /** Have this Writer processed any data? */
45 private boolean processedData = false;
47 private StringBuffer buffer = new StringBuffer();
49 public ReplaceWriter(Writer w, String pattern, String replace) {
50 this(w, new String[]{pattern}, new String[]{replace});
51 }
53 public ReplaceWriter(Writer w, String pattern, String replace,
54 String prefix, String postfix) {
56 this(w, new String[]{pattern}, new String[]{replace},
57 prefix, postfix);
58 }
60 public ReplaceWriter(Writer w, String[] patterns, String[] replaces) {
61 this(w, patterns, replaces, null, null);
62 }
64 public ReplaceWriter(Writer w, String[] patterns, String[] replaces,
65 String prefix, String postfix) {
66 super(w);
68 this.patterns = patterns;
69 this.replaces = replaces;
71 this.prefix = prefix;
72 this.postfix = postfix;
74 for (int i = 0; i < patterns.length; i++) {
75 int length = patterns[i].length();
76 if (length > maxPatternLength)
77 maxPatternLength = length;
78 }
79 }
81 private boolean processBuffer(int minLength) throws IOException {
82 if (buffer.length() == 0) return false;
84 if (!processedData) {
85 if (prefix != null)
86 super.write(prefix, 0, prefix.length());
87 processedData = true;
88 }
90 int k;
91 for (k = 0; minLength + k < buffer.length(); k++) {
92 for (int i = 0; i < patterns.length; i++) {
93 String pattern = patterns[i];
94 int length = pattern.length();
96 if (length + k > buffer.length())
97 continue;
99 boolean match = true;
101 for (int j = 0; j < length; j++) {
102 if (pattern.charAt(j) != buffer.charAt(j + k)) {
103 match = false;
104 break;
105 }
106 }
108 if (match) {
109 super.write(buffer.substring(0, k), 0, k);
110 buffer.delete(0, k + length);
111 super.write(replaces[i], 0, replaces[i].length());
112 return true;
113 }
114 }
115 }
117 super.write(buffer.substring(0, k), 0, k);
118 buffer.delete(0, k);
120 return false;
121 }
123 public void write(int c) throws IOException {
124 buffer.append((char)c);
125 processBuffer(maxPatternLength);
126 }
128 public void write(char[] cbuf, int off, int len) throws IOException {
129 for (int i = off; i < off + len; i++) {
130 buffer.append(cbuf[i]);
131 }
132 processBuffer(maxPatternLength);
133 }
135 public void write(String str, int off, int len) throws IOException {
136 buffer.append(str.substring(off, off + len));
137 processBuffer(maxPatternLength);
138 }
140 public void finish() throws IOException {
141 while (processBuffer(0));
143 // super.write(buffer.toString(), 0, buffer.length());
144 // buffer.delete(0, buffer.length());
146 if (processedData && postfix != null)
147 super.write(postfix, 0, postfix.length());
148 }
150 public void close() throws IOException {
151 finish();
152 super.close();
153 }
154 }