2 // This software is now distributed according to
3 // the Lesser Gnu Public License. Please see
4 // http://www.gnu.org/copyleft/lesser.txt for
8 package com.stevesoft.pat;
10 import java.io.IOException;
11 import java.io.Writer;
13 import com.stevesoft.pat.wrap.WriterWrap;
16 * A basic extension of FilterWriter that uses Transformer to make replacements
17 * in data as it is written out. It attempts to transform a string whenever the
18 * End-of-Line (EOL) character is written (which is, by default, the carriage
19 * return '\n'). Only the transformed portion of the line is written out,
20 * allowing the RegexWriter to wait until a complete pattern is present before
21 * attempting to write out info. Until a pattern completes, data is stored in a
22 * StringBuffer -- which can be accessed through the length() and charAt()
23 * methods of this class.
25 * Note a subtlety here -- while a Transformer normally matches at higher
26 * priority against the pattern added to it first, this will not necessarily be
27 * true when a multi-line match is in progress because one of the complete
28 * multi-line patterns may not be completely loaded in RegexWriter's buffer. For
29 * this reason, the Transformer class is equipped with a way to add a pattern
30 * and replacement rule in three pieces -- a beginning (once this matches,
31 * nothing else in the Transformer can match until the whole pattern matches),
32 * an ending (the whole pattern is a String formed by adding the beginning and
33 * ending), and a ReplaceRule.
35 * An illustration of this is given in the this <a
36 * href="../test/trans.java">example.</a>
38 public class RegexWriter extends Writer
46 StringBuffer sb = new StringBuffer();
48 PartialBuffer wrap = new PartialBuffer(sb);
54 int bufferSize = 2 * 1024;
56 public RegexWriter(Transformer t, Writer w)
59 ww = new WriterWrap(w);
60 repr = t.getReplacer();
61 repr.setBuffer(new StringBufferLike(ww));
65 public RegexWriter(Regex r, Writer w)
68 ww = new WriterWrap(w);
69 repr = r.getReplacer();
70 repr.setBuffer(new StringBufferLike(ww));
77 * This method no longer serves any purpose.
82 public char getEOLchar()
88 * This method no longer serves any purpose.
93 public void setEOLchar(char c)
101 * This method no longer serves any purpose.
106 public int getMaxLines()
112 * This method no longer serves any purpose.
117 public void setMaxLines(int ml)
122 void write() throws IOException
124 Regex rex = repr.getRegex();
126 if (rex.matchAt(wrap, epos) && !wrap.overRun)
130 w.write(sb.charAt(pos++));
132 int to = rex.matchedTo();
134 repr.apply(rex, rex.getReplaceRule());
136 if (epos == eposOld && epos < sb.length())
141 else if (!wrap.overRun && epos < sb.length())
147 w.write(sb.charAt(pos++));
149 if (epos == sb.length())
154 else if (pos > bufferSize)
156 for (int i = bufferSize; i < sb.length(); i++)
158 sb.setCharAt(i - bufferSize, sb.charAt(i));
162 sb.setLength(sb.length() - bufferSize);
166 public void write(char[] ca, int b, int n) throws IOException
169 for (int i = b; i < m; i++)
172 if (sb.length() % interval == interval - 1)
174 wrap.overRun = false;
175 while (epos + interval < sb.length() && !wrap.overRun)
183 public void flush() throws IOException
187 public void close() throws IOException
189 wrap.allowOverRun = false;
190 wrap.overRun = false;
191 while (epos < sb.length())
199 /** The current size of the StringBuffer in use by RegexWriter. */
205 /** The character at location i in the StringBuffer. */
206 public char charAt(int i)
211 /** Set the interval at which regex patterns are checked. */
212 public void setInterval(int i)
217 /** Get the interval at which regex matches are checked. */
218 public int getInterval()
223 /** Get the buffer size. */
224 public int getBufferSize()
229 /** Set the buffer size. */
230 public void setBufferSize(int i)