Browse Source

merge redundant code

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@986445 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 15 years ago
parent
commit
401473f8de
7 changed files with 73 additions and 60 deletions
  1. +2
    -17
      src/main/org/apache/tools/ant/filters/TokenFilter.java
  2. +3
    -12
      src/main/org/apache/tools/ant/taskdefs/condition/Matches.java
  3. +2
    -17
      src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java
  4. +2
    -5
      src/main/org/apache/tools/ant/types/resources/selectors/Name.java
  5. +2
    -4
      src/main/org/apache/tools/ant/types/selectors/FilenameSelector.java
  6. +2
    -5
      src/main/org/apache/tools/ant/util/RegexpPatternMapper.java
  7. +60
    -0
      src/main/org/apache/tools/ant/util/regexp/RegexpUtil.java

+ 2
- 17
src/main/org/apache/tools/ant/filters/TokenFilter.java View File

@@ -29,6 +29,7 @@ import org.apache.tools.ant.util.Tokenizer;
import org.apache.tools.ant.util.LineTokenizer; import org.apache.tools.ant.util.LineTokenizer;
import org.apache.tools.ant.util.StringUtils; import org.apache.tools.ant.util.StringUtils;
import org.apache.tools.ant.util.regexp.Regexp; import org.apache.tools.ant.util.regexp.Regexp;
import org.apache.tools.ant.util.regexp.RegexpUtil;


/** /**
* This splits up input into tokens and passes * This splits up input into tokens and passes
@@ -705,22 +706,6 @@ public class TokenFilter extends BaseFilterReader
* @return the Regexp option bits * @return the Regexp option bits
*/ */
public static int convertRegexOptions(String flags) { 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);
} }
} }

+ 3
- 12
src/main/org/apache/tools/ant/taskdefs/condition/Matches.java View File

@@ -19,9 +19,9 @@ package org.apache.tools.ant.taskdefs.condition;


import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ProjectComponent; 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.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. * Simple regular expression condition.
@@ -112,16 +112,7 @@ public class Matches extends ProjectComponent implements Condition {
if (regularExpression == null) { if (regularExpression == null) {
throw new BuildException("Missing pattern in matches."); 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()); Regexp regexp = regularExpression.getRegexp(getProject());
return regexp.matches(string, options); return regexp.matches(string, options);
} }


+ 2
- 17
src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java View File

@@ -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.types.resources.Union;
import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.regexp.Regexp; import org.apache.tools.ant.util.regexp.Regexp;
import org.apache.tools.ant.util.regexp.RegexpUtil;


/** /**
* Performs regular expression string replacements in a text * Performs regular expression string replacements in a text
@@ -513,23 +514,7 @@ public class ReplaceRegExp extends Task {
+ "time."); + "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()) { if (file != null && file.exists()) {
try { try {


+ 2
- 5
src/main/org/apache/tools/ant/types/resources/selectors/Name.java View File

@@ -22,6 +22,7 @@ import org.apache.tools.ant.types.RegularExpression;
import org.apache.tools.ant.types.Resource; import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.selectors.SelectorUtils; import org.apache.tools.ant.types.selectors.SelectorUtils;
import org.apache.tools.ant.util.regexp.Regexp; import org.apache.tools.ant.util.regexp.Regexp;
import org.apache.tools.ant.util.regexp.RegexpUtil;


/** /**
* Name ResourceSelector. * Name ResourceSelector.
@@ -137,11 +138,7 @@ public class Name implements ResourceSelector {
reg.setPattern(regex); reg.setPattern(regex);
expression = reg.getRegexp(project); 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));
} }
} }




+ 2
- 4
src/main/org/apache/tools/ant/types/selectors/FilenameSelector.java View File

@@ -24,6 +24,7 @@ import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Parameter; import org.apache.tools.ant.types.Parameter;
import org.apache.tools.ant.types.RegularExpression; import org.apache.tools.ant.types.RegularExpression;
import org.apache.tools.ant.util.regexp.Regexp; import org.apache.tools.ant.util.regexp.Regexp;
import org.apache.tools.ant.util.regexp.RegexpUtil;


/** /**
* Selector that filters files based on the filename. * Selector that filters files based on the filename.
@@ -185,10 +186,7 @@ public class FilenameSelector extends BaseExtendSelector {
reg.setPattern(regex); reg.setPattern(regex);
expression = reg.getRegexp(getProject()); 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; return expression.matches(filename, options) == !negated;
} }
} }


+ 2
- 5
src/main/org/apache/tools/ant/util/RegexpPatternMapper.java View File

@@ -22,6 +22,7 @@ import java.util.Vector;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.util.regexp.RegexpMatcher; import org.apache.tools.ant.util.regexp.RegexpMatcher;
import org.apache.tools.ant.util.regexp.RegexpMatcherFactory; import org.apache.tools.ant.util.regexp.RegexpMatcherFactory;
import org.apache.tools.ant.util.regexp.RegexpUtil;


/** /**
* Implementation of FileNameMapper that does regular expression * Implementation of FileNameMapper that does regular expression
@@ -67,11 +68,7 @@ public class RegexpPatternMapper implements FileNameMapper {
* @since Ant 1.6.3 * @since Ant 1.6.3
*/ */
public void setCaseSensitive(boolean caseSensitive) { public void setCaseSensitive(boolean caseSensitive) {
if (!caseSensitive) {
regexpOptions = RegexpMatcher.MATCH_CASE_INSENSITIVE;
} else {
regexpOptions = 0;
}
regexpOptions = RegexpUtil.asOptions(caseSensitive);
} }


/** /**


+ 60
- 0
src/main/org/apache/tools/ant/util/regexp/RegexpUtil.java View File

@@ -46,4 +46,64 @@ public class RegexpUtil {
public static int removeFlag(int options, int flag) { public static int removeFlag(int options, int flag) {
return (options & (0xFFFFFFFF - 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;
}
} }

Loading…
Cancel
Save