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.parser;
019:
020: import java.util.Iterator;
021: import java.util.Vector;
022:
023: import org.columba.addressbook.model.AddressModel;
024: import org.columba.addressbook.model.EmailModel;
025: import org.columba.addressbook.model.IEmailModel;
026: import org.columba.addressbook.model.InstantMessagingModel;
027: import org.columba.addressbook.model.PhoneModel;
028: import org.columba.addressbook.model.VCARD;
029: import org.jdom.CDATA;
030: import org.jdom.Document;
031: import org.jdom.Element;
032:
033: /**
034: * Wraps an xml document containing vCard data and offers many convienience
035: * methods to access vCard attributes.
036: * <p>
037: *
038: * @author fdietz
039: */
040: public class XMLContactDocumentParser {
041:
042: private Document doc;
043:
044: protected Element parentElement;
045:
046: private Element root;
047:
048: public XMLContactDocumentParser() {
049: doc = new Document();
050: root = new Element(VCARD.VCARD);
051: doc.addContent(root);
052:
053: parentElement = root;
054: }
055:
056: public XMLContactDocumentParser(Document document)
057: throws SyntaxException {
058: if (document == null)
059: throw new IllegalArgumentException("document == null");
060:
061: this .doc = document;
062:
063: this .root = doc.getRootElement();
064:
065: if (!root.getName().equalsIgnoreCase(VCARD.VCARD)) {
066: // wrong xml-format
067: throw new SyntaxException("Root element must be <vcard>!");
068: }
069:
070: parentElement = root;
071: }
072:
073: public Element getRootElement() {
074: return root;
075: }
076:
077: /**
078: * @return Returns the parentElement.
079: */
080: protected Element getParentElement() {
081: return parentElement;
082: }
083:
084: public void set(String key, String value) {
085: Element child = getParentElement().getChild(key);
086: if (child == null) {
087: child = new Element(key);
088: getParentElement().addContent(child);
089: }
090: child.setText(value);
091: }
092:
093: public void set(String key, String prefix, String value) {
094: Element child = getParentElement().getChild(key);
095: if (child == null) {
096: child = new Element(key);
097: getParentElement().addContent(child);
098: }
099: Element prefixchild = child.getChild(prefix);
100: if (prefixchild == null) {
101: prefixchild = new Element(prefix);
102: child.addContent(prefixchild);
103: }
104: prefixchild.setText(value);
105: }
106:
107: public String get(String key) {
108: Element child = getParentElement().getChild(key);
109: if (child == null) {
110: child = new Element(key);
111: getParentElement().addContent(child);
112: }
113: return child.getTextNormalize();
114: }
115:
116: public String get(String key, String prefix) {
117: Element child = getParentElement().getChild(key);
118: if (child == null) {
119: child = new Element(key);
120: getParentElement().addContent(child);
121: }
122: Element prefixchild = child.getChild(prefix);
123: if (prefixchild == null) {
124: prefixchild = new Element(prefix);
125: child.addContent(prefixchild);
126: }
127:
128: return prefixchild.getTextNormalize();
129: }
130:
131: public Document getDocument() {
132: return doc;
133: }
134:
135: /**
136: * @return Returns the id.
137: */
138: public String getId() {
139: return get(VCARD.ID);
140: }
141:
142: public void setId(String id) {
143: set(VCARD.ID, id);
144: }
145:
146: public void addEmail(IEmailModel model) {
147: // create <email> element, if it doesn't exist yet
148: Element child = getParentElement().getChild(VCARD.EMAIL);
149: if (child == null) {
150: child = new Element(VCARD.EMAIL);
151: getParentElement().addContent(child);
152: }
153:
154: // create <type> element
155: Element prefixchild = new Element(model.getTypeString());
156: child.addContent(prefixchild);
157:
158: prefixchild.setText(model.getAddress());
159: }
160:
161: public Iterator getEmailIterator() {
162: Element child = getParentElement().getChild(VCARD.EMAIL);
163: // if not specified return empty iterator
164: if (child == null)
165: return new Vector().iterator();
166:
167: Iterator it = child.getChildren().iterator();
168: Vector v = new Vector();
169: while (it.hasNext()) {
170: Element e = (Element) it.next();
171: v.add(new EmailModel(e.getValue(), e.getName()));
172: }
173:
174: return v.iterator();
175: }
176:
177: public Iterator getAddressIterator() {
178: Vector<AddressModel> v = new Vector<AddressModel>();
179:
180: Element child = getParentElement().getChild(VCARD.ADR);
181: // if not specified return empty iterator
182: if (child == null)
183: return v.iterator();
184:
185: Iterator it = child.getChildren().iterator();
186: // iterate over all type elements
187: while (it.hasNext()) {
188: Element typeElement = (Element) it.next();
189:
190: String poBox = "";
191: Element e1 = typeElement.getChild(VCARD.ADR_POSTOFFICEBOX);
192: if (e1 != null)
193: poBox = e1.getText();
194: String street = "";
195: Element e2 = typeElement.getChild(VCARD.ADR_STREETADDRESS);
196: if (e2 != null)
197: street = e2.getText();
198: String locality = "";
199: Element e3 = typeElement.getChild(VCARD.ADR_LOCALITY);
200: if (e3 != null)
201: locality = e3.getText();
202: String postalCode = "";
203: Element e4 = typeElement.getChild(VCARD.ADR_POSTALCODE);
204: if (e4 != null)
205: postalCode = e4.getText();
206: String region = "";
207: Element e5 = typeElement.getChild(VCARD.ADR_REGION);
208: if (e5 != null)
209: region = e5.getText();
210: String country = "";
211: Element e6 = typeElement.getChild(VCARD.ADR_COUNTRY);
212: if (e6 != null)
213: country = e6.getText();
214: String label = "";
215: Element e7 = typeElement.getChild(VCARD.LABEL);
216: if (e7 != null) {
217: if (e7.getContent() != null
218: && e7.getContent().size() > 0) {
219: CDATA cdata = (CDATA) e7.getContent().get(0);
220: if (cdata != null)
221: label = cdata.getText();
222: }
223: }
224:
225: v.add(new AddressModel(poBox, street, locality, postalCode,
226: region, country, label, typeElement.getName()));
227:
228: }
229:
230: return v.iterator();
231:
232: }
233:
234: public void addAddress(AddressModel m) {
235: // create <adr>, if it doesn't exist
236: Element child = getParentElement().getChild(VCARD.ADR);
237: if (child == null) {
238: child = new Element(VCARD.ADR);
239: getParentElement().addContent(child);
240: }
241:
242: // create <type> element
243: Element prefixchild = new Element(m.getTypeString());
244: child.addContent(prefixchild);
245:
246: Element poBoxElement = new Element(VCARD.ADR_POSTOFFICEBOX);
247: poBoxElement.setText(m.getPoBox());
248: prefixchild.addContent(poBoxElement);
249:
250: Element streetElement = new Element(VCARD.ADR_STREETADDRESS);
251: streetElement.setText(m.getStreet());
252: prefixchild.addContent(streetElement);
253:
254: Element cityElement = new Element(VCARD.ADR_LOCALITY);
255: cityElement.setText(m.getCity());
256: prefixchild.addContent(cityElement);
257:
258: Element zipPostalCodeElement = new Element(VCARD.ADR_POSTALCODE);
259: zipPostalCodeElement.setText(m.getZipPostalCode());
260: prefixchild.addContent(zipPostalCodeElement);
261:
262: Element stateProvinceCountyElement = new Element(
263: VCARD.ADR_REGION);
264: stateProvinceCountyElement.setText(m.getStateProvinceCounty());
265: prefixchild.addContent(stateProvinceCountyElement);
266:
267: Element countryElement = new Element(VCARD.ADR_COUNTRY);
268: countryElement.setText(m.getCountry());
269: prefixchild.addContent(countryElement);
270:
271: // create a CDATA section for the label
272: Element labelElement = new Element(VCARD.LABEL);
273: labelElement.addContent(new CDATA(m.getLabel()));
274: prefixchild.addContent(labelElement);
275:
276: }
277:
278: public Iterator getPhoneIterator() {
279: Element child = getParentElement().getChild(VCARD.TEL);
280: // if not specified return empty iterator
281: if (child == null)
282: return new Vector().iterator();
283:
284: Iterator it = child.getChildren().iterator();
285: Vector v = new Vector();
286: while (it.hasNext()) {
287: Element e = (Element) it.next();
288: v.add(new PhoneModel(e.getValue(), e.getName()));
289: }
290:
291: return v.iterator();
292: }
293:
294: public void addPhone(PhoneModel m) {
295: Element child = getParentElement().getChild(VCARD.TEL);
296: if (child == null) {
297: child = new Element(VCARD.TEL);
298: getParentElement().addContent(child);
299: }
300:
301: Element prefixchild = new Element(m.getTypeString());
302: child.addContent(prefixchild);
303:
304: prefixchild.setText(m.getNumber());
305:
306: }
307:
308: public Iterator getInstantMessagingIterator() {
309: Element child = getParentElement().getChild(VCARD.IM);
310: // if not specified return empty iterator
311: if (child == null)
312: return new Vector().iterator();
313:
314: Iterator it = child.getChildren().iterator();
315: Vector v = new Vector();
316: while (it.hasNext()) {
317: Element e = (Element) it.next();
318: v.add(new InstantMessagingModel(e.getValue(), e.getName()));
319: }
320:
321: return v.iterator();
322: }
323:
324: public void addInstantMessaging(InstantMessagingModel m) {
325: Element child = getParentElement().getChild(VCARD.IM);
326: if (child == null) {
327: child = new Element(VCARD.IM);
328: getParentElement().addContent(child);
329: }
330:
331: Element prefixchild = new Element(m.getTypeString());
332: child.addContent(prefixchild);
333:
334: prefixchild.setText(m.getUserId());
335:
336: }
337: }
|