001: /*
002: * gnu/regexp/REMatchEnumeration.java
003: * Copyright (C) 1998-2001 Wes Biggs
004: *
005: * This library is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU Lesser General Public License as published
007: * by the Free Software Foundation; either version 2.1 of the License, or
008: * (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
018: */
019: package gnu.regexp;
020:
021: import java.io.Serializable;
022: import java.util.Enumeration;
023: import java.util.NoSuchElementException;
024:
025: /**
026: * An REMatchEnumeration enumerates regular expression matches over a
027: * given input text. You obtain a reference to an enumeration using
028: * the <code>getMatchEnumeration()</code> methods on an instance of
029: * RE.
030: *
031: * <P>
032: *
033: * REMatchEnumeration does lazy computation; that is, it will not
034: * search for a match until it needs to. If you'd rather just get all
035: * the matches at once in a big array, use the
036: * <code>getAllMatches()</code> methods on RE. However, using an
037: * enumeration can help speed performance when the entire text does
038: * not need to be searched immediately.
039: *
040: * <P>
041: *
042: * The enumerated type is especially useful when searching on a Reader
043: * or InputStream, because the InputStream read position cannot be
044: * guaranteed after calling <code>getMatch()</code> (see the
045: * description of that method for an explanation of why). Enumeration
046: * also saves a lot of overhead required when calling
047: * <code>getMatch()</code> multiple times.
048: *
049: * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
050: */
051: public class REMatchEnumeration implements Enumeration, Serializable {
052: private static final int YES = 1;
053: private static final int MAYBE = 0;
054: private static final int NO = -1;
055:
056: private int more;
057: private REMatch match;
058: private RE expr;
059: private CharIndexed input;
060: private int eflags;
061: private int index;
062:
063: // Package scope constructor is used by RE.getMatchEnumeration()
064: REMatchEnumeration(RE expr, CharIndexed input, int index, int eflags) {
065: more = MAYBE;
066: this .expr = expr;
067: this .input = input;
068: this .index = index;
069: this .eflags = eflags;
070: }
071:
072: /** Returns true if there are more matches in the input text. */
073: public boolean hasMoreElements() {
074: return hasMoreMatches(null);
075: }
076:
077: /** Returns true if there are more matches in the input text. */
078: public boolean hasMoreMatches() {
079: return hasMoreMatches(null);
080: }
081:
082: /** Returns true if there are more matches in the input text.
083: * Saves the text leading up to the match (or to the end of the input)
084: * in the specified buffer.
085: */
086: public boolean hasMoreMatches(StringBuffer buffer) {
087: if (more == MAYBE) {
088: match = expr.getMatchImpl(input, index, eflags, buffer);
089: if (match != null) {
090: input.move((match.end[0] > 0) ? match.end[0] : 1);
091:
092: index = (match.end[0] > 0) ? match.end[0]
093: + match.offset : index + 1;
094: more = YES;
095: } else
096: more = NO;
097: }
098: return (more == YES);
099: }
100:
101: /** Returns the next match in the input text. */
102: public Object nextElement() throws NoSuchElementException {
103: return nextMatch();
104: }
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: }
|