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