1 package gnu.kawa.functions; 2 import gnu.text.*; 3 import java.text.*; 4 import java.io.Writer ; 5 import java.io.CharArrayWriter ; 6 import gnu.mapping.*; 7 import kawa.standard.Scheme; 8 import gnu.lists.AbstractFormat; 9 10 public class ObjectFormat extends ReportFormat 11 { 12 15 int maxChars; 16 boolean readable; 17 18 private static ObjectFormat readableFormat; 19 private static ObjectFormat plainFormat; 20 21 public static ObjectFormat getInstance(boolean readable) 22 { 23 if (readable) 24 { 25 if (readableFormat == null) 26 readableFormat = new ObjectFormat(true); 27 return readableFormat; 28 } 29 else 30 { 31 if (plainFormat == null) 32 plainFormat = new ObjectFormat(false); 33 return plainFormat; 34 } 35 } 36 37 public ObjectFormat(boolean readable) 38 { 39 this.readable = readable; 40 maxChars = PARAM_UNSPECIFIED; 41 } 42 43 public ObjectFormat(boolean readable, int maxChars) 44 { 45 this.readable = readable; 46 this.maxChars = maxChars; 47 } 48 49 public int format(Object [] args, int start, Writer dst, FieldPosition fpos) 50 throws java.io.IOException 51 { 52 int maxChars = getParam(this.maxChars, -1, args, start); 53 if (this.maxChars == PARAM_FROM_LIST) start++; 54 return format(args, start, dst, maxChars, readable); 55 } 56 57 private static void print (Object obj, OutPort out, 58 boolean readable) 59 { 60 boolean saveReadable = out.printReadable; 61 AbstractFormat saveFormat = out.objectFormat; 62 try 63 { 64 out.printReadable = readable; 65 AbstractFormat format 66 = readable ? Scheme.writeFormat : Scheme.displayFormat; 67 out.objectFormat = format; 68 format.writeObject(obj, (gnu.lists.Consumer) out); 69 } 70 finally 71 { 72 out.printReadable = saveReadable; 73 out.objectFormat = saveFormat; 74 } 75 } 76 77 81 public static boolean format(Object arg, Writer dst, 82 int maxChars, boolean readable) 83 throws java.io.IOException 84 { 85 if (maxChars < 0 && dst instanceof OutPort) 86 { 87 print(arg, (OutPort) dst, readable); 88 return true; 89 } 90 else if (maxChars < 0 && dst instanceof CharArrayWriter ) 91 { 92 OutPort oport = new OutPort(dst); 93 print(arg, oport, readable); 94 oport.close(); 95 return true; 96 } 97 else 98 { 99 CharArrayWriter wr = new CharArrayWriter (); 100 OutPort oport = new OutPort(wr); 101 print(arg, oport, readable); 102 oport.close(); 103 int len = wr.size(); 104 if (maxChars < 0 || len <= maxChars) 105 { 106 wr.writeTo(dst); 107 return true; 108 } 109 else 110 { 111 dst.write(wr.toCharArray(), 0, maxChars); 112 return false; 113 } 114 } 115 } 116 117 public static int format(Object [] args, int start, Writer dst, 118 int maxChars, boolean readable) 119 throws java.io.IOException 120 { 121 Object arg; 122 if (start >= args.length) 123 { 124 arg = "#<missing format argument>"; 125 start--; 126 readable = false; 127 maxChars = -1; 128 } 129 else 130 arg = args[start]; 131 format(arg, dst, maxChars, readable); 132 return start + 1; 133 } 134 135 public Object parseObject(String text, java.text.ParsePosition status) 136 { 137 throw new RuntimeException ("ObjectFormat.parseObject - not implemented"); 138 } 139 } 140 | Popular Tags |