01: package gnu.text;
02:
03: import java.text.Format;
04: import java.text.FieldPosition;
05: import java.io.Writer;
06:
07: public class CompoundFormat extends ReportFormat {
08: protected int length;
09: protected Format[] formats;
10:
11: public CompoundFormat(Format[] formats, int length) {
12: this .formats = formats;
13: this .length = length;
14: }
15:
16: public CompoundFormat(Format[] formats) {
17: this .formats = formats;
18: this .length = formats.length;
19: }
20:
21: public int format(Object[] args, int start, Writer dst,
22: FieldPosition fpos) throws java.io.IOException {
23: for (int i = 0; i < length; i++) {
24: Format fmt = formats[i];
25: if (fmt instanceof ReportFormat) {
26: start = ((ReportFormat) fmt).format(args, start, dst,
27: fpos);
28: if (start < 0)
29: return start;
30: } else if (start >= args.length)
31: dst.write("#<missing format argument>");
32: else {
33: StringBuffer sbuf = new StringBuffer();
34: fmt.format(args[start], sbuf, fpos);
35: dst.write(sbuf.toString());
36: start++;
37: }
38: }
39: return start;
40: }
41:
42: public final int format(Object[] args, int start,
43: StringBuffer sbuf, FieldPosition fpos) {
44: for (int i = 0; i < length; i++) {
45: Format fmt = formats[i];
46: if (fmt instanceof ReportFormat) {
47: start = ((ReportFormat) fmt).format(args, start, sbuf,
48: fpos);
49: if (start < 0)
50: return start;
51: } else {
52: fmt.format(args[start], sbuf, fpos);
53: start++;
54: }
55: }
56: return start;
57: }
58:
59: public Object parseObject(String text,
60: java.text.ParsePosition status) {
61: throw new Error("CompoundFormat.parseObject - not implemented");
62: }
63:
64: public String toString() {
65: StringBuffer sbuf = new StringBuffer();
66: sbuf.append("CompoundFormat[");
67: for (int i = 0; i < length; i++) {
68: if (i > 0)
69: sbuf.append(", ");
70: sbuf.append(formats[i]);
71: }
72: sbuf.append("]");
73: return sbuf.toString();
74: }
75:
76: }
|