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.addressbook.folder;
019:
020: import java.io.File;
021: import java.io.IOException;
022:
023: import org.columba.addressbook.model.ContactModelXMLFactory;
024: import org.columba.addressbook.model.IContactModel;
025: import org.columba.addressbook.parser.SyntaxException;
026: import org.columba.api.exception.StoreException;
027: import org.columba.core.xml.XmlNewIO;
028: import org.jdom.Document;
029:
030: /**
031: * Datastorage implementation using a MH-style mailbox approach with one folder
032: * containing one XML file per contact.
033: *
034: * @author fdietz
035: *
036: */
037: public class XmlDataStorage implements DataStorage {
038:
039: private AbstractFolder folder;
040:
041: /**
042: *
043: */
044: public XmlDataStorage(AbstractFolder folder) {
045: super ();
046:
047: this .folder = folder;
048: }
049:
050: /**
051: * @see org.columba.addressbook.folder.DataStorage#load(java.lang.Object)
052: */
053: public IContactModel load(String uid) throws StoreException {
054: File file = getFile(uid);
055:
056: Document doc = XmlNewIO.load(file);
057:
058: if (doc == null)
059: return null;
060:
061: IContactModel model;
062: try {
063: model = ContactModelXMLFactory.unmarshall(doc, uid);
064: } catch (SyntaxException e) {
065: throw new StoreException(e);
066: }
067:
068: return model;
069: }
070:
071: /**
072: * @param uid
073: * @return
074: */
075: private File getFile(String uid) throws StoreException {
076: File file = new File(folder.getDirectoryFile().toString() + "/"
077: + uid + ".xml");
078: return file;
079: }
080:
081: /**
082: * @see org.columba.addressbook.folder.DataStorage#save(java.lang.Object,
083: * IContactModel)
084: */
085: public void save(String uid, IContactModel contact)
086: throws StoreException {
087: File file = getFile(uid);
088:
089: Document doc;
090: try {
091: doc = ContactModelXMLFactory.marshall(contact);
092: } catch (SyntaxException e) {
093: throw new StoreException(e);
094: }
095:
096: try {
097: XmlNewIO.save(doc, file);
098: } catch (IOException e) {
099: throw new StoreException(e);
100: }
101:
102: }
103:
104: /**
105: * @see org.columba.addressbook.folder.DataStorage#modify(java.lang.Object,
106: * IContactModel)
107: */
108: public void modify(String uid, IContactModel contact)
109: throws StoreException {
110: save(uid, contact);
111:
112: }
113:
114: /**
115: * @see org.columba.addressbook.folder.DataStorage#remove(java.lang.Object)
116: */
117: public void remove(String uid) throws StoreException {
118: File file = getFile(uid);
119: file.delete();
120:
121: }
122:
123: }
|