KickJava   Java API By Example, From Geeks To Geeks.

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


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.oro.text.regex.MatchResult;
23 import org.apache.oro.text.regex.Pattern;
24 import org.apache.oro.text.regex.Perl5Compiler;
25 import org.apache.oro.text.regex.Perl5Matcher;
26 import org.apache.tools.ant.BuildException;
27
28 /**
29  * Implementation of RegexpMatcher for Jakarta-ORO.
30  *
31  */

32 public class JakartaOroMatcher implements RegexpMatcher {
33
34     private String JavaDoc pattern;
35     // CheckStyle:VisibilityModifier OFF - bc
36
protected final Perl5Compiler compiler = new Perl5Compiler();
37     protected final Perl5Matcher matcher = new Perl5Matcher();
38     // CheckStyle:VisibilityModifier ON
39

40     /**
41      * Constructor for JakartaOroMatcher.
42      */

43     public JakartaOroMatcher() {
44     }
45
46     /**
47      * Set the regexp pattern from the String description.
48      * @param pattern the pattern to match
49      */

50     public void setPattern(String JavaDoc pattern) {
51         this.pattern = pattern;
52     }
53
54     /**
55      * Get a String representation of the regexp pattern
56      * @return the pattern
57      */

58     public String JavaDoc getPattern() {
59         return this.pattern;
60     }
61
62     /**
63      * Get a compiled representation of the regexp pattern
64      * @param options the options
65      * @return the compiled pattern
66      * @throws BuildException on error
67      */

68     protected Pattern getCompiledPattern(int options)
69         throws BuildException {
70         try {
71             // compute the compiler options based on the input options first
72
Pattern p = compiler.compile(pattern, getCompilerOptions(options));
73             return p;
74         } catch (Exception JavaDoc e) {
75             throw new BuildException(e);
76         }
77     }
78
79     /**
80      * Does the given argument match the pattern using default options?
81      * @param argument the string to match against
82      * @return true if the pattern matches
83      * @throws BuildException on error
84      */

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

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

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

128     public Vector JavaDoc getGroups(String JavaDoc input, int options)
129         throws BuildException {
130         if (!matches(input, options)) {
131             return null;
132         }
133         Vector JavaDoc v = new Vector JavaDoc();
134         MatchResult mr = matcher.getMatch();
135         int cnt = mr.groups();
136         for (int i = 0; i < cnt; i++) {
137             String JavaDoc match = mr.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         int cOptions = Perl5Compiler.DEFAULT_MASK;
154
155         if (RegexpUtil.hasFlag(options, MATCH_CASE_INSENSITIVE)) {
156             cOptions |= Perl5Compiler.CASE_INSENSITIVE_MASK;
157         }
158         if (RegexpUtil.hasFlag(options, MATCH_MULTILINE)) {
159             cOptions |= Perl5Compiler.MULTILINE_MASK;
160         }
161         if (RegexpUtil.hasFlag(options, MATCH_SINGLELINE)) {
162             cOptions |= Perl5Compiler.SINGLELINE_MASK;
163         }
164
165         return cOptions;
166     }
167
168 }
169
Popular Tags