001: package gnu.text;
002:
003: import java.text.Format;
004: import java.text.FieldPosition;
005: import java.io.Writer;
006: import java.io.CharArrayWriter;
007: import gnu.lists.Consumer;
008:
009: public abstract class ReportFormat extends Format {
010: /** Some Formats use this to indicate a parameter that is the
011: * extracted from the argment list. */
012: public static final int PARAM_FROM_LIST = 0xA0000000;
013:
014: /** Some Formats use this to indicate a parameter that is the
015: * number of remaining paramaters. */
016: public static final int PARAM_FROM_COUNT = 0xB0000000;
017:
018: /** Some Formats use this to indicate an unspecified parameter. */
019: public static final int PARAM_UNSPECIFIED = 0xC0000000;
020:
021: public static int result(int resultCode, int nextArg) {
022: return (resultCode << 24) | nextArg;
023: }
024:
025: public static int nextArg(int result) {
026: return result & 0xffffff;
027: }
028:
029: public static int resultCode(int result) {
030: return result >>> 24;
031: }
032:
033: /** Format an array of arguments, and write out the result.
034: * @param dst where to write the result
035: * @param args the objects to be formatted
036: * @param start the index (in args) of the argument to start with
037: * @return an integer result(resultCode, nextArg), where
038: * nextArg is the index following the last argument processed, and
039: * code is a result code (normally 0, or negative if early termintation)
040: */
041: public abstract int format(Object[] args, int start, Writer dst,
042: FieldPosition fpos) throws java.io.IOException;
043:
044: public int format(Object arg, int start, Writer dst,
045: FieldPosition fpos) throws java.io.IOException {
046: if (arg instanceof Object[])
047: return format((Object[]) arg, start, dst, fpos);
048: else {
049: Object[] args = { arg };
050: return format(args, start, dst, fpos);
051: }
052: }
053:
054: public StringBuffer format(Object obj, StringBuffer sbuf,
055: FieldPosition fpos) {
056: format((Object[]) obj, 0, sbuf, fpos);
057: return sbuf;
058: }
059:
060: public int format(Object[] args, int start, StringBuffer sbuf,
061: FieldPosition fpos) {
062: CharArrayWriter wr = new CharArrayWriter();
063: try {
064: start = format(args, start, wr, fpos);
065: if (start < 0)
066: return start;
067: } catch (java.io.IOException ex) {
068: throw new Error("unexpected exception: " + ex);
069: }
070: sbuf.append(wr.toCharArray());
071: return start;
072: }
073:
074: public static int format(Format fmt, Object[] args, int start,
075: Writer dst, FieldPosition fpos) throws java.io.IOException {
076: if (fmt instanceof ReportFormat)
077: return ((ReportFormat) fmt).format(args, start, dst, fpos);
078: StringBuffer sbuf = new StringBuffer();
079: if (fmt instanceof java.text.MessageFormat)
080: start = format(fmt, args, start, sbuf, fpos);
081: else
082: fmt.format(args[start++], sbuf, fpos);
083: int slen = sbuf.length();
084: char[] cbuf = new char[slen];
085: sbuf.getChars(0, slen, cbuf, 0);
086: dst.write(cbuf);
087: return start;
088: }
089:
090: public static int format(Format fmt, Object[] args, int start,
091: StringBuffer sbuf, FieldPosition fpos) {
092: if (fmt instanceof ReportFormat)
093: return ((ReportFormat) fmt).format(args, start, sbuf, fpos);
094: int nargs;
095: Object arg;
096: if (fmt instanceof java.text.MessageFormat) {
097: nargs = args.length - start;
098: if (start > 0) {
099: Object[] subarr = new Object[args.length - start];
100: System.arraycopy(args, start, subarr, 0, subarr.length);
101: arg = subarr;
102: } else
103: arg = args;
104: } else {
105: arg = args[start];
106: nargs = 1;
107: }
108: fmt.format(arg, sbuf, fpos);
109: return start + nargs;
110: }
111:
112: /** (Parameters in non-standard order.) */
113: public static void print(Writer dst, String str)
114: throws java.io.IOException {
115: if (dst instanceof java.io.PrintWriter)
116: ((java.io.PrintWriter) dst).print(str);
117: else
118: dst.write(str.toCharArray());
119: }
120:
121: public static void print(Object value, Consumer out) {
122: if (value instanceof Printable)
123: ((Printable) value).print(out);
124: else
125: // Should we use out.writeObject?
126: // We need make consistent rules to avoid infinite recursion.
127: out.write(value == null ? "null" : value.toString());
128: }
129:
130: public Object parseObject(String text,
131: java.text.ParsePosition status) {
132: throw new Error("ReportFormat.parseObject - not implemented");
133: }
134:
135: public static int getParam(Object arg, int defaultValue) {
136: if (arg instanceof Number)
137: return ((Number) arg).intValue();
138: if (arg instanceof Character)
139: return ((Character) arg).charValue();
140: if (arg instanceof Char)
141: return ((Char) arg).charValue();
142: //if (arg == null || arg == Boolean.FALSE || arg == Special.dfault)
143: return defaultValue;
144: }
145:
146: protected static int getParam(int param, int defaultValue,
147: Object[] args, int start) {
148: if (param == PARAM_FROM_COUNT)
149: return args.length - start;
150: if (param == PARAM_FROM_LIST)
151: return args == null ? defaultValue : getParam(args[start],
152: defaultValue);
153: if (param == PARAM_UNSPECIFIED)
154: return defaultValue;
155: // Need to mask off flags etc?
156: return param;
157: }
158:
159: protected static char getParam(int param, char defaultValue,
160: Object[] args, int start) {
161: return (char) getParam(param, (int) defaultValue, args, start);
162: }
163:
164: /** Get the index'th parameter for the conversion specification specs[speci].
165: * Note that parameters are numbered from 1 to numParams(speci).
166: * The list of arguments to be converted is args, with the current index
167: * (as of the start of this conversion, i.e. not taking into account
168: * earlier PARAM_FROM_LIST paramaters for this conversion) in start.
169: * The default value (used if PARAM_UNSPECIFIED) is defaultValue.
170: */
171: /*
172: int getParam(int speci, int index, int defaultValue, Object[] args, int start)
173: {
174: int num_params = numParams(speci);
175: int param = index <= num_params ? specs[speci+index] : PARAM_UNSPECIFIED;
176: if (param == PARAM_FROM_LIST || param == PARAM_FROM_COUNT)
177: start += adjustArgsStart(speci, index);
178: return getParam(param, defaultValue, args, start);
179: }
180: */
181: }
|