001: package com.sun.addressbook.wabp;
002:
003: import com.sun.addressbook.ABStoreException;
004: import com.sun.addressbook.Element;
005: import com.sun.addressbook.Entry;
006: import com.sun.addressbook.Group;
007:
008: import com.iplanet.xslui.xslutil.XMLDOMBuilder;
009: import com.iplanet.xslui.xslutil.XPathTools;
010: import com.iplanet.xslui.xslutil.XMLProcessingException;
011: import com.iplanet.xslui.xslutil.XSLProcessingException;
012:
013: import org.w3c.dom.Document;
014: import java.io.ByteArrayInputStream;
015:
016: /*
017: * This class is responsible for generating an XML document that conforms
018: * to the Web Address Book Protocol (wabp).
019: *
020: */
021: public class WabpXmlUtil {
022:
023: private Entry abEntry = null;
024: private Element abElement = null;
025:
026: /*
027: * Constructor for Entry
028: */
029: public WabpXmlUtil(Entry entry) {
030: this .abEntry = entry;
031: this .abElement = (Element) entry;
032: }
033:
034: /*
035: * Constructor for Group
036: */
037: public WabpXmlUtil(Group group) {
038: this .abElement = (Element) group;
039: }
040:
041: public StringBuffer getAbPersonXml() {
042: StringBuffer buf = new StringBuffer();
043:
044: buf.append(getWabpXmlStart());
045: buf.append(getWabpXmlAbPersonStart());
046: buf.append(getWabpXmlMemberOfGroup());
047: buf.append(getWabpXmlEntryElement());
048: buf.append(getWabpXmlPersonElement());
049: buf.append(getWabpXmlPhone());
050: buf.append(getWabpXmlEmail());
051: buf.append(getWabpXmlPostalAddressElement("home"));
052: buf.append(getWabpXmlPostalAddressElement("work"));
053: buf.append(getWabpXmlUriElement());
054: buf.append(getWabpXmlAbPersonEnd());
055:
056: return buf;
057: }
058:
059: public StringBuffer getGroupXml() {
060: StringBuffer buf = new StringBuffer();
061:
062: buf.append(getWabpXmlStart());
063: buf.append(getWabpXmlGroupStart());
064: buf.append(getWabpXmlEntryElement());
065: buf.append(getWabpXmlGroupEnd());
066:
067: return buf;
068: }
069:
070: public String getWabpXmlStart() {
071: return new String(
072: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
073: }
074:
075: public String getWabpXmlAbPersonStart() {
076: return new String("<abperson>\n");
077: }
078:
079: public String getWabpXmlAbPersonEnd() {
080: return new String("</abperson>\n");
081: }
082:
083: public String getWabpXmlEntryStart() {
084: return new String("<entry>\n");
085: }
086:
087: public String getWabpXmlEntryEnd() {
088: return new String("</entry>\n");
089: }
090:
091: public String getWabpXmlPersonStart() {
092: return new String("<person>\n");
093: }
094:
095: public String getWabpXmlPersonEnd() {
096: return new String("</person>\n");
097: }
098:
099: public String getWabpXmlGroupStart() {
100: return new String("<group>\n");
101: }
102:
103: public String getWabpXmlGroupEnd() {
104: return new String("</group>\n");
105: }
106:
107: public String getWabpXmlMemberOfGroup() {
108: StringBuffer buf = new StringBuffer();
109: String val = abEntry.getMemberofpabgroup();
110:
111: if (val != null) {
112: buf.append("<memberofgroup>");
113: buf.append(val);
114: buf.append("</memberofgroup>");
115: buf.append("\n");
116: }
117:
118: return buf.toString();
119: }
120:
121: public String getWabpXmlEntryElement() {
122: StringBuffer buf = new StringBuffer();
123: String cn = getWabpXmlDisplayName();
124: String desc = getWabpXmlDescription();
125:
126: if ((cn.length() > 0) || (desc.length() > 0)) {
127: buf.append(getWabpXmlEntryStart());
128: buf.append(cn);
129: buf.append(desc);
130: buf.append(getWabpXmlEntryEnd());
131: }
132:
133: return buf.toString();
134: }
135:
136: /*
137: * This method is responsible for constructing the <person> element
138: */
139: public String getWabpXmlPersonElement() {
140: StringBuffer buf = new StringBuffer();
141: String fn = getWabpXmlGivenName();
142: String ln = getWabpXmlSurName();
143: String dob = getWabpXmlDateBirthday();
144:
145: if ((fn.length() > 0) || (ln.length() > 0)
146: || (dob.length() > 0)) {
147: buf.append(getWabpXmlPersonStart());
148: buf.append(fn);
149: buf.append(ln);
150: buf.append(dob);
151: buf.append(getWabpXmlPersonEnd());
152: }
153:
154: return buf.toString();
155: }
156:
157: public String getWabpXmlDisplayName() {
158: StringBuffer buf = new StringBuffer();
159: String val = abElement.getCn();
160:
161: if (val != null) {
162: buf.append("<displayname>");
163: buf.append(val);
164: buf.append("</displayname>");
165: buf.append("\n");
166: }
167:
168: return buf.toString();
169: }
170:
171: public String getWabpXmlDescription() {
172: StringBuffer buf = new StringBuffer();
173: String val = abElement.getDescription();
174:
175: if (val != null) {
176: buf.append("<description>");
177: buf.append(val);
178: buf.append("</description>");
179: buf.append("\n");
180: }
181:
182: return buf.toString();
183: }
184:
185: public String getWabpXmlGivenName() {
186: StringBuffer buf = new StringBuffer();
187: String val = abEntry.getFn();
188:
189: if (val != null) {
190: buf.append("<givenname>");
191: buf.append(val);
192: buf.append("</givenname>");
193: buf.append("\n");
194: }
195:
196: return buf.toString();
197: }
198:
199: public String getWabpXmlSurName() {
200: StringBuffer buf = new StringBuffer();
201: String val = abEntry.getLn();
202:
203: if (val != null) {
204: buf.append("<surname>");
205: buf.append(val);
206: buf.append("</surname>");
207: buf.append("\n");
208: }
209:
210: return buf.toString();
211: }
212:
213: public String getWabpXmlDateBirthday() {
214: StringBuffer buf = new StringBuffer();
215: String val = abEntry.getDob();
216:
217: if (val != null) {
218: buf.append("<date type=\"birthday\">");
219: buf.append(val);
220: buf.append("</date>");
221: buf.append("\n");
222: }
223:
224: return buf.toString();
225: }
226:
227: public String getWabpXmlPhone() {
228: StringBuffer buf = new StringBuffer();
229: String bp = abEntry.getBp();
230: String hp = abEntry.getHp();
231: String mp = abEntry.getMp();
232: String fp = abEntry.getFp();
233: String pp = abEntry.getPp();
234: int priority = 1;
235:
236: if (bp != null) {
237: buf.append("<phone priority=\"");
238: buf.append(priority);
239: buf.append("\" type=\"work\">");
240: buf.append(bp);
241: buf.append("</phone>");
242: buf.append("\n");
243: priority++;
244: }
245: if (hp != null) {
246: buf.append("<phone priority=\"");
247: buf.append(priority);
248: buf.append("\" type=\"home\">");
249: buf.append(hp);
250: buf.append("</phone>");
251: buf.append("\n");
252: priority++;
253: }
254: if (mp != null) {
255: buf.append("<phone priority=\"");
256: buf.append(priority);
257: buf.append("\" type=\"mobile\">");
258: buf.append(mp);
259: buf.append("</phone>");
260: buf.append("\n");
261: priority++;
262: }
263: if (pp != null) {
264: buf.append("<phone priority=\"");
265: buf.append(priority);
266: buf.append("\" type=\"pager\">");
267: buf.append(pp);
268: buf.append("</phone>");
269: buf.append("\n");
270: priority++;
271: }
272: if (fp != null) {
273: buf.append("<phone priority=\"");
274: buf.append(priority);
275: buf.append("\" type=\"fax\">");
276: buf.append(fp);
277: buf.append("</phone>");
278: buf.append("\n");
279: priority++;
280: }
281:
282: return buf.toString();
283: }
284:
285: public String getWabpXmlEmail() {
286: StringBuffer buf = new StringBuffer();
287: String em = abEntry.getEm();
288: String sms = abEntry.getSmsId();
289:
290: if (em != null) {
291: buf.append("<email priority=\"1\" type=\"work\">");
292: buf.append(em);
293: buf.append("</email>");
294: buf.append("\n");
295: }
296: if (sms != null) {
297: buf.append("<email priority=\"2\" type=\"sms\">");
298: buf.append(sms);
299: buf.append("</email>");
300: buf.append("\n");
301: }
302:
303: return buf.toString();
304: }
305:
306: public String getWabpXmlPostalAddressElement(String type) {
307: StringBuffer buf = new StringBuffer();
308: String street = null;
309: String city = null;
310: String state = null;
311: String zip = null;
312: String country = null;
313:
314: if ((type != null) && (type.equals("home"))) {
315: street = abEntry.getHomeStreet();
316: city = abEntry.getHomeCity();
317: state = abEntry.getHomeState();
318: zip = abEntry.getHomeZip();
319: country = abEntry.getHomeCountry();
320: } else {
321: street = abEntry.getBusinessStreet();
322: city = abEntry.getBusinessCity();
323: state = abEntry.getBusinessState();
324: zip = abEntry.getBusinessZip();
325: country = abEntry.getBusinessCountry();
326: }
327:
328: if ((street != null) || (city != null) || (state != null)
329: || (zip != null) || (country != null)) {
330:
331: buf.append("<postaladdress type=\"");
332: buf.append(type);
333: buf.append("\">\n");
334:
335: if (street != null) {
336: buf.append("<street>");
337: buf.append(street);
338: buf.append("</street>");
339: buf.append("\n");
340: }
341: if (city != null) {
342: buf.append("<city>");
343: buf.append(city);
344: buf.append("</city>");
345: buf.append("\n");
346: }
347: if (zip != null) {
348: buf.append("<postalcode>");
349: buf.append(zip);
350: buf.append("</postalcode>");
351: buf.append("\n");
352: }
353: if (state != null) {
354: buf.append("<state>");
355: buf.append(state);
356: buf.append("</state>");
357: buf.append("\n");
358: }
359: if (country != null) {
360: buf.append("<country>");
361: buf.append(country);
362: buf.append("</country>");
363: buf.append("\n");
364: }
365:
366: buf.append("</postaladdress>\n");
367: }
368:
369: return buf.toString();
370: }
371:
372: public String getWabpXmlUriElement() {
373: StringBuffer buf = new StringBuffer();
374: String val = abEntry.getUri();
375:
376: if (val != null) {
377: buf.append("<weburl priority=\"1\">\n");
378: buf.append("<urladdr>");
379: buf.append(val);
380: buf.append("</urladdr>");
381: buf.append("\n");
382: buf.append("</weburl>\n");
383: }
384:
385: return buf.toString();
386: }
387:
388: /*
389: *
390: *
391: * @param xml XML document in wabp format
392: * @param type the node type to retrieve, i.e. <abperson>, <group>
393: *
394: */
395: public static org.w3c.dom.Element getElementFromWabpXml(
396: StringBuffer xml, String type) throws ABStoreException {
397:
398: org.w3c.dom.Element element = null;
399: XMLDOMBuilder builder = null;
400: Document document = null;
401:
402: try {
403: builder = new XMLDOMBuilder();
404: document = builder.newDocument();
405: document = builder.parse(new ByteArrayInputStream(xml
406: .toString().getBytes()), false);
407: element = (org.w3c.dom.Element) XPathTools
408: .getFirstNodeByXPath(document, type);
409:
410: } catch (XMLProcessingException xme) {
411: throw new ABStoreException(
412: "WabpAddressBook: failed to parse wabp xml "
413: + "document. " + xme);
414: } catch (XSLProcessingException xse) {
415: throw new ABStoreException(
416: "WabpAddressBook: could not find element " + type
417: + "in wabp xml document. " + xse);
418: }
419:
420: return element;
421: }
422:
423: }
|