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. package com.sun.org.apache.xerces.internal.impl.xpath.regex;
  58. import java.text.CharacterIterator;
  59. /**
  60. *
  61. * An instance of this class has ranges captured in matching.
  62. *
  63. * @see RegularExpression#matches(char[], int, int, Match)
  64. * @see RegularExpression#matches(char[], Match)
  65. * @see RegularExpression#matches(java.text.CharacterIterator, Match)
  66. * @see RegularExpression#matches(java.lang.String, int, int, Match)
  67. * @see RegularExpression#matches(java.lang.String, Match)
  68. * @author TAMURA Kent <kent@trl.ibm.co.jp>
  69. * @version $Id: Match.java,v 1.4 2002/08/09 15:18:17 neilg Exp $
  70. */
  71. public class Match implements Cloneable {
  72. int[] beginpos = null;
  73. int[] endpos = null;
  74. int nofgroups = 0;
  75. CharacterIterator ciSource = null;
  76. String strSource = null;
  77. char[] charSource = null;
  78. /**
  79. * Creates an instance.
  80. */
  81. public Match() {
  82. }
  83. /**
  84. *
  85. */
  86. public synchronized Object clone() {
  87. Match ma = new Match();
  88. if (this.nofgroups > 0) {
  89. ma.setNumberOfGroups(this.nofgroups);
  90. if (this.ciSource != null) ma.setSource(this.ciSource);
  91. if (this.strSource != null) ma.setSource(this.strSource);
  92. for (int i = 0; i < this.nofgroups; i ++) {
  93. ma.setBeginning(i, this.getBeginning(i));
  94. ma.setEnd(i, this.getEnd(i));
  95. }
  96. }
  97. return ma;
  98. }
  99. /**
  100. *
  101. */
  102. protected void setNumberOfGroups(int n) {
  103. int oldn = this.nofgroups;
  104. this.nofgroups = n;
  105. if (oldn <= 0
  106. || oldn < n || n*2 < oldn) {
  107. this.beginpos = new int[n];
  108. this.endpos = new int[n];
  109. }
  110. for (int i = 0; i < n; i ++) {
  111. this.beginpos[i] = -1;
  112. this.endpos[i] = -1;
  113. }
  114. }
  115. /**
  116. *
  117. */
  118. protected void setSource(CharacterIterator ci) {
  119. this.ciSource = ci;
  120. this.strSource = null;
  121. this.charSource = null;
  122. }
  123. /**
  124. *
  125. */
  126. protected void setSource(String str) {
  127. this.ciSource = null;
  128. this.strSource = str;
  129. this.charSource = null;
  130. }
  131. /**
  132. *
  133. */
  134. protected void setSource(char[] chars) {
  135. this.ciSource = null;
  136. this.strSource = null;
  137. this.charSource = chars;
  138. }
  139. /**
  140. *
  141. */
  142. protected void setBeginning(int index, int v) {
  143. this.beginpos[index] = v;
  144. }
  145. /**
  146. *
  147. */
  148. protected void setEnd(int index, int v) {
  149. this.endpos[index] = v;
  150. }
  151. /**
  152. * Return the number of regular expression groups.
  153. * This method returns 1 when the regular expression has no capturing-parenthesis.
  154. */
  155. public int getNumberOfGroups() {
  156. if (this.nofgroups <= 0)
  157. throw new IllegalStateException("A result is not set.");
  158. return this.nofgroups;
  159. }
  160. /**
  161. * Return a start position in the target text matched to specified regular expression group.
  162. *
  163. * @param index Less than <code>getNumberOfGroups()</code>.
  164. */
  165. public int getBeginning(int index) {
  166. if (this.beginpos == null)
  167. throw new IllegalStateException("A result is not set.");
  168. if (index < 0 || this.nofgroups <= index)
  169. throw new IllegalArgumentException("The parameter must be less than "
  170. +this.nofgroups+": "+index);
  171. return this.beginpos[index];
  172. }
  173. /**
  174. * Return an end position in the target text matched to specified regular expression group.
  175. *
  176. * @param index Less than <code>getNumberOfGroups()</code>.
  177. */
  178. public int getEnd(int index) {
  179. if (this.endpos == null)
  180. throw new IllegalStateException("A result is not set.");
  181. if (index < 0 || this.nofgroups <= index)
  182. throw new IllegalArgumentException("The parameter must be less than "
  183. +this.nofgroups+": "+index);
  184. return this.endpos[index];
  185. }
  186. /**
  187. * Return an substring of the target text matched to specified regular expression group.
  188. *
  189. * @param index Less than <code>getNumberOfGroups()</code>.
  190. */
  191. public String getCapturedText(int index) {
  192. if (this.beginpos == null)
  193. throw new IllegalStateException("match() has never been called.");
  194. if (index < 0 || this.nofgroups <= index)
  195. throw new IllegalArgumentException("The parameter must be less than "
  196. +this.nofgroups+": "+index);
  197. String ret;
  198. int begin = this.beginpos[index], end = this.endpos[index];
  199. if (begin < 0 || end < 0) return null;
  200. if (this.ciSource != null) {
  201. ret = REUtil.substring(this.ciSource, begin, end);
  202. } else if (this.strSource != null) {
  203. ret = this.strSource.substring(begin, end);
  204. } else {
  205. ret = new String(this.charSource, begin, end-begin);
  206. }
  207. return ret;
  208. }
  209. }