git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@986445 13f79535-47bb-0310-9956-ffa450edef68master
@@ -29,6 +29,7 @@ import org.apache.tools.ant.util.Tokenizer; | |||
import org.apache.tools.ant.util.LineTokenizer; | |||
import org.apache.tools.ant.util.StringUtils; | |||
import org.apache.tools.ant.util.regexp.Regexp; | |||
import org.apache.tools.ant.util.regexp.RegexpUtil; | |||
/** | |||
* This splits up input into tokens and passes | |||
@@ -705,22 +706,6 @@ public class TokenFilter extends BaseFilterReader | |||
* @return the Regexp option bits | |||
*/ | |||
public static int convertRegexOptions(String flags) { | |||
if (flags == null) { | |||
return 0; | |||
} | |||
int options = 0; | |||
if (flags.indexOf('g') != -1) { | |||
options |= Regexp.REPLACE_ALL; | |||
} | |||
if (flags.indexOf('i') != -1) { | |||
options |= Regexp.MATCH_CASE_INSENSITIVE; | |||
} | |||
if (flags.indexOf('m') != -1) { | |||
options |= Regexp.MATCH_MULTILINE; | |||
} | |||
if (flags.indexOf('s') != -1) { | |||
options |= Regexp.MATCH_SINGLELINE; | |||
} | |||
return options; | |||
return RegexpUtil.asOptions(flags); | |||
} | |||
} |
@@ -19,9 +19,9 @@ package org.apache.tools.ant.taskdefs.condition; | |||
import org.apache.tools.ant.BuildException; | |||
import org.apache.tools.ant.ProjectComponent; | |||
import org.apache.tools.ant.util.regexp.Regexp; | |||
import org.apache.tools.ant.types.RegularExpression; | |||
import org.apache.tools.ant.util.regexp.RegexpMatcher; | |||
import org.apache.tools.ant.util.regexp.Regexp; | |||
import org.apache.tools.ant.util.regexp.RegexpUtil; | |||
/** | |||
* Simple regular expression condition. | |||
@@ -112,16 +112,7 @@ public class Matches extends ProjectComponent implements Condition { | |||
if (regularExpression == null) { | |||
throw new BuildException("Missing pattern in matches."); | |||
} | |||
int options = RegexpMatcher.MATCH_DEFAULT; | |||
if (!caseSensitive) { | |||
options = options | RegexpMatcher.MATCH_CASE_INSENSITIVE; | |||
} | |||
if (multiLine) { | |||
options = options | RegexpMatcher.MATCH_MULTILINE; | |||
} | |||
if (singleLine) { | |||
options = options | RegexpMatcher.MATCH_SINGLELINE; | |||
} | |||
int options = RegexpUtil.asOptions(caseSensitive, multiLine, singleLine); | |||
Regexp regexp = regularExpression.getRegexp(getProject()); | |||
return regexp.matches(string, options); | |||
} | |||
@@ -43,6 +43,7 @@ import org.apache.tools.ant.types.resources.FileProvider; | |||
import org.apache.tools.ant.types.resources.Union; | |||
import org.apache.tools.ant.util.FileUtils; | |||
import org.apache.tools.ant.util.regexp.Regexp; | |||
import org.apache.tools.ant.util.regexp.RegexpUtil; | |||
/** | |||
* Performs regular expression string replacements in a text | |||
@@ -513,23 +514,7 @@ public class ReplaceRegExp extends Task { | |||
+ "time."); | |||
} | |||
int options = 0; | |||
if (flags.indexOf('g') != -1) { | |||
options |= Regexp.REPLACE_ALL; | |||
} | |||
if (flags.indexOf('i') != -1) { | |||
options |= Regexp.MATCH_CASE_INSENSITIVE; | |||
} | |||
if (flags.indexOf('m') != -1) { | |||
options |= Regexp.MATCH_MULTILINE; | |||
} | |||
if (flags.indexOf('s') != -1) { | |||
options |= Regexp.MATCH_SINGLELINE; | |||
} | |||
int options = RegexpUtil.asOptions(flags); | |||
if (file != null && file.exists()) { | |||
try { | |||
@@ -22,6 +22,7 @@ import org.apache.tools.ant.types.RegularExpression; | |||
import org.apache.tools.ant.types.Resource; | |||
import org.apache.tools.ant.types.selectors.SelectorUtils; | |||
import org.apache.tools.ant.util.regexp.Regexp; | |||
import org.apache.tools.ant.util.regexp.RegexpUtil; | |||
/** | |||
* Name ResourceSelector. | |||
@@ -137,11 +138,7 @@ public class Name implements ResourceSelector { | |||
reg.setPattern(regex); | |||
expression = reg.getRegexp(project); | |||
} | |||
int options = Regexp.MATCH_DEFAULT; | |||
if (!cs) { | |||
options |= Regexp.MATCH_CASE_INSENSITIVE; | |||
} | |||
return expression.matches(modify(name), options); | |||
return expression.matches(modify(name), RegexpUtil.asOptions(cs)); | |||
} | |||
} | |||
@@ -24,6 +24,7 @@ import org.apache.tools.ant.Project; | |||
import org.apache.tools.ant.types.Parameter; | |||
import org.apache.tools.ant.types.RegularExpression; | |||
import org.apache.tools.ant.util.regexp.Regexp; | |||
import org.apache.tools.ant.util.regexp.RegexpUtil; | |||
/** | |||
* Selector that filters files based on the filename. | |||
@@ -185,10 +186,7 @@ public class FilenameSelector extends BaseExtendSelector { | |||
reg.setPattern(regex); | |||
expression = reg.getRegexp(getProject()); | |||
} | |||
int options = Regexp.MATCH_DEFAULT; | |||
if (!casesensitive) { | |||
options |= Regexp.MATCH_CASE_INSENSITIVE; | |||
} | |||
int options = RegexpUtil.asOptions(casesensitive); | |||
return expression.matches(filename, options) == !negated; | |||
} | |||
} | |||
@@ -22,6 +22,7 @@ import java.util.Vector; | |||
import org.apache.tools.ant.BuildException; | |||
import org.apache.tools.ant.util.regexp.RegexpMatcher; | |||
import org.apache.tools.ant.util.regexp.RegexpMatcherFactory; | |||
import org.apache.tools.ant.util.regexp.RegexpUtil; | |||
/** | |||
* Implementation of FileNameMapper that does regular expression | |||
@@ -67,11 +68,7 @@ public class RegexpPatternMapper implements FileNameMapper { | |||
* @since Ant 1.6.3 | |||
*/ | |||
public void setCaseSensitive(boolean caseSensitive) { | |||
if (!caseSensitive) { | |||
regexpOptions = RegexpMatcher.MATCH_CASE_INSENSITIVE; | |||
} else { | |||
regexpOptions = 0; | |||
} | |||
regexpOptions = RegexpUtil.asOptions(caseSensitive); | |||
} | |||
/** | |||
@@ -46,4 +46,64 @@ public class RegexpUtil { | |||
public static int removeFlag(int options, int flag) { | |||
return (options & (0xFFFFFFFF - flag)); | |||
} | |||
/** | |||
* convert regex option flag characters to regex options | |||
* <dl> | |||
* <li>g - Regexp.REPLACE_ALL</li> | |||
* <li>i - RegexpMatcher.MATCH_CASE_INSENSITIVE</li> | |||
* <li>m - RegexpMatcher.MATCH_MULTILINE</li> | |||
* <li>s - RegexpMatcher.MATCH_SINGLELINE</li> | |||
* </dl> | |||
* @param flags the string containing the flags | |||
* @return the Regexp option bits | |||
* @since Ant 1.8.2 | |||
*/ | |||
public static int asOptions(String flags) { | |||
int options = RegexpMatcher.MATCH_DEFAULT; | |||
if (flags != null) { | |||
options = asOptions(flags.indexOf('i') == -1, | |||
flags.indexOf('m') != -1, | |||
flags.indexOf('s') != -1); | |||
if (flags.indexOf('g') != -1) { | |||
options |= Regexp.REPLACE_ALL; | |||
} | |||
} | |||
return options; | |||
} | |||
/** | |||
* Convert flag to regex options. | |||
* | |||
* @param caseSensitive opposite of RegexpMatcher.MATCH_CASE_INSENSITIVE | |||
* @return the Regexp option bits | |||
* @since Ant 1.8.2 | |||
*/ | |||
public static int asOptions(boolean caseSensitive) { | |||
return asOptions(caseSensitive, false, false); | |||
} | |||
/** | |||
* Convert flags to regex options. | |||
* | |||
* @param caseSensitive opposite of RegexpMatcher.MATCH_CASE_INSENSITIVE | |||
* @param multiLine RegexpMatcher.MATCH_MULTILINE | |||
* @param singleLine RegexpMatcher.MATCH_SINGLELINE | |||
* @return the Regexp option bits | |||
* @since Ant 1.8.2 | |||
*/ | |||
public static int asOptions(boolean caseSensitive, boolean multiLine, | |||
boolean singleLine) { | |||
int options = RegexpMatcher.MATCH_DEFAULT; | |||
if (!caseSensitive) { | |||
options = options | RegexpMatcher.MATCH_CASE_INSENSITIVE; | |||
} | |||
if (multiLine) { | |||
options = options | RegexpMatcher.MATCH_MULTILINE; | |||
} | |||
if (singleLine) { | |||
options = options | RegexpMatcher.MATCH_SINGLELINE; | |||
} | |||
return options; | |||
} | |||
} |