1 21 22 package org.armedbear.j.mail; 23 24 import java.io.BufferedWriter ; 25 import java.io.IOException ; 26 import java.io.OutputStreamWriter ; 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 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 ; 39 import org.xml.sax.ContentHandler ; 40 import org.xml.sax.InputSource ; 41 import org.xml.sax.SAXException ; 42 import org.xml.sax.XMLReader ; 43 import org.xml.sax.helpers.DefaultHandler ; 44 45 public final class MailboxProperties 46 { 47 private static final int MAX_ENTRIES = 100; 48 49 private static final String lineSeparator = 50 System.getProperty("line.separator"); 51 52 private static ArrayList 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 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 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 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 (); 118 File file = getFile(); 119 if (file.isFile()) { 120 XMLReader xmlReader = Utilities.getDefaultXMLReader(); 121 if (xmlReader != null) { 122 try { 123 xmlReader.setContentHandler(new Handler ()); 124 InputSource inputSource = 125 new InputSource (file.getInputStream()); 126 xmlReader.parse(inputSource); 127 } 128 catch (Exception e) { 129 Log.error(e); 130 } 131 } 132 } 133 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 writer = 152 new BufferedWriter (new OutputStreamWriter (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 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 getVersion() 179 { 180 return "1"; 181 } 182 183 private static class Entry 184 { 185 final String name; 186 final PropertyList properties; 187 long when; 188 189 Entry(String name, PropertyList properties) 190 { 191 this.name = name; 192 this.properties = properties; 193 when = System.currentTimeMillis(); 194 } 195 196 Entry(String name) 197 { 198 this.name = name; 199 properties = new PropertyList(); 200 } 201 202 String 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 it = properties.keyIterator(); 214 if (it != null) { 215 while (it.hasNext()) { 216 Property property = (Property) it.next(); 217 Object 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 propertyToXml(String name, String 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 (sb.toString()); 238 } 239 } 240 241 private static class Handler extends DefaultHandler implements ContentHandler 242 { 243 private Entry currentEntry = null; 244 245 public void startElement(String uri, String localName, String qName, 246 Attributes attributes) throws SAXException 247 { 248 if (localName.equals("mailboxes") || qName.equals("mailboxes")) { 249 String version = attributes.getValue("version"); 250 if (!version.equals(MailboxProperties.getVersion())) 251 throw new SAXException ("Unknown mailbox history format"); 252 } else if (localName.equals("mailbox") || qName.equals("mailbox")) { 253 String mailboxName = attributes.getValue("name"); 255 currentEntry = new Entry(mailboxName); 256 try { 257 currentEntry.when = 258 Long.parseLong(attributes.getValue("when")); 259 } 260 catch (NumberFormatException e) { 261 Log.error(e); 262 } 263 } else if (localName.equals("property") || qName.equals("property")) { 264 Debug.assertTrue(currentEntry != null); 265 String key = attributes.getValue("name"); 266 if (key != null) { 267 String 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 uri, String localName, String qName) 279 { 280 if (localName.equals("mailbox") || qName.equals("mailbox")) { 281 MailboxProperties.add(currentEntry); 282 currentEntry = null; 283 } 284 } 285 } 286 } 287 | Popular Tags |