KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > xpath > regex > Match


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

57
58 package com.sun.org.apache.xerces.internal.impl.xpath.regex;
59
60 import java.text.CharacterIterator JavaDoc;
61
62 /**
63  *
64  * An instance of this class has ranges captured in matching.
65  *
66  * @see RegularExpression#matches(char[], int, int, Match)
67  * @see RegularExpression#matches(char[], Match)
68  * @see RegularExpression#matches(java.text.CharacterIterator, Match)
69  * @see RegularExpression#matches(java.lang.String, int, int, Match)
70  * @see RegularExpression#matches(java.lang.String, Match)
71  * @author TAMURA Kent &lt;kent@trl.ibm.co.jp&gt;
72  * @version $Id: Match.java,v 1.4 2002/08/09 15:18:17 neilg Exp $
73  */

74 public class Match implements Cloneable JavaDoc {
75     int[] beginpos = null;
76     int[] endpos = null;
77     int nofgroups = 0;
78
79     CharacterIterator JavaDoc ciSource = null;
80     String JavaDoc strSource = null;
81     char[] charSource = null;
82
83     /**
84      * Creates an instance.
85      */

86     public Match() {
87     }
88
89     /**
90      *
91      */

92     public synchronized Object JavaDoc clone() {
93         Match ma = new Match();
94         if (this.nofgroups > 0) {
95             ma.setNumberOfGroups(this.nofgroups);
96             if (this.ciSource != null) ma.setSource(this.ciSource);
97             if (this.strSource != null) ma.setSource(this.strSource);
98             for (int i = 0; i < this.nofgroups; i ++) {
99                 ma.setBeginning(i, this.getBeginning(i));
100                 ma.setEnd(i, this.getEnd(i));
101             }
102         }
103         return ma;
104     }
105
106     /**
107      *
108      */

109     protected void setNumberOfGroups(int n) {
110         int oldn = this.nofgroups;
111         this.nofgroups = n;
112         if (oldn <= 0
113             || 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 JavaDoc ci) {
127         this.ciSource = ci;
128         this.strSource = null;
129         this.charSource = null;
130     }
131     /**
132      *
133      */

134     protected void setSource(String JavaDoc str) {
135         this.ciSource = null;
136         this.strSource = str;
137         this.charSource = null;
138     }
139     /**
140      *
141      */

142     protected void setSource(char[] chars) {
143         this.ciSource = null;
144         this.strSource = null;
145         this.charSource = chars;
146     }
147
148     /**
149      *
150      */

151     protected void setBeginning(int index, int v) {
152         this.beginpos[index] = v;
153     }
154
155     /**
156      *
157      */

158     protected void setEnd(int index, int v) {
159         this.endpos[index] = v;
160     }
161
162     /**
163      * Return the number of regular expression groups.
164      * This method returns 1 when the regular expression has no capturing-parenthesis.
165      */

166     public int getNumberOfGroups() {
167         if (this.nofgroups <= 0)
168             throw new IllegalStateException JavaDoc("A result is not set.");
169         return this.nofgroups;
170     }
171
172     /**
173      * Return a start position in the target text matched to specified regular expression group.
174      *
175      * @param index Less than <code>getNumberOfGroups()</code>.
176      */

177     public int getBeginning(int index) {
178         if (this.beginpos == null)
179             throw new IllegalStateException JavaDoc("A result is not set.");
180         if (index < 0 || this.nofgroups <= index)
181             throw new IllegalArgumentException JavaDoc("The parameter must be less than "
182                                                +this.nofgroups+": "+index);
183         return this.beginpos[index];
184     }
185
186     /**
187      * Return an end position in the target text matched to specified regular expression group.
188      *
189      * @param index Less than <code>getNumberOfGroups()</code>.
190      */

191     public int getEnd(int index) {
192         if (this.endpos == null)
193             throw new IllegalStateException JavaDoc("A result is not set.");
194         if (index < 0 || this.nofgroups <= index)
195             throw new IllegalArgumentException JavaDoc("The parameter must be less than "
196                                                +this.nofgroups+": "+index);
197         return this.endpos[index];
198     }
199
200     /**
201      * Return an substring of the target text matched to specified regular expression group.
202      *
203      * @param index Less than <code>getNumberOfGroups()</code>.
204      */

205     public String JavaDoc getCapturedText(int index) {
206         if (this.beginpos == null)
207             throw new IllegalStateException JavaDoc("match() has never been called.");
208         if (index < 0 || this.nofgroups <= index)
209             throw new IllegalArgumentException JavaDoc("The parameter must be less than "
210                                                +this.nofgroups+": "+index);
211         String JavaDoc ret;
212         int begin = this.beginpos[index], end = this.endpos[index];
213         if (begin < 0 || end < 0) return null;
214         if (this.ciSource != null) {
215             ret = REUtil.substring(this.ciSource, begin, end);
216         } else if (this.strSource != null) {
217             ret = this.strSource.substring(begin, end);
218         } else {
219             ret = new String JavaDoc(this.charSource, begin, end-begin);
220         }
221         return ret;
222     }
223 }
224
Popular Tags