001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 1999,2000 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.xerces.utils.regex;
059:
060: import java.text.CharacterIterator;
061:
062: /**
063: *
064: * An instance of this class has ranges captured in matching.
065: *
066: * @see org.apache.xerces.utils.regex.RegularExpression#matches(char[], int, int, org.apache.xerces.utils.regex.Match)
067: * @see org.apache.xerces.utils.regex.RegularExpression#matches(char[], org.apache.xerces.utils.regex.Match)
068: * @see org.apache.xerces.utils.regex.RegularExpression#matches(java.text.CharacterIterator, org.apache.xerces.utils.regex.Match)
069: * @see org.apache.xerces.utils.regex.RegularExpression#matches(java.lang.String, int, int, org.apache.xerces.utils.regex.Match)
070: * @see org.apache.xerces.utils.regex.RegularExpression#matches(java.lang.String, org.apache.xerces.utils.regex.Match)
071: * @author TAMURA Kent <kent@trl.ibm.co.jp>
072: */
073: public class Match implements Cloneable {
074: int[] beginpos = null;
075: int[] endpos = null;
076: int nofgroups = 0;
077:
078: CharacterIterator ciSource = null;
079: String strSource = null;
080: char[] charSource = null;
081:
082: /**
083: * Creates an instance.
084: */
085: public Match() {
086: }
087:
088: /**
089: *
090: */
091: public synchronized Object clone() {
092: Match ma = new Match();
093: if (this .nofgroups > 0) {
094: ma.setNumberOfGroups(this .nofgroups);
095: if (this .ciSource != null)
096: ma.setSource(this .ciSource);
097: if (this .strSource != null)
098: ma.setSource(this .strSource);
099: for (int i = 0; i < this .nofgroups; i++) {
100: ma.setBeginning(i, this .getBeginning(i));
101: ma.setEnd(i, this .getEnd(i));
102: }
103: }
104: return ma;
105: }
106:
107: /**
108: *
109: */
110: protected void setNumberOfGroups(int n) {
111: int oldn = this .nofgroups;
112: this .nofgroups = n;
113: if (oldn <= 0 || oldn < n || n * 2 < oldn) {
114: this .beginpos = new int[n];
115: this .endpos = new int[n];
116: }
117: for (int i = 0; i < n; i++) {
118: this .beginpos[i] = -1;
119: this .endpos[i] = -1;
120: }
121: }
122:
123: /**
124: *
125: */
126: protected void setSource(CharacterIterator ci) {
127: this .ciSource = ci;
128: this .strSource = null;
129: this .charSource = null;
130: }
131:
132: /**
133: *
134: */
135: protected void setSource(String str) {
136: this .ciSource = null;
137: this .strSource = str;
138: this .charSource = null;
139: }
140:
141: /**
142: *
143: */
144: protected void setSource(char[] chars) {
145: this .ciSource = null;
146: this .strSource = null;
147: this .charSource = chars;
148: }
149:
150: /**
151: *
152: */
153: protected void setBeginning(int index, int v) {
154: this .beginpos[index] = v;
155: }
156:
157: /**
158: *
159: */
160: protected void setEnd(int index, int v) {
161: this .endpos[index] = v;
162: }
163:
164: /**
165: * Return the number of regular expression groups.
166: * This method returns 1 when the regular expression has no capturing-parenthesis.
167: */
168: public int getNumberOfGroups() {
169: if (this .nofgroups <= 0)
170: throw new IllegalStateException("A result is not set.");
171: return this .nofgroups;
172: }
173:
174: /**
175: * Return a start position in the target text matched to specified regular expression group.
176: *
177: * @param index Less than <code>getNumberOfGroups()</code>.
178: */
179: public int getBeginning(int index) {
180: if (this .beginpos == null)
181: throw new IllegalStateException("A result is not set.");
182: if (index < 0 || this .nofgroups <= index)
183: throw new IllegalArgumentException(
184: "The parameter must be less than " + this .nofgroups
185: + ": " + index);
186: return this .beginpos[index];
187: }
188:
189: /**
190: * Return an end position in the target text matched to specified regular expression group.
191: *
192: * @param index Less than <code>getNumberOfGroups()</code>.
193: */
194: public int getEnd(int index) {
195: if (this .endpos == null)
196: throw new IllegalStateException("A result is not set.");
197: if (index < 0 || this .nofgroups <= index)
198: throw new IllegalArgumentException(
199: "The parameter must be less than " + this .nofgroups
200: + ": " + index);
201: return this .endpos[index];
202: }
203:
204: /**
205: * Return an substring of the target text matched to specified regular expression group.
206: *
207: * @param index Less than <code>getNumberOfGroups()</code>.
208: */
209: public String getCapturedText(int index) {
210: if (this .beginpos == null)
211: throw new IllegalStateException(
212: "match() has never been called.");
213: if (index < 0 || this .nofgroups <= index)
214: throw new IllegalArgumentException(
215: "The parameter must be less than " + this .nofgroups
216: + ": " + index);
217: String ret;
218: int begin = this .beginpos[index], end = this .endpos[index];
219: if (begin < 0 || end < 0)
220: return null;
221: if (this .ciSource != null) {
222: ret = REUtil.substring(this .ciSource, begin, end);
223: } else if (this .strSource != null) {
224: ret = this .strSource.substring(begin, end);
225: } else {
226: ret = new String(this.charSource, begin, end - begin);
227: }
228: return ret;
229: }
230: }
|