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