1 package org.apache.oro.text.regex; 2 3 59 60 import java.util.Vector ; 61 62 123 public class Perl5Substitution extends StringSubstitution { 124 129 public static final int INTERPOLATE_ALL = 0; 130 131 136 public static final int INTERPOLATE_NONE = -1; 137 138 int _numInterpolations; 139 Vector _substitutions; 140 transient String _lastInterpolation; 141 142 static Vector _parseSubs(String sub) { 143 boolean saveDigits, storedInterpolation; 144 int current; 145 char[] str; 146 Vector subs; 147 StringBuffer numBuffer, strBuffer; 148 149 subs = new Vector (5); 150 numBuffer = new StringBuffer (5); 151 strBuffer = new StringBuffer (10); 152 153 str = sub.toCharArray(); 154 current = 0; 155 saveDigits = false; 156 storedInterpolation = false; 157 158 while(current < str.length) { 159 if(saveDigits && Character.isDigit(str[current])) { 160 numBuffer.append(str[current]); 161 162 if(strBuffer.length() > 0) { 163 subs.addElement(strBuffer.toString()); 164 strBuffer.setLength(0); 165 } 166 } else { 167 if(saveDigits) { 168 try { 169 subs.addElement(new Integer (numBuffer.toString())); 170 storedInterpolation = true; 171 } catch(NumberFormatException e) { 172 subs.addElement(numBuffer.toString()); 173 } 174 175 numBuffer.setLength(0); 176 saveDigits = false; 177 } 178 179 if(str[current] == '$' && 180 current + 1 < str.length && str[current + 1] != '0' && 181 Character.isDigit(str[current + 1])) 182 saveDigits = true; 183 else 184 strBuffer.append(str[current]); 185 } 186 187 ++current; 188 } 190 191 if(saveDigits) { 192 try { 193 subs.addElement(new Integer (numBuffer.toString())); 194 storedInterpolation = true; 195 } catch(NumberFormatException e) { 196 subs.addElement(numBuffer.toString()); 197 } 198 } else if(strBuffer.length() > 0) 199 subs.addElement(strBuffer.toString()); 200 201 return (storedInterpolation ? subs : null); 202 } 203 204 205 String _finalInterpolatedSub(MatchResult result) { 206 StringBuffer buffer = new StringBuffer (10); 207 _calcSub(buffer, result); 208 return buffer.toString(); 209 } 210 211 void _calcSub(StringBuffer buffer, MatchResult result) { 212 int size, element, value; 213 Object obj; 214 Integer integer; 215 String group; 216 217 size = _substitutions.size(); 218 219 for(element=0; element < size; element++) { 220 obj = _substitutions.elementAt(element); 221 222 if(obj instanceof String ) 223 buffer.append(obj); 224 else { 225 integer = (Integer )obj; 226 value = integer.intValue(); 227 228 if(value > 0 && value < result.groups()) { 229 group = result.group(value); 230 231 if(group != null) 232 buffer.append(group); 233 } else { 234 buffer.append('$'); 235 buffer.append(value); 236 } 237 } 238 } 239 } 240 241 242 247 public Perl5Substitution() { 248 this("", INTERPOLATE_ALL); 249 } 250 251 258 public Perl5Substitution(String substitution) { 259 this(substitution, INTERPOLATE_ALL); 260 } 261 262 280 public Perl5Substitution(String substitution, int numInterpolations) { 281 setSubstitution(substitution, numInterpolations); 282 } 283 284 285 295 public void setSubstitution(String substitution) { 296 setSubstitution(substitution, INTERPOLATE_ALL); 297 } 298 299 300 321 public void setSubstitution(String substitution, int numInterpolations) { 322 super.setSubstitution(substitution); 323 _numInterpolations = numInterpolations; 324 325 if(numInterpolations != INTERPOLATE_NONE && 326 substitution.indexOf('$') != -1) 327 _substitutions = _parseSubs(substitution); 328 else 329 _substitutions = null; 330 _lastInterpolation = null; 331 } 332 333 334 351 public void appendSubstitution(StringBuffer appendBuffer, MatchResult match, 352 int substitutionCount, String originalInput, 353 PatternMatcher matcher, Pattern pattern) 354 { 355 if(_substitutions == null) { 356 super.appendSubstitution(appendBuffer, match, substitutionCount, 357 originalInput, matcher, pattern); 358 return; 359 } 360 361 if(_numInterpolations < 1 || substitutionCount < _numInterpolations) 362 _calcSub(appendBuffer, match); 363 else { 364 if(substitutionCount == _numInterpolations) 365 _lastInterpolation = _finalInterpolatedSub(match); 366 appendBuffer.append(_lastInterpolation); 367 } 368 } 369 370 } 371 | Popular Tags |