001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.util.regexp;
020:
021: import java.util.Vector;
022: import org.apache.oro.text.regex.MatchResult;
023: import org.apache.oro.text.regex.Pattern;
024: import org.apache.oro.text.regex.Perl5Compiler;
025: import org.apache.oro.text.regex.Perl5Matcher;
026: import org.apache.tools.ant.BuildException;
027:
028: /**
029: * Implementation of RegexpMatcher for Jakarta-ORO.
030: *
031: */
032: public class JakartaOroMatcher implements RegexpMatcher {
033:
034: private String pattern;
035: // CheckStyle:VisibilityModifier OFF - bc
036: protected final Perl5Compiler compiler = new Perl5Compiler();
037: protected final Perl5Matcher matcher = new Perl5Matcher();
038:
039: // CheckStyle:VisibilityModifier ON
040:
041: /**
042: * Constructor for JakartaOroMatcher.
043: */
044: public JakartaOroMatcher() {
045: }
046:
047: /**
048: * Set the regexp pattern from the String description.
049: * @param pattern the pattern to match
050: */
051: public void setPattern(String pattern) {
052: this .pattern = pattern;
053: }
054:
055: /**
056: * Get a String representation of the regexp pattern
057: * @return the pattern
058: */
059: public String getPattern() {
060: return this .pattern;
061: }
062:
063: /**
064: * Get a compiled representation of the regexp pattern
065: * @param options the options
066: * @return the compiled pattern
067: * @throws BuildException on error
068: */
069: protected Pattern getCompiledPattern(int options)
070: throws BuildException {
071: try {
072: // compute the compiler options based on the input options first
073: Pattern p = compiler.compile(pattern,
074: getCompilerOptions(options));
075: return p;
076: } catch (Exception e) {
077: throw new BuildException(e);
078: }
079: }
080:
081: /**
082: * Does the given argument match the pattern using default options?
083: * @param argument the string to match against
084: * @return true if the pattern matches
085: * @throws BuildException on error
086: */
087: public boolean matches(String argument) throws BuildException {
088: return matches(argument, MATCH_DEFAULT);
089: }
090:
091: /**
092: * Does the given argument match the pattern?
093: * @param input the string to match against
094: * @param options the regex options to use
095: * @return true if the pattern matches
096: * @throws BuildException on error
097: */
098: public boolean matches(String input, int options)
099: throws BuildException {
100: Pattern p = getCompiledPattern(options);
101: return matcher.contains(input, p);
102: }
103:
104: /**
105: * Returns a Vector of matched groups found in the argument
106: * using default options.
107: *
108: * <p>Group 0 will be the full match, the rest are the
109: * parenthesized subexpressions</p>.
110: *
111: * @param argument the string to match against
112: * @return the vector of groups
113: * @throws BuildException on error
114: */
115: public Vector getGroups(String argument) throws BuildException {
116: return getGroups(argument, MATCH_DEFAULT);
117: }
118:
119: /**
120: * Returns a Vector of matched groups found in the argument.
121: *
122: * <p>Group 0 will be the full match, the rest are the
123: * parenthesized subexpressions</p>.
124: *
125: * @param input the string to match against
126: * @param options the regex options to use
127: * @return the vector of groups
128: * @throws BuildException on error
129: */
130: public Vector getGroups(String input, int options)
131: throws BuildException {
132: if (!matches(input, options)) {
133: return null;
134: }
135: Vector v = new Vector();
136: MatchResult mr = matcher.getMatch();
137: int cnt = mr.groups();
138: for (int i = 0; i < cnt; i++) {
139: String match = mr.group(i);
140: // treat non-matching groups as empty matches
141: if (match == null) {
142: match = "";
143: }
144: v.addElement(match);
145: }
146: return v;
147: }
148:
149: /**
150: * Convert the generic options to the regex compiler specific options.
151: * @param options the generic options
152: * @return the specific options
153: */
154: protected int getCompilerOptions(int options) {
155: int cOptions = Perl5Compiler.DEFAULT_MASK;
156:
157: if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {
158: cOptions |= Perl5Compiler.CASE_INSENSITIVE_MASK;
159: }
160: if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {
161: cOptions |= Perl5Compiler.MULTILINE_MASK;
162: }
163: if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {
164: cOptions |= Perl5Compiler.SINGLELINE_MASK;
165: }
166:
167: return cOptions;
168: }
169:
170: }
|