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