01: package gnu.lists;
02:
03: import gnu.mapping.*;
04: import java.text.FieldPosition;
05: import gnu.text.Path;
06:
07: public abstract class AbstractFormat extends java.text.Format {
08: protected void write(String str, Consumer out) {
09: out.write(str);
10: }
11:
12: public void write(int v, Consumer out) {
13: out.write(v);
14: }
15:
16: /** Write a long.
17: * The default is to call writeLong on the Consumer. */
18: public void writeLong(long v, Consumer out) {
19: out.writeLong(v);
20: }
21:
22: /** Write an int.
23: * The default is to call writeLong, so sub-classes only need to
24: * override the latter. */
25: public void writeInt(int i, Consumer out) {
26: writeLong(i, out);
27: }
28:
29: public void writeBoolean(boolean v, Consumer out) {
30: out.writeBoolean(v);
31: }
32:
33: public void startElement(Object type, Consumer out) {
34: write("(", out);
35: write(type.toString(), out);
36: write(" ", out);
37: }
38:
39: public void endElement(Consumer out) {
40: write(")", out);
41: }
42:
43: public void startAttribute(Object attrType, Consumer out) {
44: write(attrType.toString(), out);
45: write(": ", out);
46: }
47:
48: public void endAttribute(Consumer out) {
49: write(" ", out); // FIXME
50: }
51:
52: public abstract void writeObject(Object v, Consumer out);
53:
54: public void format(Object value, Consumer out) {
55: if (out instanceof OutPort) {
56: OutPort pout = (OutPort) out;
57: AbstractFormat saveFormat = pout.objectFormat;
58: try {
59: pout.objectFormat = this ;
60: out.writeObject(value);
61: } finally {
62: pout.objectFormat = saveFormat;
63: }
64: } else
65: out.writeObject(value);
66: }
67:
68: public final void writeObject(Object obj, PrintConsumer out) {
69: writeObject(obj, (Consumer) out);
70: }
71:
72: public final void writeObject(Object obj, java.io.Writer out) {
73: if (out instanceof Consumer)
74: writeObject(obj, (Consumer) out);
75: else {
76: OutPort port = new OutPort(out, false, true);
77: writeObject(obj, (Consumer) out);
78: port.close();
79: }
80: }
81:
82: public StringBuffer format(Object val, StringBuffer sbuf,
83: FieldPosition fpos) {
84: CharArrayOutPort out = new CharArrayOutPort();
85: writeObject(val, out);
86: sbuf.append(out.toCharArray());
87: out.close();
88: return sbuf;
89: }
90:
91: public Object parseObject(String text,
92: java.text.ParsePosition status) {
93: throw new Error(this .getClass().getName()
94: + ".parseObject - not implemented");
95: }
96: }
|