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 |
|
---|
18 | package org.tp23.antinstaller.antmod;
|
---|
19 |
|
---|
20 | import java.util.Vector;
|
---|
21 | import java.util.regex.Matcher;
|
---|
22 | import java.util.regex.Pattern;
|
---|
23 | import java.util.regex.PatternSyntaxException;
|
---|
24 |
|
---|
25 | import org.apache.tools.ant.BuildException;
|
---|
26 | import org.apache.tools.ant.util.regexp.RegexpMatcher;
|
---|
27 | import 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 | */
|
---|
34 | public 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
|
---|
100 | * parenthesized subexpressions</p>.
|
---|
101 | */
|
---|
102 | public Vector getGroups(String input, int options)
|
---|
103 | throws BuildException {
|
---|
104 | Pattern p = getCompiledPattern(options);
|
---|
105 | Matcher matcher = p.matcher(input);
|
---|
106 | if (!matcher.find()) {
|
---|
107 | return null;
|
---|
108 | }
|
---|
109 | Vector v = new Vector();
|
---|
110 | int cnt = matcher.groupCount();
|
---|
111 | for (int i = 0; i <= cnt; i++) {
|
---|
112 | String match = matcher.group(i);
|
---|
113 | // treat non-matching groups as empty matches
|
---|
114 | if (match == null) {
|
---|
115 | match = "";
|
---|
116 | }
|
---|
117 | v.addElement(match);
|
---|
118 | }
|
---|
119 | return v;
|
---|
120 | }
|
---|
121 |
|
---|
122 | protected int getCompilerOptions(int options) {
|
---|
123 | // be strict about line separator
|
---|
124 | int cOptions = Pattern.UNIX_LINES;
|
---|
125 |
|
---|
126 | if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {
|
---|
127 | cOptions |= Pattern.CASE_INSENSITIVE;
|
---|
128 | }
|
---|
129 | if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {
|
---|
130 | cOptions |= Pattern.MULTILINE;
|
---|
131 | }
|
---|
132 | if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {
|
---|
133 | cOptions |= Pattern.DOTALL;
|
---|
134 | }
|
---|
135 |
|
---|
136 | return cOptions;
|
---|
137 | }
|
---|
138 |
|
---|
139 | }
|
---|