KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.xpath.regex;
18
19 import java.text.CharacterIterator JavaDoc;
20
21 /**
22  * An instance of this class has ranges captured in matching.
23  *
24  * @xerces.internal
25  *
26  * @see RegularExpression#matches(char[], int, int, Match)
27  * @see RegularExpression#matches(char[], Match)
28  * @see RegularExpression#matches(java.text.CharacterIterator, Match)
29  * @see RegularExpression#matches(java.lang.String, int, int, Match)
30  * @see RegularExpression#matches(java.lang.String, Match)
31  * @author TAMURA Kent <kent@trl.ibm.co.jp>
32  * @version $Id: Match.java,v 1.6 2004/10/04 22:07:40 mrglavas Exp $
33  */

34 public class Match implements Cloneable JavaDoc {
35     int[] beginpos = null;
36     int[] endpos = null;
37     int nofgroups = 0;
38
39     CharacterIterator JavaDoc ciSource = null;
40     String JavaDoc strSource = null;
41     char[] charSource = null;
42
43     /**
44      * Creates an instance.
45      */

46     public Match() {
47     }
48
49     /**
50      *
51      */

52     public synchronized Object JavaDoc clone() {
53         Match ma = new Match();
54         if (this.nofgroups > 0) {
55             ma.setNumberOfGroups(this.nofgroups);
56             if (this.ciSource != null) ma.setSource(this.ciSource);
57             if (this.strSource != null) ma.setSource(this.strSource);
58             for (int i = 0; i < this.nofgroups; i ++) {
59                 ma.setBeginning(i, this.getBeginning(i));
60                 ma.setEnd(i, this.getEnd(i));
61             }
62         }
63         return ma;
64     }
65
66     /**
67      *
68      */

69     protected void setNumberOfGroups(int n) {
70         int oldn = this.nofgroups;
71         this.nofgroups = n;
72         if (oldn <= 0
73             || oldn < n || n*2 < oldn) {
74             this.beginpos = new int[n];
75             this.endpos = new int[n];
76         }
77         for (int i = 0; i < n; i ++) {
78             this.beginpos[i] = -1;
79             this.endpos[i] = -1;
80         }
81     }
82
83     /**
84      *
85      */

86     protected void setSource(CharacterIterator JavaDoc ci) {
87         this.ciSource = ci;
88         this.strSource = null;
89         this.charSource = null;
90     }
91     /**
92      *
93      */

94     protected void setSource(String JavaDoc str) {
95         this.ciSource = null;
96         this.strSource = str;
97         this.charSource = null;
98     }
99     /**
100      *
101      */

102     protected void setSource(char[] chars) {
103         this.ciSource = null;
104         this.strSource = null;
105         this.charSource = chars;
106     }
107
108     /**
109      *
110      */

111     protected void setBeginning(int index, int v) {
112         this.beginpos[index] = v;
113     }
114
115     /**
116      *
117      */

118     protected void setEnd(int index, int v) {
119         this.endpos[index] = v;
120     }
121
122     /**
123      * Return the number of regular expression groups.
124      * This method returns 1 when the regular expression has no capturing-parenthesis.
125      */

126     public int getNumberOfGroups() {
127         if (this.nofgroups <= 0)
128             throw new IllegalStateException JavaDoc("A result is not set.");
129         return this.nofgroups;
130     }
131
132     /**
133      * Return a start position in the target text matched to specified regular expression group.
134      *
135      * @param index Less than <code>getNumberOfGroups()</code>.
136      */

137     public int getBeginning(int index) {
138         if (this.beginpos == null)
139             throw new IllegalStateException JavaDoc("A result is not set.");
140         if (index < 0 || this.nofgroups <= index)
141             throw new IllegalArgumentException JavaDoc("The parameter must be less than "
142                                                +this.nofgroups+": "+index);
143         return this.beginpos[index];
144     }
145
146     /**
147      * Return an end position in the target text matched to specified regular expression group.
148      *
149      * @param index Less than <code>getNumberOfGroups()</code>.
150      */

151     public int getEnd(int index) {
152         if (this.endpos == null)
153             throw new IllegalStateException JavaDoc("A result is not set.");
154         if (index < 0 || this.nofgroups <= index)
155             throw new IllegalArgumentException JavaDoc("The parameter must be less than "
156                                                +this.nofgroups+": "+index);
157         return this.endpos[index];
158     }
159
160     /**
161      * Return an substring of the target text matched to specified regular expression group.
162      *
163      * @param index Less than <code>getNumberOfGroups()</code>.
164      */

165     public String JavaDoc getCapturedText(int index) {
166         if (this.beginpos == null)
167             throw new IllegalStateException JavaDoc("match() has never been called.");
168         if (index < 0 || this.nofgroups <= index)
169             throw new IllegalArgumentException JavaDoc("The parameter must be less than "
170                                                +this.nofgroups+": "+index);
171         String JavaDoc ret;
172         int begin = this.beginpos[index], end = this.endpos[index];
173         if (begin < 0 || end < 0) return null;
174         if (this.ciSource != null) {
175             ret = REUtil.substring(this.ciSource, begin, end);
176         } else if (this.strSource != null) {
177             ret = this.strSource.substring(begin, end);
178         } else {
179             ret = new String JavaDoc(this.charSource, begin, end-begin);
180         }
181         return ret;
182     }
183 }
184
Popular Tags