001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017:
018: package org.tp23.antinstaller.antmod;
019:
020: import java.util.Vector;
021: import java.util.regex.Matcher;
022: import java.util.regex.Pattern;
023: import java.util.regex.PatternSyntaxException;
024:
025: import org.apache.tools.ant.BuildException;
026: import org.apache.tools.ant.util.regexp.RegexpMatcher;
027: import org.apache.tools.ant.util.regexp.RegexpUtil;
028:
029: /**
030: * Implementation of RegexpMatcher for the built-in regexp matcher of
031: * JDK 1.4. UNIX_LINES option is enabled as a default.
032: *
033: */
034: public class Jdk14RegexpMatcher implements RegexpMatcher {
035:
036: private String pattern;
037:
038: public Jdk14RegexpMatcher() {
039: }
040:
041: /**
042: * Set the regexp pattern from the String description.
043: */
044: public void setPattern(String pattern) {
045: this .pattern = pattern;
046: }
047:
048: /**
049: * Get a String representation of the regexp pattern
050: */
051: public String getPattern() {
052: return pattern;
053: }
054:
055: protected Pattern getCompiledPattern(int options)
056: throws BuildException {
057: int cOptions = getCompilerOptions(options);
058: try {
059: Pattern p = Pattern.compile(this .pattern, cOptions);
060: return p;
061: } catch (PatternSyntaxException e) {
062: throw new BuildException(e);
063: }
064: }
065:
066: /**
067: * Does the given argument match the pattern?
068: */
069: public boolean matches(String argument) throws BuildException {
070: return matches(argument, MATCH_DEFAULT);
071: }
072:
073: /**
074: * Does the given argument match the pattern?
075: */
076: public boolean matches(String input, int options)
077: throws BuildException {
078: try {
079: Pattern p = getCompiledPattern(options);
080: return p.matcher(input).find();
081: } catch (Exception e) {
082: throw new BuildException(e);
083: }
084: }
085:
086: /**
087: * Returns a Vector of matched groups found in the argument.
088: *
089: * <p>Group 0 will be the full match, the rest are the
090: * parenthesized subexpressions</p>.
091: */
092: public Vector getGroups(String argument) throws BuildException {
093: return getGroups(argument, MATCH_DEFAULT);
094: }
095:
096: /**
097: * Returns a Vector of matched groups found in the argument.
098: *
099: * <p>Group 0 will be the full match, the rest are the
100: * parenthesized subexpressions</p>.
101: */
102: public Vector getGroups(String input, int options)
103: throws BuildException {
104: Pattern p = getCompiledPattern(options);
105: Matcher matcher = p.matcher(input);
106: if (!matcher.find()) {
107: return null;
108: }
109: Vector v = new Vector();
110: int cnt = matcher.groupCount();
111: for (int i = 0; i <= cnt; i++) {
112: String match = matcher.group(i);
113: // treat non-matching groups as empty matches
114: if (match == null) {
115: match = "";
116: }
117: v.addElement(match);
118: }
119: return v;
120: }
121:
122: protected int getCompilerOptions(int options) {
123: // be strict about line separator
124: int cOptions = Pattern.UNIX_LINES;
125:
126: if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {
127: cOptions |= Pattern.CASE_INSENSITIVE;
128: }
129: if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {
130: cOptions |= Pattern.MULTILINE;
131: }
132: if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {
133: cOptions |= Pattern.DOTALL;
134: }
135:
136: return cOptions;
137: }
138:
139: }
|