1 16 17 package org.apache.xerces.impl.xpath.regex; 18 19 import java.text.CharacterIterator ; 20 21 34 public class Match implements Cloneable { 35 int[] beginpos = null; 36 int[] endpos = null; 37 int nofgroups = 0; 38 39 CharacterIterator ciSource = null; 40 String strSource = null; 41 char[] charSource = null; 42 43 46 public Match() { 47 } 48 49 52 public synchronized Object clone() { 53 Match ma = new Match(); 54 if (this.nofgroups > 0) { 55 ma.setNumberOfGroups(this.nofgroups); 56 if (this.ciSource != null) ma.setSource(this.ciSource); 57 if (this.strSource != null) ma.setSource(this.strSource); 58 for (int i = 0; i < this.nofgroups; i ++) { 59 ma.setBeginning(i, this.getBeginning(i)); 60 ma.setEnd(i, this.getEnd(i)); 61 } 62 } 63 return ma; 64 } 65 66 69 protected void setNumberOfGroups(int n) { 70 int oldn = this.nofgroups; 71 this.nofgroups = n; 72 if (oldn <= 0 73 || oldn < n || n*2 < oldn) { 74 this.beginpos = new int[n]; 75 this.endpos = new int[n]; 76 } 77 for (int i = 0; i < n; i ++) { 78 this.beginpos[i] = -1; 79 this.endpos[i] = -1; 80 } 81 } 82 83 86 protected void setSource(CharacterIterator ci) { 87 this.ciSource = ci; 88 this.strSource = null; 89 this.charSource = null; 90 } 91 94 protected void setSource(String str) { 95 this.ciSource = null; 96 this.strSource = str; 97 this.charSource = null; 98 } 99 102 protected void setSource(char[] chars) { 103 this.ciSource = null; 104 this.strSource = null; 105 this.charSource = chars; 106 } 107 108 111 protected void setBeginning(int index, int v) { 112 this.beginpos[index] = v; 113 } 114 115 118 protected void setEnd(int index, int v) { 119 this.endpos[index] = v; 120 } 121 122 126 public int getNumberOfGroups() { 127 if (this.nofgroups <= 0) 128 throw new IllegalStateException ("A result is not set."); 129 return this.nofgroups; 130 } 131 132 137 public int getBeginning(int index) { 138 if (this.beginpos == null) 139 throw new IllegalStateException ("A result is not set."); 140 if (index < 0 || this.nofgroups <= index) 141 throw new IllegalArgumentException ("The parameter must be less than " 142 +this.nofgroups+": "+index); 143 return this.beginpos[index]; 144 } 145 146 151 public int getEnd(int index) { 152 if (this.endpos == null) 153 throw new IllegalStateException ("A result is not set."); 154 if (index < 0 || this.nofgroups <= index) 155 throw new IllegalArgumentException ("The parameter must be less than " 156 +this.nofgroups+": "+index); 157 return this.endpos[index]; 158 } 159 160 165 public String getCapturedText(int index) { 166 if (this.beginpos == null) 167 throw new IllegalStateException ("match() has never been called."); 168 if (index < 0 || this.nofgroups <= index) 169 throw new IllegalArgumentException ("The parameter must be less than " 170 +this.nofgroups+": "+index); 171 String ret; 172 int begin = this.beginpos[index], end = this.endpos[index]; 173 if (begin < 0 || end < 0) return null; 174 if (this.ciSource != null) { 175 ret = REUtil.substring(this.ciSource, begin, end); 176 } else if (this.strSource != null) { 177 ret = this.strSource.substring(begin, end); 178 } else { 179 ret = new String (this.charSource, begin, end-begin); 180 } 181 return ret; 182 } 183 } 184 | Popular Tags |