1 /*
2  * Copyright  2001-2004 The Apache Software Foundation
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10 *  Unless required by applicable law or agreed to in writing, software
11 *  distributed under the License is distributed on an "AS IS" BASIS,
12 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *  See the License for the specific language governing permissions and
14 *  limitations under the License.
15 *
16 */
17
18package org.tp23.antinstaller.antmod;
19
20import java.util.Vector;
21import java.util.regex.Matcher;
22import java.util.regex.Pattern;
23import java.util.regex.PatternSyntaxException;
24
25import org.apache.tools.ant.BuildException;
26import org.apache.tools.ant.util.regexp.RegexpMatcher;
27import org.apache.tools.ant.util.regexp.RegexpUtil;
28
29/**
30 * Implementation of RegexpMatcher for the built-in regexp matcher of
31 * JDK 1.4. UNIX_LINES option is enabled as a default.
32 *
33 */
34public class Jdk14RegexpMatcher implements RegexpMatcher {
35
36    private String pattern;
37
38    public Jdk14RegexpMatcher() {
39    }
40
41    /**
42     * Set the regexp pattern from the String description.
43     */
44    public void setPattern(String pattern) {
45        this.pattern = pattern;
46    }
47
48    /**
49     * Get a String representation of the regexp pattern
50     */
51    public String getPattern() {
52        return pattern;
53    }
54
55    protected Pattern getCompiledPattern(int options)
56        throws BuildException {
57        int cOptions = getCompilerOptions(options);
58        try {
59            Pattern p = Pattern.compile(this.pattern, cOptions);
60            return p;
61        } catch (PatternSyntaxException e) {
62            throw new BuildException(e);
63        }
64    }
65
66    /**
67     * Does the given argument match the pattern?
68     */
69    public boolean matches(String argument) throws BuildException {
70        return matches(argument, MATCH_DEFAULT);
71    }
72
73    /**
74     * Does the given argument match the pattern?
75     */
76    public boolean matches(String input, int options)
77        throws BuildException {
78        try {
79            Pattern p = getCompiledPattern(options);
80            return p.matcher(input).find();
81        } catch (Exception e) {
82            throw new BuildException(e);
83        }
84    }
85
86    /**
87     * Returns a Vector of matched groups found in the argument.
88     *
89     * <p>Group 0 will be the full match, the rest are the
90     * parenthesized subexpressions</p>.
91     */
92    public Vector getGroups(String argument) throws BuildException {
93        return getGroups(argument, MATCH_DEFAULT);
94    }
95
96    /**
97     * Returns a Vector of matched groups found in the argument.
98     *
99     * <p>Group 0 will be the full match, the rest are the
00     * parenthesized subexpressions</p>.
01     */
02    public Vector getGroups(String input, int options)
03        throws BuildException {
04        Pattern p = getCompiledPattern(options);
05        Matcher matcher = p.matcher(input);
06        if (!matcher.find()) {
07            return null;
08        }
09        Vector v = new Vector();
10        int cnt = matcher.groupCount();
11        for (int i = 0; i <= cnt; i++) {
12            String match = matcher.group(i);
13            // treat non-matching groups as empty matches
14            if (match == null) {
15                match = "";
16            }
17            v.addElement(match);
18        }
19        return v;
20    }
21
22    protected int getCompilerOptions(int options) {
23        // be strict about line separator
24        int cOptions = Pattern.UNIX_LINES;
25
26        if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {
27            cOptions |= Pattern.CASE_INSENSITIVE;
28        }
29        if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {
30            cOptions |= Pattern.MULTILINE;
31        }
32        if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {
33            cOptions |= Pattern.DOTALL;
34        }
35
36        return cOptions;
37    }
38
39}
40