KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.regexp.RE;
23 import org.apache.regexp.RESyntaxException;
24 import org.apache.tools.ant.BuildException;
25
26 /**
27  * Implementation of RegexpMatcher for Jakarta-Regexp.
28  *
29  */

30 public class JakartaRegexpMatcher implements RegexpMatcher {
31
32     private String JavaDoc pattern;
33
34     /**
35      * Set the regexp pattern from the String description.
36      * @param pattern the pattern to match
37      */

38     public void setPattern(String JavaDoc pattern) {
39         this.pattern = pattern;
40     }
41
42     /**
43      * Get a String representation of the regexp pattern
44      * @return the pattern
45      */

46     public String JavaDoc getPattern() {
47         return pattern;
48     }
49
50     /**
51      * Compile the pattern.
52      *
53      * @param options the ant regexp options
54      * @return a compiled pattern
55      * @exception BuildException if an error occurs
56      */

57     protected RE getCompiledPattern(int options)
58         throws BuildException {
59         int cOptions = getCompilerOptions(options);
60         try {
61             RE reg = new RE(pattern);
62             reg.setMatchFlags(cOptions);
63             return reg;
64         } catch (RESyntaxException e) {
65             throw new BuildException(e);
66         }
67     }
68
69     /**
70      * Does the given argument match the pattern?
71      * @param argument the string to match against
72      * @return true if the pattern matches
73      * @throws BuildException on error
74      */

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

86     public boolean matches(String JavaDoc input, int options)
87         throws BuildException {
88         return matches(input, getCompiledPattern(options));
89     }
90
91     private boolean matches(String JavaDoc input, RE reg) {
92         return reg.match(input);
93     }
94
95     /**
96      * Returns a Vector of matched groups found in the argument
97      * using default options.
98      *
99      * <p>Group 0 will be the full match, the rest are the
100      * parenthesized subexpressions</p>.
101      *
102      * @param argument the string to match against
103      * @return the vector of groups
104      * @throws BuildException on error
105      */

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

121     public Vector JavaDoc getGroups(String JavaDoc input, int options)
122         throws BuildException {
123         RE reg = getCompiledPattern(options);
124         if (!matches(input, reg)) {
125             return null;
126         }
127         Vector JavaDoc v = new Vector JavaDoc();
128         int cnt = reg.getParenCount();
129         for (int i = 0; i < cnt; i++) {
130             String JavaDoc match = reg.getParen(i);
131             // treat non-matching groups as empty matches
132
if (match == null) {
133                 match = "";
134             }
135             v.addElement(match);
136         }
137         return v;
138     }
139
140     /**
141      * Convert the generic options to the regex compiler specific options.
142      * @param options the generic options
143      * @return the specific options
144      */

145     protected int getCompilerOptions(int options) {
146         int cOptions = RE.MATCH_NORMAL;
147
148         if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {
149             cOptions |= RE.MATCH_CASEINDEPENDENT;
150         }
151         if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {
152             cOptions |= RE.MATCH_MULTILINE;
153         }
154         if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {
155             cOptions |= RE.MATCH_SINGLELINE;
156         }
157
158         return cOptions;
159     }
160
161 }
162
Popular Tags