Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/memoire/re/REFilterInputStream.java


1   
2   
3   package com.memoire.re;
4   import com.memoire.re.*;
5   
6   /*
7    *  gnu/regexp/REFilterInputStream.java
8    *  Copyright (C) 1998 Wes Biggs
9    *
10   *  This library is free software; you can redistribute it and/or modify
11   *  it under the terms of the GNU Library General Public License as published
12   *  by the Free Software Foundation; either version 2 of the License, or
13   *  (at your option) any later version.
14   *
15   *  This library is distributed in the hope that it will be useful,
16   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   *  GNU Library General Public License for more details.
19   *
20   *  You should have received a copy of the GNU Library General Public License
21   *  along with this program; if not, write to the Free Software
22   *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23   */
24  
25  
26  import java.io.FilterInputStream;
27  import java.io.InputStream;
28  
29  /**
30   * Replaces instances of a given RE with replacement text. 
31   *
32   * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
33   * @since gnu.regexp 1.0.5
34   */
35  
36  public class REFilterInputStream extends FilterInputStream {
37  
38    private RE m_expr;
39    private String m_replace;
40    private String m_buffer;
41    private int m_bufpos;
42    private int m_offset;
43    private RECharIndexedInputStream m_stream;
44  
45    /**
46     * Creates an REFilterInputStream.  When reading from this stream,
47     * occurrences of patterns matching the supplied regular expression
48     * will be replaced with the supplied replacement text (the
49     * metacharacters $0 through $9 may be used to refer to the full
50     * match or subexpression matches.
51     *
52     * @param f_stream The InputStream to be filtered.
53     * @param f_expr The regular expression to search for.
54     * @param f_replace The text pattern to replace matches with.  
55     */
56    public REFilterInputStream(InputStream f_stream, RE f_expr, String f_replace) {
57      super(f_stream);
58      m_stream = new RECharIndexedInputStream(f_stream,0);
59      m_expr = f_expr;
60      m_replace = f_replace;
61    }
62  
63    /**
64     * Reads the next byte from the stream per the general contract of
65     * InputStream.read().  Returns -1 on error or end of stream.
66     */
67    public int read() {
68      // If we have buffered replace data, use it.
69      if ((m_buffer != null) && (m_bufpos < m_buffer.length())) {
70        return (int) m_buffer.charAt(m_bufpos++);
71      }
72  
73      // check if input is at a valid position
74      if (!m_stream.isValid()) return -1;
75  
76      REMatch mymatch = new REMatch(m_expr.getNumSubs(),m_offset);
77      int[] result = m_expr.match(m_stream,0,0,mymatch);
78      if (result != null) {
79        mymatch.end[0] = result[0];
80        mymatch.finish(m_stream);
81        m_stream.move(mymatch.toString().length());
82        m_offset += mymatch.toString().length();
83        m_buffer = mymatch.substituteInto(m_replace);
84        m_bufpos = 1;
85  
86        // This is prone to infinite loops if replace string turns out empty.
87        return m_buffer.charAt(0);
88      } else {
89        char ch = m_stream.charAt(0);
90        if (ch == RECharIndexed.OUT_OF_BOUNDS) return -1;
91        m_stream.move(1);
92        m_offset++;
93        return ch;
94      }
95    }
96  
97    /** 
98     * Returns false.  REFilterInputStream does not support mark() and
99     * reset() methods. 
100    */
101   public boolean markSupported() {
102     return false;
103   }
104 
105   /** Reads from the stream into the provided array. */
106   public int read(byte[] b, int off, int len) {
107     int i;
108     int ok = 0;
109     while (len-- > 0) {
110       i = read();
111       if (i == -1) return (ok == 0) ? -1 : ok;
112       b[off++] = (byte) i;
113       ok++;
114     }
115     return ok;
116   }
117 
118   /** Reads from the stream into the provided array. */
119   public int read(byte[] b) {
120     return read(b,0,b.length);
121   }
122 }