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