1 18 package org.apache.tools.ant.taskdefs.condition; 19 20 import org.apache.tools.ant.BuildException; 21 import org.apache.tools.ant.ProjectComponent; 22 import org.apache.tools.ant.util.regexp.Regexp; 23 import org.apache.tools.ant.types.RegularExpression; 24 import org.apache.tools.ant.util.regexp.RegexpMatcher; 25 26 31 public class Matches extends ProjectComponent implements Condition { 32 33 private String string; 34 private boolean caseSensitive = true; 35 private boolean multiLine = false; 36 private boolean singleLine = false; 37 private RegularExpression regularExpression; 38 39 44 public void setString(String string) { 45 this.string = string; 46 } 47 48 53 public void setPattern(String pattern) { 54 if (regularExpression != null) { 55 throw new BuildException( 56 "Only one regular expression is allowed."); 57 } 58 regularExpression = new RegularExpression(); 59 regularExpression.setPattern(pattern); 60 } 61 62 69 public void addRegexp(RegularExpression regularExpression) { 70 if (this.regularExpression != null) { 71 throw new BuildException( 72 "Only one regular expression is allowed."); 73 } 74 this.regularExpression = regularExpression; 75 } 76 77 82 public void setCasesensitive(boolean b) { 83 caseSensitive = b; 84 } 85 86 90 public void setMultiline(boolean b) { 91 multiLine = b; 92 } 93 94 99 public void setSingleLine(boolean b) { 100 singleLine = b; 101 } 102 103 107 public boolean eval() throws BuildException { 108 if (string == null) { 109 throw new BuildException( 110 "Parameter string is required in matches."); 111 } 112 if (regularExpression == null) { 113 throw new BuildException("Missing pattern in matches."); 114 } 115 int options = RegexpMatcher.MATCH_DEFAULT; 116 if (!caseSensitive) { 117 options = options | RegexpMatcher.MATCH_CASE_INSENSITIVE; 118 } 119 if (multiLine) { 120 options = options | RegexpMatcher.MATCH_MULTILINE; 121 } 122 if (singleLine) { 123 options = options | RegexpMatcher.MATCH_SINGLELINE; 124 } 125 Regexp regexp = regularExpression.getRegexp(getProject()); 126 return regexp.matches(string, options); 127 } 128 } 129 | Popular Tags |