source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/util/regexp/JakartaRegexpMatcher.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 4.8 KB
Line 
1/*
2 * Copyright 2000-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.apache.tools.ant.util.regexp;
19
20import java.util.Vector;
21import org.apache.regexp.RE;
22import org.apache.regexp.RESyntaxException;
23import org.apache.tools.ant.BuildException;
24
25/**
26 * Implementation of RegexpMatcher for Jakarta-Regexp.
27 *
28 */
29public class JakartaRegexpMatcher implements RegexpMatcher {
30
31 private String pattern;
32
33 /**
34 * Set the regexp pattern from the String description.
35 * @param pattern the pattern to match
36 */
37 public void setPattern(String pattern) {
38 this.pattern = pattern;
39 }
40
41 /**
42 * Get a String representation of the regexp pattern
43 * @return the pattern
44 */
45 public String getPattern() {
46 return pattern;
47 }
48
49 /**
50 * Compile the pattern.
51 *
52 * @param options the ant regexp options
53 * @return a compiled pattern
54 * @exception BuildException if an error occurs
55 */
56 protected RE getCompiledPattern(int options)
57 throws BuildException {
58 int cOptions = getCompilerOptions(options);
59 try {
60 RE reg = new RE(pattern);
61 reg.setMatchFlags(cOptions);
62 return reg;
63 } catch (RESyntaxException e) {
64 throw new BuildException(e);
65 }
66 }
67
68 /**
69 * Does the given argument match the pattern?
70 * @param argument the string to match against
71 * @return true if the pattern matches
72 * @throws BuildException on error
73 */
74 public boolean matches(String argument) throws BuildException {
75 return matches(argument, MATCH_DEFAULT);
76 }
77
78 /**
79 * Does the given argument match the pattern?
80 * @param input the string to match against
81 * @param options the regex options to use
82 * @return true if the pattern matches
83 * @throws BuildException on error
84 */
85 public boolean matches(String input, int options)
86 throws BuildException {
87 return matches(input, getCompiledPattern(options));
88 }
89
90 private boolean matches(String input, RE reg) {
91 return reg.match(input);
92 }
93
94 /**
95 * Returns a Vector of matched groups found in the argument
96 * using default options.
97 *
98 * <p>Group 0 will be the full match, the rest are the
99 * parenthesized subexpressions</p>.
100 *
101 * @param argument the string to match against
102 * @return the vector of groups
103 * @throws BuildException on error
104 */
105 public Vector getGroups(String argument) throws BuildException {
106 return getGroups(argument, MATCH_DEFAULT);
107 }
108
109 /**
110 * Returns a Vector of matched groups found in the argument.
111 *
112 * <p>Group 0 will be the full match, the rest are the
113 * parenthesized subexpressions</p>.
114 *
115 * @param input the string to match against
116 * @param options the regex options to use
117 * @return the vector of groups
118 * @throws BuildException on error
119 */
120 public Vector getGroups(String input, int options)
121 throws BuildException {
122 RE reg = getCompiledPattern(options);
123 if (!matches(input, reg)) {
124 return null;
125 }
126 Vector v = new Vector();
127 int cnt = reg.getParenCount();
128 for (int i = 0; i < cnt; i++) {
129 String match = reg.getParen(i);
130 // treat non-matching groups as empty matches
131 if (match == null) {
132 match = "";
133 }
134 v.addElement(match);
135 }
136 return v;
137 }
138
139 /**
140 * Convert the generic options to the regex compiler specific options.
141 * @param options the generic options
142 * @return the specific options
143 */
144 protected int getCompilerOptions(int options) {
145 int cOptions = RE.MATCH_NORMAL;
146
147 if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {
148 cOptions |= RE.MATCH_CASEINDEPENDENT;
149 }
150 if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {
151 cOptions |= RE.MATCH_MULTILINE;
152 }
153 if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {
154 cOptions |= RE.MATCH_SINGLELINE;
155 }
156
157 return cOptions;
158 }
159
160}
Note: See TracBrowser for help on using the repository browser.