001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.folder.headercache;
019:
020: import java.awt.Color;
021: import java.util.Date;
022:
023: import org.columba.core.base.BooleanCompressor;
024: import org.columba.core.gui.base.ColorFactory;
025: import org.columba.mail.message.ColumbaHeader;
026: import org.columba.mail.message.IColumbaHeader;
027: import org.columba.ristretto.message.Address;
028: import org.columba.ristretto.parser.AddressParser;
029: import org.columba.ristretto.parser.ParserException;
030:
031: import com.sleepycat.bind.tuple.TupleBinding;
032: import com.sleepycat.bind.tuple.TupleInput;
033: import com.sleepycat.bind.tuple.TupleOutput;
034:
035: public class DefaultHeaderBinding extends TupleBinding {
036:
037: public Object entryToObject(TupleInput in) {
038: ColumbaHeader header = new ColumbaHeader();
039:
040: header.getAttributes().put("columba.uid",
041: new Integer(in.readInt()));
042:
043: // load boolean headerfields, which are compressed in one int value
044: int compressedFlags = 0;
045:
046: compressedFlags = in.readInt();
047:
048: for (int i = 0; i < CachedHeaderfields.INTERNAL_COMPRESSED_HEADERFIELDS.length; i++) {
049: header
050: .set(
051: CachedHeaderfields.INTERNAL_COMPRESSED_HEADERFIELDS[i],
052: BooleanCompressor.decompress(
053: compressedFlags, i));
054: }
055:
056: // load other internal headerfields, non-boolean type
057: String[] columnNames = CachedHeaderfields.INTERNAL_HEADERFIELDS;
058: Class[] columnTypes = CachedHeaderfields.INTERNAL_HEADERFIELDS_TYPE;
059:
060: for (int j = 0; j < columnNames.length; j++) {
061: Object value = null;
062:
063: if (columnTypes[j] == Integer.class) {
064: value = new Integer(in.readInt());
065: } else if (columnTypes[j] == Date.class) {
066: value = new Date(in.readLong());
067: } else if (columnTypes[j] == Color.class) {
068: value = ColorFactory.getColor(in.readInt());
069: } else if (columnTypes[j] == Address.class) {
070: try {
071: value = AddressParser.parseAddress(in.readString());
072: } catch (IndexOutOfBoundsException e) {
073: } catch (IllegalArgumentException e) {
074: } catch (ParserException e) {
075: } finally {
076: if (value == null)
077: value = "";
078: }
079: } else
080: value = in.readString();
081:
082: if (value != null) {
083: header.set(columnNames[j], value);
084: }
085: }
086:
087: //load default headerfields, as defined in RFC822
088: columnNames = CachedHeaderfields.getDefaultHeaderfields();
089:
090: for (int j = 0; j < columnNames.length; j++) {
091: String value = in.readString();
092: if (value != null) {
093: header.set(columnNames[j], value);
094: }
095: }
096:
097: return header;
098: }
099:
100: public void objectToEntry(Object arg0, TupleOutput out) {
101: IColumbaHeader header = (IColumbaHeader) arg0;
102:
103: out.writeInt(((Integer) header.getAttributes().get(
104: "columba.uid")).intValue());
105:
106: // save boolean headerfields, compressing them to one int value
107: Boolean[] b = new Boolean[CachedHeaderfields.INTERNAL_COMPRESSED_HEADERFIELDS.length];
108:
109: for (int i = 0; i < b.length; i++) {
110: b[i] = (Boolean) header
111: .get(CachedHeaderfields.INTERNAL_COMPRESSED_HEADERFIELDS[i]);
112:
113: // if value doesn't exist, use false as default
114: if (b[i] == null) {
115: b[i] = Boolean.FALSE;
116: }
117: }
118:
119: out.writeInt(new Integer(BooleanCompressor.compress(b))
120: .intValue());
121:
122: // save other internal headerfields, of non-boolean type
123: String[] columnNames = CachedHeaderfields.INTERNAL_HEADERFIELDS;
124: Class[] columnTypes = CachedHeaderfields.INTERNAL_HEADERFIELDS_TYPE;
125: Object o;
126:
127: for (int j = 0; j < columnNames.length; j++) {
128: o = header.get(columnNames[j]);
129:
130: //System.out.println("type="+o.getClass());
131:
132: if (columnTypes[j] == Integer.class)
133: out.writeInt(((Integer) o).intValue());
134: else if (columnTypes[j] == Date.class) {
135: out.writeLong(((Date) o).getTime());
136: } else if (columnTypes[j] == Color.class) {
137: out.writeInt(((Color) o).getRGB());
138: } else if (columnTypes[j] == Address.class) {
139: if (o instanceof Address)
140: out.writeString(((Address) o).toString());
141: else
142: out.writeString((String) o);
143: } else
144: out.writeString(o.toString());
145: }
146:
147: // save default headerfields, as defined in RFC822
148: columnNames = CachedHeaderfields.getDefaultHeaderfields();
149:
150: for (int j = 0; j < columnNames.length; j++) {
151: String v = (String) header.get(columnNames[j]);
152: if (v == null)
153: v = "";
154: out.writeString(v);
155: }
156:
157: }
158:
159: }
|