Search in sources :

Example 1 with RegularExpression

use of org.apache.tools.ant.types.RegularExpression in project JikesRVM by JikesRVM.

the class SelectRegexTask method setPattern.

public void setPattern(final String pattern) {
    this.pattern = new RegularExpression();
    this.pattern.setPattern(pattern);
    this.patternLines = pattern.split(System.getProperty("line.separator")).length;
}
Also used : RegularExpression(org.apache.tools.ant.types.RegularExpression)

Example 2 with RegularExpression

use of org.apache.tools.ant.types.RegularExpression in project ant by apache.

the class LineContainsRegExp method read.

/**
 * Returns the next character in the filtered stream, only including
 * lines from the original stream which match all of the specified
 * regular expressions.
 *
 * @return the next character in the resulting stream, or -1
 * if the end of the resulting stream has been reached
 *
 * @exception IOException if the underlying stream throws an IOException
 * during reading
 */
public int read() throws IOException {
    if (!getInitialized()) {
        initialize();
        setInitialized(true);
    }
    int ch = -1;
    if (line != null) {
        ch = line.charAt(0);
        if (line.length() == 1) {
            line = null;
        } else {
            line = line.substring(1);
        }
    } else {
        for (line = readLine(); line != null; line = readLine()) {
            boolean matches = true;
            for (RegularExpression regexp : regexps) {
                if (!regexp.getRegexp(getProject()).matches(line, regexpOptions)) {
                    matches = false;
                    break;
                }
            }
            if (matches ^ isNegated()) {
                break;
            }
        }
        if (line != null) {
            return read();
        }
    }
    return ch;
}
Also used : RegularExpression(org.apache.tools.ant.types.RegularExpression)

Example 3 with RegularExpression

use of org.apache.tools.ant.types.RegularExpression in project ant by apache.

the class FilenameSelector method isSelected.

/**
 * The heart of the matter. This is where the selector gets to decide
 * on the inclusion of a file in a particular fileset. Most of the work
 * for this selector is offloaded into SelectorUtils, a static class
 * that provides the same services for both FilenameSelector and
 * DirectoryScanner.
 *
 * @param basedir the base directory the scan is being done from
 * @param filename is the name of the file to check
 * @param file is a java.io.File object the selector can use
 * @return whether the file should be selected or not
 */
public boolean isSelected(File basedir, String filename, File file) {
    validate();
    if (pattern != null) {
        return SelectorUtils.matchPath(pattern, filename, casesensitive) == !(negated);
    }
    if (reg == null) {
        reg = new RegularExpression();
        reg.setPattern(regex);
        expression = reg.getRegexp(getProject());
    }
    int options = RegexpUtil.asOptions(casesensitive);
    return expression.matches(filename, options) == !negated;
}
Also used : RegularExpression(org.apache.tools.ant.types.RegularExpression)

Example 4 with RegularExpression

use of org.apache.tools.ant.types.RegularExpression in project ant-ivy by apache.

the class IvyExtractFromSources method configureConcat.

private void configureConcat() {
    concat.setProject(getProject());
    concat.setTaskName(getTaskName());
    FilterChain filterChain = new FilterChain();
    LineContainsRegExp lcre = new LineContainsRegExp();
    RegularExpression regexp = new RegularExpression();
    regexp.setPattern("^import .+;");
    lcre.addConfiguredRegexp(regexp);
    filterChain.add(lcre);
    TokenFilter tf = new TokenFilter();
    TokenFilter.ReplaceRegex rre = new TokenFilter.ReplaceRegex();
    rre.setPattern("import (.+);.*");
    rre.setReplace("\\1");
    tf.add(rre);
    filterChain.add(tf);
    concat.addFilterChain(filterChain);
}
Also used : RegularExpression(org.apache.tools.ant.types.RegularExpression) LineContainsRegExp(org.apache.tools.ant.filters.LineContainsRegExp) FilterChain(org.apache.tools.ant.types.FilterChain) TokenFilter(org.apache.tools.ant.filters.TokenFilter)

Example 5 with RegularExpression

use of org.apache.tools.ant.types.RegularExpression in project JikesRVM by JikesRVM.

the class LineFilterTask method execute.

public void execute() throws BuildException {
    if (null == src)
        throw new BuildException("src not set.");
    if (null == dest)
        throw new BuildException("dest not set.");
    if (0 == patterns.size())
        throw new BuildException("No patterns specified.");
    final ArrayList<Regexp> regexpList = new ArrayList<Regexp>();
    for (final RegularExpression pattern : patterns) {
        regexpList.add(pattern.getRegexp(getProject()));
    }
    final Regexp[] regexps = regexpList.toArray(new Regexp[regexpList.size()]);
    BufferedReader reader = null;
    BufferedWriter writer = null;
    try {
        reader = new BufferedReader(new FileReader(src));
        writer = new BufferedWriter(new FileWriter(dest));
        String line = reader.readLine();
        while (null != line) {
            if (!lineMatches(line, regexps)) {
                writer.write(line);
                writer.write('\n');
            }
            line = reader.readLine();
        }
    } catch (IOException e) {
        throw new BuildException("Error truncating file " + src, e);
    } finally {
        if (null != reader) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
        if (null != writer) {
            try {
                writer.close();
            } catch (final IOException ioe) {
                throw new BuildException(ioe);
            }
        }
    }
}
Also used : RegularExpression(org.apache.tools.ant.types.RegularExpression) Regexp(org.apache.tools.ant.util.regexp.Regexp) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Aggregations

RegularExpression (org.apache.tools.ant.types.RegularExpression)11 BuildException (org.apache.tools.ant.BuildException)4 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 LineContainsRegExp (org.apache.tools.ant.filters.LineContainsRegExp)2 BufferedWriter (java.io.BufferedWriter)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 TokenFilter (org.apache.tools.ant.filters.TokenFilter)1 FilterChain (org.apache.tools.ant.types.FilterChain)1 RedirectorElement (org.apache.tools.ant.types.RedirectorElement)1 Regexp (org.apache.tools.ant.util.regexp.Regexp)1