001/*
002 *  gnu/regexp/REFilterReader.java
003 *  Copyright (C) 2001 Lee Sau Dan
004 *  Based on gnu.regexp.REFilterInputStream by Wes Biggs
005 *
006 *  This library is free software; you can redistribute it and/or modify
007 *  it under the terms of the GNU Lesser General Public License as published
008 *  by the Free Software Foundation; either version 2.1 of the License, or
009 *  (at your option) any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 *  GNU Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public License
017 *  along with this program; if not, write to the Free Software
018 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019 */
020
021package gnu.regexp;
022import java.io.FilterReader;
023import java.io.Reader;
024
025/**
026 * Replaces instances of a given RE with replacement text. 
027 *
028 * @author <A HREF="http://www.csis.hku.hk/~sdlee/">Lee Sau Dan</A>
029 * @since gnu.regexp 1.1.0
030 */
031
032public class REFilterReader extends FilterReader {
033
034  private RE expr;
035  private String replace;
036  private String buffer;
037  private int bufpos;
038  private int offset;
039  private CharIndexedReader stream;
040
041  /**
042   * Creates an REFilterReader.  When reading from this stream,
043   * occurrences of patterns matching the supplied regular expression
044   * will be replaced with the supplied replacement text (the
045   * metacharacters $0 through $9 may be used to refer to the full
046   * match or subexpression matches.
047   *
048   * @param stream The Reader to be filtered.
049   * @param expr The regular expression to search for.
050   * @param replace The text pattern to replace matches with.  
051   */
052  public REFilterReader(Reader stream, RE expr, String replace) {
053    super(stream);
054    this.stream = new CharIndexedReader(stream,0);
055    this.expr = expr;
056    this.replace = replace;
057  }
058
059  /**
060   * Reads the next character from the stream per the general contract of
061   * Reader.read().  Returns -1 on error or end of stream.
062   */
063  public int read() {
064    // If we have buffered replace data, use it.
065    if ((buffer != null) && (bufpos < buffer.length())) {
066      return (int) buffer.charAt(bufpos++);
067    }
068
069    // check if input is at a valid position
070    if (!stream.isValid()) return -1;
071
072    REMatch mymatch = new REMatch(expr.getNumSubs(),offset,0);
073    if (expr.match(stream,mymatch)) {
074      mymatch.end[0] = mymatch.index;
075      mymatch.finish(stream);
076      stream.move(mymatch.toString().length());
077      offset += mymatch.toString().length();
078      buffer = mymatch.substituteInto(replace);
079      bufpos = 1;
080
081      if (buffer.length() > 0) {
082          return buffer.charAt(0);
083      }
084    }
085    char ch = stream.charAt(0);
086    if (ch == CharIndexed.OUT_OF_BOUNDS) return -1;
087    stream.move(1);
088    offset++;
089    return ch;
090  }
091
092  /** 
093   * Returns false.  REFilterReader does not support mark() and
094   * reset() methods. 
095   */
096  public boolean markSupported() {
097    return false;
098  }
099
100  /** Reads from the stream into the provided array. */
101  public int read(char[] b, int off, int len) {
102    int i;
103    int ok = 0;
104    while (len-- > 0) {
105      i = read();
106      if (i == -1) return (ok == 0) ? -1 : ok;
107      b[off++] = (char) i;
108      ok++;
109    }
110    return ok;
111  }
112
113  /** Reads from the stream into the provided array. */
114  public int read(char[] b) {
115    return read(b,0,b.length);
116  }
117}