1 19 package gnu.regexp; 20 import java.io.Serializable ; 21 import java.util.Enumeration ; 22 import java.util.NoSuchElementException ; 23 24 50 public class REMatchEnumeration implements Enumeration , Serializable { 51 private static final int YES = 1; 52 private static final int MAYBE = 0; 53 private static final int NO = -1; 54 55 private int more; 56 private REMatch match; 57 private RE expr; 58 private CharIndexed input; 59 private int eflags; 60 private int index; 61 62 REMatchEnumeration(RE expr, CharIndexed input, int index, int eflags) { 64 more = MAYBE; 65 this.expr = expr; 66 this.input = input; 67 this.index = index; 68 this.eflags = eflags; 69 } 70 71 72 public boolean hasMoreElements() { 73 return hasMoreMatches(null); 74 } 75 76 77 public boolean hasMoreMatches() { 78 return hasMoreMatches(null); 79 } 80 81 85 public boolean hasMoreMatches(StringBuffer buffer) { 86 if (more == MAYBE) { 87 match = expr.getMatchImpl(input,index,eflags,buffer); 88 if (match != null) { 89 input.move((match.end[0] > 0) ? match.end[0] : 1); 90 91 index = (match.end[0] > 0) ? match.end[0] + match.offset : index + 1; 92 more = YES; 93 } else more = NO; 94 } 95 return (more == YES); 96 } 97 98 99 public Object nextElement() throws NoSuchElementException { 100 return nextMatch(); 101 } 102 103 108 public REMatch nextMatch() throws NoSuchElementException { 109 if (hasMoreElements()) { 110 more = (input.isValid()) ? MAYBE : NO; 111 return match; 112 } 113 throw new NoSuchElementException (); 114 } 115 } 116 117 | Popular Tags |