public class JakartaOroMatcher implements RegexpMatcher { private String pattern; // CheckStyle:VisibilityModifier OFF - bc protected final Perl5Compiler compiler = new Perl5Compiler(); protected final Perl5Matcher matcher = new Perl5Matcher(); // CheckStyle:VisibilityModifier ON /** * Constructor for JakartaOroMatcher. */ public JakartaOroMatcher() { } /** * Set the regexp pattern from the String description. * @param pattern the pattern to match */ public void setPattern(String pattern) { this.pattern = pattern; } /** * Get a String representation of the regexp pattern * @return the pattern */ public String getPattern() { return this.pattern; } /** * Get a compiled representation of the regexp pattern * @param options the options * @return the compiled pattern * @throws BuildException on error */ protected Pattern getCompiledPattern(int options) throws BuildException { try { // compute the compiler options based on the input options first Pattern p = compiler.compile(pattern, getCompilerOptions(options)); return p; } catch (Exception e) { throw new BuildException(e); } } /** * Does the given argument match the pattern using default options? * @param argument the string to match against * @return true if the pattern matches * @throws BuildException on error */ public boolean matches(String argument) throws BuildException { return matches(argument, MATCH_DEFAULT); } /** * Does the given argument match the pattern? * @param input the string to match against * @param options the regex options to use * @return true if the pattern matches * @throws BuildException on error */ public boolean matches(String input, int options) throws BuildException { Pattern p = getCompiledPattern(options); return matcher.contains(input, p); } /** * Returns a Vector of matched groups found in the argument * using default options. * * <p>Group 0 will be the full match, the rest are the * parenthesized subexpressions</p>. * * @param argument the string to match against * @return the vector of groups * @throws BuildException on error */ public Vector getGroups(String argument) throws BuildException { return getGroups(argument, MATCH_DEFAULT); } /** * Returns a Vector of matched groups found in the argument. * * <p>Group 0 will be the full match, the rest are the * parenthesized subexpressions</p>. * * @param input the string to match against * @param options the regex options to use * @return the vector of groups * @throws BuildException on error */ public Vector getGroups(String input, int options) throws BuildException { if (!matches(input, options)) { return null; } Vector v = new Vector(); MatchResult mr = matcher.getMatch(); int cnt = mr.groups(); for (int i = 0; i < cnt; i++) { String match = mr.group(i); // treat non-matching groups as empty matches if (match == null) { match = ""; } v.addElement(match); } return v; } /** * Convert the generic options to the regex compiler specific options. * @param options the generic options * @return the specific options */ protected int getCompilerOptions(int options) { int cOptions = Perl5Compiler.DEFAULT_MASK; if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) { cOptions |= Perl5Compiler.CASE_INSENSITIVE_MASK; } if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) { cOptions |= Perl5Compiler.MULTILINE_MASK; } if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) { cOptions |= Perl5Compiler.SINGLELINE_MASK; } return cOptions;
public class JakartaRegexpMatcher implements RegexpMatcher { private String pattern; /** * Set the regexp pattern from the String description. * @param pattern the pattern to match */ public void setPattern(String pattern) { this.pattern = pattern; } /** * Get a String representation of the regexp pattern * @return the pattern */ public String getPattern() { return pattern; } /** * Compile the pattern. * * @param options the ant regexp options * @return a compiled pattern * @exception BuildException if an error occurs */ protected RE getCompiledPattern(int options) throws BuildException { int cOptions = getCompilerOptions(options); try { RE reg = new RE(pattern); reg.setMatchFlags(cOptions); return reg; } catch (RESyntaxException e) { throw new BuildException(e); } } /** * Does the given argument match the pattern? * @param argument the string to match against * @return true if the pattern matches * @throws BuildException on error */ public boolean matches(String argument) throws BuildException { return matches(argument, MATCH_DEFAULT); } /** * Does the given argument match the pattern? * @param input the string to match against * @param options the regex options to use * @return true if the pattern matches * @throws BuildException on error */ public boolean matches(String input, int options) throws BuildException { return matches(input, getCompiledPattern(options)); } private boolean matches(String input, RE reg) { return reg.match(input); } /** * Returns a Vector of matched groups found in the argument * using default options. * * <p>Group 0 will be the full match, the rest are the * parenthesized subexpressions</p>. * * @param argument the string to match against * @return the vector of groups * @throws BuildException on error */ public Vector getGroups(String argument) throws BuildException { return getGroups(argument, MATCH_DEFAULT); } /** * Returns a Vector of matched groups found in the argument. * * <p>Group 0 will be the full match, the rest are the * parenthesized subexpressions</p>. * * @param input the string to match against * @param options the regex options to use * @return the vector of groups * @throws BuildException on error */ public Vector getGroups(String input, int options) throws BuildException { RE reg = getCompiledPattern(options); if (!matches(input, reg)) { return null; } Vector v = new Vector(); int cnt = reg.getParenCount(); for (int i = 0; i < cnt; i++) { String match = reg.getParen(i); // treat non-matching groups as empty matches if (match == null) { match = ""; } v.addElement(match); } return v; } /** * Convert the generic options to the regex compiler specific options. * @param options the generic options * @return the specific options */ protected int getCompilerOptions(int options) { int cOptions = RE.MATCH_NORMAL; if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) { cOptions |= RE.MATCH_CASEINDEPENDENT; } if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) { cOptions |= RE.MATCH_MULTILINE; } if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) { cOptions |= RE.MATCH_SINGLELINE; } return cOptions;
Clone fragments detected by clone detection tool
File path: /apache-ant-1.7.0/src/org/apache/tools/ant/util/regexp/JakartaOroMatcher.java File path: /apache-ant-1.7.0/src/org/apache/tools/ant/util/regexp/JakartaRegexpMatcher.java
Method name: Method name:
Number of AST nodes: 0 Number of AST nodes: 0
1
public class JakartaOroMatcher implements RegexpMatcher {
1
public class JakartaRegexpMatcher implements RegexpMatcher {
2
    private String pattern;
2
    private String pattern;
3
    // CheckStyle:VisibilityModifier OFF - bc
4
    protected final Perl5Compiler compiler = new Perl5Compiler();
5
    protected final Perl5Matcher matcher = new Perl5Matcher();
6
    // CheckStyle:VisibilityModifier ON
7
    /**
8
     * Constructor for JakartaOroMatcher.
9
     */
10
    public JakartaOroMatcher() {
11
    }
12
    /**
3
    /**
13
     * Set the regexp pattern from the String description.
4
     * Set the regexp pattern from the String description.
14
     * @param pattern the pattern to match
5
     * @param pattern the pattern to match
15
     */
6
     */
16
    public void setPattern(String pattern) {
7
    public void setPattern(String pattern) {
17
        this.pattern = pattern;
8
        this.pattern = pattern;
18
    }
9
    }
19
    /**
10
    /**
20
     * Get a String representation of the regexp pattern
11
     * Get a String representation of the regexp pattern
21
     * @return the pattern
12
     * @return the pattern
22
     */
13
     */
23
    public String getPattern() {
14
    public String getPattern() {
24
        return this.pattern;
15
        return pattern;
25
    }
16
    }
26
    /**
17
    /**
27
     * Get a compiled representation of the regexp pattern
18
     * Compile the pattern.
19
     *
28
     * @param options the options
20
     * @param options the ant regexp options
29
     * @return the compiled pattern
21
     * @return a compiled pattern
30
     * @throws BuildException on error
22
     * @exception BuildException if an error occurs
31
     */
23
     */
32
    protected Pattern getCompiledPattern(int options)
24
    protected RE getCompiledPattern(int options)
33
        throws BuildException {
25
        throws BuildException {
34
        try {
26
        
35
            // compute the compiler options based on the input options first
27
int cOptions = getCompilerOptions
36
            Pattern p = compiler.compile(pattern, getCompilerOptions(o
28
(options);
29
        try {
30
            RE reg = new RE(pattern);
37
ptions));
31
            reg.setMatchFlags(cOptions);
38
            return p;
32
            return reg;
39
        } catch (Exception e) {
33
        } catch (RESyntaxException e) {
40
            throw new BuildException(e);
34
            throw new BuildException(e);
41
        }
35
        }
42
    }
36
    }
43
    /**
37
    /**
44
     * Does the given argument match the pattern using default options?
38
     * Does the given argument match the pattern?
45
     * @param argument the string to match against
39
     * @param argument the string to match against
46
     * @return true if the pattern matches
40
     * @return true if the pattern matches
47
     * @throws BuildException on error
41
     * @throws BuildException on error
48
     */
42
     */
49
    public boolean matches(String argument) throws BuildException {
43
    public boolean matches(String argument) throws BuildException {
50
        return matches(argument, MATCH_DEFAULT);
44
        return matches(argument, MATCH_DEFAULT);
51
    }
45
    }
52
    /**
46
    /**
53
     * Does the given argument match the pattern?
47
     * Does the given argument match the pattern?
54
     * @param input the string to match against
48
     * @param input the string to match against
55
     * @param options the regex options to use
49
     * @param options the regex options to use
56
     * @return true if the pattern matches
50
     * @return true if the pattern matches
57
     * @throws BuildException on error
51
     * @throws BuildException on error
58
     */
52
     */
59
    public boolean matches(String input, int options)
53
    public boolean matches(String input, int options)
60
        throws BuildException {
54
        throws BuildException {
61
        Pattern p = getCompiledPattern(options);
55
        return matches(input, getCompiledPattern(options));
62
    
56
    }
63
    return matcher.contains(input, p
57
    private boolean matches(String input, RE reg) {
64
);
58
        return reg.match(input);
65
    }
59
    }
66
    /**
60
    /**
67
     * Returns a Vector of matched groups found in the argument
61
     * Returns a Vector of matched groups found in the argument
68
     * using default options.
62
     * using default options.
69
     *
63
     *
70
     * <p>Group 0 will be the full match, the rest are the
64
     * <p>Group 0 will be the full match, the rest are the
71
     * parenthesized subexpressions</p>.
65
     * parenthesized subexpressions</p>.
72
     *
66
     *
73
     * @param argument the string to match against
67
     * @param argument the string to match against
74
     * @return the vector of groups
68
     * @return the vector of groups
75
     * @throws BuildException on error
69
     * @throws BuildException on error
76
     */
70
     */
77
    public Vector getGroups(String argument) throws BuildException {
71
    public Vector getGroups(String argument) throws BuildException {
78
        return getGroups(argument, MATCH_DEFAULT);
72
        return getGroups(argument, MATCH_DEFAULT);
79
    }
73
    }
80
    /**
74
    /**
81
     * Returns a Vector of matched groups found in the argument.
75
     * Returns a Vector of matched groups found in the argument.
82
     *
76
     *
83
     * <p>Group 0 will be the full match, the rest are the
77
     * <p>Group 0 will be the full match, the rest are the
84
     * parenthesized subexpressions</p>.
78
     * parenthesized subexpressions</p>.
85
     *
79
     *
86
     * @param input the string to match against
80
     * @param input the string to match against
87
     * @param options the regex options to use
81
     * @param options the regex options to use
88
     * @return the vector of groups
82
     * @return the vector of groups
89
     * @throws BuildException on error
83
     * @throws BuildException on error
90
     */
84
     */
91
    public Vector getGroups(String input, int options)
85
    public Vector getGroups(String input, int options)
92
        throws BuildException {
86
        throws BuildException {
87
        RE reg = getCompiledPattern(options);
93
        if (!matches(input, options)) {
88
        if (!matches(input, reg)) {
94
            return null;
89
            return null;
95
        }
90
        }
96
        Vector v = new Vector();
91
        Vector v = new Vector();
97
        MatchResult mr = matcher.getMatch();
92
        
98
        int cnt = mr.groups();
93
int cnt = reg.getParenCount();
99
        for (int i = 0; i < cnt; i++) {
94
        for (int i = 0; i < cnt; i++) {
100
            String match = mr.group(i);
95
            String match = reg.getParen(i);
101
            // treat non-matching groups as empty matches
96
            // treat non-matching groups as empty matches
102
            if (match == null) {
97
            if (match == null) {
103
                match = "";
98
                match = "";
104
            }
99
            }
105
            v.addElement(match);
100
            v.addElement(match);
106
        }
101
        }
107
        return v;
102
        return v;
108
    }
103
    }
109
    /**
104
    /**
110
     * Convert the generic options to the regex compiler specific options.
105
     * Convert the generic options to the regex compiler specific options.
111
     * @param options the generic options
106
     * @param options the generic options
112
     * @return the specific options
107
     * @return the specific options
113
     */
108
     */
114
    protected int getCompilerOptions(int options) {
109
    protected int getCompilerOptions(int options) {
115
        int cOptions = Perl5Compiler.DEFAULT_MASK;
110
        int cOptions = RE.MATCH_NORMAL;
116
        if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {
111
        if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {
117
            cOptions |= Perl5Compiler.CASE_INSENSITIVE_MASK;
112
            cOptions |= RE.MATCH_CASEINDEPENDENT;
118
        }
113
        }
119
        if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {
114
        if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {
120
            cOptions |= Perl5Compiler.MULTILINE_MASK;
115
            cOptions |= RE.MATCH_MULTILINE;
121
        }
116
        }
122
        if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {
117
        if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {
123
            cOptions |= Perl5Compiler.SINGLELINE_MASK;
118
            cOptions |= RE.MATCH_SINGLELINE;
124
        }
119
        }
125
        return cOptions;
120
        return cOptions;
126
    
121
    
Summary
Number of common nesting structure subtrees0
Number of refactorable cases0
Number of non-refactorable cases0
Time elapsed for finding largest common nesting structure subtrees (ms)0.0
Clones location
Number of node comparisons0