001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.xpath.regex;
019:
020: import java.text.CharacterIterator;
021:
022: /**
023: * An instance of this class has ranges captured in matching.
024: *
025: * @xerces.internal
026: *
027: * @see RegularExpression#matches(char[], int, int, Match)
028: * @see RegularExpression#matches(char[], Match)
029: * @see RegularExpression#matches(java.text.CharacterIterator, Match)
030: * @see RegularExpression#matches(java.lang.String, int, int, Match)
031: * @see RegularExpression#matches(java.lang.String, Match)
032: * @author TAMURA Kent <kent@trl.ibm.co.jp>
033: * @version $Id: Match.java 446721 2006-09-15 20:35:34Z mrglavas $
034: */
035: public class Match implements Cloneable {
036: int[] beginpos = null;
037: int[] endpos = null;
038: int nofgroups = 0;
039:
040: CharacterIterator ciSource = null;
041: String strSource = null;
042: char[] charSource = null;
043:
044: /**
045: * Creates an instance.
046: */
047: public Match() {
048: }
049:
050: /**
051: *
052: */
053: public synchronized Object clone() {
054: Match ma = new Match();
055: if (this .nofgroups > 0) {
056: ma.setNumberOfGroups(this .nofgroups);
057: if (this .ciSource != null)
058: ma.setSource(this .ciSource);
059: if (this .strSource != null)
060: ma.setSource(this .strSource);
061: for (int i = 0; i < this .nofgroups; i++) {
062: ma.setBeginning(i, this .getBeginning(i));
063: ma.setEnd(i, this .getEnd(i));
064: }
065: }
066: return ma;
067: }
068:
069: /**
070: *
071: */
072: protected void setNumberOfGroups(int n) {
073: int oldn = this .nofgroups;
074: this .nofgroups = n;
075: if (oldn <= 0 || oldn < n || n * 2 < oldn) {
076: this .beginpos = new int[n];
077: this .endpos = new int[n];
078: }
079: for (int i = 0; i < n; i++) {
080: this .beginpos[i] = -1;
081: this .endpos[i] = -1;
082: }
083: }
084:
085: /**
086: *
087: */
088: protected void setSource(CharacterIterator ci) {
089: this .ciSource = ci;
090: this .strSource = null;
091: this .charSource = null;
092: }
093:
094: /**
095: *
096: */
097: protected void setSource(String str) {
098: this .ciSource = null;
099: this .strSource = str;
100: this .charSource = null;
101: }
102:
103: /**
104: *
105: */
106: protected void setSource(char[] chars) {
107: this .ciSource = null;
108: this .strSource = null;
109: this .charSource = chars;
110: }
111:
112: /**
113: *
114: */
115: protected void setBeginning(int index, int v) {
116: this .beginpos[index] = v;
117: }
118:
119: /**
120: *
121: */
122: protected void setEnd(int index, int v) {
123: this .endpos[index] = v;
124: }
125:
126: /**
127: * Return the number of regular expression groups.
128: * This method returns 1 when the regular expression has no capturing-parenthesis.
129: */
130: public int getNumberOfGroups() {
131: if (this .nofgroups <= 0)
132: throw new IllegalStateException("A result is not set.");
133: return this .nofgroups;
134: }
135:
136: /**
137: * Return a start position in the target text matched to specified regular expression group.
138: *
139: * @param index Less than <code>getNumberOfGroups()</code>.
140: */
141: public int getBeginning(int index) {
142: if (this .beginpos == null)
143: throw new IllegalStateException("A result is not set.");
144: if (index < 0 || this .nofgroups <= index)
145: throw new IllegalArgumentException(
146: "The parameter must be less than " + this .nofgroups
147: + ": " + index);
148: return this .beginpos[index];
149: }
150:
151: /**
152: * Return an end position in the target text matched to specified regular expression group.
153: *
154: * @param index Less than <code>getNumberOfGroups()</code>.
155: */
156: public int getEnd(int index) {
157: if (this .endpos == null)
158: throw new IllegalStateException("A result is not set.");
159: if (index < 0 || this .nofgroups <= index)
160: throw new IllegalArgumentException(
161: "The parameter must be less than " + this .nofgroups
162: + ": " + index);
163: return this .endpos[index];
164: }
165:
166: /**
167: * Return an substring of the target text matched to specified regular expression group.
168: *
169: * @param index Less than <code>getNumberOfGroups()</code>.
170: */
171: public String getCapturedText(int index) {
172: if (this .beginpos == null)
173: throw new IllegalStateException(
174: "match() has never been called.");
175: if (index < 0 || this .nofgroups <= index)
176: throw new IllegalArgumentException(
177: "The parameter must be less than " + this .nofgroups
178: + ": " + index);
179: String ret;
180: int begin = this .beginpos[index], end = this .endpos[index];
181: if (begin < 0 || end < 0)
182: return null;
183: if (this .ciSource != null) {
184: ret = REUtil.substring(this .ciSource, begin, end);
185: } else if (this .strSource != null) {
186: ret = this .strSource.substring(begin, end);
187: } else {
188: ret = new String(this.charSource, begin, end - begin);
189: }
190: return ret;
191: }
192: }
|