KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > addressbook > folder > XmlDataStorage


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.addressbook.folder;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 import org.columba.addressbook.model.ContactModelXMLFactory;
24 import org.columba.addressbook.model.IContactModel;
25 import org.columba.addressbook.parser.SyntaxException;
26 import org.columba.api.exception.StoreException;
27 import org.columba.core.xml.XmlNewIO;
28 import org.jdom.Document;
29
30 /**
31  * Datastorage implementation using a MH-style mailbox approach with one folder
32  * containing one XML file per contact.
33  *
34  * @author fdietz
35  *
36  */

37 public class XmlDataStorage implements DataStorage {
38
39     private AbstractFolder folder;
40
41     /**
42      *
43      */

44     public XmlDataStorage(AbstractFolder folder) {
45         super();
46
47         this.folder = folder;
48     }
49
50     /**
51      * @see org.columba.addressbook.folder.DataStorage#load(java.lang.Object)
52      */

53     public IContactModel load(String JavaDoc uid) throws StoreException {
54         File JavaDoc file = getFile(uid);
55
56         Document doc = XmlNewIO.load(file);
57
58         if (doc == null)
59             return null;
60
61         IContactModel model;
62         try {
63             model = ContactModelXMLFactory.unmarshall(doc, uid);
64         } catch (SyntaxException e) {
65             throw new StoreException(e);
66         }
67
68         return model;
69     }
70
71     /**
72      * @param uid
73      * @return
74      */

75     private File JavaDoc getFile(String JavaDoc uid) throws StoreException {
76         File JavaDoc file = new File JavaDoc(folder.getDirectoryFile().toString() + "/" + uid
77                 + ".xml");
78         return file;
79     }
80
81     /**
82      * @see org.columba.addressbook.folder.DataStorage#save(java.lang.Object,
83      * IContactModel)
84      */

85     public void save(String JavaDoc uid, IContactModel contact) throws StoreException {
86         File JavaDoc file = getFile(uid);
87
88         Document doc;
89         try {
90             doc = ContactModelXMLFactory.marshall(contact);
91         } catch (SyntaxException e) {
92             throw new StoreException(e);
93         }
94
95         try {
96             XmlNewIO.save(doc, file);
97         } catch (IOException JavaDoc e) {
98             throw new StoreException(e);
99         }
100
101     }
102
103     /**
104      * @see org.columba.addressbook.folder.DataStorage#modify(java.lang.Object,
105      * IContactModel)
106      */

107     public void modify(String JavaDoc uid, IContactModel contact) throws StoreException {
108         save(uid, contact);
109
110     }
111
112     /**
113      * @see org.columba.addressbook.folder.DataStorage#remove(java.lang.Object)
114      */

115     public void remove(String JavaDoc uid) throws StoreException {
116         File JavaDoc file = getFile(uid);
117         file.delete();
118
119     }
120
121 }
Popular Tags