/interpreter/tags/at2-build060407/src/edu/vub/util/regexp/REFilterInputStream.java

http://ambienttalk.googlecode.com/ · Java · 140 lines · 57 code · 13 blank · 70 comment · 12 complexity · 7f8a74b16e3a42752ccc6a2a833339c1 MD5 · raw file

  1. /* gnu/regexp/REFilterInputStream.java
  2. Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package edu.vub.util.regexp;
  32. import java.io.FilterInputStream;
  33. import java.io.InputStream;
  34. /**
  35. * Replaces instances of a given RE found within an InputStream
  36. * with replacement text. The replacements are interpolated into the
  37. * stream when a match is found.
  38. *
  39. * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
  40. * @deprecated This class cannot properly handle all character
  41. * encodings. For proper handling, use the REFilterReader
  42. * class instead.
  43. */
  44. public class REFilterInputStream extends FilterInputStream {
  45. private RE expr;
  46. private String replace;
  47. private String buffer;
  48. private int bufpos;
  49. private int offset;
  50. private CharIndexedInputStream stream;
  51. /**
  52. * Creates an REFilterInputStream. When reading from this stream,
  53. * occurrences of patterns matching the supplied regular expression
  54. * will be replaced with the supplied replacement text (the
  55. * metacharacters $0 through $9 may be used to refer to the full
  56. * match or subexpression matches).
  57. *
  58. * @param stream The InputStream to be filtered.
  59. * @param expr The regular expression to search for.
  60. * @param replace The text pattern to replace matches with.
  61. */
  62. public REFilterInputStream(InputStream stream, RE expr, String replace) {
  63. super(stream);
  64. this.stream = new CharIndexedInputStream(stream,0);
  65. this.expr = expr;
  66. this.replace = replace;
  67. }
  68. /**
  69. * Reads the next byte from the stream per the general contract of
  70. * InputStream.read(). Returns -1 on error or end of stream.
  71. */
  72. public int read() {
  73. // If we have buffered replace data, use it.
  74. if ((buffer != null) && (bufpos < buffer.length())) {
  75. return (int) buffer.charAt(bufpos++);
  76. }
  77. // check if input is at a valid position
  78. if (!stream.isValid()) return -1;
  79. REMatch mymatch = new REMatch(expr.getNumSubs(),offset,0);
  80. if (expr.match(stream, mymatch)) {
  81. mymatch.end[0] = mymatch.index;
  82. mymatch.finish(stream);
  83. stream.move(mymatch.toString().length());
  84. offset += mymatch.toString().length();
  85. buffer = mymatch.substituteInto(replace);
  86. bufpos = 1;
  87. // This is prone to infinite loops if replace string turns out empty.
  88. if (buffer.length() > 0) {
  89. return buffer.charAt(0);
  90. }
  91. }
  92. char ch = stream.charAt(0);
  93. if (ch == CharIndexed.OUT_OF_BOUNDS) return -1;
  94. stream.move(1);
  95. offset++;
  96. return ch;
  97. }
  98. /**
  99. * Returns false. REFilterInputStream does not support mark() and
  100. * reset() methods.
  101. */
  102. public boolean markSupported() {
  103. return false;
  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. /** Reads from the stream into the provided array. */
  118. public int read(byte[] b) {
  119. return read(b,0,b.length);
  120. }
  121. }