All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.text.TextBoundary

java.lang.Object
   |
   +----java.text.TextBoundary

public class TextBoundary
extends Object
implements Cloneable, Serializable
The TextBoundary class implements methods for finding the location of boundaries in text. Instances of TextBoundary maintain a current position and scan over text returning the index of characters where boundaries occur. Internally, TextBoundary scans text using a CharacterIterator, and is thus able to scan text held by any object implementing that protocol. A StringCharacterIterator is used to scan Strings passed to setText. Line boundary analysis determines where a text string can be broken when line-wrapping. The mechanism correctly handles punctuation and hyphenated words. Sentence boundary analysis allows selection with correct interpretation of periods within numbers and abbreviations, and trailing punctuation marks such as quotation marks and parentheses. Word boundary analysis is used by search and replace functions, as well as within text editing applications that allow the user to select words with a double click. Word selection provides correct interpretation of punctuation marks within and following words. Characters that are not part of a word, such as symbols or punctuation marks, have word-breaks on both sides. Character boundary analysis allows users to interact with characters as they expect to, for example, when moving the cursor through a text string. Character boundary analysis provides correct navigation of through character strings, regardless of how the character is stored. For example, an accented character might be stored as a base character and a diacritical mark. What users consider to be a character can differ between languages. This is the interface for all text boundaries.

Examples:

Creating and using text boundaries

 public static void main(String args[]) {
     if (args.length == 1) {
         String stringToExamine = args[0];
         //print each word in order
         TextBoundary boundary = TextBoundary.getWordBreak();
         boundary.setText(stringToExamine);
         printEachForward(boundary);
         //print each sentence in reverse order
         boundary = TextBoundary.getSentenceBreak();
         boundary.setText(stringToExamine)
         printEachbackward(boundary);
     }
 }
 
Print each element in order
 public void printEachForward(TextBoundary boundary) {
     int start = boundary.first();
     for (int end = boundary.next();
          end != TextBoundary.DONE;
          start = end, end = boundary.next) {
         System.out.println(boundary.getText().substring(start, end);
     }
 }
 
Print each element in reverse order
 public void printEachbackward(TextBoundary boundary) {
     int end = boundary.last();
     for (int start = boundary.previous();
          start != TextBoundary.DONE;
          end = start, start = boundary.previous) {
         System.out.println(boundary.getText().substring(start, end);
     }
 }
 
Print first element
 public void printFirst(TextBoundary boundary) {
     int start = boundary.first();
     int end = boundary.next();
     System.out.println(boundary.getText().substring(start, end));
 }
 
Print last element
 public void printLast(TextBoundary boundary) {
     int end = boundary.last();
     int start = boundary.previous();
     System.out.println(boundary.getText().substring(start, end));
 }
 
Print the element at a specified position
 public void printAt(TextBoundary boundary, int pos) {
     int end = boundary.nextAfter(pos);
     int start = boundary.previous();
     System.out.println(boundary.getText().substring(start, end));
 }
 

See Also:
CharacterIterator

Variable Index

 o DONE
DONE is returned by previous() and next() after all valid boundaries have been returned.

Constructor Index

 o TextBoundary()
Constructor.

Method Index

 o clone()
Create a copy of this boundary
 o current()
Return character index of the text boundary that was most recently returned by next(), previous(), first(), or last()
 o first()
Return the index of first character in the text being scanned.
 o getAvailableLocales()
Get the set of Locales for which TextBoundaries are installed
 o getCharacterBreak()
Create TextBoundary for character-breaks using default locale Returns an instance of a TextBoundary implementing character breaks.
 o getCharacterBreak(Locale)
Create TextBoundary for character-breaks using specified locale Returns an instance of a TextBoundary implementing character breaks.
 o getDisplayName(Locale)
Get name of the object for the desired Locale, in the langauge of the default locale.
 o getDisplayName(Locale, Locale)
Get name of the object for the desired Locale, in the desired langauge.
 o getLineBreak()
Create TextBoundary for line-breaks using default locale.
 o getLineBreak(Locale)
Create TextBoundary for line-breaks using default locale.
 o getSentenceBreak()
Create TextBoundary for sentence-breaks using default locale Returns an instance of a TextBoundary implementing sentence breaks.
 o getSentenceBreak(Locale)
Create TextBoundary for sentence-breaks using specified locale Returns an instance of a TextBoundary implementing sentence breaks.
 o getText()
Get the text being scanned
 o getWordBreak()
Create TextBoundary for word-breaks using default locale.
 o getWordBreak(Locale)
Create TextBoundary for word-breaks using default locale.
 o last()
Return the index of last character in the text being scanned.
 o next()
Return the boundary following the current boundary.
 o nextAfter(int)
Return the first boundary after the specified offset.
 o nthFromCurrent(int)
Return the nth boundary from the current boundary
 o previous()
Return the boundary preceding the current boundary.
 o setText(CharacterIterator)
Set a new text for scanning.
 o setText(String)
Set a new text string to be scanned.

Variables

 o DONE
  public final static int DONE
DONE is returned by previous() and next() after all valid boundaries have been returned.

Constructors

 o TextBoundary
  protected TextBoundary()
Constructor. TextBoundary is stateless and has no default behavior.

Methods

 o clone
  public Object clone()
Create a copy of this boundary

Returns:
A copy of this
Overrides:
clone in class Object
 o first
  public abstract int first()
Return the index of first character in the text being scanned. The value is determined by the CharacterIterator used to scan the text.

Returns:
the index of first character in the text being scanned.
 o last
  public abstract int last()
Return the index of last character in the text being scanned. The value is determined by the CharacterIterator used to scan the text.

Returns:
the index of last character in the text being scanned.
 o nthFromCurrent
  public abstract int nthFromCurrent(int n)
Return the nth boundary from the current boundary

Parameters:
n - which boundary to return. A value of 0 does nothing. Negative values move to previous boundaries and positive values move to later boundaries.
Returns:
The index of the nth boundary from the current position.
 o previous
  public abstract int previous()
Return the boundary preceding the current boundary.

Returns:
The character index of the previous text boundary or DONE if all boundaries have been returned.
 o next
  public abstract int next()
Return the boundary following the current boundary.

Returns:
The character index of the next text boundary or DONE if all boundaries have been returned.
 o nextAfter
  public abstract int nextAfter(int offset)
Return the first boundary after the specified offset. The value returned is always greater than the offset or the value TextBoundary.DONE

Parameters:
offset - the offset to begin scanning. Valid values are determined by the CharacterIterator passed to setText(). Invalid values cause an IllegalArgumentException to be thrown.
Returns:
The first boundary after the specified offset.
 o current
  public abstract int current()
Return character index of the text boundary that was most recently returned by next(), previous(), first(), or last()

Returns:
The boundary most recently returned.
 o getText
  public abstract String getText()
Get the text being scanned

Returns:
the text being scanned
 o setText
  public void setText(String newText)
Set a new text string to be scanned. The current scan position is reset to first().

Parameters:
newText - new text to scan.
 o setText
  public abstract void setText(CharacterIterator newText)
Set a new text for scanning. The current scan position is reset to first().

Parameters:
newText - new text to scan.
 o getWordBreak
  public static TextBoundary getWordBreak()
Create TextBoundary for word-breaks using default locale. Returns an instance of a TextBoundary implementing word breaks. WordBreak is usefull for word selection (ex. double click)

Returns:
A TextBoundary for word-breaks
See Also:
getDefault
 o getWordBreak
  public static TextBoundary getWordBreak(Locale where)
Create TextBoundary for word-breaks using default locale. Returns an instance of a TextBoundary implementing word breaks. WordBreak is usefull for word selection (ex. double click)

Parameters:
where - the local. If a specific WordBreak is not avaliable for the specified local, a default WordBreak is returned.
Returns:
A TextBoundary for word-breaks
 o getLineBreak
  public static TextBoundary getLineBreak()
Create TextBoundary for line-breaks using default locale. Returns an instance of a TextBoundary implementing line breaks. Line breaks are logically possible line breaks, actual line breaks are usually determined based on display width. LineBreak is usefull for word wrapping text.

Returns:
A TextBoundary for line-breaks
See Also:
getDefault
 o getLineBreak
  public static TextBoundary getLineBreak(Locale where)
Create TextBoundary for line-breaks using default locale. Returns an instance of a TextBoundary implementing line breaks. Line breaks are logically possible line breaks, actual line breaks are usually determined based on display width. LineBreak is usefull for word wrapping text.

Parameters:
where - the local. If a specific LineBreak is not avaliable for the specified local, a default LineBreak is returned.
Returns:
A TextBoundary for line-breaks
 o getCharacterBreak
  public static TextBoundary getCharacterBreak()
Create TextBoundary for character-breaks using default locale Returns an instance of a TextBoundary implementing character breaks. Character breaks are boundaries of combining character sequences.

Returns:
A TextBoundary for character-breaks
See Also:
getDefault
 o getCharacterBreak
  public static TextBoundary getCharacterBreak(Locale where)
Create TextBoundary for character-breaks using specified locale Returns an instance of a TextBoundary implementing character breaks. Character breaks are boundaries of combining character sequences.

Parameters:
where - the local. If a specific character break is not avaliable for the specified local, a default character break is returned.
Returns:
A TextBoundary for character-breaks
 o getSentenceBreak
  public static TextBoundary getSentenceBreak()
Create TextBoundary for sentence-breaks using default locale Returns an instance of a TextBoundary implementing sentence breaks.

Returns:
A TextBoundary for sentence-breaks
See Also:
getDefault
 o getSentenceBreak
  public static TextBoundary getSentenceBreak(Locale where)
Create TextBoundary for sentence-breaks using specified locale Returns an instance of a TextBoundary implementing sentence breaks.

Parameters:
where - the local. If a specific SentenceBreak is not avaliable for the specified local, a default SentenceBreak is returned.
Returns:
A TextBoundary for sentence-breaks
 o getAvailableLocales
  public static synchronized Locale[] getAvailableLocales()
Get the set of Locales for which TextBoundaries are installed

Returns:
available locales
 o getDisplayName
  public static synchronized String getDisplayName(Locale objectLocale,
                                                   Locale displayLocale)
Get name of the object for the desired Locale, in the desired langauge.

Parameters:
objectLocale - must be from getAvailableLocales.
displayLocale - specifies the desired locale for output. Uses best match.
Returns:
user-displayable name
See Also:
ResourceBundle
 o getDisplayName
  public final static String getDisplayName(Locale objectLocale)
Get name of the object for the desired Locale, in the langauge of the default locale.

Parameters:
objectLocale - must be from getMatchingLocales
Returns:
user-displayable name
See Also:
ResourceBundle

All Packages  Class Hierarchy  This Package  Previous  Next  Index