KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > oro > text > awk > AwkMatchResult


1 package org.apache.oro.text.awk;
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 import org.apache.oro.text.regex.*;
61
62 /**
63  * A class used to store and access the results of an AwkPattern match.
64  * It is important for you to remember that AwkMatcher does not save
65  * parenthesized sub-group information. Therefore the number of groups
66  * saved in an AwkMatchResult will always be 1.
67
68  @author <a HREF="dfs@savarese.org">Daniel F. Savarese</a>
69  @version $Id: AwkMatchResult.java,v 1.1.1.1 2000/07/23 23:08:49 jon Exp $
70
71  * @see org.apache.oro.text.regex.PatternMatcher
72  * @see AwkMatcher
73  * @see AwkCompiler
74  */

75
76 final class AwkMatchResult implements MatchResult {
77   /**
78    * The character offset into the line or stream where the match
79    * begins. Pattern matching methods that look for matches a line at
80    * a time should use this field as the offset into the line
81    * of the match. Methods that look for matches independent of line
82    * boundaries should use this field as the offset into the entire
83    * text stream.
84    */

85   private int __matchBeginOffset;
86
87
88   /**
89    * The length of the match. Stored as a convenience to avoid calling
90    * the String length(). Since groups aren't saved, all we need is the
91    * length and the offset into the stream.
92    */

93   private int __length;
94
95
96   /**
97    * The entire string that matched the pattern.
98    */

99   private String JavaDoc __match;
100
101
102   /**
103    * Default constructor given default access to prevent instantiation
104    * outside the package.
105    */

106   AwkMatchResult(String JavaDoc match, int matchBeginOffset){
107     __match = match;
108     __length = match.length();
109     __matchBeginOffset = matchBeginOffset;
110   }
111
112
113   /**
114    * @return The length of the match.
115    */

116   public int length(){ return __length; }
117
118
119   /**
120    * @return The number of groups contained in the result. This number
121    * includes the 0th group. In other words, the result refers
122    * to the number of parenthesized subgroups plus the entire match
123    * itself. Because Awk doesn't save parenthesized groups, this
124    * always returns 1.
125    */

126   public int groups(){ return 1; }
127
128
129   /**
130    * @param group The pattern subgroup to return.
131    * @return A string containing the indicated pattern subgroup. Group
132    * 0 always refers to the entire match. If a group was never
133    * matched, it returns null. This is not to be confused with
134    * a group matching the null string, which will return a String
135    * of length 0.
136    */

137   public String JavaDoc group(int group){ return (group == 0 ? __match : null); }
138
139
140   /**
141    * @param group The pattern subgroup.
142    * @return The offset into group 0 of the first token in the indicated
143    * pattern subgroup. If a group was never matched or does
144    * not exist, returns -1.
145    */

146   public int begin(int group){ return (group == 0 ? 0 : -1); }
147
148   /**
149    * @param group The pattern subgroup.
150    * @return Returns one plus the offset into group 0 of the last token in
151    * the indicated pattern subgroup. If a group was never matched
152    * or does not exist, returns -1. A group matching the null
153    * string will return its start offset.
154    */

155   public int end(int group){ return (group == 0 ? __length : -1); }
156
157
158   /**
159    * Returns an offset marking the beginning of the pattern match
160    * relative to the beginning of the input.
161    * <p>
162    * @param group The pattern subgroup.
163    * @return The offset of the first token in the indicated
164    * pattern subgroup. If a group was never matched or does
165    * not exist, returns -1.
166    */

167   public int beginOffset(int group){
168     return (group == 0 ? __matchBeginOffset : -1);
169   }
170
171   /**
172    * Returns an offset marking the end of the pattern match
173    * relative to the beginning of the input.
174    * <p>
175    * @param group The pattern subgroup.
176    * @return Returns one plus the offset of the last token in
177    * the indicated pattern subgroup. If a group was never matched
178    * or does not exist, returns -1. A group matching the null
179    * string will return its start offset.
180    */

181   public int endOffset(int group){
182     return (group == 0 ? __matchBeginOffset + __length : -1);
183   }
184
185
186   /**
187    * The same as group(0).
188    *
189    * @return A string containing the entire match.
190    */

191   public String JavaDoc toString() { return group(0); }
192
193 }
194
195
196
197
198
199
200
Popular Tags