1 /* BreakIterator.java -- Breaks text into elements
2 Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 As a special exception, if you link this library with other files to
22 produce an executable, this library does not by itself cause the
23 resulting executable to be covered by the GNU General Public License.
24 This exception does not however invalidate any other reasons why the
25 executable file might be covered by the GNU General Public License. */
30 import java.util.Locale;
31 import java.util.MissingResourceException;
32 import java.util.ResourceBundle;
35 * This class iterates over text elements such as words, lines, sentences,
36 * and characters. It can only iterate over one of these text elements at
37 * a time. An instance of this class configured for the desired iteration
38 * type is created by calling one of the static factory methods, not
39 * by directly calling a constructor.
41 * @author Tom Tromey <tromey@cygnus.com>
42 * @author Aaron M. Renn (arenn@urbanophile.com)
43 * @date March 19, 1999
45 /* Written using "Java Class Libraries", 2nd edition, plus online
46 * API docs for JDK 1.2 beta from http://www.javasoft.com.
47 * Status: Believed complete and correct to 1.1.
49 public abstract class BreakIterator implements Cloneable
52 * This value is returned by the <code>next()</code> and
53 * <code>previous</code> in order to indicate that the end of the
54 * text has been reached.
56 // The value was discovered by writing a test program.
57 public static final int DONE = -1;
60 * This method initializes a new instance of <code>BreakIterator</code>.
61 * This protected constructor is available to subclasses as a default
62 * no-arg superclass constructor.
64 protected BreakIterator ()
69 * This method returns the index of the current text element boundary.
71 * @return The current text boundary.
73 public abstract int current ();
76 * This method returns the first text element boundary in the text being
79 * @return The first text boundary.
81 public abstract int first ();
84 * This methdod returns the offset of the text element boundary following
85 * the specified offset.
87 * @param offset The text index from which to find the next text boundary.
89 * @param The next text boundary following the specified index.
91 public abstract int following (int pos);
94 * This method returns a list of locales for which instances of
95 * <code>BreakIterator</code> are available.
97 * @return A list of available locales
99 public static synchronized Locale[] getAvailableLocales ()
101 Locale[] l = new Locale[1];
106 private static BreakIterator getInstance (String type, Locale loc)
112 = ResourceBundle.getBundle("gnu.java.locale.LocaleInformation",
114 className = res.getString(type);
116 catch (MissingResourceException x)
122 Class k = Class.forName(className);
123 return (BreakIterator) k.newInstance();
125 catch (ClassNotFoundException x1)
129 catch (InstantiationException x2)
133 catch (IllegalAccessException x3)
140 * This method returns an instance of <code>BreakIterator</code> that will
141 * iterate over characters as defined in the default locale.
143 * @return A <code>BreakIterator</code> instance for the default locale.
145 public static BreakIterator getCharacterInstance ()
147 return getCharacterInstance (Locale.getDefault());
151 * This method returns an instance of <code>BreakIterator</code> that will
152 * iterate over characters as defined in the specified locale. If the
153 * desired locale is not available, the default locale is used.
155 * @param locale The desired locale.
157 * @return A <code>BreakIterator</code> instance for the default locale.
159 public static BreakIterator getCharacterInstance (Locale loc)
161 BreakIterator r = getInstance ("CharacterIterator", loc);
163 r = new gnu.java.text.CharacterBreakIterator ();
168 * This method returns an instance of <code>BreakIterator</code> that will
169 * iterate over line breaks as defined in the default locale.
171 * @return A <code>BreakIterator</code> instance for the default locale.
173 public static BreakIterator getLineInstance ()
175 return getLineInstance (Locale.getDefault());
179 * This method returns an instance of <code>BreakIterator</code> that will
180 * iterate over line breaks as defined in the specified locale. If the
181 * desired locale is not available, the default locale is used.
183 * @param locale The desired locale.
185 * @return A <code>BreakIterator</code> instance for the default locale.
187 public static BreakIterator getLineInstance (Locale loc)
189 BreakIterator r = getInstance ("LineIterator", loc);
191 r = new gnu.java.text.LineBreakIterator ();
196 * This method returns an instance of <code>BreakIterator</code> that will
197 * iterate over sentences as defined in the default locale.
199 * @return A <code>BreakIterator</code> instance for the default locale.
201 public static BreakIterator getSentenceInstance ()
203 return getSentenceInstance (Locale.getDefault());
207 * This method returns an instance of <code>BreakIterator</code> that will
208 * iterate over sentences as defined in the specified locale. If the
209 * desired locale is not available, the default locale is used.
211 * @param locale The desired locale.
213 * @return A <code>BreakIterator</code> instance for the default locale.
215 public static BreakIterator getSentenceInstance (Locale loc)
217 BreakIterator r = getInstance ("SentenceIterator", loc);
219 r = new gnu.java.text.SentenceBreakIterator ();
224 * This method returns the text this object is iterating over as a
225 * <code>CharacterIterator</code>.
227 * @param The text being iterated over.
229 public abstract CharacterIterator getText ();
232 * This method returns an instance of <code>BreakIterator</code> that will
233 * iterate over words as defined in the default locale.
235 * @return A <code>BreakIterator</code> instance for the default locale.
237 public static BreakIterator getWordInstance ()
239 return getWordInstance (Locale.getDefault());
243 * This method returns an instance of <code>BreakIterator</code> that will
244 * iterate over words as defined in the specified locale. If the
245 * desired locale is not available, the default locale is used.
247 * @param locale The desired locale.
249 * @return A <code>BreakIterator</code> instance for the default locale.
251 public static BreakIterator getWordInstance (Locale loc)
253 BreakIterator r = getInstance ("WordIterator", loc);
255 r = new gnu.java.text.WordBreakIterator ();
260 * This method tests whether or not the specified position is a text
263 * @param offset The text position to test.
265 * @return <code>true</code> if the position is a boundary,
266 * <code>false</code> otherwise.
268 public boolean isBoundary (int pos)
272 return following (pos - 1) == pos;
276 * This method returns the last text element boundary in the text being
279 * @return The last text boundary.
281 public abstract int last ();
284 * This method returns the text element boundary following the current
287 * @return The next text boundary.
289 public abstract int next ();
292 * This method returns the n'th text element boundary following the current
295 * @param n The number of text element boundaries to skip.
297 * @return The next text boundary.
299 public abstract int next (int n);
302 * This methdod returns the offset of the text element boundary preceding
303 * the specified offset.
305 * @param offset The text index from which to find the preceding
308 * @returns The next text boundary preceding the specified index.
310 public int preceding (int pos)
312 if (following (pos) == DONE)
314 while (previous () >= pos)
320 * This method returns the text element boundary preceding the current
323 * @return The previous text boundary.
325 public abstract int previous ();
328 * This method sets the text string to iterate over.
330 * @param str The <code>String</code> to iterate over.
332 public void setText (String newText)
334 setText (new StringCharacterIterator (newText));
338 * This method sets the text to iterate over from the specified
339 * <code>CharacterIterator</code>.
341 * @param ci The desired <code>CharacterIterator</code>.
343 public abstract void setText (CharacterIterator newText);