KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > store > LocalXMLFileStore


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.calendar.store;
19
20 import java.io.File JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.logging.Logger JavaDoc;
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     /** JDK 1.4+ logging framework logger, used for logging. */
37     @SuppressWarnings JavaDoc("unused")
38     private static final Logger JavaDoc LOG = Logger
39             .getLogger("org.columba.calendar.store");
40     private File JavaDoc directory;
41
42     public LocalXMLFileStore(File JavaDoc directory) throws StoreException {
43         super();
44
45         if (directory == null)
46             throw new IllegalArgumentException JavaDoc("directory == null");
47
48         this.directory = directory;
49
50         DiskIO.ensureDirectory(directory);
51
52     }
53
54     public Document load(Object JavaDoc id) throws StoreException {
55         if (id == null)
56             throw new IllegalArgumentException JavaDoc("uuid == null");
57
58         File JavaDoc file = getFile(id);
59
60         SAXBuilder builder = new SAXBuilder();
61         // builder.setValidation(true);
62
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 JavaDoc e) {
70             throw new StoreException(e);
71         }
72
73         return doc;
74     }
75
76     public void save(Object JavaDoc id, Document document) throws StoreException {
77         if (id == null)
78             throw new IllegalArgumentException JavaDoc("id == null");
79         if (document == null)
80             throw new IllegalArgumentException JavaDoc("document == null");
81
82         // FIXME check if the id is a correct file name!
83
File JavaDoc file = getFile(id);
84
85         XMLOutputter outp = new XMLOutputter();
86
87         try {
88             FileOutputStream JavaDoc s = new FileOutputStream JavaDoc(file);
89             outp.output(document, s);
90             s.close();
91         } catch (FileNotFoundException JavaDoc e) {
92             throw new StoreException(e);
93         } catch (IOException JavaDoc e) {
94             throw new StoreException(e);
95         }
96
97     }
98
99     public void modify(Object JavaDoc id, Document document) throws StoreException {
100         if (id == null)
101             throw new IllegalArgumentException JavaDoc("id == null");
102         if (document == null)
103             throw new IllegalArgumentException JavaDoc("document == null");
104
105         save(id, document);
106
107     }
108
109     public void remove(Object JavaDoc id) throws StoreException {
110         if (id == null)
111             throw new IllegalArgumentException JavaDoc("id == null");
112
113         File JavaDoc file = getFile(id);
114         file.delete();
115     }
116
117     /**
118      * @param uid
119      * @return file
120      */

121     private File JavaDoc getFile(Object JavaDoc id) throws StoreException {
122
123         if (id == null)
124             throw new IllegalArgumentException JavaDoc("id == null");
125
126         File JavaDoc file = new File JavaDoc(directory.toString() + File.separator
127                 + (id.toString()) + ".xcs");
128
129         return file;
130     }
131
132     public boolean exists(Object JavaDoc uid) throws StoreException {
133
134         if (uid == null)
135             throw new IllegalArgumentException JavaDoc("uid == null");
136
137         return (getFile(uid) != null);
138     }
139
140     public Iterator JavaDoc iterator() throws StoreException {
141         return new StoreIterator();
142     }
143
144     class StoreIterator implements Iterator JavaDoc {
145
146         private File JavaDoc[] 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 JavaDoc next() {
161             
162             Document document = null;
163             
164             // filename = "uuid.xcs"
165
while (files[nextIndex].getName().indexOf(".") == -1) {
166                 nextIndex++;
167             }
168             
169             String JavaDoc filename = files[nextIndex].getName();
170
171             // remove ".xcs"
172
String JavaDoc 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 JavaDoc uid = files[nextIndex].getName();
182
183             LocalXMLFileStore.this.remove(uid);
184
185             nextIndex++;
186         }
187     }
188 }
189
Popular Tags