The java.text Package

   

The java.text Package

The most advanced and complex Internationalization API features are found in the java.text package. They include many classes for formatting and organizing text in a language-independent way. You have already seen some of the classes and features in the previous sections of this chapter. To refresh your memory, date formatting can be problematic for programmers. In America, dates are written in month-day-year order, but in Europe, dates are written in day-month-year order. This makes interpreting a date like 10/2/97 difficult: Does this represent October 2, 1997 or February 10, 1997? One goal of the classes within the java.text package is parsing and formatting text properly based on the correct Locale so that the data is always parsed and formatted correctly. A second but still very important goal of the java.text package is to reduce the amount of work a Java developer has to do to accomplish goal number 1. The classes within the java.text package don't necessary deal with internationalization features directly although they are designed to deal with text, dates, numbers , and messages in a manner independent of natural languages. As we saw with the MessageFormat example from Listing 24.4, these classes can assist the developer in building internationalization into your Java applications.

Text collating , on the other hand, is the process of sorting text according to particular rules. In English, sorting in alphabetical order is relatively easy because English lacks many special characters (such as accents) that could complicate things. In French, however, things are not so simple. Two words that look very similar (like p ch and p che) have entirely different meanings. Which should come first alphabetically ? And what about hyphenation or punctuation characters? The Java Collation class provides a way of defining language-specific sort criteria in a robust, consistent manner.

Text boundaries can also be ambiguous across languages. Where do words, sentences, and paragraphs begin and end? In English, a period generally marks the end of a sentence and a space defines the boundary for a word, but this is not always the case. The BreakIterator and CharacterIterator classes can intelligently break up text into various subunits based on language-specific criteria. Java comes with built-in support for some languages, but you can always define your own set of rules, as well. The BreakIterator works by returning the integer index of boundaries that occur within a String, as demonstrated by the example in Listing 24.5, which breaks up a String by words:

Listing 24.5 An Example of Using the BreakIterator Class.
 import java.text.BreakIterator; import java.util.Locale; public class BreakIteratorExample {   // Default Constructor   public BreakIteratorExample()   {     super();   }   public void breakString( String stringToExamine, Locale aLocale )   {     // Create a BreakIterator for the string     BreakIterator boundary = BreakIterator.getWordInstance( aLocale );     boundary.setText(stringToExamine);     // Setup the start of the sentence     int start = boundary.first();      for (int end = boundary.next();       end != BreakIterator.DONE;           start = end, end = boundary.next())     {       String subStr = stringToExamine.substring(start,end);       System.out.println( subStr );     }   }   public static void main(String[] args)   {     BreakIteratorExample example = new BreakIteratorExample();     example.breakString( "This is a test.", Locale.US );   } } 

This snippet of code prints out each word on its own line. Although this example is trivial, text boundaries can be extremely important, especially in GUI applications that require text selection, intelligent word wrapping, and so on.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net