1 57 58 package com.sun.org.apache.xerces.internal.impl.xpath.regex; 59 60 import java.text.CharacterIterator ; 61 62 74 public class Match implements Cloneable { 75 int[] beginpos = null; 76 int[] endpos = null; 77 int nofgroups = 0; 78 79 CharacterIterator ciSource = null; 80 String strSource = null; 81 char[] charSource = null; 82 83 86 public Match() { 87 } 88 89 92 public synchronized Object clone() { 93 Match ma = new Match(); 94 if (this.nofgroups > 0) { 95 ma.setNumberOfGroups(this.nofgroups); 96 if (this.ciSource != null) ma.setSource(this.ciSource); 97 if (this.strSource != null) ma.setSource(this.strSource); 98 for (int i = 0; i < this.nofgroups; i ++) { 99 ma.setBeginning(i, this.getBeginning(i)); 100 ma.setEnd(i, this.getEnd(i)); 101 } 102 } 103 return ma; 104 } 105 106 109 protected void setNumberOfGroups(int n) { 110 int oldn = this.nofgroups; 111 this.nofgroups = n; 112 if (oldn <= 0 113 || oldn < n || n*2 < oldn) { 114 this.beginpos = new int[n]; 115 this.endpos = new int[n]; 116 } 117 for (int i = 0; i < n; i ++) { 118 this.beginpos[i] = -1; 119 this.endpos[i] = -1; 120 } 121 } 122 123 126 protected void setSource(CharacterIterator ci) { 127 this.ciSource = ci; 128 this.strSource = null; 129 this.charSource = null; 130 } 131 134 protected void setSource(String str) { 135 this.ciSource = null; 136 this.strSource = str; 137 this.charSource = null; 138 } 139 142 protected void setSource(char[] chars) { 143 this.ciSource = null; 144 this.strSource = null; 145 this.charSource = chars; 146 } 147 148 151 protected void setBeginning(int index, int v) { 152 this.beginpos[index] = v; 153 } 154 155 158 protected void setEnd(int index, int v) { 159 this.endpos[index] = v; 160 } 161 162 166 public int getNumberOfGroups() { 167 if (this.nofgroups <= 0) 168 throw new IllegalStateException ("A result is not set."); 169 return this.nofgroups; 170 } 171 172 177 public int getBeginning(int index) { 178 if (this.beginpos == null) 179 throw new IllegalStateException ("A result is not set."); 180 if (index < 0 || this.nofgroups <= index) 181 throw new IllegalArgumentException ("The parameter must be less than " 182 +this.nofgroups+": "+index); 183 return this.beginpos[index]; 184 } 185 186 191 public int getEnd(int index) { 192 if (this.endpos == null) 193 throw new IllegalStateException ("A result is not set."); 194 if (index < 0 || this.nofgroups <= index) 195 throw new IllegalArgumentException ("The parameter must be less than " 196 +this.nofgroups+": "+index); 197 return this.endpos[index]; 198 } 199 200 205 public String getCapturedText(int index) { 206 if (this.beginpos == null) 207 throw new IllegalStateException ("match() has never been called."); 208 if (index < 0 || this.nofgroups <= index) 209 throw new IllegalArgumentException ("The parameter must be less than " 210 +this.nofgroups+": "+index); 211 String ret; 212 int begin = this.beginpos[index], end = this.endpos[index]; 213 if (begin < 0 || end < 0) return null; 214 if (this.ciSource != null) { 215 ret = REUtil.substring(this.ciSource, begin, end); 216 } else if (this.strSource != null) { 217 ret = this.strSource.substring(begin, end); 218 } else { 219 ret = new String (this.charSource, begin, end-begin); 220 } 221 return ret; 222 } 223 } 224 | Popular Tags |