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.model;
019:
020: import java.util.Date;
021: import java.util.Iterator;
022:
023: import javax.swing.Icon;
024: import javax.swing.ImageIcon;
025:
026: import org.columba.addressbook.parser.ParserUtil;
027: import org.columba.addressbook.parser.SyntaxException;
028: import org.columba.addressbook.parser.XMLContactDocumentParser;
029: import org.jdom.Document;
030:
031: /**
032: * Factory creates xml document from contact model or the other way around.
033: *
034: * @author fdietz
035: */
036: public class ContactModelXMLFactory {
037:
038: public static Document marshall(IContactModel contactModel)
039: throws SyntaxException {
040:
041: if (contactModel == null)
042: throw new IllegalArgumentException("contactModel == null");
043:
044: XMLContactDocumentParser parser = new XMLContactDocumentParser();
045: parser.setId(contactModel.getId());
046:
047: // <sortstring>value</sortstring>
048: if (contactModel.getSortString() != null)
049: parser.set(VCARD.SORTSTRING, contactModel.getSortString());
050:
051: // <fn>value</fn>
052: if (contactModel.getFormattedName() != null)
053: parser.set(VCARD.FN, contactModel.getFormattedName());
054: // <nickname>value</nickname>
055: if (contactModel.getNickName() != null)
056: parser.set(VCARD.NICKNAME, contactModel.getNickName());
057:
058: // <n>
059: // <family>value</family>
060: // <given>value</given>
061: // <prefix>value</prefix>
062: // <suffix>value</suffix>
063: // <additionalnames>value</additionalnames>
064: // </n>
065: if (contactModel.getFamilyName() != null)
066: parser.set(VCARD.N, VCARD.N_FAMILY, contactModel
067: .getFamilyName());
068: if (contactModel.getGivenName() != null)
069: parser.set(VCARD.N, VCARD.N_GIVEN, contactModel
070: .getGivenName());
071: if (contactModel.getNamePrefix() != null)
072: parser.set(VCARD.N, VCARD.N_PREFIX, contactModel
073: .getNamePrefix());
074: if (contactModel.getNameSuffix() != null)
075: parser.set(VCARD.N, VCARD.N_SUFFIX, contactModel
076: .getNameSuffix());
077: if (contactModel.getAdditionalNames() != null)
078: parser.set(VCARD.N, VCARD.N_ADDITIONALNAMES, contactModel
079: .getAdditionalNames());
080:
081: // <url>value</url>
082: if (contactModel.getHomePage() != null)
083: parser.set(VCARD.URL, contactModel.getHomePage());
084:
085: if (contactModel.getWeblog() != null)
086: parser.set(VCARD.X_COLUMBA_URL_BLOG, contactModel
087: .getWeblog());
088: if (contactModel.getFreeBusy() != null)
089: parser.set(VCARD.X_COLUMBA_URL_FREEBUSY, contactModel
090: .getFreeBusy());
091: if (contactModel.getCalendar() != null)
092: parser.set(VCARD.X_COLUMBA_URL_CALENDAR, contactModel
093: .getCalendar());
094:
095: // <org>value</org>
096: parser.set(VCARD.ORG, contactModel.getOrganisation());
097: // <department>value</department>
098: parser.set(VCARD.X_COLUMBA_DEPARTMENT, contactModel
099: .getDepartment());
100: // <office>value</office>
101: parser.set(VCARD.X_COLUMBA_OFFICE, contactModel.getOffice());
102:
103: if (contactModel.getProfession() != null)
104: parser.set(VCARD.ROLE, contactModel.getProfession());
105: if (contactModel.getTitle() != null)
106: parser.set(VCARD.TITLE, contactModel.getTitle());
107:
108: Date date = contactModel.getBirthday();
109: if (date != null) {
110: long time = date.getTime();
111: parser.set(VCARD.BDAY, new Long(time).toString());
112: }
113:
114: // <email>
115: // <work>value</work>
116: // <home>value</home>
117: // </email>
118: Iterator it = contactModel.getEmailIterator();
119: while (it.hasNext()) {
120: IEmailModel m = ((IEmailModel) it.next());
121: parser.addEmail(m);
122: }
123:
124: // <im>
125: // <aol>value</aol>
126: // <yahoo>value</yahoo>
127: // <jabber>value</jabber>
128: // </im>
129: it = contactModel.getInstantMessagingIterator();
130: while (it.hasNext()) {
131: InstantMessagingModel m = ((InstantMessagingModel) it
132: .next());
133: parser.addInstantMessaging(m);
134: }
135:
136: // <phone>
137: // <business>value</business>
138: // <home>value</home>
139: // </phone>
140: it = contactModel.getPhoneIterator();
141: while (it.hasNext()) {
142: PhoneModel m = (PhoneModel) it.next();
143: parser.addPhone(m);
144: }
145:
146: // <adr>
147: // <work>
148: // <pobox>value</pobox>
149: // <street>value</street
150: // ..
151: // </work>
152: // ..
153: // </adr>
154: it = contactModel.getAddressIterator();
155: while (it.hasNext()) {
156: AddressModel m = (AddressModel) it.next();
157: parser.addAddress(m);
158: }
159:
160: if (contactModel.getNote() != null)
161: parser.set(VCARD.NOTE, contactModel.getNote());
162:
163: // base64 encode photo byte[] to string
164: ImageIcon image = contactModel.getPhoto();
165: if (image != null)
166: parser.set(VCARD.PHOTO, ParserUtil
167: .createBase64StringFromImage(image));
168:
169: // comma-separated category list
170: // <category>value1,value2,value3</category>
171: if (contactModel.getCategory() != null)
172: parser.set(VCARD.CATEGORY, contactModel.getCategory());
173:
174: return parser.getDocument();
175:
176: }
177:
178: public static IContactModel unmarshall(Document document, String id)
179: throws SyntaxException {
180:
181: if (document == null)
182: throw new IllegalArgumentException("document == null");
183: if (id == null)
184: throw new IllegalArgumentException("id == null");
185:
186: XMLContactDocumentParser parser = new XMLContactDocumentParser(
187: document);
188:
189: ContactModel model = new ContactModel(id);
190:
191: model.setSortString(parser.get(VCARD.SORTSTRING));
192: // compatibility - using SORTSTRING internally
193: if (model.getSortString() == null) {
194: model.setSortString(parser.get(VCARD.DISPLAYNAME));
195: }
196:
197: model.setFormattedName(parser.get(VCARD.FN));
198: model.setNickName(parser.get(VCARD.NICKNAME));
199:
200: model.setFamilyName(parser.get(VCARD.N, VCARD.N_FAMILY));
201: model.setGivenName(parser.get(VCARD.N, VCARD.N_GIVEN));
202: model.setNamePrefix(parser.get(VCARD.N, VCARD.N_PREFIX));
203: model.setNameSuffix(parser.get(VCARD.N, VCARD.N_SUFFIX));
204: model.setAdditionalNames(parser.get(VCARD.N,
205: VCARD.N_ADDITIONALNAMES));
206:
207: model.setHomePage(parser.get(VCARD.URL));
208: model.setWeblog(parser.get(VCARD.X_COLUMBA_URL_BLOG));
209: model.setFreeBusy(parser.get(VCARD.X_COLUMBA_URL_FREEBUSY));
210: model.setCalendar(parser.get(VCARD.X_COLUMBA_URL_CALENDAR));
211:
212: try {
213: String datestring = parser.get(VCARD.BDAY);
214: model.setBirthday(new Date(Long.parseLong(datestring)));
215: } catch (NumberFormatException e) {
216: model.setBirthday(null);
217: }
218:
219: model.setOrganisation(parser.get(VCARD.ORG));
220: model.setDepartment(parser.get(VCARD.X_COLUMBA_DEPARTMENT));
221: model.setOffice(parser.get(VCARD.X_COLUMBA_OFFICE));
222:
223: model.setProfession(parser.get(VCARD.ROLE));
224: model.setTitle(parser.get(VCARD.TITLE));
225:
226: Iterator it = parser.getEmailIterator();
227: while (it.hasNext()) {
228: model.addEmail((IEmailModel) it.next());
229: }
230:
231: it = parser.getPhoneIterator();
232: while (it.hasNext()) {
233: model.addPhone((PhoneModel) it.next());
234: }
235:
236: it = parser.getInstantMessagingIterator();
237: while (it.hasNext()) {
238: model
239: .addInstantMessaging((InstantMessagingModel) it
240: .next());
241: }
242:
243: it = parser.getAddressIterator();
244: while (it.hasNext()) {
245: model.addAddress((AddressModel) it.next());
246: }
247:
248: model.setNote(parser.get(VCARD.NOTE));
249:
250: String imageString = parser.get(VCARD.PHOTO);
251: ImageIcon image = ParserUtil
252: .createImageFromBase64String(imageString);
253: model.setPhoto(image);
254:
255: // comma-separated category list
256: // <category>value1,value2,value3</category>
257: model.setCategory(parser.get(VCARD.CATEGORY));
258:
259: return model;
260: }
261: }
|