/interpreter/tags/at2dist130208/src/edu/vub/util/regexp/REMatchEnumeration.java

http://ambienttalk.googlecode.com/ · Java · 135 lines · 49 code · 11 blank · 75 comment · 7 complexity · d6087a02b4255974dac50d9dccbd806f MD5 · raw file

  1. /* gnu/regexp/REMatchEnumeration.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.Serializable;
  33. import java.util.Enumeration;
  34. import java.util.NoSuchElementException;
  35. /**
  36. * An REMatchEnumeration enumerates regular expression matches over a
  37. * given input text. You obtain a reference to an enumeration using
  38. * the <code>getMatchEnumeration()</code> methods on an instance of
  39. * RE.
  40. *
  41. * <P>
  42. *
  43. * REMatchEnumeration does lazy computation; that is, it will not
  44. * search for a match until it needs to. If you'd rather just get all
  45. * the matches at once in a big array, use the
  46. * <code>getAllMatches()</code> methods on RE. However, using an
  47. * enumeration can help speed performance when the entire text does
  48. * not need to be searched immediately.
  49. *
  50. * <P>
  51. *
  52. * The enumerated type is especially useful when searching on a Reader
  53. * or InputStream, because the InputStream read position cannot be
  54. * guaranteed after calling <code>getMatch()</code> (see the
  55. * description of that method for an explanation of why). Enumeration
  56. * also saves a lot of overhead required when calling
  57. * <code>getMatch()</code> multiple times.
  58. *
  59. * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
  60. */
  61. public class REMatchEnumeration implements Enumeration, Serializable {
  62. private static final int YES = 1;
  63. private static final int MAYBE = 0;
  64. private static final int NO = -1;
  65. private int more;
  66. private REMatch match;
  67. private RE expr;
  68. private CharIndexed input;
  69. private int eflags;
  70. private int index;
  71. // Package scope constructor is used by RE.getMatchEnumeration()
  72. REMatchEnumeration(RE expr, CharIndexed input, int index, int eflags) {
  73. more = MAYBE;
  74. this.expr = expr;
  75. this.input = input;
  76. this.index = index;
  77. this.eflags = eflags;
  78. }
  79. /** Returns true if there are more matches in the input text. */
  80. public boolean hasMoreElements() {
  81. return hasMoreMatches(null);
  82. }
  83. /** Returns true if there are more matches in the input text. */
  84. public boolean hasMoreMatches() {
  85. return hasMoreMatches(null);
  86. }
  87. /** Returns true if there are more matches in the input text.
  88. * Saves the text leading up to the match (or to the end of the input)
  89. * in the specified buffer.
  90. */
  91. public boolean hasMoreMatches(StringBuffer buffer) {
  92. if (more == MAYBE) {
  93. match = expr.getMatchImpl(input,index,eflags,buffer);
  94. if (match != null) {
  95. input.move((match.end[0] > 0) ? match.end[0] : 1);
  96. index = (match.end[0] > 0) ? match.end[0] + match.offset : index + 1;
  97. more = YES;
  98. } else more = NO;
  99. }
  100. return (more == YES);
  101. }
  102. /** Returns the next match in the input text. */
  103. public Object nextElement() throws NoSuchElementException {
  104. return nextMatch();
  105. }
  106. /**
  107. * Returns the next match in the input text. This method is provided
  108. * for convenience to avoid having to explicitly cast the return value
  109. * to class REMatch.
  110. */
  111. public REMatch nextMatch() throws NoSuchElementException {
  112. if (hasMoreElements()) {
  113. more = (input.isValid()) ? MAYBE : NO;
  114. return match;
  115. }
  116. throw new NoSuchElementException();
  117. }
  118. }