KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > text > CaseConvertFormat


1 package gnu.text;
2 import java.text.Format JavaDoc;
3 import java.text.FieldPosition JavaDoc;
4 import java.io.Writer JavaDoc;
5
6 public class CaseConvertFormat extends ReportFormat
7 {
8   // 'U' - convert to uppercase.
9
// 'L' - convert to lowercase.
10
// 'C' - capitalize each word
11
// 'T' - capitalize first word, lowercase rest.
12
char code;
13
14   Format JavaDoc baseFormat;
15
16   public CaseConvertFormat(Format JavaDoc baseFormat, char action)
17   {
18     this.baseFormat = baseFormat;
19     this.code = action;
20   }
21
22   public Format JavaDoc getBaseFormat() { return baseFormat; }
23   public void setBaseFormat(Format JavaDoc baseFormat) { this.baseFormat = baseFormat;}
24
25   public int format(Object JavaDoc[] args, int start, Writer JavaDoc dst, FieldPosition JavaDoc fpos)
26     throws java.io.IOException JavaDoc
27   {
28     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc(100);
29     int result = format(baseFormat, args, start, sbuf, fpos);
30     int len = sbuf.length();
31     char prev = ' ';
32     for (int i = 0; i < len; i++)
33       {
34     char ch = sbuf.charAt(i);
35     if (code == 'U')
36       ch = Character.toUpperCase(ch);
37     else if ((code == 'T' && i == 0)
38          || (code == 'C' && ! Character.isLetterOrDigit(prev)))
39       ch = Character.toTitleCase(ch);
40     else
41       ch = Character.toLowerCase(ch);
42     prev = ch;
43     dst.write(ch);
44       }
45     return result;
46   }
47 }
48
Popular Tags