PageRenderTime 1297ms CodeModel.GetById 37ms RepoModel.GetById 10ms app.codeStats 0ms

/demo/jakarta-oro-2.0.8/src/java/org/apache/oro/text/regex/MatchResult.java

https://github.com/mdr/scalify
Java | 250 lines | 11 code | 18 blank | 221 comment | 0 complexity | 95d20f1d44dbc3e618e38b4b19f63e0f MD5 | raw file
  1. /*
  2. * $Id: MatchResult.java,v 1.7 2003/11/07 20:16:25 dfs Exp $
  3. *
  4. * ====================================================================
  5. * The Apache Software License, Version 1.1
  6. *
  7. * Copyright (c) 2000 The Apache Software Foundation. All rights
  8. * reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * 3. The end-user documentation included with the redistribution,
  23. * if any, must include the following acknowledgment:
  24. * "This product includes software developed by the
  25. * Apache Software Foundation (http://www.apache.org/)."
  26. * Alternately, this acknowledgment may appear in the software itself,
  27. * if and wherever such third-party acknowledgments normally appear.
  28. *
  29. * 4. The names "Apache" and "Apache Software Foundation", "Jakarta-Oro"
  30. * must not be used to endorse or promote products derived from this
  31. * software without prior written permission. For written
  32. * permission, please contact apache@apache.org.
  33. *
  34. * 5. Products derived from this software may not be called "Apache"
  35. * or "Jakarta-Oro", nor may "Apache" or "Jakarta-Oro" appear in their
  36. * name, without prior written permission of the Apache Software Foundation.
  37. *
  38. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  39. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  40. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  41. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  45. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  46. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  47. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  48. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  49. * SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This software consists of voluntary contributions made by many
  53. * individuals on behalf of the Apache Software Foundation. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.oro.text.regex;
  58. /**
  59. * The MatchResult interface allows PatternMatcher implementors to return
  60. * results storing match information in whatever format they like, while
  61. * presenting a consistent way of accessing that information. However,
  62. * MatchResult implementations should strictly follow the behavior
  63. * described for the interface methods.
  64. * <p>
  65. *
  66. * A MatchResult instance contains a pattern match and its saved groups.
  67. * You can access the entire match directly using the
  68. * {@link #group(int)} method with an argument of 0,
  69. * or by the {@link #toString()} method which is
  70. * defined to return the same thing. It is also possible to obtain
  71. * the beginning and ending offsets of a match relative to the input
  72. * producing the match by using the
  73. * {@link #beginOffset(int)} and {@link #endOffset(int)} methods. The
  74. * {@link #begin(int)} and {@link #end(int)} are useful in some
  75. * circumstances and return the begin and end offsets of the subgroups
  76. * of a match relative to the beginning of the match.
  77. * <p>
  78. *
  79. * You might use a MatchResult as follows:
  80. * <blockquote><pre>
  81. * int groups;
  82. * PatternMatcher matcher;
  83. * PatternCompiler compiler;
  84. * Pattern pattern;
  85. * PatternMatcherInput input;
  86. * MatchResult result;
  87. *
  88. * compiler = new Perl5Compiler();
  89. * matcher = new Perl5Matcher();
  90. *
  91. * try {
  92. * pattern = compiler.compile(somePatternString);
  93. * } catch(MalformedPatternException e) {
  94. * System.out.println("Bad pattern.");
  95. * System.out.println(e.getMessage());
  96. * return;
  97. * }
  98. *
  99. * input = new PatternMatcherInput(someStringInput);
  100. *
  101. * while(matcher.contains(input, pattern)) {
  102. * result = matcher.getMatch();
  103. * // Perform whatever processing on the result you want.
  104. * // Here we just print out all its elements to show how its
  105. * // methods are used.
  106. *
  107. * System.out.println("Match: " + result.toString());
  108. * System.out.println("Length: " + result.length());
  109. * groups = result.groups();
  110. * System.out.println("Groups: " + groups);
  111. * System.out.println("Begin offset: " + result.beginOffset(0));
  112. * System.out.println("End offset: " + result.endOffset(0));
  113. * System.out.println("Saved Groups: ");
  114. *
  115. * // Start at 1 because we just printed out group 0
  116. * for(int group = 1; group < groups; group++) {
  117. * System.out.println(group + ": " + result.group(group));
  118. * System.out.println("Begin: " + result.begin(group));
  119. * System.out.println("End: " + result.end(group));
  120. * }
  121. * }
  122. * </pre></blockquote>
  123. *
  124. * @version @version@
  125. * @since 1.0
  126. * @see PatternMatcher
  127. */
  128. public interface MatchResult {
  129. /**
  130. * A convenience method returning the length of the entire match.
  131. * If you want to get the length of a particular subgroup you should
  132. * use the {@link #group(int)} method to get
  133. * the string and then access its length() method as follows:
  134. * <p>
  135. * <blockquote><pre>
  136. * int length = -1; // Use -1 to indicate group doesn't exist
  137. * MatchResult result;
  138. * String subgroup;
  139. *
  140. * // Initialization of result omitted
  141. *
  142. * subgroup = result.group(1);
  143. * if(subgroup != null)
  144. * length = subgroup.length();
  145. *
  146. * </pre></blockquote>
  147. * <p>
  148. *
  149. * The length() method serves as a more a more efficient way to do:
  150. * <p>
  151. * <blockquote><pre>
  152. * length = result.group(0).length();
  153. * </pre></blockquote>
  154. * <p>
  155. *
  156. * @return The length of the match.
  157. */
  158. public int length();
  159. /**
  160. * @return The number of groups contained in the result. This number
  161. * includes the 0th group. In other words, the result refers
  162. * to the number of parenthesized subgroups plus the entire match
  163. * itself.
  164. */
  165. public int groups();
  166. /**
  167. * Returns the contents of the parenthesized subgroups of a match,
  168. * counting parentheses from left to right and starting from 1.
  169. * Group 0 always refers to the entire match. For example, if the
  170. * pattern <code> foo(\d+) </code> is used to extract a match
  171. * from the input <code> abfoo123 </code>, then <code> group(0) </code>
  172. * will return <code> foo123 </code> and <code> group(1) </code> will return
  173. * <code> 123 </code>. <code> group(2) </code> will return
  174. * <code> null </code> because there is only one subgroup in the original
  175. * pattern.
  176. * <p>
  177. * @param group The pattern subgroup to return.
  178. * @return A string containing the indicated pattern subgroup. Group
  179. * 0 always refers to the entire match. If a group was never
  180. * matched, it returns null. This is not to be confused with
  181. * a group matching the null string, which will return a String
  182. * of length 0.
  183. */
  184. public String group(int group);
  185. /**
  186. * @param group The pattern subgroup.
  187. * @return The offset into group 0 of the first token in the indicated
  188. * pattern subgroup. If a group was never matched or does
  189. * not exist, returns -1. Be aware that a group that matches
  190. * the null string at the end of a match will have an offset
  191. * equal to the length of the string, so you shouldn't blindly
  192. * use the offset to index an array or String.
  193. */
  194. public int begin(int group);
  195. /**
  196. * @param group The pattern subgroup.
  197. * @return Returns one plus the offset into group 0 of the last token in
  198. * the indicated pattern subgroup. If a group was never matched
  199. * or does not exist, returns -1. A group matching the null
  200. * string will return its start offset.
  201. */
  202. public int end(int group);
  203. /**
  204. * Returns an offset marking the beginning of the pattern match
  205. * relative to the beginning of the input from which the match
  206. * was extracted.
  207. * <p>
  208. * @param group The pattern subgroup.
  209. * @return The offset of the first token in the indicated
  210. * pattern subgroup. If a group was never matched or does
  211. * not exist, returns -1.
  212. */
  213. public int beginOffset(int group);
  214. /**
  215. * Returns an offset marking the end of the pattern match
  216. * relative to the beginning of the input from which the match was
  217. * extracted.
  218. * <p>
  219. * @param group The pattern subgroup.
  220. * @return Returns one plus the offset of the last token in
  221. * the indicated pattern subgroup. If a group was never matched
  222. * or does not exist, returns -1. A group matching the null
  223. * string will return its start offset.
  224. */
  225. public int endOffset(int group);
  226. /**
  227. * Returns the same as group(0).
  228. *
  229. * @return A string containing the entire match.
  230. */
  231. public String toString();
  232. }