PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/gnu/regexp/REFilterReader.java

#
Java | 117 lines | 57 code | 12 blank | 48 comment | 12 complexity | 357a8e1a089c3026c19698a275c2151b MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * gnu/regexp/REFilterReader.java
  3. * Copyright (C) 2001 Lee Sau Dan
  4. * Based on gnu.regexp.REFilterInputStream by Wes Biggs
  5. *
  6. * This library is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published
  8. * by the Free Software Foundation; either version 2.1 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. package gnu.regexp;
  21. import java.io.FilterReader;
  22. import java.io.Reader;
  23. /**
  24. * Replaces instances of a given RE with replacement text.
  25. *
  26. * @author <A HREF="http://www.csis.hku.hk/~sdlee/">Lee Sau Dan</A>
  27. * @since gnu.regexp 1.1.0
  28. */
  29. public class REFilterReader extends FilterReader {
  30. private RE expr;
  31. private String replace;
  32. private String buffer;
  33. private int bufpos;
  34. private int offset;
  35. private CharIndexedReader stream;
  36. /**
  37. * Creates an REFilterReader. When reading from this stream,
  38. * occurrences of patterns matching the supplied regular expression
  39. * will be replaced with the supplied replacement text (the
  40. * metacharacters $0 through $9 may be used to refer to the full
  41. * match or subexpression matches.
  42. *
  43. * @param stream The Reader to be filtered.
  44. * @param expr The regular expression to search for.
  45. * @param replace The text pattern to replace matches with.
  46. */
  47. public REFilterReader(Reader stream, RE expr, String replace) {
  48. super(stream);
  49. this.stream = new CharIndexedReader(stream,0);
  50. this.expr = expr;
  51. this.replace = replace;
  52. }
  53. /**
  54. * Reads the next character from the stream per the general contract of
  55. * Reader.read(). Returns -1 on error or end of stream.
  56. */
  57. public int read() {
  58. // If we have buffered replace data, use it.
  59. if ((buffer != null) && (bufpos < buffer.length())) {
  60. return (int) buffer.charAt(bufpos++);
  61. }
  62. // check if input is at a valid position
  63. if (!stream.isValid()) return -1;
  64. REMatch mymatch = new REMatch(expr.getNumSubs(),offset,0);
  65. if (expr.match(stream,mymatch)) {
  66. mymatch.end[0] = mymatch.index;
  67. mymatch.finish(stream);
  68. stream.move(mymatch.toString().length());
  69. offset += mymatch.toString().length();
  70. buffer = mymatch.substituteInto(replace);
  71. bufpos = 1;
  72. if (buffer.length() > 0) {
  73. return buffer.charAt(0);
  74. }
  75. }
  76. char ch = stream.charAt(0);
  77. if (ch == CharIndexed.OUT_OF_BOUNDS) return -1;
  78. stream.move(1);
  79. offset++;
  80. return ch;
  81. }
  82. /**
  83. * Returns false. REFilterReader does not support mark() and
  84. * reset() methods.
  85. */
  86. public boolean markSupported() {
  87. return false;
  88. }
  89. /** Reads from the stream into the provided array. */
  90. public int read(char[] b, int off, int len) {
  91. int i;
  92. int ok = 0;
  93. while (len-- > 0) {
  94. i = read();
  95. if (i == -1) return (ok == 0) ? -1 : ok;
  96. b[off++] = (char) i;
  97. ok++;
  98. }
  99. return ok;
  100. }
  101. /** Reads from the stream into the provided array. */
  102. public int read(char[] b) {
  103. return read(b,0,b.length);
  104. }
  105. }