/interpreter/tags/at2dist170907/src/edu/vub/util/regexp/REMatchEnumeration.java
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 4This file is part of GNU Classpath. 5 6GNU Classpath is free software; you can redistribute it and/or modify 7it under the terms of the GNU General Public License as published by 8the Free Software Foundation; either version 2, or (at your option) 9any later version. 10 11GNU Classpath is distributed in the hope that it will be useful, but 12WITHOUT ANY WARRANTY; without even the implied warranty of 13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14General Public License for more details. 15 16You should have received a copy of the GNU General Public License 17along with GNU Classpath; see the file COPYING. If not, write to the 18Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 1902110-1301 USA. 20 21Linking this library statically or dynamically with other modules is 22making a combined work based on this library. Thus, the terms and 23conditions of the GNU General Public License cover the whole 24combination. 25 26As a special exception, the copyright holders of this library give you 27permission to link this library with independent modules to produce an 28executable, regardless of the license terms of these independent 29modules, and to copy and distribute the resulting executable under 30terms of your choice, provided that you also meet, for each linked 31independent module, the terms and conditions of the license of that 32module. An independent module is a module which is not derived from 33or based on this library. If you modify this library, you may extend 34this exception to your version of the library, but you are not 35obligated to do so. If you do not wish to do so, delete this 36exception statement from your version. */ 37 38package edu.vub.util.regexp; 39import java.io.Serializable; 40import java.util.Enumeration; 41import java.util.NoSuchElementException; 42 43/** 44 * An REMatchEnumeration enumerates regular expression matches over a 45 * given input text. You obtain a reference to an enumeration using 46 * the <code>getMatchEnumeration()</code> methods on an instance of 47 * RE. 48 * 49 * <P> 50 * 51 * REMatchEnumeration does lazy computation; that is, it will not 52 * search for a match until it needs to. If you'd rather just get all 53 * the matches at once in a big array, use the 54 * <code>getAllMatches()</code> methods on RE. However, using an 55 * enumeration can help speed performance when the entire text does 56 * not need to be searched immediately. 57 * 58 * <P> 59 * 60 * The enumerated type is especially useful when searching on a Reader 61 * or InputStream, because the InputStream read position cannot be 62 * guaranteed after calling <code>getMatch()</code> (see the 63 * description of that method for an explanation of why). Enumeration 64 * also saves a lot of overhead required when calling 65 * <code>getMatch()</code> multiple times. 66 * 67 * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A> 68 */ 69public class REMatchEnumeration implements Enumeration, Serializable { 70 private static final int YES = 1; 71 private static final int MAYBE = 0; 72 private static final int NO = -1; 73 74 private int more; 75 private REMatch match; 76 private RE expr; 77 private CharIndexed input; 78 private int eflags; 79 private int index; 80 81 // Package scope constructor is used by RE.getMatchEnumeration() 82 REMatchEnumeration(RE expr, CharIndexed input, int index, int eflags) { 83 more = MAYBE; 84 this.expr = expr; 85 this.input = input; 86 this.index = index; 87 this.eflags = eflags; 88 } 89 90 /** Returns true if there are more matches in the input text. */ 91 public boolean hasMoreElements() { 92 return hasMoreMatches(null); 93 } 94 95 /** Returns true if there are more matches in the input text. */ 96 public boolean hasMoreMatches() { 97 return hasMoreMatches(null); 98 } 99 100 /** Returns true if there are more matches in the input text. 101 * Saves the text leading up to the match (or to the end of the input) 102 * in the specified buffer. 103 */ 104 public boolean hasMoreMatches(StringBuffer buffer) { 105 if (more == MAYBE) { 106 match = expr.getMatchImpl(input,index,eflags,buffer); 107 if (match != null) { 108 input.move((match.end[0] > 0) ? match.end[0] : 1); 109 110 index = (match.end[0] > 0) ? match.end[0] + match.offset : index + 1; 111 more = YES; 112 } else more = NO; 113 } 114 return (more == YES); 115 } 116 117 /** Returns the next match in the input text. */ 118 public Object nextElement() throws NoSuchElementException { 119 return nextMatch(); 120 } 121 122 /** 123 * Returns the next match in the input text. This method is provided 124 * for convenience to avoid having to explicitly cast the return value 125 * to class REMatch. 126 */ 127 public REMatch nextMatch() throws NoSuchElementException { 128 if (hasMoreElements()) { 129 more = (input.isValid()) ? MAYBE : NO; 130 return match; 131 } 132 throw new NoSuchElementException(); 133 } 134} 135