View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 1999-2002,2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package com.sun.org.apache.xerces.internal.impl.xpath.regex;
22  
23  import java.text.CharacterIterator;
24  
25  /**
26   * An instance of this class has ranges captured in matching.
27   *
28   * @xerces.internal
29   *
30   * @see RegularExpression#matches(char[], int, int, Match)
31   * @see RegularExpression#matches(char[], Match)
32   * @see RegularExpression#matches(java.text.CharacterIterator, Match)
33   * @see RegularExpression#matches(java.lang.String, int, int, Match)
34   * @see RegularExpression#matches(java.lang.String, Match)
35   * @author TAMURA Kent <kent@trl.ibm.co.jp>
36   */
37  public class Match implements Cloneable {
38      int[] beginpos = null;
39      int[] endpos = null;
40      int nofgroups = 0;
41  
42      CharacterIterator ciSource = null;
43      String strSource = null;
44      char[] charSource = null;
45  
46      /**
47       * Creates an instance.
48       */
49      public Match() {
50      }
51  
52      /**
53       *
54       */
55      public synchronized Object clone() {
56          Match ma = new Match();
57          if (this.nofgroups > 0) {
58              ma.setNumberOfGroups(this.nofgroups);
59              if (this.ciSource != null)  ma.setSource(this.ciSource);
60              if (this.strSource != null)  ma.setSource(this.strSource);
61              for (int i = 0;  i < this.nofgroups;  i ++) {
62                  ma.setBeginning(i, this.getBeginning(i));
63                  ma.setEnd(i, this.getEnd(i));
64              }
65          }
66          return ma;
67      }
68  
69      /**
70       *
71       */
72      protected void setNumberOfGroups(int n) {
73          int oldn = this.nofgroups;
74          this.nofgroups = n;
75          if (oldn <= 0
76              || oldn < n || n*2 < oldn) {
77              this.beginpos = new int[n];
78              this.endpos = new int[n];
79          }
80          for (int i = 0;  i < n;  i ++) {
81              this.beginpos[i] = -1;
82              this.endpos[i] = -1;
83          }
84      }
85  
86      /**
87       *
88       */
89      protected void setSource(CharacterIterator ci) {
90          this.ciSource = ci;
91          this.strSource = null;
92          this.charSource = null;
93      }
94      /**
95       *
96       */
97      protected void setSource(String str) {
98          this.ciSource = null;
99          this.strSource = str;
100         this.charSource = null;
101     }
102     /**
103      *
104      */
105     protected void setSource(char[] chars) {
106         this.ciSource = null;
107         this.strSource = null;
108         this.charSource = chars;
109     }
110 
111     /**
112      *
113      */
114     protected void setBeginning(int index, int v) {
115         this.beginpos[index] = v;
116     }
117 
118     /**
119      *
120      */
121     protected void setEnd(int index, int v) {
122         this.endpos[index] = v;
123     }
124 
125     /**
126      * Return the number of regular expression groups.
127      * This method returns 1 when the regular expression has no capturing-parenthesis.
128      */
129     public int getNumberOfGroups() {
130         if (this.nofgroups <= 0)
131             throw new IllegalStateException("A result is not set.");
132         return this.nofgroups;
133     }
134 
135     /**
136      * Return a start position in the target text matched to specified regular expression group.
137      *
138      * @param index Less than <code>getNumberOfGroups()</code>.
139      */
140     public int getBeginning(int index) {
141         if (this.beginpos == null)
142             throw new IllegalStateException("A result is not set.");
143         if (index < 0 || this.nofgroups <= index)
144             throw new IllegalArgumentException("The parameter must be less than "
145                                                +this.nofgroups+": "+index);
146         return this.beginpos[index];
147     }
148 
149     /**
150      * Return an end position in the target text matched to specified regular expression group.
151      *
152      * @param index Less than <code>getNumberOfGroups()</code>.
153      */
154     public int getEnd(int index) {
155         if (this.endpos == null)
156             throw new IllegalStateException("A result is not set.");
157         if (index < 0 || this.nofgroups <= index)
158             throw new IllegalArgumentException("The parameter must be less than "
159                                                +this.nofgroups+": "+index);
160         return this.endpos[index];
161     }
162 
163     /**
164      * Return an substring of the target text matched to specified regular expression group.
165      *
166      * @param index Less than <code>getNumberOfGroups()</code>.
167      */
168     public String getCapturedText(int index) {
169         if (this.beginpos == null)
170             throw new IllegalStateException("match() has never been called.");
171         if (index < 0 || this.nofgroups <= index)
172             throw new IllegalArgumentException("The parameter must be less than "
173                                                +this.nofgroups+": "+index);
174         String ret;
175         int begin = this.beginpos[index], end = this.endpos[index];
176         if (begin < 0 || end < 0)  return null;
177         if (this.ciSource != null) {
178             ret = REUtil.substring(this.ciSource, begin, end);
179         } else if (this.strSource != null) {
180             ret = this.strSource.substring(begin, end);
181         } else {
182             ret = new String(this.charSource, begin, end-begin);
183         }
184         return ret;
185     }
186 }