1 package org.columba.mail.folder.headercache; 19 20 import java.awt.Color ; 21 import java.util.Date ; 22 23 import org.columba.core.base.BooleanCompressor; 24 import org.columba.core.gui.base.ColorFactory; 25 import org.columba.mail.message.ColumbaHeader; 26 import org.columba.mail.message.IColumbaHeader; 27 import org.columba.ristretto.message.Address; 28 import org.columba.ristretto.parser.AddressParser; 29 import org.columba.ristretto.parser.ParserException; 30 31 import com.sleepycat.bind.tuple.TupleBinding; 32 import com.sleepycat.bind.tuple.TupleInput; 33 import com.sleepycat.bind.tuple.TupleOutput; 34 35 public class DefaultHeaderBinding extends TupleBinding { 36 37 public Object entryToObject(TupleInput in) { 38 ColumbaHeader header = new ColumbaHeader(); 39 40 header.getAttributes().put("columba.uid", new Integer (in.readInt())); 41 42 int compressedFlags = 0; 44 45 compressedFlags = in.readInt(); 46 47 for (int i = 0; i < CachedHeaderfields.INTERNAL_COMPRESSED_HEADERFIELDS.length; i++) { 48 header.set(CachedHeaderfields.INTERNAL_COMPRESSED_HEADERFIELDS[i], 49 BooleanCompressor.decompress(compressedFlags, i)); 50 } 51 52 String [] columnNames = CachedHeaderfields.INTERNAL_HEADERFIELDS; 54 Class [] columnTypes = CachedHeaderfields.INTERNAL_HEADERFIELDS_TYPE; 55 56 for (int j = 0; j < columnNames.length; j++) { 57 Object value = null; 58 59 if (columnTypes[j] == Integer .class) { 60 value = new Integer (in.readInt()); 61 } else if (columnTypes[j] == Date .class) { 62 value = new Date (in.readLong()); 63 } else if (columnTypes[j] == Color .class) { 64 value = ColorFactory.getColor(in.readInt()); 65 } else if (columnTypes[j] == Address.class) { 66 try { 67 value = AddressParser.parseAddress(in.readString()); 68 } catch (IndexOutOfBoundsException e) { 69 } catch (IllegalArgumentException e) { 70 } catch (ParserException e) { 71 } finally { 72 if (value == null) 73 value = ""; 74 } 75 } else 76 value = in.readString(); 77 78 if (value != null) { 79 header.set(columnNames[j], value); 80 } 81 } 82 83 columnNames = CachedHeaderfields.getDefaultHeaderfields(); 85 86 for (int j = 0; j < columnNames.length; j++) { 87 String value = in.readString(); 88 if (value != null) { 89 header.set(columnNames[j], value); 90 } 91 } 92 93 return header; 94 } 95 96 public void objectToEntry(Object arg0, TupleOutput out) { 97 IColumbaHeader header = (IColumbaHeader) arg0; 98 99 out.writeInt(((Integer )header.getAttributes().get("columba.uid")).intValue()); 100 101 Boolean [] b = new Boolean [CachedHeaderfields.INTERNAL_COMPRESSED_HEADERFIELDS.length]; 103 104 for (int i = 0; i < b.length; i++) { 105 b[i] = (Boolean ) header 106 .get(CachedHeaderfields.INTERNAL_COMPRESSED_HEADERFIELDS[i]); 107 108 if (b[i] == null) { 110 b[i] = Boolean.FALSE; 111 } 112 } 113 114 out.writeInt(new Integer (BooleanCompressor.compress(b)).intValue()); 115 116 String [] columnNames = CachedHeaderfields.INTERNAL_HEADERFIELDS; 118 Class [] columnTypes = CachedHeaderfields.INTERNAL_HEADERFIELDS_TYPE; 119 Object o; 120 121 for (int j = 0; j < columnNames.length; j++) { 122 o = header.get(columnNames[j]); 123 124 126 if (columnTypes[j] == Integer .class) 127 out.writeInt(((Integer ) o).intValue()); 128 else if (columnTypes[j] == Date .class) { 129 out.writeLong(((Date ) o).getTime()); 130 } else if (columnTypes[j] == Color .class) { 131 out.writeInt(((Color ) o).getRGB()); 132 } else if (columnTypes[j] == Address.class) { 133 if (o instanceof Address) 134 out.writeString(((Address) o).toString()); 135 else 136 out.writeString((String ) o); 137 } else 138 out.writeString(o.toString()); 139 } 140 141 columnNames = CachedHeaderfields.getDefaultHeaderfields(); 143 144 for (int j = 0; j < columnNames.length; j++) { 145 String v = (String ) header.get(columnNames[j]); 146 if ( v==null) v = ""; 147 out.writeString(v); 148 } 149 150 151 } 152 153 } 154 | Popular Tags |