KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > MailboxProperties


1 /*
2  * MailboxProperties.java
3  *
4  * Copyright (C) 2002-2003 Peter Graves
5  * $Id: MailboxProperties.java,v 1.4 2003/06/25 18:36:45 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import java.io.BufferedWriter JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.OutputStreamWriter JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import org.armedbear.j.Debug;
30 import org.armedbear.j.Directories;
31 import org.armedbear.j.Editor;
32 import org.armedbear.j.FastStringBuffer;
33 import org.armedbear.j.File;
34 import org.armedbear.j.Log;
35 import org.armedbear.j.Property;
36 import org.armedbear.j.PropertyList;
37 import org.armedbear.j.Utilities;
38 import org.xml.sax.Attributes JavaDoc;
39 import org.xml.sax.ContentHandler JavaDoc;
40 import org.xml.sax.InputSource JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42 import org.xml.sax.XMLReader JavaDoc;
43 import org.xml.sax.helpers.DefaultHandler JavaDoc;
44
45 public final class MailboxProperties
46 {
47     private static final int MAX_ENTRIES = 100;
48
49     private static final String JavaDoc lineSeparator =
50         System.getProperty("line.separator");
51
52     private static ArrayList JavaDoc list;
53
54     private MailboxProperties()
55     {
56     }
57
58     public static synchronized PropertyList getProperties(MailboxURL url)
59     {
60         if (list == null)
61             initialize();
62         String JavaDoc name = url.getCanonicalName();
63         for (int i = list.size()-1; i >= 0; i--) {
64             Entry entry = (Entry) list.get(i);
65             if (entry.name.equals(name))
66                 return entry.properties;
67         }
68         return null;
69     }
70
71     public static synchronized void saveProperties(Mailbox mb)
72     {
73         MailboxURL url = mb.getUrl();
74         if (url == null) {
75             Debug.bug();
76             return;
77         }
78         String JavaDoc name = url.getCanonicalName();
79         if (name == null) {
80             Debug.bug();
81             return;
82         }
83         if (name.length() == 0) {
84             Debug.bug();
85             return;
86         }
87         PropertyList properties = mb.getProperties();
88         if (properties == null || properties.size() == 0) {
89             Log.debug("MailboxProperties.saveProperties no properties set");
90             return;
91         }
92         if (list == null)
93             initialize();
94         Entry newEntry = new Entry(name, properties);
95         final int limit = list.size();
96         for (int i = 0; i < limit; i++) {
97             Entry entry = (Entry) list.get(i);
98             if (entry.name.equals(name)) {
99                 if (i == 0) {
100                     list.set(0, newEntry);
101                     save();
102                     return;
103                 }
104                 list.remove(i);
105                 break;
106             }
107         }
108         // Add new entry.
109
list.add(0, newEntry);
110         save();
111     }
112
113     private static synchronized void initialize()
114     {
115         if (list == null) {
116             Editor.protect(MailboxProperties.class);
117             list = new ArrayList JavaDoc();
118             File file = getFile();
119             if (file.isFile()) {
120                 XMLReader JavaDoc xmlReader = Utilities.getDefaultXMLReader();
121                 if (xmlReader != null) {
122                     try {
123                         xmlReader.setContentHandler(new Handler JavaDoc());
124                         InputSource JavaDoc inputSource =
125                             new InputSource JavaDoc(file.getInputStream());
126                         xmlReader.parse(inputSource);
127                     }
128                     catch (Exception JavaDoc e) {
129                         Log.error(e);
130                     }
131                 }
132             }
133             // Delete old mailboxes.xml in ~/.j (if any).
134
File oldFile =
135                 File.getInstance(Directories.getEditorDirectory(),
136                     "mailboxes.xml");
137             if (oldFile != null && oldFile.isFile())
138                 oldFile.delete();
139         }
140     }
141
142     private static void add(Entry entry)
143     {
144         list.add(entry);
145     }
146
147     private static synchronized void save()
148     {
149         try {
150             File file = getFile();
151             BufferedWriter JavaDoc writer =
152                 new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(file.getOutputStream()));
153             writer.write("<?xml version=\"1.0\"?>");
154             writer.newLine();
155             writer.write("<mailboxes version=\"" + getVersion() + "\">");
156             writer.newLine();
157             final int limit = Math.min(list.size(), MAX_ENTRIES);
158             for (int i = 0; i < limit; i++) {
159                 Entry entry = (Entry) list.get(i);
160                 writer.write(entry.toXml());
161                 writer.newLine();
162             }
163             writer.write("</mailboxes>");
164             writer.flush();
165             writer.close();
166         }
167         catch (IOException JavaDoc e) {
168             Log.error(e);
169         }
170     }
171
172     private static final File getFile()
173     {
174         return File.getInstance(Directories.getMailDirectory(),
175             "mailboxes.xml");
176     }
177
178     private static final String JavaDoc getVersion()
179     {
180         return "1";
181     }
182
183     private static class Entry
184     {
185         final String JavaDoc name;
186         final PropertyList properties;
187         long when;
188
189         Entry(String JavaDoc name, PropertyList properties)
190         {
191             this.name = name;
192             this.properties = properties;
193             when = System.currentTimeMillis();
194         }
195
196         Entry(String JavaDoc name)
197         {
198             this.name = name;
199             properties = new PropertyList();
200         }
201
202         String JavaDoc toXml()
203         {
204             FastStringBuffer sb = new FastStringBuffer(" <mailbox name=\"");
205             sb.append(name);
206             sb.append("\"");
207             sb.append(" when=\"");
208             sb.append(String.valueOf(when));
209             sb.append("\"");
210             sb.append(">");
211             sb.append(lineSeparator);
212             if (properties != null) {
213                 Iterator JavaDoc it = properties.keyIterator();
214                 if (it != null) {
215                     while (it.hasNext()) {
216                         Property property = (Property) it.next();
217                         Object JavaDoc value = properties.getProperty(property);
218                         if (value != null) {
219                             sb.append(propertyToXml(property.getDisplayName(),
220                                 value.toString()));
221                         }
222                     }
223                 }
224             }
225             sb.append(" </mailbox>");
226             return sb.toString();
227         }
228
229         private static String JavaDoc propertyToXml(String JavaDoc name, String JavaDoc value)
230         {
231             FastStringBuffer sb = new FastStringBuffer(" <property name=\"");
232             sb.append(name);
233             sb.append("\" value=\"");
234             sb.append(value);
235             sb.append("\"/>");
236             sb.append(lineSeparator);
237             return new String JavaDoc(sb.toString());
238         }
239     }
240
241     private static class Handler extends DefaultHandler JavaDoc implements ContentHandler JavaDoc
242     {
243         private Entry currentEntry = null;
244
245         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
246             Attributes JavaDoc attributes) throws SAXException JavaDoc
247         {
248             if (localName.equals("mailboxes") || qName.equals("mailboxes")) {
249                 String JavaDoc version = attributes.getValue("version");
250                 if (!version.equals(MailboxProperties.getVersion()))
251                     throw new SAXException JavaDoc("Unknown mailbox history format");
252             } else if (localName.equals("mailbox") || qName.equals("mailbox")) {
253                 // Start a new entry.
254
String JavaDoc mailboxName = attributes.getValue("name");
255                 currentEntry = new Entry(mailboxName);
256                 try {
257                     currentEntry.when =
258                         Long.parseLong(attributes.getValue("when"));
259                 }
260                 catch (NumberFormatException JavaDoc e) {
261                     Log.error(e);
262                 }
263             } else if (localName.equals("property") || qName.equals("property")) {
264                 Debug.assertTrue(currentEntry != null);
265                 String JavaDoc key = attributes.getValue("name");
266                 if (key != null) {
267                     String JavaDoc value = attributes.getValue("value");
268                     if (value != null) {
269                         Property property = Property.findProperty(key);
270                         if (property != null)
271                             currentEntry.properties.setPropertyFromString(property,
272                                 value);
273                     }
274                 }
275             }
276         }
277
278         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
279         {
280             if (localName.equals("mailbox") || qName.equals("mailbox")) {
281                 MailboxProperties.add(currentEntry);
282                 currentEntry = null;
283             }
284         }
285     }
286 }
287
Popular Tags