Pattern.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* Pattern.java -- Compiled regular expression ready to be applied.
  2. Copyright (C) 2002, 2004, 2005, 2007, 2010
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.util.regex;
  33. import gnu.java.lang.CPStringBuilder;
  34. import gnu.java.util.regex.RE;
  35. import gnu.java.util.regex.REException;
  36. import gnu.java.util.regex.RESyntax;
  37. import java.io.Serializable;
  38. import java.util.ArrayList;
  39. /**
  40. * Compiled regular expression ready to be applied.
  41. *
  42. * @since 1.4
  43. */
  44. public final class Pattern implements Serializable
  45. {
  46. private static final long serialVersionUID = 5073258162644648461L;
  47. public static final int CANON_EQ = 128;
  48. public static final int CASE_INSENSITIVE = 2;
  49. public static final int COMMENTS = 4;
  50. public static final int DOTALL = 32;
  51. public static final int MULTILINE = 8;
  52. public static final int UNICODE_CASE = 64;
  53. public static final int UNIX_LINES = 1;
  54. private final String regex;
  55. private final int flags;
  56. private final RE re;
  57. private Pattern (String regex, int flags)
  58. throws PatternSyntaxException
  59. {
  60. this.regex = regex;
  61. this.flags = flags;
  62. RESyntax syntax = RESyntax.RE_SYNTAX_JAVA_1_4;
  63. int gnuFlags = 0;
  64. gnuFlags |= RE.REG_ICASE_USASCII;
  65. if ((flags & CASE_INSENSITIVE) != 0)
  66. gnuFlags |= RE.REG_ICASE;
  67. if ((flags & MULTILINE) != 0)
  68. {
  69. gnuFlags |= RE.REG_MULTILINE;
  70. syntax = new RESyntax(syntax);
  71. syntax.setLineSeparator(null);
  72. }
  73. if ((flags & DOTALL) != 0)
  74. gnuFlags |= RE.REG_DOT_NEWLINE;
  75. if ((flags & UNICODE_CASE) != 0)
  76. gnuFlags &= ~RE.REG_ICASE_USASCII;
  77. // not yet supported:
  78. // if ((flags & CANON_EQ) != 0) gnuFlags =
  79. if ((flags & UNIX_LINES) != 0)
  80. {
  81. // Use a syntax set with \n for linefeeds?
  82. syntax = new RESyntax(syntax);
  83. syntax.setLineSeparator("\n");
  84. }
  85. if ((flags & COMMENTS) != 0)
  86. {
  87. gnuFlags |= RE.REG_X_COMMENTS;
  88. }
  89. try
  90. {
  91. this.re = new RE(regex, gnuFlags, syntax);
  92. }
  93. catch (REException e)
  94. {
  95. PatternSyntaxException pse;
  96. pse = new PatternSyntaxException(e.getMessage(),
  97. regex, e.getPosition());
  98. pse.initCause(e);
  99. throw pse;
  100. }
  101. }
  102. // package private accessor method
  103. RE getRE()
  104. {
  105. return re;
  106. }
  107. /**
  108. * @param regex The regular expression
  109. *
  110. * @exception PatternSyntaxException If the expression's syntax is invalid
  111. */
  112. public static Pattern compile (String regex)
  113. throws PatternSyntaxException
  114. {
  115. return compile(regex, 0);
  116. }
  117. /**
  118. * @param regex The regular expression
  119. * @param flags The match flags, a bit mask
  120. *
  121. * @exception PatternSyntaxException If the expression's syntax is invalid
  122. * @exception IllegalArgumentException If bit values other than those
  123. * corresponding to the defined match flags are set in flags
  124. */
  125. public static Pattern compile (String regex, int flags)
  126. throws PatternSyntaxException
  127. {
  128. // FIXME: check which flags are really accepted
  129. if ((flags & ~0xEF) != 0)
  130. throw new IllegalArgumentException ();
  131. return new Pattern (regex, flags);
  132. }
  133. public int flags ()
  134. {
  135. return this.flags;
  136. }
  137. /**
  138. * @param regex The regular expression
  139. * @param input The character sequence to be matched
  140. *
  141. * @exception PatternSyntaxException If the expression's syntax is invalid
  142. */
  143. public static boolean matches (String regex, CharSequence input)
  144. {
  145. return compile(regex).matcher(input).matches();
  146. }
  147. /**
  148. * @param input The character sequence to be matched
  149. */
  150. public Matcher matcher (CharSequence input)
  151. {
  152. return new Matcher(this, input);
  153. }
  154. /**
  155. * @param input The character sequence to be matched
  156. */
  157. public String[] split (CharSequence input)
  158. {
  159. return split(input, 0);
  160. }
  161. /**
  162. * @param input The character sequence to be matched
  163. * @param limit The result threshold
  164. */
  165. public String[] split (CharSequence input, int limit)
  166. {
  167. Matcher matcher = new Matcher(this, input);
  168. ArrayList<String> list = new ArrayList<String>();
  169. int empties = 0;
  170. int count = 0;
  171. int start = 0;
  172. int end;
  173. boolean matched = matcher.find();
  174. while (matched && (limit <= 0 || count < limit - 1))
  175. {
  176. ++count;
  177. end = matcher.start();
  178. if (start == end)
  179. empties++;
  180. else
  181. {
  182. while (empties > 0)
  183. {
  184. list.add("");
  185. empties--;
  186. }
  187. String text = input.subSequence(start, end).toString();
  188. list.add(text);
  189. }
  190. start = matcher.end();
  191. matched = matcher.find();
  192. }
  193. // We matched nothing.
  194. if (!matched && count == 0)
  195. return new String[] { input.toString() };
  196. // Is the last token empty?
  197. boolean emptyLast = (start == input.length());
  198. // Can/Must we add empties or an extra last token at the end?
  199. if (list.size() < limit || limit < 0 || (limit == 0 && !emptyLast))
  200. {
  201. if (limit > list.size())
  202. {
  203. int max = limit - list.size();
  204. empties = (empties > max) ? max : empties;
  205. }
  206. while (empties > 0)
  207. {
  208. list.add("");
  209. empties--;
  210. }
  211. }
  212. // last token at end
  213. if (limit != 0 || (limit == 0 && !emptyLast))
  214. {
  215. String t = input.subSequence(start, input.length()).toString();
  216. if ("".equals(t) && limit == 0)
  217. { /* Don't add. */ }
  218. else
  219. list.add(t);
  220. }
  221. return list.toArray(new String[list.size()]);
  222. }
  223. public String pattern ()
  224. {
  225. return regex;
  226. }
  227. /**
  228. * Returns a literal pattern for the specified String.
  229. *
  230. * @param String to return a literal pattern for.
  231. * @return a literal pattern for the specified String.
  232. * @exception NullPointerException if str is null.
  233. * @since 1.5
  234. */
  235. public static String quote(String str)
  236. {
  237. int eInd = str.indexOf("\\E");
  238. if (eInd < 0)
  239. {
  240. // No need to handle backslashes.
  241. return "\\Q" + str + "\\E";
  242. }
  243. CPStringBuilder sb = new CPStringBuilder(str.length() + 16);
  244. sb.append("\\Q"); // start quote
  245. int pos = 0;
  246. do
  247. {
  248. // A backslash is quoted by another backslash;
  249. // 'E' is not needed to be quoted.
  250. sb.append(str.substring(pos, eInd))
  251. .append("\\E" + "\\\\" + "E" + "\\Q");
  252. pos = eInd + 2;
  253. } while ((eInd = str.indexOf("\\E", pos)) >= 0);
  254. sb.append(str.substring(pos, str.length()))
  255. .append("\\E"); // end quote
  256. return sb.toString();
  257. }
  258. /**
  259. * Return the regular expression used to construct this object.
  260. * @specnote Prior to JDK 1.5 this method had a different behavior
  261. * @since 1.5
  262. */
  263. public String toString()
  264. {
  265. return regex;
  266. }
  267. }