1 package org.columba.calendar.store; 19 20 import java.io.File ; 21 import java.io.FileNotFoundException ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.util.Iterator ; 25 import java.util.logging.Logger ; 26 27 import org.columba.calendar.store.api.StoreException; 28 import org.columba.core.io.DiskIO; 29 import org.jdom.Document; 30 import org.jdom.JDOMException; 31 import org.jdom.input.SAXBuilder; 32 import org.jdom.output.XMLOutputter; 33 34 public class LocalXMLFileStore { 35 36 37 @SuppressWarnings ("unused") 38 private static final Logger LOG = Logger 39 .getLogger("org.columba.calendar.store"); 40 private File directory; 41 42 public LocalXMLFileStore(File directory) throws StoreException { 43 super(); 44 45 if (directory == null) 46 throw new IllegalArgumentException ("directory == null"); 47 48 this.directory = directory; 49 50 DiskIO.ensureDirectory(directory); 51 52 } 53 54 public Document load(Object id) throws StoreException { 55 if (id == null) 56 throw new IllegalArgumentException ("uuid == null"); 57 58 File file = getFile(id); 59 60 SAXBuilder builder = new SAXBuilder(); 61 builder.setIgnoringElementContentWhitespace(true); 63 Document doc = null; 64 try { 65 doc = builder.build(file); 66 67 } catch (JDOMException e) { 68 throw new StoreException(e); 69 } catch (IOException e) { 70 throw new StoreException(e); 71 } 72 73 return doc; 74 } 75 76 public void save(Object id, Document document) throws StoreException { 77 if (id == null) 78 throw new IllegalArgumentException ("id == null"); 79 if (document == null) 80 throw new IllegalArgumentException ("document == null"); 81 82 File file = getFile(id); 84 85 XMLOutputter outp = new XMLOutputter(); 86 87 try { 88 FileOutputStream s = new FileOutputStream (file); 89 outp.output(document, s); 90 s.close(); 91 } catch (FileNotFoundException e) { 92 throw new StoreException(e); 93 } catch (IOException e) { 94 throw new StoreException(e); 95 } 96 97 } 98 99 public void modify(Object id, Document document) throws StoreException { 100 if (id == null) 101 throw new IllegalArgumentException ("id == null"); 102 if (document == null) 103 throw new IllegalArgumentException ("document == null"); 104 105 save(id, document); 106 107 } 108 109 public void remove(Object id) throws StoreException { 110 if (id == null) 111 throw new IllegalArgumentException ("id == null"); 112 113 File file = getFile(id); 114 file.delete(); 115 } 116 117 121 private File getFile(Object id) throws StoreException { 122 123 if (id == null) 124 throw new IllegalArgumentException ("id == null"); 125 126 File file = new File (directory.toString() + File.separator 127 + (id.toString()) + ".xcs"); 128 129 return file; 130 } 131 132 public boolean exists(Object uid) throws StoreException { 133 134 if (uid == null) 135 throw new IllegalArgumentException ("uid == null"); 136 137 return (getFile(uid) != null); 138 } 139 140 public Iterator iterator() throws StoreException { 141 return new StoreIterator(); 142 } 143 144 class StoreIterator implements Iterator { 145 146 private File [] files; 147 int nextIndex = 0; 148 149 StoreIterator() { 150 files = directory.listFiles(); 151 } 152 153 public boolean hasNext() { 154 if (nextIndex < files.length) 155 return true; 156 157 return false; 158 } 159 160 public Object next() { 161 162 Document document = null; 163 164 while (files[nextIndex].getName().indexOf(".") == -1) { 166 nextIndex++; 167 } 168 169 String filename = files[nextIndex].getName(); 170 171 String uid = filename.substring(0, filename.indexOf(".")); 173 document = load(uid); 174 175 nextIndex++; 176 return document; 177 178 } 179 180 public void remove() { 181 String uid = files[nextIndex].getName(); 182 183 LocalXMLFileStore.this.remove(uid); 184 185 nextIndex++; 186 } 187 } 188 } 189 | Popular Tags |