01: package gnu.kawa.functions;
02:
03: import gnu.lists.*;
04: import java.text.MessageFormat;
05: import gnu.text.ReportFormat;
06: import gnu.mapping.*;
07:
08: public class Format extends ProcedureN {
09: public static final Format format = new Format();
10: static {
11: format.setName("format");
12: }
13:
14: public static void format(OutPort dst, Object[] args, int arg_offset) {
15: Object format = args[arg_offset++];
16: Object[] vals = new Object[args.length - arg_offset];
17: System.arraycopy(args, arg_offset, vals, 0, vals.length);
18: if (format instanceof MessageFormat) {
19: String out = ((MessageFormat) format).format(vals);
20: dst.print(out);
21: } else {
22: if (!(format instanceof ReportFormat))
23: format = ParseFormat.parseFormat.apply1(format);
24: try {
25: ((ReportFormat) format).format(vals, 0, dst, null);
26: } catch (java.io.IOException ex) {
27: throw new RuntimeException("Error in format: " + ex);
28: }
29: }
30: }
31:
32: public static FString formatToString(Object[] args, int arg_offset) {
33: CharArrayOutPort port = new CharArrayOutPort();
34: format(port, args, arg_offset);
35: char[] chars = port.toCharArray();
36: port.close();
37: return new FString(chars);
38: }
39:
40: /**
41: * Apply format and argument, yielding an FString.
42: * @param style either '%' (C/Emacs-style format specifiers), or
43: * '~' (Common Lisp-style format specifiers).
44: * @param fmt the format string or specification
45: * @param args the arguments to be formatted
46: */
47: public static FString formatToString(char style, Object fmt,
48: Object[] args) {
49: ReportFormat rfmt = ParseFormat.asFormat(fmt, style);
50: CharArrayOutPort port = new CharArrayOutPort();
51: try {
52: rfmt.format(args, 0, port, null);
53: } catch (java.io.IOException ex) {
54: throw new RuntimeException("Error in format: " + ex);
55: }
56: char[] chars = port.toCharArray();
57: port.close();
58: return new FString(chars);
59: }
60:
61: public Object applyN(Object[] args) {
62: return format$V(args);
63: }
64:
65: public static Object format$V(Object[] args) {
66: Object port_arg = args[0];
67: if (port_arg == Boolean.TRUE) {
68: format(OutPort.outDefault(), args, 1);
69: return Values.empty;
70: } else if (port_arg == Boolean.FALSE) {
71: return formatToString(args, 1);
72: } else if (port_arg instanceof FString
73: || port_arg instanceof MessageFormat
74: || port_arg instanceof ReportFormat) {
75: return formatToString(args, 0);
76: } else if (port_arg instanceof OutPort) {
77: format((OutPort) port_arg, args, 1);
78: return Values.empty;
79: } else if (port_arg instanceof java.io.Writer) {
80: OutPort port = new OutPort((java.io.Writer) port_arg);
81: format(port, args, 1);
82: port.close();
83: return Values.empty;
84: } else if (port_arg instanceof java.io.OutputStream) {
85: OutPort port = new OutPort((java.io.OutputStream) port_arg);
86: format(port, args, 1);
87: port.close();
88: return Values.empty;
89: } else
90: throw new RuntimeException("bad first argument to format");
91: }
92: }
|