001: /*
002: * $Id: AwkMatchResult.java,v 1.8 2003/11/07 20:16:24 dfs Exp $
003: *
004: * ====================================================================
005: * The Apache Software License, Version 1.1
006: *
007: * Copyright (c) 2000 The Apache Software Foundation. All rights
008: * reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions
012: * are met:
013: *
014: * 1. Redistributions of source code must retain the above copyright
015: * notice, this list of conditions and the following disclaimer.
016: *
017: * 2. Redistributions in binary form must reproduce the above copyright
018: * notice, this list of conditions and the following disclaimer in
019: * the documentation and/or other materials provided with the
020: * distribution.
021: *
022: * 3. The end-user documentation included with the redistribution,
023: * if any, must include the following acknowledgment:
024: * "This product includes software developed by the
025: * Apache Software Foundation (http://www.apache.org/)."
026: * Alternately, this acknowledgment may appear in the software itself,
027: * if and wherever such third-party acknowledgments normally appear.
028: *
029: * 4. The names "Apache" and "Apache Software Foundation", "Jakarta-Oro"
030: * must not be used to endorse or promote products derived from this
031: * software without prior written permission. For written
032: * permission, please contact apache@apache.org.
033: *
034: * 5. Products derived from this software may not be called "Apache"
035: * or "Jakarta-Oro", nor may "Apache" or "Jakarta-Oro" appear in their
036: * name, without prior written permission of the Apache Software Foundation.
037: *
038: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
039: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
040: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
041: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
042: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
043: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
044: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
045: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
046: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
047: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
048: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: * ====================================================================
051: *
052: * This software consists of voluntary contributions made by many
053: * individuals on behalf of the Apache Software Foundation. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.oro.text.awk;
059:
060: import org.apache.oro.text.regex.*;
061:
062: /**
063: * A class used to store and access the results of an AwkPattern match.
064: * It is important for you to remember that AwkMatcher does not save
065: * parenthesized sub-group information. Therefore the number of groups
066: * saved in an AwkMatchResult will always be 1.
067: *
068: * @version @version@
069: * @since 1.0
070: * @see org.apache.oro.text.regex.PatternMatcher
071: * @see AwkMatcher
072: * @see AwkCompiler
073: */
074:
075: final class AwkMatchResult implements MatchResult {
076: /**
077: * The character offset into the line or stream where the match
078: * begins. Pattern matching methods that look for matches a line at
079: * a time should use this field as the offset into the line
080: * of the match. Methods that look for matches independent of line
081: * boundaries should use this field as the offset into the entire
082: * text stream.
083: */
084: private int __matchBeginOffset;
085:
086: /**
087: * The length of the match. Stored as a convenience to avoid calling
088: * the String length(). Since groups aren't saved, all we need is the
089: * length and the offset into the stream.
090: */
091: private int __length;
092:
093: /**
094: * The entire string that matched the pattern.
095: */
096: private String __match;
097:
098: /**
099: * Default constructor given default access to prevent instantiation
100: * outside the package.
101: */
102: AwkMatchResult(String match, int matchBeginOffset) {
103: __match = match;
104: __length = match.length();
105: __matchBeginOffset = matchBeginOffset;
106: }
107:
108: /**
109: * Adjusts the relative offset where the match begins to an absolute
110: * value. Only used by AwkMatcher to adjust the offset for stream
111: * matches.
112: */
113: void _incrementMatchBeginOffset(int streamOffset) {
114: __matchBeginOffset += streamOffset;
115: }
116:
117: /**
118: * @return The length of the match.
119: */
120: public int length() {
121: return __length;
122: }
123:
124: /**
125: * @return The number of groups contained in the result. This number
126: * includes the 0th group. In other words, the result refers
127: * to the number of parenthesized subgroups plus the entire match
128: * itself. Because Awk doesn't save parenthesized groups, this
129: * always returns 1.
130: */
131: public int groups() {
132: return 1;
133: }
134:
135: /**
136: * @param group The pattern subgroup to return.
137: * @return A string containing the indicated pattern subgroup. Group
138: * 0 always refers to the entire match. If a group was never
139: * matched, it returns null. This is not to be confused with
140: * a group matching the null string, which will return a String
141: * of length 0.
142: */
143: public String group(int group) {
144: return (group == 0 ? __match : null);
145: }
146:
147: /**
148: * @param group The pattern subgroup.
149: * @return The offset into group 0 of the first token in the indicated
150: * pattern subgroup. If a group was never matched or does
151: * not exist, returns -1.
152: */
153: public int begin(int group) {
154: return (group == 0 ? 0 : -1);
155: }
156:
157: /**
158: * @param group The pattern subgroup.
159: * @return Returns one plus the offset into group 0 of the last token in
160: * the indicated pattern subgroup. If a group was never matched
161: * or does not exist, returns -1. A group matching the null
162: * string will return its start offset.
163: */
164: public int end(int group) {
165: return (group == 0 ? __length : -1);
166: }
167:
168: /**
169: * Returns an offset marking the beginning of the pattern match
170: * relative to the beginning of the input.
171: * <p>
172: * @param group The pattern subgroup.
173: * @return The offset of the first token in the indicated
174: * pattern subgroup. If a group was never matched or does
175: * not exist, returns -1.
176: */
177: public int beginOffset(int group) {
178: return (group == 0 ? __matchBeginOffset : -1);
179: }
180:
181: /**
182: * Returns an offset marking the end of the pattern match
183: * relative to the beginning of the input.
184: * <p>
185: * @param group The pattern subgroup.
186: * @return Returns one plus the offset of the last token in
187: * the indicated pattern subgroup. If a group was never matched
188: * or does not exist, returns -1. A group matching the null
189: * string will return its start offset.
190: */
191: public int endOffset(int group) {
192: return (group == 0 ? __matchBeginOffset + __length : -1);
193: }
194:
195: /**
196: * The same as group(0).
197: *
198: * @return A string containing the entire match.
199: */
200: public String toString() {
201: return group(0);
202: }
203:
204: }
|