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.regexp.RE;
023: import org.apache.regexp.RESyntaxException;
024: import org.apache.tools.ant.BuildException;
025:
026: /**
027: * Implementation of RegexpMatcher for Jakarta-Regexp.
028: *
029: */
030: public class JakartaRegexpMatcher implements RegexpMatcher {
031:
032: private String pattern;
033:
034: /**
035: * Set the regexp pattern from the String description.
036: * @param pattern the pattern to match
037: */
038: public void setPattern(String pattern) {
039: this .pattern = pattern;
040: }
041:
042: /**
043: * Get a String representation of the regexp pattern
044: * @return the pattern
045: */
046: public String getPattern() {
047: return pattern;
048: }
049:
050: /**
051: * Compile the pattern.
052: *
053: * @param options the ant regexp options
054: * @return a compiled pattern
055: * @exception BuildException if an error occurs
056: */
057: protected RE getCompiledPattern(int options) throws BuildException {
058: int cOptions = getCompilerOptions(options);
059: try {
060: RE reg = new RE(pattern);
061: reg.setMatchFlags(cOptions);
062: return reg;
063: } catch (RESyntaxException e) {
064: throw new BuildException(e);
065: }
066: }
067:
068: /**
069: * Does the given argument match the pattern?
070: * @param argument the string to match against
071: * @return true if the pattern matches
072: * @throws BuildException on error
073: */
074: public boolean matches(String argument) throws BuildException {
075: return matches(argument, MATCH_DEFAULT);
076: }
077:
078: /**
079: * Does the given argument match the pattern?
080: * @param input the string to match against
081: * @param options the regex options to use
082: * @return true if the pattern matches
083: * @throws BuildException on error
084: */
085: public boolean matches(String input, int options)
086: throws BuildException {
087: return matches(input, getCompiledPattern(options));
088: }
089:
090: private boolean matches(String input, RE reg) {
091: return reg.match(input);
092: }
093:
094: /**
095: * Returns a Vector of matched groups found in the argument
096: * using default options.
097: *
098: * <p>Group 0 will be the full match, the rest are the
099: * 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: }
|