001: package gnu.kawa.functions;
002:
003: import gnu.text.*;
004: import java.text.*;
005: import java.io.Writer;
006: import java.io.CharArrayWriter;
007: import gnu.mapping.*;
008: import kawa.standard.Scheme;
009: import gnu.lists.AbstractFormat;
010:
011: public class ObjectFormat extends ReportFormat {
012: /** Maxiumum number of characters to show.
013: * Truncate any following characters.
014: * The value PARAM_UNSPECIFIED means "no limit". */
015: int maxChars;
016: boolean readable;
017:
018: private static ObjectFormat readableFormat;
019: private static ObjectFormat plainFormat;
020:
021: public static ObjectFormat getInstance(boolean readable) {
022: if (readable) {
023: if (readableFormat == null)
024: readableFormat = new ObjectFormat(true);
025: return readableFormat;
026: } else {
027: if (plainFormat == null)
028: plainFormat = new ObjectFormat(false);
029: return plainFormat;
030: }
031: }
032:
033: public ObjectFormat(boolean readable) {
034: this .readable = readable;
035: maxChars = PARAM_UNSPECIFIED;
036: }
037:
038: public ObjectFormat(boolean readable, int maxChars) {
039: this .readable = readable;
040: this .maxChars = maxChars;
041: }
042:
043: public int format(Object[] args, int start, Writer dst,
044: FieldPosition fpos) throws java.io.IOException {
045: int maxChars = getParam(this .maxChars, -1, args, start);
046: if (this .maxChars == PARAM_FROM_LIST)
047: start++;
048: return format(args, start, dst, maxChars, readable);
049: }
050:
051: private static void print(Object obj, OutPort out, boolean readable) {
052: boolean saveReadable = out.printReadable;
053: AbstractFormat saveFormat = out.objectFormat;
054: try {
055: out.printReadable = readable;
056: AbstractFormat format = readable ? Scheme.writeFormat
057: : Scheme.displayFormat;
058: out.objectFormat = format;
059: format.writeObject(obj, (gnu.lists.Consumer) out);
060: } finally {
061: out.printReadable = saveReadable;
062: out.objectFormat = saveFormat;
063: }
064: }
065:
066: /**
067: * Return false iff truncation.
068: * @param maxChars maximum number of characters; -1 means unlimited
069: */
070: public static boolean format(Object arg, Writer dst, int maxChars,
071: boolean readable) throws java.io.IOException {
072: if (maxChars < 0 && dst instanceof OutPort) {
073: print(arg, (OutPort) dst, readable);
074: return true;
075: } else if (maxChars < 0 && dst instanceof CharArrayWriter) {
076: OutPort oport = new OutPort(dst);
077: print(arg, oport, readable);
078: oport.close();
079: return true;
080: } else {
081: CharArrayWriter wr = new CharArrayWriter();
082: OutPort oport = new OutPort(wr);
083: print(arg, oport, readable);
084: oport.close();
085: int len = wr.size();
086: if (maxChars < 0 || len <= maxChars) {
087: wr.writeTo(dst);
088: return true;
089: } else {
090: dst.write(wr.toCharArray(), 0, maxChars);
091: return false;
092: }
093: }
094: }
095:
096: public static int format(Object[] args, int start, Writer dst,
097: int maxChars, boolean readable) throws java.io.IOException {
098: Object arg;
099: if (start >= args.length) {
100: arg = "#<missing format argument>";
101: start--;
102: readable = false;
103: maxChars = -1;
104: } else
105: arg = args[start];
106: format(arg, dst, maxChars, readable);
107: return start + 1;
108: }
109:
110: public Object parseObject(String text,
111: java.text.ParsePosition status) {
112: throw new RuntimeException(
113: "ObjectFormat.parseObject - not implemented");
114: }
115: }
|