view src/kryshen/tema/ReplaceWriter.java @ 0:1d2fe61a3a62

Tema 0.1 (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 16 May 2006 18:04:09 +0400
parents
children
line source
1 /*
2 * Copyright (C) 2006 Mikhail A. Kryshen
3 *
4 * $Id: ReplaceWriter.java,v 1.1.1.1 2006/05/16 14:04:09 mikhail Exp $
5 */
7 package kryshen.tema;
9 import java.io.Writer;
10 import java.io.FilterWriter;
11 import java.io.IOException;
13 import java.util.LinkedList;
14 import java.util.Iterator;
16 /**
17 * FilterWriter which replaces characters with escape strings.
18 *
19 * @author Mikhail A. Kryshen
20 */
21 class ReplaceWriter extends FilterWriter {
22 private String[] patterns;
23 private String[] replaces;
25 private String prefix;
26 private String postfix;
28 private int maxPatternLength = 0;
30 /** Had this Writer processed any data? */
31 private boolean processedData = false;
33 private StringBuffer buffer = new StringBuffer();
35 public ReplaceWriter(Writer w, String pattern, String replace) {
36 this(w, new String[]{pattern}, new String[]{replace});
37 }
39 public ReplaceWriter(Writer w, String pattern, String replace,
40 String prefix, String postfix) {
42 this(w, new String[]{pattern}, new String[]{replace},
43 prefix, postfix);
44 }
46 public ReplaceWriter(Writer w, String[] patterns, String[] replaces) {
47 this(w, patterns, replaces, null, null);
48 }
50 public ReplaceWriter(Writer w, String[] patterns, String[] replaces,
51 String prefix, String postfix) {
52 super(w);
54 this.patterns = patterns;
55 this.replaces = replaces;
57 this.prefix = prefix;
58 this.postfix = postfix;
60 for (int i = 0; i < patterns.length; i++) {
61 int length = patterns[i].length();
62 if (length > maxPatternLength)
63 maxPatternLength = length;
64 }
65 }
67 private boolean processBuffer(int minLength) throws IOException {
68 if (buffer.length() == 0) return false;
70 if (!processedData) {
71 if (prefix != null)
72 super.write(prefix, 0, prefix.length());
73 processedData = true;
74 }
76 int k;
77 for (k = 0; minLength + k < buffer.length(); k++) {
78 for (int i = 0; i < patterns.length; i++) {
79 String pattern = patterns[i];
80 int length = pattern.length();
82 if (length + k > buffer.length())
83 continue;
85 boolean match = true;
87 for (int j = 0; j < length; j++) {
88 if (pattern.charAt(j) != buffer.charAt(j + k)) {
89 match = false;
90 break;
91 }
92 }
94 if (match) {
95 super.write(buffer.substring(0, k), 0, k);
96 buffer.delete(0, k + length);
97 super.write(replaces[i], 0, replaces[i].length());
98 return true;
99 }
100 }
101 }
103 super.write(buffer.substring(0, k), 0, k);
104 buffer.delete(0, k);
106 return false;
107 }
109 public void write(int c) throws IOException {
110 buffer.append((char)c);
111 processBuffer(maxPatternLength);
112 }
114 public void write(char[] cbuf, int off, int len) throws IOException {
115 for (int i = off; i < off + len; i++) {
116 buffer.append(cbuf[i]);
117 }
118 processBuffer(maxPatternLength);
119 }
121 public void write(String str, int off, int len) throws IOException {
122 buffer.append(str.substring(off, off + len));
123 processBuffer(maxPatternLength);
124 }
126 public void finish() throws IOException {
127 while (processBuffer(0));
129 // super.write(buffer.toString(), 0, buffer.length());
130 // buffer.delete(0, buffer.length());
132 if (processedData && postfix != null)
133 super.write(postfix, 0, postfix.length());
134 }
136 public void close() throws IOException {
137 finish();
138 super.close();
139 }
140 }