1 /* BreakIterator.java -- Breaks text into elements
2 Copyright (C) 1998, 1999, 2001, 2004, 2005, 2007, 2012
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
42 import gnu.java.locale.LocaleHelper;
44 import gnu.java.text.CharacterBreakIterator;
45 import gnu.java.text.LineBreakIterator;
46 import gnu.java.text.SentenceBreakIterator;
47 import gnu.java.text.WordBreakIterator;
49 import java.text.spi.BreakIteratorProvider;
51 import java.util.Locale;
52 import java.util.MissingResourceException;
53 import java.util.ResourceBundle;
54 import java.util.ServiceLoader;
57 * This class iterates over text elements such as words, lines, sentences,
58 * and characters. It can only iterate over one of these text elements at
59 * a time. An instance of this class configured for the desired iteration
60 * type is created by calling one of the static factory methods, not
61 * by directly calling a constructor.
63 * The standard iterators created by the factory methods in this
64 * class will be valid upon creation. That is, their methods will
65 * not cause exceptions if called before you call setText().
67 * @author Tom Tromey (tromey@cygnus.com)
68 * @author Aaron M. Renn (arenn@urbanophile.com)
69 * @date March 19, 1999
71 /* Written using "Java Class Libraries", 2nd edition, plus online
72 * API docs for JDK 1.2 beta from http://www.javasoft.com.
73 * Status: Believed complete and correct to 1.1.
75 public abstract class BreakIterator implements Cloneable
78 * This value is returned by the <code>next()</code> and
79 * <code>previous</code> in order to indicate that the end of the
80 * text has been reached.
82 // The value was discovered by writing a test program.
83 public static final int DONE = -1;
86 * This method initializes a new instance of <code>BreakIterator</code>.
87 * This protected constructor is available to subclasses as a default
88 * no-arg superclass constructor.
90 protected BreakIterator ()
95 * Create a clone of this object.
97 public Object clone ()
101 return super.clone();
103 catch (CloneNotSupportedException e)
110 * This method returns the index of the current text element boundary.
112 * @return The current text boundary.
114 public abstract int current ();
117 * This method returns the first text element boundary in the text being
120 * @return The first text boundary.
122 public abstract int first ();
125 * This methdod returns the offset of the text element boundary following
126 * the specified offset.
128 * @param pos The text index from which to find the next text boundary.
130 * @return The next text boundary following the specified index.
132 public abstract int following (int pos);
135 * This method returns a list of locales for which instances of
136 * <code>BreakIterator</code> are available.
138 * @return A list of available locales
140 public static synchronized Locale[] getAvailableLocales ()
142 Locale[] l = new Locale[1];
147 private static BreakIterator getInstance (String type, Locale loc)
153 = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
154 loc, ClassLoader.getSystemClassLoader());
155 className = res.getString(type);
157 catch (MissingResourceException x)
163 Class<?> k = Class.forName(className);
164 return (BreakIterator) k.newInstance();
166 catch (ClassNotFoundException x1)
170 catch (InstantiationException x2)
174 catch (IllegalAccessException x3)
181 * This method returns an instance of <code>BreakIterator</code> that will
182 * iterate over characters as defined in the default locale.
184 * @return A <code>BreakIterator</code> instance for the default locale.
186 public static BreakIterator getCharacterInstance ()
188 return getCharacterInstance (Locale.getDefault());
192 * This method returns an instance of <code>BreakIterator</code> that will
193 * iterate over characters as defined in the specified locale.
195 * @param locale The desired locale.
197 * @return A <code>BreakIterator</code> instance for the specified locale.
199 public static BreakIterator getCharacterInstance (Locale locale)
201 BreakIterator r = getInstance("CharacterIterator", locale);
204 for (BreakIteratorProvider p :
205 ServiceLoader.load(BreakIteratorProvider.class))
207 for (Locale loc : p.getAvailableLocales())
209 if (loc.equals(locale))
211 BreakIterator bi = p.getCharacterInstance(locale);
218 if (locale.equals(Locale.ROOT))
219 return new CharacterBreakIterator();
220 return getCharacterInstance(LocaleHelper.getFallbackLocale(locale));
224 * This method returns an instance of <code>BreakIterator</code> that will
225 * iterate over line breaks as defined in the default locale.
227 * @return A <code>BreakIterator</code> instance for the default locale.
229 public static BreakIterator getLineInstance ()
231 return getLineInstance (Locale.getDefault());
235 * This method returns an instance of <code>BreakIterator</code> that will
236 * iterate over line breaks as defined in the specified locale.
238 * @param locale The desired locale.
240 * @return A <code>BreakIterator</code> instance for the default locale.
242 public static BreakIterator getLineInstance (Locale locale)
244 BreakIterator r = getInstance ("LineIterator", locale);
247 for (BreakIteratorProvider p :
248 ServiceLoader.load(BreakIteratorProvider.class))
250 for (Locale loc : p.getAvailableLocales())
252 if (loc.equals(locale))
254 BreakIterator bi = p.getLineInstance(locale);
261 if (locale.equals(Locale.ROOT))
262 return new LineBreakIterator();
263 return getLineInstance(LocaleHelper.getFallbackLocale(locale));
267 * This method returns an instance of <code>BreakIterator</code> that will
268 * iterate over sentences as defined in the default locale.
270 * @return A <code>BreakIterator</code> instance for the default locale.
272 public static BreakIterator getSentenceInstance ()
274 return getSentenceInstance (Locale.getDefault());
278 * This method returns an instance of <code>BreakIterator</code> that will
279 * iterate over sentences as defined in the specified locale.
281 * @param locale The desired locale.
283 * @return A <code>BreakIterator</code> instance for the default locale.
285 public static BreakIterator getSentenceInstance (Locale locale)
287 BreakIterator r = getInstance ("SentenceIterator", locale);
290 for (BreakIteratorProvider p :
291 ServiceLoader.load(BreakIteratorProvider.class))
293 for (Locale loc : p.getAvailableLocales())
295 if (loc.equals(locale))
297 BreakIterator bi = p.getSentenceInstance(locale);
304 if (locale.equals(Locale.ROOT))
305 return new SentenceBreakIterator();
306 return getSentenceInstance(LocaleHelper.getFallbackLocale(locale));
310 * This method returns the text this object is iterating over as a
311 * <code>CharacterIterator</code>.
313 * @return The text being iterated over.
315 public abstract CharacterIterator getText ();
318 * This method returns an instance of <code>BreakIterator</code> that will
319 * iterate over words as defined in the default locale.
321 * @return A <code>BreakIterator</code> instance for the default locale.
323 public static BreakIterator getWordInstance ()
325 return getWordInstance (Locale.getDefault());
329 * This method returns an instance of <code>BreakIterator</code> that will
330 * iterate over words as defined in the specified locale.
332 * @param locale The desired locale.
334 * @return A <code>BreakIterator</code> instance for the default locale.
336 public static BreakIterator getWordInstance (Locale locale)
338 BreakIterator r = getInstance ("WordIterator", locale);
341 for (BreakIteratorProvider p :
342 ServiceLoader.load(BreakIteratorProvider.class))
344 for (Locale loc : p.getAvailableLocales())
346 if (loc.equals(locale))
348 BreakIterator bi = p.getWordInstance(locale);
355 if (locale.equals(Locale.ROOT))
356 return new WordBreakIterator();
357 return getWordInstance(LocaleHelper.getFallbackLocale(locale));
361 * This method tests whether or not the specified position is a text
364 * @param pos The text position to test.
366 * @return <code>true</code> if the position is a boundary,
367 * <code>false</code> otherwise.
369 public boolean isBoundary (int pos)
373 return following (pos - 1) == pos;
377 * This method returns the last text element boundary in the text being
380 * @return The last text boundary.
382 public abstract int last ();
385 * This method returns the text element boundary following the current
388 * @return The next text boundary.
390 public abstract int next ();
393 * This method returns the n'th text element boundary following the current
396 * @param n The number of text element boundaries to skip.
398 * @return The next text boundary.
400 public abstract int next (int n);
403 * This methdod returns the offset of the text element boundary preceding
404 * the specified offset.
406 * @param pos The text index from which to find the preceding text boundary.
408 * @returns The next text boundary preceding the specified index.
410 public int preceding (int pos)
412 if (following (pos) == DONE)
414 while (previous () >= pos)
420 * This method returns the text element boundary preceding the current
423 * @return The previous text boundary.
425 public abstract int previous ();
428 * This method sets the text string to iterate over.
430 * @param newText The <code>String</code> to iterate over.
432 public void setText (String newText)
434 setText (new StringCharacterIterator (newText));
438 * This method sets the text to iterate over from the specified
439 * <code>CharacterIterator</code>.
441 * @param newText The desired <code>CharacterIterator</code>.
443 public abstract void setText (CharacterIterator newText);