001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.calendar.store;
019:
020: import java.io.File;
021: import java.io.FileNotFoundException;
022: import java.io.FileOutputStream;
023: import java.io.IOException;
024: import java.util.Iterator;
025: import java.util.logging.Logger;
026:
027: import org.columba.calendar.store.api.StoreException;
028: import org.columba.core.io.DiskIO;
029: import org.jdom.Document;
030: import org.jdom.JDOMException;
031: import org.jdom.input.SAXBuilder;
032: import org.jdom.output.XMLOutputter;
033:
034: public class LocalXMLFileStore {
035:
036: /** JDK 1.4+ logging framework logger, used for logging. */
037: @SuppressWarnings("unused")
038: private static final Logger LOG = Logger
039: .getLogger("org.columba.calendar.store");
040: private File directory;
041:
042: public LocalXMLFileStore(File directory) throws StoreException {
043: super ();
044:
045: if (directory == null)
046: throw new IllegalArgumentException("directory == null");
047:
048: this .directory = directory;
049:
050: DiskIO.ensureDirectory(directory);
051:
052: }
053:
054: public Document load(Object id) throws StoreException {
055: if (id == null)
056: throw new IllegalArgumentException("uuid == null");
057:
058: File file = getFile(id);
059:
060: SAXBuilder builder = new SAXBuilder();
061: // builder.setValidation(true);
062: builder.setIgnoringElementContentWhitespace(true);
063: Document doc = null;
064: try {
065: doc = builder.build(file);
066:
067: } catch (JDOMException e) {
068: throw new StoreException(e);
069: } catch (IOException e) {
070: throw new StoreException(e);
071: }
072:
073: return doc;
074: }
075:
076: public void save(Object id, Document document)
077: throws StoreException {
078: if (id == null)
079: throw new IllegalArgumentException("id == null");
080: if (document == null)
081: throw new IllegalArgumentException("document == null");
082:
083: // FIXME check if the id is a correct file name!
084: File file = getFile(id);
085:
086: XMLOutputter outp = new XMLOutputter();
087:
088: try {
089: FileOutputStream s = new FileOutputStream(file);
090: outp.output(document, s);
091: s.close();
092: } catch (FileNotFoundException e) {
093: throw new StoreException(e);
094: } catch (IOException e) {
095: throw new StoreException(e);
096: }
097:
098: }
099:
100: public void modify(Object id, Document document)
101: throws StoreException {
102: if (id == null)
103: throw new IllegalArgumentException("id == null");
104: if (document == null)
105: throw new IllegalArgumentException("document == null");
106:
107: save(id, document);
108:
109: }
110:
111: public void remove(Object id) throws StoreException {
112: if (id == null)
113: throw new IllegalArgumentException("id == null");
114:
115: File file = getFile(id);
116: file.delete();
117: }
118:
119: /**
120: * @param uid
121: * @return file
122: */
123: private File getFile(Object id) throws StoreException {
124:
125: if (id == null)
126: throw new IllegalArgumentException("id == null");
127:
128: File file = new File(directory.toString() + File.separator
129: + (id.toString()) + ".xcs");
130:
131: return file;
132: }
133:
134: public boolean exists(Object uid) throws StoreException {
135:
136: if (uid == null)
137: throw new IllegalArgumentException("uid == null");
138:
139: return (getFile(uid) != null);
140: }
141:
142: public Iterator iterator() throws StoreException {
143: return new StoreIterator();
144: }
145:
146: class StoreIterator implements Iterator {
147:
148: private File[] files;
149: int nextIndex = 0;
150:
151: StoreIterator() {
152: files = directory.listFiles();
153: }
154:
155: public boolean hasNext() {
156: if (nextIndex < files.length)
157: return true;
158:
159: return false;
160: }
161:
162: public Object next() {
163:
164: Document document = null;
165:
166: // filename = "uuid.xcs"
167: while (files[nextIndex].getName().indexOf(".") == -1) {
168: nextIndex++;
169: }
170:
171: String filename = files[nextIndex].getName();
172:
173: // remove ".xcs"
174: String uid = filename.substring(0, filename.indexOf("."));
175: document = load(uid);
176:
177: nextIndex++;
178: return document;
179:
180: }
181:
182: public void remove() {
183: String uid = files[nextIndex].getName();
184:
185: LocalXMLFileStore.this.remove(uid);
186:
187: nextIndex++;
188: }
189: }
190: }
|