1 /*******************************************************************************
2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $(date) The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
20 *******************************************************************************/
22 // This software is now distributed according to
23 // the Lesser Gnu Public License. Please see
24 // http://www.gnu.org/copyleft/lesser.txt for
26 // -- Happy Computing!
28 package com.stevesoft.pat;
30 import java.io.IOException;
31 import java.io.Reader;
33 import com.stevesoft.pat.wrap.StringBufferWrap;
36 * This class allows you to replace the text in strings as you read them in. Be
37 * careful what you do with this freedom... using Regex.perlCode("s{.*}{x}s") as
38 * your pattern will result in loading the entire contents of the Reader into
41 public class RegexReader extends Reader
43 RBuffer rb = new RBuffer(new StringBuffer());
45 PartialBuffer wrap = new PartialBuffer(rb.sb);
47 boolean moreToRead = true;
56 public RegexReader(Regex rex, Reader r)
59 rp = rex.getReplacer();
62 public RegexReader(Transformer tex, Reader r)
65 rp = tex.getReplacer();
68 public void reset() throws IOException
71 rb = new RBuffer(new StringBuffer());
72 wrap = new PartialBuffer(rb.sb);
76 void readData() throws IOException
80 while ((c = r.read()) != -1)
82 rb.sb.append((char) c);
88 if (c == -1 && n == 0)
91 wrap.allowOverRun = false;
95 void getMoreData() throws IOException
97 while (rb.pos >= rb.epos)
108 else if (rb.epos >= rb.sb.length() && rb.epos > nmax)
115 else if (rb.epos >= rb.sb.length() && moreToRead)
119 else if (rp.getRegex().matchAt(wrap, rb.epos))
127 StringBufferWrap sbw = new StringBufferWrap();
128 StringBufferLike sbl = new StringBufferLike(sbw);
130 * ReplaceRule rr = rex.getReplaceRule(); while(rr != null) {
131 * rr.apply(sbl,rex); rr = rr.next; }
133 Regex rex = rp.getRegex();
134 int npos = rex.matchedTo();
138 rp.apply(rex, rex.getReplaceRule());
140 RBuffer rb2 = new RBuffer((StringBuffer) sbw.unwrap());
141 rb2.epos = rb2.sb.length();
142 RBuffer rb3 = new RBuffer(rb.sb);
150 if (rb3.epos > rb3.sb.length())
152 if (rb.pos >= rb.epos)
156 rb3.pos = rb3.epos = 0;
164 rb3.pos = rb3.epos = npos;
175 else if (rb.epos < rb.sb.length())
187 public int read() throws IOException
189 if (rb.pos >= rb.epos)
192 if (rb.pos >= rb.epos)
197 // System.out.println(rb);
198 return rb.sb.charAt(rb.pos++);
201 public int read(char[] buf, int off, int len) throws IOException
205 for (int i = off; i < end; i++)
221 public void close() throws IOException
226 public boolean markSupported()
232 * Get the size of the working buffer. The current buffer may be larger if the
233 * pattern demands it.
235 public int getBufferSize()
241 * Set the size of the working buffer. The current buffer may be larger if the
242 * pattern demands it.
244 public void setBufferSize(int n)
252 * This function no longer serves any purpose.
256 public int getMaxLines()
262 * This function no longer serves any purpose.
266 public void setMaxLines(int ml)
274 * This function no longer serves any purpose.
278 public char getEOLchar()
284 * This function no longer serves any purpose.
288 public void setEOLchar(char c)
293 public long skip(long d) throws IOException
295 // This is probably inefficient, I just did it
296 // this way to avoid possible bugs.
298 while (n < d && read() != -1)
306 * static void test(String re,String inp,int n) throws Exception { Reader r =
307 * new StringReader(inp); r = new BufferedReader(r); Regex rex =
308 * Regex.perlCode(re); String res1 = rex.replaceAll(inp); int c = -1;
309 * StringBuffer sb = new StringBuffer(); RegexReader rr = new
310 * RegexReader(rex,r); rr.setBufferSize(n); while( (c = rr.read()) != -1)
311 * sb.append((char)c); String res2 = sb.toString(); if(!res1.equals(res2)) {
312 * System.out.println("nmax="+n); System.out.println("re="+re);
313 * System.out.println("inp="+inp); System.out.println("res1="+res1);
314 * System.out.println("res2="+res2); System.exit(255); } } public static void
315 * main(String[] args) throws Exception { for(int n=6;n<15;n++) {
316 * test("s/x/y/","-----x123456789",n); test("s/x/y/","x123456789",n);
317 * test("s/x/y/","-----x",n);
318 * test("s/x.*?x/y/",".xx..x..x...x...x....x....x",n);
319 * test("s/x.*x/[$&]/","--x........x--xx",n);
320 * test("s/x.*x/[$&]/","--x........x------",n);
321 * test("s/.$/a/m","bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbbbbbbbbbbbb",n);
322 * test("s/.$/a/","123",n);
323 * test("s/.$/a/","bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb",n);
324 * test("s/^./a/","bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb",n);
325 * test("s/$/a/","bbb",n); test("s/^/a/","bbb",n); test("s/^/a/","",n);
326 * test("s{.*}{N}","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",n);
327 * test("s/.{0,7}/y/","AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",n);
328 * test("s/x/$&/","xxx",n); } System.out.println("Success!!!"); }