1 18 package org.apache.tools.ant.filters; 19 20 import java.io.IOException ; 21 import java.io.Reader ; 22 import java.util.Vector ; 23 import java.util.Enumeration ; 24 import org.apache.tools.ant.BuildException; 25 import org.apache.tools.ant.ProjectComponent; 26 import org.apache.tools.ant.types.RegularExpression; 27 import org.apache.tools.ant.types.Substitution; 28 import org.apache.tools.ant.util.Tokenizer; 29 import org.apache.tools.ant.util.LineTokenizer; 30 import org.apache.tools.ant.util.StringUtils; 31 import org.apache.tools.ant.util.regexp.Regexp; 32 33 42 public class TokenFilter extends BaseFilterReader 43 implements ChainableReader { 44 47 public interface Filter { 48 55 String filter(String string); 56 } 57 58 59 60 private Vector filters = new Vector (); 61 62 private Tokenizer tokenizer = null; 63 64 private String delimOutput = null; 65 66 private String line = null; 67 68 private int linePos = 0; 69 70 75 public TokenFilter() { 76 super(); 77 } 78 79 85 public TokenFilter(final Reader in) { 86 super(in); 87 } 88 89 90 101 102 public int read() throws IOException { 103 if (tokenizer == null) { 104 tokenizer = new LineTokenizer(); 105 } 106 while (line == null || line.length() == 0) { 107 line = tokenizer.getToken(in); 108 if (line == null) { 109 return -1; 110 } 111 for (Enumeration e = filters.elements(); e.hasMoreElements();) { 112 Filter filter = (Filter) e.nextElement(); 113 line = filter.filter(line); 114 if (line == null) { 115 break; 116 } 117 } 118 linePos = 0; 119 if (line != null) { 120 if (tokenizer.getPostToken().length() != 0) { 121 if (delimOutput != null) { 122 line = line + delimOutput; 123 } else { 124 line = line + tokenizer.getPostToken(); 125 } 126 } 127 } 128 } 129 int ch = line.charAt(linePos); 130 linePos++; 131 if (linePos == line.length()) { 132 line = null; 133 } 134 return ch; 135 } 136 137 145 146 public final Reader chain(final Reader reader) { 147 TokenFilter newFilter = new TokenFilter(reader); 148 newFilter.filters = filters; 149 newFilter.tokenizer = tokenizer; 150 newFilter.delimOutput = delimOutput; 151 newFilter.setProject(getProject()); 152 return newFilter; 153 } 154 155 160 161 public void setDelimOutput(String delimOutput) { 162 this.delimOutput = resolveBackSlash(delimOutput); 163 } 164 165 169 173 174 public void addLineTokenizer(LineTokenizer tokenizer) { 175 add(tokenizer); 176 } 177 178 182 183 public void addStringTokenizer(StringTokenizer tokenizer) { 184 add(tokenizer); 185 } 186 187 191 public void addFileTokenizer(FileTokenizer tokenizer) { 192 add(tokenizer); 193 } 194 195 199 200 public void add(Tokenizer tokenizer) { 201 if (this.tokenizer != null) { 202 throw new BuildException("Only one tokenizer allowed"); 203 } 204 this.tokenizer = tokenizer; 205 } 206 207 211 215 public void addReplaceString(ReplaceString filter) { 216 filters.addElement(filter); 217 } 218 219 223 public void addContainsString(ContainsString filter) { 224 filters.addElement(filter); 225 } 226 227 231 public void addReplaceRegex(ReplaceRegex filter) { 232 filters.addElement(filter); 233 } 234 235 239 public void addContainsRegex(ContainsRegex filter) { 240 filters.addElement(filter); 241 } 242 243 247 public void addTrim(Trim filter) { 248 filters.addElement(filter); 249 } 250 251 255 public void addIgnoreBlank(IgnoreBlank filter) { 256 filters.addElement(filter); 257 } 258 259 263 public void addDeleteCharacters(DeleteCharacters filter) { 264 filters.addElement(filter); 265 } 266 267 271 public void add(Filter filter) { 272 filters.addElement(filter); 273 } 274 275 276 282 285 public static class FileTokenizer 286 extends org.apache.tools.ant.util.FileTokenizer { 287 } 288 289 297 public static class StringTokenizer 298 extends org.apache.tools.ant.util.StringTokenizer { 299 } 300 301 307 311 public abstract static class ChainableReaderFilter extends ProjectComponent 312 implements ChainableReader, Filter { 313 private boolean byLine = true; 314 315 320 public void setByLine(boolean byLine) { 321 this.byLine = byLine; 322 } 323 324 330 public Reader chain(Reader reader) { 331 TokenFilter tokenFilter = new TokenFilter(reader); 332 if (!byLine) { 333 tokenFilter.add(new FileTokenizer()); 334 } 335 tokenFilter.add(this); 336 return tokenFilter; 337 } 338 } 339 340 343 public static class ReplaceString extends ChainableReaderFilter { 344 private String from; 345 private String to; 346 347 352 public void setFrom(String from) { 353 this.from = from; 354 } 355 356 361 public void setTo(String to) { 362 this.to = to; 363 } 364 365 371 public String filter(String line) { 372 if (from == null) { 373 throw new BuildException("Missing from in stringreplace"); 374 } 375 StringBuffer ret = new StringBuffer (); 376 int start = 0; 377 int found = line.indexOf(from); 378 while (found >= 0) { 379 if (found > start) { 381 ret.append(line.substring(start, found)); 382 } 383 384 if (to != null) { 386 ret.append(to); 387 } 388 389 start = found + from.length(); 391 found = line.indexOf(from, start); 392 } 393 394 if (line.length() > start) { 396 ret.append(line.substring(start, line.length())); 397 } 398 399 return ret.toString(); 400 } 401 } 402 403 406 public static class ContainsString extends ProjectComponent 407 implements Filter { 408 private String contains; 409 410 414 public void setContains(String contains) { 415 this.contains = contains; 416 } 417 418 425 public String filter(String string) { 426 if (contains == null) { 427 throw new BuildException("Missing contains in containsstring"); 428 } 429 if (string.indexOf(contains) > -1) { 430 return string; 431 } 432 return null; 433 } 434 } 435 436 439 public static class ReplaceRegex extends ChainableReaderFilter { 440 private String from; 441 private String to; 442 private RegularExpression regularExpression; 443 private Substitution substitution; 444 private boolean initialized = false; 445 private String flags = ""; 446 private int options; 447 private Regexp regexp; 448 449 453 public void setPattern(String from) { 454 this.from = from; 455 } 456 460 public void setReplace(String to) { 461 this.to = to; 462 } 463 464 467 public void setFlags(String flags) { 468 this.flags = flags; 469 } 470 471 private void initialize() { 472 if (initialized) { 473 return; 474 } 475 options = convertRegexOptions(flags); 476 if (from == null) { 477 throw new BuildException("Missing pattern in replaceregex"); 478 } 479 regularExpression = new RegularExpression(); 480 regularExpression.setPattern(from); 481 regexp = regularExpression.getRegexp(getProject()); 482 if (to == null) { 483 to = ""; 484 } 485 substitution = new Substitution(); 486 substitution.setExpression(to); 487 } 488 489 493 public String filter(String line) { 494 initialize(); 495 496 if (!regexp.matches(line, options)) { 497 return line; 498 } 499 return regexp.substitute( 500 line, substitution.getExpression(getProject()), options); 501 } 502 } 503 504 507 public static class ContainsRegex extends ChainableReaderFilter { 508 private String from; 509 private String to; 510 private RegularExpression regularExpression; 511 private Substitution substitution; 512 private boolean initialized = false; 513 private String flags = ""; 514 private int options; 515 private Regexp regexp; 516 517 518 521 public void setPattern(String from) { 522 this.from = from; 523 } 524 525 528 public void setReplace(String to) { 529 this.to = to; 530 } 531 532 535 public void setFlags(String flags) { 536 this.flags = flags; 537 } 538 539 private void initialize() { 540 if (initialized) { 541 return; 542 } 543 options = convertRegexOptions(flags); 544 if (from == null) { 545 throw new BuildException("Missing from in containsregex"); 546 } 547 regularExpression = new RegularExpression(); 548 regularExpression.setPattern(from); 549 regexp = regularExpression.getRegexp(getProject()); 550 if (to == null) { 551 return; 552 } 553 substitution = new Substitution(); 554 substitution.setExpression(to); 555 } 556 557 562 public String filter(String string) { 563 initialize(); 564 if (!regexp.matches(string, options)) { 565 return null; 566 } 567 if (substitution == null) { 568 return string; 569 } 570 return regexp.substitute( 571 string, substitution.getExpression(getProject()), options); 572 } 573 } 574 575 576 public static class Trim extends ChainableReaderFilter { 577 581 public String filter(String line) { 582 return line.trim(); 583 } 584 } 585 586 587 588 589 public static class IgnoreBlank extends ChainableReaderFilter { 590 594 public String filter(String line) { 595 if (line.trim().length() == 0) { 596 return null; 597 } 598 return line; 599 } 600 } 601 602 605 public static class DeleteCharacters extends ProjectComponent 606 implements Filter, ChainableReader { 607 609 private String deleteChars = ""; 610 611 615 public void setChars(String deleteChars) { 616 this.deleteChars = resolveBackSlash(deleteChars); 617 } 618 619 624 public String filter(String string) { 625 StringBuffer output = new StringBuffer (string.length()); 626 for (int i = 0; i < string.length(); ++i) { 627 char ch = string.charAt(i); 628 if (!(isDeleteCharacter(ch))) { 629 output.append(ch); 630 } 631 } 632 return output.toString(); 633 } 634 635 642 public Reader chain(Reader reader) { 643 return new BaseFilterReader(reader) { 644 647 public int read() 648 throws IOException { 649 while (true) { 650 int c = in.read(); 651 if (c == -1) { 652 return c; 653 } 654 if (!(isDeleteCharacter((char) c))) { 655 return c; 656 } 657 } 658 } 659 }; 660 } 661 662 663 private boolean isDeleteCharacter(char c) { 664 for (int d = 0; d < deleteChars.length(); ++d) { 665 if (deleteChars.charAt(d) == c) { 666 return true; 667 } 668 } 669 return false; 670 } 671 } 672 673 677 687 public static String resolveBackSlash(String input) { 688 return StringUtils.resolveBackSlash(input); 689 } 690 691 702 public static int convertRegexOptions(String flags) { 703 if (flags == null) { 704 return 0; 705 } 706 int options = 0; 707 if (flags.indexOf('g') != -1) { 708 options |= Regexp.REPLACE_ALL; 709 } 710 if (flags.indexOf('i') != -1) { 711 options |= Regexp.MATCH_CASE_INSENSITIVE; 712 } 713 if (flags.indexOf('m') != -1) { 714 options |= Regexp.MATCH_MULTILINE; 715 } 716 if (flags.indexOf('s') != -1) { 717 options |= Regexp.MATCH_SINGLELINE; 718 } 719 return options; 720 } 721 } 722 | Popular Tags |