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