KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > oro > text > regex > Perl5MatchResult


1 package org.apache.oro.text.regex;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2000 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation", "Jakarta-Oro"
29  * must not be used to endorse or promote products derived from this
30  * software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache"
34  * or "Jakarta-Oro", nor may "Apache" or "Jakarta-Oro" appear in their
35  * name, without prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  *
56  * Portions of this software are based upon software originally written
57  * by Daniel F. Savarese. We appreciate his contributions.
58  */

59
60 /**
61  * A class used to store and access the results of a Perl5Pattern match.
62
63  @author <a HREF="dfs@savarese.org">Daniel F. Savarese</a>
64  @version $Id: Perl5MatchResult.java,v 1.1.1.1 2000/07/23 23:08:52 jon Exp $
65
66  * @see PatternMatcher
67  * @see Perl5Matcher
68  */

69 final class Perl5MatchResult implements MatchResult {
70   /**
71    * The character offset into the line or stream where the match
72    * begins. Pattern matching methods that look for matches a line at
73    * a time should use this field as the offset into the line
74    * of the match. Methods that look for matches independent of line
75    * boundaries should use this field as the offset into the entire
76    * text stream.
77    */

78    int _matchBeginOffset;
79
80
81   /**
82    * Arrays containing the beginning and end offsets of the pattern
83    * groups matched within the actual matched pattern contained in the
84    * variable <code>match</code>.
85    * Pattern matching methods that do not match subgroups, will only contain
86    * entries for group 0, which always refers to the entire pattern.
87    * <code>beginGroupOffset</code> contains the start offset of the groups,
88    * indexed by group number, which will always be 0 for group 0.
89    * <code>endGroupOffset</code> contains the ending offset + 1 of the groups.
90    * A group matching the null string will have <code>beginGroupOffset</code>
91    * and <code>endGroupOffset</code> entries of equal value. Following a
92    * convention established by the GNU regular expression library for the
93    * C language, groups that are not part of a match contain -1 as their
94    * begin and end offsets.
95    */

96    int[] _beginGroupOffset, _endGroupOffset;
97
98
99   /**
100    * The entire string that matched the pattern.
101    */

102    String JavaDoc _match;
103
104
105   /**
106    * Constructs a MatchResult able to store match information for
107    * a number of subpattern groups.
108    * <p>
109    * @param groups The number of groups this MatchResult can store.
110    * Only postitive values greater than or equal to 1 make any
111    * sense. At minimum, a MatchResult stores one group which
112    * represents the entire pattern matched including all subparts.
113    */

114   Perl5MatchResult(int groups){
115     _beginGroupOffset = new int[groups];
116     _endGroupOffset = new int[groups];
117   }
118
119
120   /**
121    * @return The length of the match.
122    */

123   public int length(){
124     return _match.length();
125   }
126
127
128   /**
129    * @return The number of groups contained in the result. This number
130    * includes the 0th group. In other words, the result refers
131    * to the number of parenthesized subgroups plus the entire match
132    * itself.
133    */

134   public int groups(){
135     return _beginGroupOffset.length;
136   }
137
138   /**
139    * @param group The pattern subgroup to return.
140    * @return A string containing the indicated pattern subgroup. Group
141    * 0 always refers to the entire match. If a group was never
142    * matched, it returns null. This is not to be confused with
143    * a group matching the null string, which will return a String
144    * of length 0.
145    */

146   public String JavaDoc group(int group){
147     int begin, end, length;
148
149     if(group < _beginGroupOffset.length){
150       begin = _beginGroupOffset[group];
151       end = _endGroupOffset[group];
152       length = _match.length();
153
154       if(begin >= 0 && end >= 0) {
155     if(begin < length && end <= length && end > begin)
156       return _match.substring(begin, end);
157     else if(begin <= end)
158       return "";
159       }
160     }
161
162     return null;
163   }
164
165   /**
166    * @param group The pattern subgroup.
167    * @return The offset into group 0 of the first token in the indicated
168    * pattern subgroup. If a group was never matched or does
169    * not exist, returns -1.
170    */

171   public int begin(int group){
172     int begin, end;//, length;
173
if(group < _beginGroupOffset.length){
174       begin = _beginGroupOffset[group];
175       end = _endGroupOffset[group];
176       //length = _match.length();
177
if(begin >= 0 && end >= 0)// && begin < length && end <= length)
178
//return _beginGroupOffset[group];
179
return begin;
180     }
181
182     return -1;
183   }
184
185   /**
186    * @param group The pattern subgroup.
187    * @return Returns one plus the offset into group 0 of the last token in
188    * the indicated pattern subgroup. If a group was never matched
189    * or does not exist, returns -1. A group matching the null
190    * string will return its start offset.
191    */

192   public int end(int group){
193     int begin, end; //, length;
194
if(group < _beginGroupOffset.length){
195       begin = _beginGroupOffset[group];
196       end = _endGroupOffset[group];
197       //length = _match.length();
198
if(begin >= 0 && end >= 0)// && begin < length && end <= length)
199
//return _endGroupOffset[group];
200
return end;
201     }
202     return -1;
203   }
204
205   /**
206    * Returns an offset marking the beginning of the pattern match
207    * relative to the beginning of the input.
208    * <p>
209    * @param group The pattern subgroup.
210    * @return The offset of the first token in the indicated
211    * pattern subgroup. If a group was never matched or does
212    * not exist, returns -1.
213    */

214   public int beginOffset(int group){
215     int begin, end;//, length;
216
if(group < _beginGroupOffset.length){
217       begin = _beginGroupOffset[group];
218       end = _endGroupOffset[group];
219       //length = _match.length();
220
if(begin >= 0 && end >= 0)// && begin < length && end <= length)
221
//return _matchBeginOffset + _beginGroupOffset[group];
222
return _matchBeginOffset + begin;
223     }
224     return -1;
225   }
226
227   /**
228    * Returns an offset marking the end of the pattern match
229    * relative to the beginning of the input.
230    * <p>
231    * @param group The pattern subgroup.
232    * @return Returns one plus the offset of the last token in
233    * the indicated pattern subgroup. If a group was never matched
234    * or does not exist, returns -1. A group matching the null
235    * string will return its start offset.
236    */

237   public int endOffset(int group){
238     int begin, end;//, length;
239
if(group < _endGroupOffset.length){
240       begin = _beginGroupOffset[group];
241       end = _endGroupOffset[group];
242       //length = _match.length();
243
if(begin >= 0 && end >= 0)// && begin < length && end <= length)
244
//return _matchBeginOffset + _endGroupOffset[group];
245
return _matchBeginOffset + end;
246     }
247     return -1;
248   }
249
250
251   /**
252    * The same as group(0).
253    *
254    * @return A string containing the entire match.
255    */

256   public String JavaDoc toString() {
257     return group(0);
258   }
259 }
260
Popular Tags