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 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java
.util
.Locale
;
42 import java
.util
.MissingResourceException
;
43 import java
.util
.ResourceBundle
;
46 * This class iterates over text elements such as words, lines, sentences,
47 * and characters. It can only iterate over one of these text elements at
48 * a time. An instance of this class configured for the desired iteration
49 * type is created by calling one of the static factory methods, not
50 * by directly calling a constructor.
52 * @author Tom Tromey <tromey@cygnus.com>
53 * @author Aaron M. Renn (arenn@urbanophile.com)
54 * @date March 19, 1999
56 /* Written using "Java Class Libraries", 2nd edition, plus online
57 * API docs for JDK 1.2 beta from http://www.javasoft.com.
58 * Status: Believed complete and correct to 1.1.
60 public abstract class BreakIterator
implements Cloneable
63 * This value is returned by the <code>next()</code> and
64 * <code>previous</code> in order to indicate that the end of the
65 * text has been reached.
67 // The value was discovered by writing a test program.
68 public static final int DONE
= -1;
71 * This method initializes a new instance of <code>BreakIterator</code>.
72 * This protected constructor is available to subclasses as a default
73 * no-arg superclass constructor.
75 protected BreakIterator ()
80 * Create a clone of this object.
82 public Object
clone ()
88 catch (CloneNotSupportedException e
)
95 * This method returns the index of the current text element boundary.
97 * @return The current text boundary.
99 public abstract int current ();
102 * This method returns the first text element boundary in the text being
105 * @return The first text boundary.
107 public abstract int first ();
110 * This methdod returns the offset of the text element boundary following
111 * the specified offset.
113 * @param offset The text index from which to find the next text boundary.
115 * @param The next text boundary following the specified index.
117 public abstract int following (int pos
);
120 * This method returns a list of locales for which instances of
121 * <code>BreakIterator</code> are available.
123 * @return A list of available locales
125 public static synchronized Locale
[] getAvailableLocales ()
127 Locale
[] l
= new Locale
[1];
132 private static BreakIterator
getInstance (String type
, Locale loc
)
138 = ResourceBundle
.getBundle("gnu.java.locale.LocaleInformation",
140 className
= res
.getString(type
);
142 catch (MissingResourceException x
)
148 Class k
= Class
.forName(className
);
149 return (BreakIterator
) k
.newInstance();
151 catch (ClassNotFoundException x1
)
155 catch (InstantiationException x2
)
159 catch (IllegalAccessException x3
)
166 * This method returns an instance of <code>BreakIterator</code> that will
167 * iterate over characters as defined in the default locale.
169 * @return A <code>BreakIterator</code> instance for the default locale.
171 public static BreakIterator
getCharacterInstance ()
173 return getCharacterInstance (Locale
.getDefault());
177 * This method returns an instance of <code>BreakIterator</code> that will
178 * iterate over characters as defined in the specified locale. If the
179 * desired locale is not available, the default locale is used.
181 * @param locale The desired locale.
183 * @return A <code>BreakIterator</code> instance for the default locale.
185 public static BreakIterator
getCharacterInstance (Locale loc
)
187 BreakIterator r
= getInstance ("CharacterIterator", loc
);
189 r
= new gnu
.java
.text
.CharacterBreakIterator ();
194 * This method returns an instance of <code>BreakIterator</code> that will
195 * iterate over line breaks as defined in the default locale.
197 * @return A <code>BreakIterator</code> instance for the default locale.
199 public static BreakIterator
getLineInstance ()
201 return getLineInstance (Locale
.getDefault());
205 * This method returns an instance of <code>BreakIterator</code> that will
206 * iterate over line breaks as defined in the specified locale. If the
207 * desired locale is not available, the default locale is used.
209 * @param locale The desired locale.
211 * @return A <code>BreakIterator</code> instance for the default locale.
213 public static BreakIterator
getLineInstance (Locale loc
)
215 BreakIterator r
= getInstance ("LineIterator", loc
);
217 r
= new gnu
.java
.text
.LineBreakIterator ();
222 * This method returns an instance of <code>BreakIterator</code> that will
223 * iterate over sentences as defined in the default locale.
225 * @return A <code>BreakIterator</code> instance for the default locale.
227 public static BreakIterator
getSentenceInstance ()
229 return getSentenceInstance (Locale
.getDefault());
233 * This method returns an instance of <code>BreakIterator</code> that will
234 * iterate over sentences as defined in the specified locale. If the
235 * desired locale is not available, the default locale is used.
237 * @param locale The desired locale.
239 * @return A <code>BreakIterator</code> instance for the default locale.
241 public static BreakIterator
getSentenceInstance (Locale loc
)
243 BreakIterator r
= getInstance ("SentenceIterator", loc
);
245 r
= new gnu
.java
.text
.SentenceBreakIterator ();
250 * This method returns the text this object is iterating over as a
251 * <code>CharacterIterator</code>.
253 * @param The text being iterated over.
255 public abstract CharacterIterator
getText ();
258 * This method returns an instance of <code>BreakIterator</code> that will
259 * iterate over words as defined in the default locale.
261 * @return A <code>BreakIterator</code> instance for the default locale.
263 public static BreakIterator
getWordInstance ()
265 return getWordInstance (Locale
.getDefault());
269 * This method returns an instance of <code>BreakIterator</code> that will
270 * iterate over words as defined in the specified locale. If the
271 * desired locale is not available, the default locale is used.
273 * @param locale The desired locale.
275 * @return A <code>BreakIterator</code> instance for the default locale.
277 public static BreakIterator
getWordInstance (Locale loc
)
279 BreakIterator r
= getInstance ("WordIterator", loc
);
281 r
= new gnu
.java
.text
.WordBreakIterator ();
286 * This method tests whether or not the specified position is a text
289 * @param offset The text position to test.
291 * @return <code>true</code> if the position is a boundary,
292 * <code>false</code> otherwise.
294 public boolean isBoundary (int pos
)
298 return following (pos
- 1) == pos
;
302 * This method returns the last text element boundary in the text being
305 * @return The last text boundary.
307 public abstract int last ();
310 * This method returns the text element boundary following the current
313 * @return The next text boundary.
315 public abstract int next ();
318 * This method returns the n'th text element boundary following the current
321 * @param n The number of text element boundaries to skip.
323 * @return The next text boundary.
325 public abstract int next (int n
);
328 * This methdod returns the offset of the text element boundary preceding
329 * the specified offset.
331 * @param offset The text index from which to find the preceding
334 * @returns The next text boundary preceding the specified index.
336 public int preceding (int pos
)
338 if (following (pos
) == DONE
)
340 while (previous () >= pos
)
346 * This method returns the text element boundary preceding the current
349 * @return The previous text boundary.
351 public abstract int previous ();
354 * This method sets the text string to iterate over.
356 * @param str The <code>String</code> to iterate over.
358 public void setText (String newText
)
360 setText (new StringCharacterIterator (newText
));
364 * This method sets the text to iterate over from the specified
365 * <code>CharacterIterator</code>.
367 * @param ci The desired <code>CharacterIterator</code>.
369 public abstract void setText (CharacterIterator newText
);