Search in sources :

Example 1 with Regexp

use of org.apache.tools.ant.util.regexp.Regexp in project JikesRVM by JikesRVM.

the class SelectRegexTask method matchFile.

private String matchFile() {
    BufferedReader input = null;
    try {
        final Regexp regexp = this.pattern.getRegexp(getProject());
        input = new BufferedReader(new FileReader(file));
        String[] lines = new String[patternLines];
        String sep = System.getProperty("line.separator");
        for (int i = 0; i < lines.length; i++) {
            lines[i] = "";
        }
        int nextLine = 0;
        while ((lines[nextLine] = input.readLine()) != null) {
            StringBuilder sb = new StringBuilder();
            for (int i = nextLine + 1; i <= nextLine + lines.length; i++) {
                String line = lines[i % lines.length];
                sb.append(line);
                sb.append(sep);
            }
            String result = performMatching(sb.toString());
            if (result != null) {
                return result;
            }
            nextLine = (nextLine + 1) % lines.length;
        }
        return null;
    } catch (IOException ioe) {
        throw new BuildException("Error loading file " + file, ioe, getLocation());
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (final IOException ioe) {
            // ignore
            }
        }
    }
}
Also used : Regexp(org.apache.tools.ant.util.regexp.Regexp) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException)

Example 2 with Regexp

use of org.apache.tools.ant.util.regexp.Regexp in project ant by apache.

the class ReplaceRegExp method doReplace.

/**
 * Invoke a regular expression (r) on a string (input) using
 * substitutions (s) for a matching regex.
 *
 * @param r a regular expression
 * @param s a Substitution
 * @param input the string to do the replacement on
 * @param options The options for the regular expression
 * @return the replacement result
 */
protected String doReplace(RegularExpression r, Substitution s, String input, int options) {
    String res = input;
    Regexp regexp = r.getRegexp(getProject());
    if (regexp.matches(input, options)) {
        log("Found match; substituting", Project.MSG_DEBUG);
        res = regexp.substitute(input, s.getExpression(getProject()), options);
    }
    return res;
}
Also used : Regexp(org.apache.tools.ant.util.regexp.Regexp)

Example 3 with Regexp

use of org.apache.tools.ant.util.regexp.Regexp 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)

Example 4 with Regexp

use of org.apache.tools.ant.util.regexp.Regexp in project JikesRVM by JikesRVM.

the class SelectRegexTask method performMatching.

private String performMatching(final String input) {
    final Regexp regexp = this.pattern.getRegexp(getProject());
    final Vector groups = regexp.getGroups(input, 0);
    if (groups != null && !groups.isEmpty()) {
        String output = select;
        final int count = groups.size();
        for (int i = 0; i < count; i++) {
            final String group = (String) groups.get(i);
            output = output.replace("\\" + i, group);
        }
        return output;
    }
    return null;
}
Also used : Regexp(org.apache.tools.ant.util.regexp.Regexp) Vector(java.util.Vector)

Example 5 with Regexp

use of org.apache.tools.ant.util.regexp.Regexp in project ant by apache.

the class Matches method eval.

/**
 * @return true if the string matches the regular expression pattern
 * @exception BuildException if the attributes are not set correctly
 */
public boolean eval() throws BuildException {
    if (string == null) {
        throw new BuildException("Parameter string is required in matches.");
    }
    if (regularExpression == null) {
        throw new BuildException("Missing pattern in matches.");
    }
    int options = RegexpUtil.asOptions(caseSensitive, multiLine, singleLine);
    Regexp regexp = regularExpression.getRegexp(getProject());
    return regexp.matches(string, options);
}
Also used : Regexp(org.apache.tools.ant.util.regexp.Regexp) BuildException(org.apache.tools.ant.BuildException)

Aggregations

Regexp (org.apache.tools.ant.util.regexp.Regexp)5 BuildException (org.apache.tools.ant.BuildException)3 BufferedReader (java.io.BufferedReader)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 BufferedWriter (java.io.BufferedWriter)1 FileWriter (java.io.FileWriter)1 ArrayList (java.util.ArrayList)1 Vector (java.util.Vector)1 RegularExpression (org.apache.tools.ant.types.RegularExpression)1