KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > folder > headercache > DefaultHeaderBinding


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.folder.headercache;
19
20 import java.awt.Color JavaDoc;
21 import java.util.Date JavaDoc;
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 JavaDoc entryToObject(TupleInput in) {
38         ColumbaHeader header = new ColumbaHeader();
39
40         header.getAttributes().put("columba.uid", new Integer JavaDoc(in.readInt()));
41         
42         // load boolean headerfields, which are compressed in one int value
43
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         // load other internal headerfields, non-boolean type
53
String JavaDoc[] columnNames = CachedHeaderfields.INTERNAL_HEADERFIELDS;
54         Class JavaDoc[] columnTypes = CachedHeaderfields.INTERNAL_HEADERFIELDS_TYPE;
55
56         for (int j = 0; j < columnNames.length; j++) {
57             Object JavaDoc value = null;
58
59             if (columnTypes[j] == Integer JavaDoc.class) {
60                 value = new Integer JavaDoc(in.readInt());
61             } else if (columnTypes[j] == Date JavaDoc.class) {
62                 value = new Date JavaDoc(in.readLong());
63             } else if (columnTypes[j] == Color JavaDoc.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 JavaDoc e) {
69                 } catch (IllegalArgumentException JavaDoc 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         //load default headerfields, as defined in RFC822
84
columnNames = CachedHeaderfields.getDefaultHeaderfields();
85
86         for (int j = 0; j < columnNames.length; j++) {
87             String JavaDoc 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 JavaDoc arg0, TupleOutput out) {
97         IColumbaHeader header = (IColumbaHeader) arg0;
98         
99         out.writeInt(((Integer JavaDoc)header.getAttributes().get("columba.uid")).intValue());
100         
101         // save boolean headerfields, compressing them to one int value
102
Boolean JavaDoc[] b = new Boolean JavaDoc[CachedHeaderfields.INTERNAL_COMPRESSED_HEADERFIELDS.length];
103
104         for (int i = 0; i < b.length; i++) {
105             b[i] = (Boolean JavaDoc) header
106                     .get(CachedHeaderfields.INTERNAL_COMPRESSED_HEADERFIELDS[i]);
107
108             // if value doesn't exist, use false as default
109
if (b[i] == null) {
110                 b[i] = Boolean.FALSE;
111             }
112         }
113
114         out.writeInt(new Integer JavaDoc(BooleanCompressor.compress(b)).intValue());
115
116         // save other internal headerfields, of non-boolean type
117
String JavaDoc[] columnNames = CachedHeaderfields.INTERNAL_HEADERFIELDS;
118         Class JavaDoc[] columnTypes = CachedHeaderfields.INTERNAL_HEADERFIELDS_TYPE;
119         Object JavaDoc o;
120
121         for (int j = 0; j < columnNames.length; j++) {
122             o = header.get(columnNames[j]);
123
124             //System.out.println("type="+o.getClass());
125

126             if (columnTypes[j] == Integer JavaDoc.class)
127                 out.writeInt(((Integer JavaDoc) o).intValue());
128             else if (columnTypes[j] == Date JavaDoc.class) {
129                 out.writeLong(((Date JavaDoc) o).getTime());
130             } else if (columnTypes[j] == Color JavaDoc.class) {
131                 out.writeInt(((Color JavaDoc) 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 JavaDoc) o);
137             } else
138                 out.writeString(o.toString());
139         }
140
141         // save default headerfields, as defined in RFC822
142
columnNames = CachedHeaderfields.getDefaultHeaderfields();
143
144         for (int j = 0; j < columnNames.length; j++) {
145             String JavaDoc v = (String JavaDoc) header.get(columnNames[j]);
146             if ( v==null) v = "";
147             out.writeString(v);
148         }
149
150
151     }
152
153 }
154
Popular Tags