BreakIterator.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* BreakIterator.java -- Breaks text into elements
  2. Copyright (C) 1998, 1999, 2001, 2004, 2005, 2007, 2012
  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.text;
  33. import gnu.java.locale.LocaleHelper;
  34. import gnu.java.text.CharacterBreakIterator;
  35. import gnu.java.text.LineBreakIterator;
  36. import gnu.java.text.SentenceBreakIterator;
  37. import gnu.java.text.WordBreakIterator;
  38. import java.text.spi.BreakIteratorProvider;
  39. import java.util.Locale;
  40. import java.util.MissingResourceException;
  41. import java.util.ResourceBundle;
  42. import java.util.ServiceLoader;
  43. /**
  44. * This class iterates over text elements such as words, lines, sentences,
  45. * and characters. It can only iterate over one of these text elements at
  46. * a time. An instance of this class configured for the desired iteration
  47. * type is created by calling one of the static factory methods, not
  48. * by directly calling a constructor.
  49. *
  50. * The standard iterators created by the factory methods in this
  51. * class will be valid upon creation. That is, their methods will
  52. * not cause exceptions if called before you call setText().
  53. *
  54. * @author Tom Tromey (tromey@cygnus.com)
  55. * @author Aaron M. Renn (arenn@urbanophile.com)
  56. * @date March 19, 1999
  57. */
  58. /* Written using "Java Class Libraries", 2nd edition, plus online
  59. * API docs for JDK 1.2 beta from http://www.javasoft.com.
  60. * Status: Believed complete and correct to 1.1.
  61. */
  62. public abstract class BreakIterator implements Cloneable
  63. {
  64. /**
  65. * This value is returned by the <code>next()</code> and
  66. * <code>previous</code> in order to indicate that the end of the
  67. * text has been reached.
  68. */
  69. // The value was discovered by writing a test program.
  70. public static final int DONE = -1;
  71. /**
  72. * This method initializes a new instance of <code>BreakIterator</code>.
  73. * This protected constructor is available to subclasses as a default
  74. * no-arg superclass constructor.
  75. */
  76. protected BreakIterator ()
  77. {
  78. }
  79. /**
  80. * Create a clone of this object.
  81. */
  82. public Object clone ()
  83. {
  84. try
  85. {
  86. return super.clone();
  87. }
  88. catch (CloneNotSupportedException e)
  89. {
  90. return null;
  91. }
  92. }
  93. /**
  94. * This method returns the index of the current text element boundary.
  95. *
  96. * @return The current text boundary.
  97. */
  98. public abstract int current ();
  99. /**
  100. * This method returns the first text element boundary in the text being
  101. * iterated over.
  102. *
  103. * @return The first text boundary.
  104. */
  105. public abstract int first ();
  106. /**
  107. * This methdod returns the offset of the text element boundary following
  108. * the specified offset.
  109. *
  110. * @param pos The text index from which to find the next text boundary.
  111. *
  112. * @return The next text boundary following the specified index.
  113. */
  114. public abstract int following (int pos);
  115. /**
  116. * This method returns a list of locales for which instances of
  117. * <code>BreakIterator</code> are available.
  118. *
  119. * @return A list of available locales
  120. */
  121. public static synchronized Locale[] getAvailableLocales ()
  122. {
  123. Locale[] l = new Locale[1];
  124. l[0] = Locale.US;
  125. return l;
  126. }
  127. private static BreakIterator getInstance (String type, Locale loc)
  128. {
  129. String className;
  130. try
  131. {
  132. ResourceBundle res
  133. = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
  134. loc, ClassLoader.getSystemClassLoader());
  135. className = res.getString(type);
  136. }
  137. catch (MissingResourceException x)
  138. {
  139. return null;
  140. }
  141. try
  142. {
  143. Class<?> k = Class.forName(className);
  144. return (BreakIterator) k.newInstance();
  145. }
  146. catch (ClassNotFoundException x1)
  147. {
  148. return null;
  149. }
  150. catch (InstantiationException x2)
  151. {
  152. return null;
  153. }
  154. catch (IllegalAccessException x3)
  155. {
  156. return null;
  157. }
  158. }
  159. /**
  160. * This method returns an instance of <code>BreakIterator</code> that will
  161. * iterate over characters as defined in the default locale.
  162. *
  163. * @return A <code>BreakIterator</code> instance for the default locale.
  164. */
  165. public static BreakIterator getCharacterInstance ()
  166. {
  167. return getCharacterInstance (Locale.getDefault());
  168. }
  169. /**
  170. * This method returns an instance of <code>BreakIterator</code> that will
  171. * iterate over characters as defined in the specified locale.
  172. *
  173. * @param locale The desired locale.
  174. *
  175. * @return A <code>BreakIterator</code> instance for the specified locale.
  176. */
  177. public static BreakIterator getCharacterInstance (Locale locale)
  178. {
  179. BreakIterator r = getInstance("CharacterIterator", locale);
  180. if (r != null)
  181. return r;
  182. for (BreakIteratorProvider p :
  183. ServiceLoader.load(BreakIteratorProvider.class))
  184. {
  185. for (Locale loc : p.getAvailableLocales())
  186. {
  187. if (loc.equals(locale))
  188. {
  189. BreakIterator bi = p.getCharacterInstance(locale);
  190. if (bi != null)
  191. return bi;
  192. break;
  193. }
  194. }
  195. }
  196. if (locale.equals(Locale.ROOT))
  197. return new CharacterBreakIterator();
  198. return getCharacterInstance(LocaleHelper.getFallbackLocale(locale));
  199. }
  200. /**
  201. * This method returns an instance of <code>BreakIterator</code> that will
  202. * iterate over line breaks as defined in the default locale.
  203. *
  204. * @return A <code>BreakIterator</code> instance for the default locale.
  205. */
  206. public static BreakIterator getLineInstance ()
  207. {
  208. return getLineInstance (Locale.getDefault());
  209. }
  210. /**
  211. * This method returns an instance of <code>BreakIterator</code> that will
  212. * iterate over line breaks as defined in the specified locale.
  213. *
  214. * @param locale The desired locale.
  215. *
  216. * @return A <code>BreakIterator</code> instance for the default locale.
  217. */
  218. public static BreakIterator getLineInstance (Locale locale)
  219. {
  220. BreakIterator r = getInstance ("LineIterator", locale);
  221. if (r != null)
  222. return r;
  223. for (BreakIteratorProvider p :
  224. ServiceLoader.load(BreakIteratorProvider.class))
  225. {
  226. for (Locale loc : p.getAvailableLocales())
  227. {
  228. if (loc.equals(locale))
  229. {
  230. BreakIterator bi = p.getLineInstance(locale);
  231. if (bi != null)
  232. return bi;
  233. break;
  234. }
  235. }
  236. }
  237. if (locale.equals(Locale.ROOT))
  238. return new LineBreakIterator();
  239. return getLineInstance(LocaleHelper.getFallbackLocale(locale));
  240. }
  241. /**
  242. * This method returns an instance of <code>BreakIterator</code> that will
  243. * iterate over sentences as defined in the default locale.
  244. *
  245. * @return A <code>BreakIterator</code> instance for the default locale.
  246. */
  247. public static BreakIterator getSentenceInstance ()
  248. {
  249. return getSentenceInstance (Locale.getDefault());
  250. }
  251. /**
  252. * This method returns an instance of <code>BreakIterator</code> that will
  253. * iterate over sentences as defined in the specified locale.
  254. *
  255. * @param locale The desired locale.
  256. *
  257. * @return A <code>BreakIterator</code> instance for the default locale.
  258. */
  259. public static BreakIterator getSentenceInstance (Locale locale)
  260. {
  261. BreakIterator r = getInstance ("SentenceIterator", locale);
  262. if (r != null)
  263. return r;
  264. for (BreakIteratorProvider p :
  265. ServiceLoader.load(BreakIteratorProvider.class))
  266. {
  267. for (Locale loc : p.getAvailableLocales())
  268. {
  269. if (loc.equals(locale))
  270. {
  271. BreakIterator bi = p.getSentenceInstance(locale);
  272. if (bi != null)
  273. return bi;
  274. break;
  275. }
  276. }
  277. }
  278. if (locale.equals(Locale.ROOT))
  279. return new SentenceBreakIterator();
  280. return getSentenceInstance(LocaleHelper.getFallbackLocale(locale));
  281. }
  282. /**
  283. * This method returns the text this object is iterating over as a
  284. * <code>CharacterIterator</code>.
  285. *
  286. * @return The text being iterated over.
  287. */
  288. public abstract CharacterIterator getText ();
  289. /**
  290. * This method returns an instance of <code>BreakIterator</code> that will
  291. * iterate over words as defined in the default locale.
  292. *
  293. * @return A <code>BreakIterator</code> instance for the default locale.
  294. */
  295. public static BreakIterator getWordInstance ()
  296. {
  297. return getWordInstance (Locale.getDefault());
  298. }
  299. /**
  300. * This method returns an instance of <code>BreakIterator</code> that will
  301. * iterate over words as defined in the specified locale.
  302. *
  303. * @param locale The desired locale.
  304. *
  305. * @return A <code>BreakIterator</code> instance for the default locale.
  306. */
  307. public static BreakIterator getWordInstance (Locale locale)
  308. {
  309. BreakIterator r = getInstance ("WordIterator", locale);
  310. if (r != null)
  311. return r;
  312. for (BreakIteratorProvider p :
  313. ServiceLoader.load(BreakIteratorProvider.class))
  314. {
  315. for (Locale loc : p.getAvailableLocales())
  316. {
  317. if (loc.equals(locale))
  318. {
  319. BreakIterator bi = p.getWordInstance(locale);
  320. if (bi != null)
  321. return bi;
  322. break;
  323. }
  324. }
  325. }
  326. if (locale.equals(Locale.ROOT))
  327. return new WordBreakIterator();
  328. return getWordInstance(LocaleHelper.getFallbackLocale(locale));
  329. }
  330. /**
  331. * This method tests whether or not the specified position is a text
  332. * element boundary.
  333. *
  334. * @param pos The text position to test.
  335. *
  336. * @return <code>true</code> if the position is a boundary,
  337. * <code>false</code> otherwise.
  338. */
  339. public boolean isBoundary (int pos)
  340. {
  341. if (pos == 0)
  342. return true;
  343. return following (pos - 1) == pos;
  344. }
  345. /**
  346. * This method returns the last text element boundary in the text being
  347. * iterated over.
  348. *
  349. * @return The last text boundary.
  350. */
  351. public abstract int last ();
  352. /**
  353. * This method returns the text element boundary following the current
  354. * text position.
  355. *
  356. * @return The next text boundary.
  357. */
  358. public abstract int next ();
  359. /**
  360. * This method returns the n'th text element boundary following the current
  361. * text position.
  362. *
  363. * @param n The number of text element boundaries to skip.
  364. *
  365. * @return The next text boundary.
  366. */
  367. public abstract int next (int n);
  368. /**
  369. * This methdod returns the offset of the text element boundary preceding
  370. * the specified offset.
  371. *
  372. * @param pos The text index from which to find the preceding text boundary.
  373. *
  374. * @returns The next text boundary preceding the specified index.
  375. */
  376. public int preceding (int pos)
  377. {
  378. if (following (pos) == DONE)
  379. last ();
  380. while (previous () >= pos)
  381. ;
  382. return current ();
  383. }
  384. /**
  385. * This method returns the text element boundary preceding the current
  386. * text position.
  387. *
  388. * @return The previous text boundary.
  389. */
  390. public abstract int previous ();
  391. /**
  392. * This method sets the text string to iterate over.
  393. *
  394. * @param newText The <code>String</code> to iterate over.
  395. */
  396. public void setText (String newText)
  397. {
  398. setText (new StringCharacterIterator (newText));
  399. }
  400. /**
  401. * This method sets the text to iterate over from the specified
  402. * <code>CharacterIterator</code>.
  403. *
  404. * @param newText The desired <code>CharacterIterator</code>.
  405. */
  406. public abstract void setText (CharacterIterator newText);
  407. }