KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > regexp > Jdk14RegexpMatcher


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.util.regexp;
20
21 import java.util.Vector JavaDoc;
22 import java.util.regex.Matcher JavaDoc;
23 import java.util.regex.Pattern JavaDoc;
24 import java.util.regex.PatternSyntaxException JavaDoc;
25 import org.apache.tools.ant.BuildException;
26
27 /**
28  * Implementation of RegexpMatcher for the built-in regexp matcher of
29  * JDK 1.4. UNIX_LINES option is enabled as a default.
30  *
31  */

32 public class Jdk14RegexpMatcher implements RegexpMatcher {
33
34     private String JavaDoc pattern;
35
36     /** Constructor for JakartaOroRegexp */
37     public Jdk14RegexpMatcher() {
38     }
39
40     /**
41      * Set the regexp pattern from the String description.
42      * @param pattern the pattern to match
43      */

44     public void setPattern(String JavaDoc pattern) {
45         this.pattern = pattern;
46     }
47
48     /**
49      * Get a String representation of the regexp pattern
50      * @return the pattern
51      * @throws BuildException on error
52      */

53     public String JavaDoc getPattern() {
54         return pattern;
55     }
56
57     /**
58      * Get a compiled representation of the regexp pattern
59      * @param options the options
60      * @return the compiled pattern
61      * @throws BuildException on error
62      */

63     protected Pattern JavaDoc getCompiledPattern(int options)
64         throws BuildException {
65         int cOptions = getCompilerOptions(options);
66         try {
67             Pattern JavaDoc p = Pattern.compile(this.pattern, cOptions);
68             return p;
69         } catch (PatternSyntaxException JavaDoc e) {
70             throw new BuildException(e);
71         }
72     }
73
74     /**
75      * Does the given argument match the pattern using default options?
76      * @param argument the string to match against
77      * @return true if the pattern matches
78      * @throws BuildException on error
79      */

80     public boolean matches(String JavaDoc argument) throws BuildException {
81         return matches(argument, MATCH_DEFAULT);
82     }
83
84     /**
85      * Does the given argument match the pattern?
86      * @param input the string to match against
87      * @param options the regex options to use
88      * @return true if the pattern matches
89      * @throws BuildException on error
90      */

91     public boolean matches(String JavaDoc input, int options)
92         throws BuildException {
93         try {
94             Pattern JavaDoc p = getCompiledPattern(options);
95             return p.matcher(input).find();
96         } catch (Exception JavaDoc e) {
97             throw new BuildException(e);
98         }
99     }
100
101     /**
102      * Returns a Vector of matched groups found in the argument
103      * using default options.
104      *
105      * <p>Group 0 will be the full match, the rest are the
106      * parenthesized subexpressions</p>.
107      *
108      * @param argument the string to match against
109      * @return the vector of groups
110      * @throws BuildException on error
111      */

112     public Vector JavaDoc getGroups(String JavaDoc argument) throws BuildException {
113         return getGroups(argument, MATCH_DEFAULT);
114     }
115
116     /**
117      * Returns a Vector of matched groups found in the argument.
118      *
119      * <p>Group 0 will be the full match, the rest are the
120      * parenthesized subexpressions</p>.
121      *
122      * @param input the string to match against
123      * @param options the regex options to use
124      * @return the vector of groups
125      * @throws BuildException on error
126      */

127     public Vector JavaDoc getGroups(String JavaDoc input, int options)
128         throws BuildException {
129         Pattern JavaDoc p = getCompiledPattern(options);
130         Matcher JavaDoc matcher = p.matcher(input);
131         if (!matcher.find()) {
132             return null;
133         }
134         Vector JavaDoc v = new Vector JavaDoc();
135         int cnt = matcher.groupCount();
136         for (int i = 0; i <= cnt; i++) {
137             String JavaDoc match = matcher.group(i);
138             // treat non-matching groups as empty matches
139
if (match == null) {
140                 match = "";
141             }
142             v.addElement(match);
143         }
144         return v;
145     }
146
147     /**
148      * Convert the generic options to the regex compiler specific options.
149      * @param options the generic options
150      * @return the specific options
151      */

152     protected int getCompilerOptions(int options) {
153         // be strict about line separator
154
int cOptions = Pattern.UNIX_LINES;
155
156         if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {
157             cOptions |= Pattern.CASE_INSENSITIVE;
158         }
159         if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {
160             cOptions |= Pattern.MULTILINE;
161         }
162         if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {
163             cOptions |= Pattern.DOTALL;
164         }
165
166         return cOptions;
167     }
168
169 }
170
Popular Tags