KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > web > format > EmailAddressFormatter


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: EmailAddressFormatter.java,v $
31  * Revision 1.3 2005/04/10 18:47:41 colinmacleod
32  * Changed i tag to em and b tag to strong.
33  *
34  * Revision 1.2 2005/04/09 17:19:59 colinmacleod
35  * Changed copyright text to GPL v2 explicitly.
36  *
37  * Revision 1.1.1.1 2005/03/10 17:51:36 colinmacleod
38  * Restructured ivata op around Hibernate/PicoContainer.
39  * Renamed ivata groupware.
40  *
41  * Revision 1.2 2004/11/03 16:10:11 colinmacleod
42  * Changed todo comments to TODO: all caps.
43  *
44  * Revision 1.1 2004/09/30 15:16:01 colinmacleod
45  * Split off addressbook elements into security subproject.
46  *
47  * Revision 1.4 2004/07/13 19:41:15 colinmacleod
48  * Moved project to POJOs from EJBs.
49  * Applied PicoContainer to services layer (replacing session EJBs).
50  * Applied Hibernate to persistence layer (replacing entity EJBs).
51  *
52  * Revision 1.3 2004/03/21 21:16:08 colinmacleod
53  * Shortened name to ivata op.
54  *
55  * Revision 1.2 2004/02/01 22:00:33 colinmacleod
56  * Added full names to author tags
57  *
58  * Revision 1.1.1.1 2004/01/27 20:57:56 colinmacleod
59  * Moved ivata openportal to SourceForge..
60  *
61  * Revision 1.3 2003/11/07 14:54:15 jano
62  * commitng after fixing some bugs
63  *
64  * Revision 1.2 2003/10/17 12:36:13 jano
65  * fixing problems with building
66  * converting intranet -> portal
67  * Eclipse building
68  *
69  * Revision 1.1.1.1 2003/10/13 20:50:09 colin
70  * Restructured portal into subprojects
71  *
72  * Revision 1.3 2003/03/07 02:19:53 colin
73  * fixed quotes, used onclick rather than javasciprt: href
74  *
75  * Revision 1.2 2003/03/04 14:20:58 colin
76  * changed popup width to 550
77  *
78  * Revision 1.1 2003/02/24 19:33:32 colin
79  * moved to jsp
80  *
81  * Revision 1.6 2003/02/18 11:12:52 colin
82  * converted to using popups
83  *
84  * Revision 1.5 2003/02/04 17:43:46 colin
85  * copyright notice
86  *
87  * Revision 1.4 2002/09/25 11:36:43 colin
88  * changed to make the email address formatter point at the
89  * webmail subsystem
90  *
91  * Revision 1.3 2002/09/09 13:47:11 peter
92  * *** empty log message ***
93  *
94  * Revision 1.2 2002/06/21 16:19:16 colin
95  * added constructors to allow importing of formats from another
96  * formatter.
97  *
98  * Revision 1.1 2002/06/21 11:58:37 colin
99  * restructured com.ivata.groupware.web into separate
100  * subcategories:
101  * fornat, javascript, theme and tree.
102  * -----------------------------------------------------------------------------
103  */

104 package com.ivata.groupware.web.format;
105
106
107 import java.util.Arrays JavaDoc;
108 import java.util.HashMap JavaDoc;
109 import java.util.List JavaDoc;
110 import java.util.ListIterator JavaDoc;
111 import java.util.Vector JavaDoc;
112
113 import javax.mail.Address JavaDoc;
114 import javax.mail.internet.InternetAddress JavaDoc;
115 import javax.servlet.jsp.PageContext JavaDoc;
116
117 import com.ivata.mask.web.format.HTMLFormatter;
118 import com.ivata.mask.web.format.URLFormat;
119 import com.ivata.mask.web.javascript.JavaScriptWindow;
120
121
122 /**
123  * <p>Convert email addresses into links against the ivata groupware
124  * email
125  * system. This class can either convert single addreses or lists into
126  * <code>HTML</code> anchors.</p>
127  *
128  * @since 2002-06-19
129  * @author Colin MacLeod
130  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
131  * @version $Revision: 1.3 $
132  *
133  */

134 public class EmailAddressFormatter extends HTMLFormatter {
135
136     /**
137      * <p>Stores the string used as a separator between email
138      * addresses.</p>
139      *
140      * @see #format
141      *
142      */

143     private String JavaDoc separator = ";";
144
145     /**
146      * <p>Whether or not the email address itself should appear in the
147      * link. If <code>false</code> the name of the person appears in the
148      * link, otherwise the email address itself will appear.</p>
149      */

150     private boolean showAddress = false;
151
152     /**
153      * <p>Stores the current page context. This is needed to create the
154      * <em>JavaScript</em> window.</p>
155      */

156     private PageContext JavaDoc pageContext;
157
158     /**
159      * <p>Format one email address as an anchor link. This expects the
160      * address to be
161      * formatted according to <a
162      * HREF='http://www.faqs.org/rfcs/rfc822.HTML'>RFC822</a>.</p>
163      *
164      * @param addressString the emailAddress string to format.
165      * @return a formatted email address linking to the ivata groupware
166      * email system.
167      */

168     public String JavaDoc format(final String JavaDoc addressString) {
169         // simple prerequisite: rubbish in -> rubbish out...
170
if (addressString == null) {
171             return null;
172         }
173         int startPosition, endPosition;
174         InternetAddress JavaDoc address;
175
176         // look for email address of the form "My Name" <my.name@provider.com>
177
// TODO: change these links to links to the intranet
178
try {
179             if (((startPosition = addressString.indexOf('<')) != -1) &&
180                 ((endPosition = addressString.indexOf('>')) != -1)) {
181                 address = new InternetAddress JavaDoc(addressString.substring(startPosition + 1, endPosition), addressString.substring(0, startPosition));
182             } else {
183                 address = new InternetAddress JavaDoc(addressString);
184             }
185             return format(address);
186         } catch (java.io.UnsupportedEncodingException JavaDoc e) {
187             throw new RuntimeException JavaDoc(e);
188         } catch (javax.mail.internet.AddressException JavaDoc e) {
189             throw new RuntimeException JavaDoc(e);
190         }
191     }
192
193     /**
194      * <p>Format an array of addresses as anchor links.</p>
195      *
196      * @param addresses the array of email adddresses to format.
197      * @return a separated list of email address linking to the
198      * ivata groupware email
199      * system. Each address is separated by the separator specified in
200      * {@link
201      * #setSeperator setSeperator}.
202      *
203      */

204     public String JavaDoc format(final Address JavaDoc[] addresses) {
205         // simple prerequisite: rubbish in -> rubbish out...
206
if (addresses == null) {
207             return null;
208         }
209         return format(Arrays.asList(addresses));
210     }
211
212     /**
213      * <p>Format a list of email addresses as anchor links.</p>
214      *
215      * @param addresses the list of email addresses to format.
216      * @return a separated list of email address linking to the
217      * ivata groupware email
218      * system. Each address is separated by the separator specified in
219      * {@link
220      * #setSeperator setSeperator}.
221      *
222      */

223     public String JavaDoc format(final List JavaDoc addresses) {
224         // simple prerequisite: rubbish in -> rubbish out...
225
if (addresses == null) {
226             return null;
227         }
228         ListIterator JavaDoc addressesIterator = addresses.listIterator();
229         String JavaDoc returnString = "";
230         URLFormat URLFormat = new URLFormat();
231
232         // go thro' all the emails and format them
233
while (addressesIterator.hasNext()) {
234             Object JavaDoc address = addressesIterator.next();
235             String JavaDoc addressString;
236
237             if ((address instanceof InternetAddress JavaDoc) || (address instanceof String JavaDoc)) {
238                 if (address instanceof InternetAddress JavaDoc) {
239                     InternetAddress JavaDoc addressInternet = (InternetAddress JavaDoc) address;
240                     String JavaDoc personalString;
241
242                     addressString = addressInternet.getAddress();
243                     if ((addressInternet.getPersonal() != null) &&
244                         !addressInternet.getPersonal().equals("")) {
245
246                         /* TODO: this code can be used to strip the quotes - not sure if this is needed
247                          StringBuffer personalStringBuffer = new StringBuffer(addressInternet.getPersonal());
248                          // strip leading and trailing quotes and spaces
249                          char ch;
250                          while((personalStringBuffer.length() > 0) &&
251                          ((ch = personalStringBuffer.charAt(0)) == '"' || ch==' ')) {
252                          personalStringBuffer.deleteCharAt(0);
253                          }
254                          int length = personalStringBuffer.length();
255                          while((--length > 0) &&
256                          ((ch = personalStringBuffer.charAt(length)) == '"' || ch==' ')) {
257                          personalStringBuffer.deleteCharAt(length);
258                          }
259                          personalString = personalStringBuffer.toString();*/

260                         personalString = addressInternet.getPersonal();
261                     } else {
262                         // if no name was given, use the address itself
263
personalString = addressInternet.getAddress();
264                     }
265                     // we can only format the link if the pageContext has been
266
// set
267
if (pageContext == null) {
268                         if (personalString.equals(addressString)) {
269                             addressString = personalString + " <" + addressString + ">";
270                         }
271                     } else {
272                         // NOTE: please keep this up to date with the code in
273
// /webapp/mail/include/composePopUp.jspf
274
JavaScriptWindow popUp = new JavaScriptWindow();
275                         HashMap JavaDoc composeParams = new HashMap JavaDoc();
276
277                         if (personalString.equals(addressString)) {
278                             composeParams.put("to", addressString);
279                         } else {
280                             // remove quotes from the personal string
281
StringBuffer JavaDoc to = new StringBuffer JavaDoc();
282
283                             /**
284                              * WE need quotes,
285                              *
286                             StringTokenizer st = new StringTokenizer(personalString, "\"");
287
288                             while (st.hasMoreElements()) {
289                                 to.append(st.nextElement());
290                             */

291                             to.append(" <");
292                             to.append(addressString);
293                             to.append(">");
294                             composeParams.put("to", to.toString());
295                         }
296                         popUp.setParams(composeParams);
297                         popUp.setWindowName("composeWindow");
298                         popUp.setFrameName("ivataCompose");
299                         popUp.setPage("/mail/compose.jsp");
300                         popUp.setHeight(540);
301                         popUp.setWidth(550);
302                         popUp.setHasScrollBars(false);
303                         popUp.setPageContext(pageContext);
304                         String JavaDoc tagBody = showAddress ? addressString : personalString;
305
306                         // convert spaces in the name string
307
addressString = "<a HREF='' onclick='"
308                                 + popUp.toString()
309                                 + "return false'>" + super.format(tagBody) + "</a>";
310                     }
311                 } else {
312                     addressString = format((String JavaDoc) address);
313                 }
314                 // can't convert non-internet addresses
315
} else {
316                 addressString = super.format(address.toString());
317             }
318             returnString += addressString;
319             // print a semicolon between all the addresses
320
if (addressesIterator.hasNext()) {
321                 returnString += separator + " ";
322             }
323         }
324         return returnString;
325     }
326
327     /**
328      * <p>Format a single address as an anchor link.</p>
329      *
330      * @param address the email Address to format.
331      * @return a formatted email address linking to the ivata groupware
332      * email system.
333      *
334      */

335     public String JavaDoc format(final Address JavaDoc address) {
336         // simple prerequisite: rubbish in -> rubbish out...
337
if (address == null) {
338             return null;
339         }
340         Address JavaDoc[ ] addresses = { address };
341
342         return format(Arrays.asList(addresses));
343     }
344
345     /**
346      * <p>Get the string used as a separator between email addresses.</p>
347      *
348      * @see #format
349      *
350      * @return the current value of the separator used between email
351      * addresses.
352      *
353      */

354     public final String JavaDoc getSeperator() {
355         return separator;
356     }
357
358     /**
359      * <p>Set the string used as a separator between email addresses.</p>
360      *
361      * @see #format
362      *
363      * @param separator the new value of the separator used between email
364      * addresses.
365      *
366      */

367     public final void setSeperator(final String JavaDoc separator) {
368         this.separator = separator;
369     }
370
371     /**
372      * <p>Default constructor.</p>
373      *
374      */

375     public EmailAddressFormatter() {
376         super();
377     }
378
379     /**
380      * <p>Construct a new email formatter, using all of the formats of
381      * another
382      * formatter.</p>
383      *
384      */

385     public EmailAddressFormatter(HTMLFormatter formatter) {
386         super();
387         // just set our formats to be the same as those of the formatter provided
388
setFormats(new Vector JavaDoc(formatter.getFormats()));
389     }
390
391     /**
392      * <p>Get whether or not the email address or the person's name should
393      * appear in the body of the link.</p>
394      *
395      * @return <code>false</code> if the name of the person will appear in
396      * the link, otherwise the email address itself will appear
397      */

398     public final boolean getShowAddress() {
399         return showAddress;
400     }
401
402     /**
403      * <p>Set whether or not the email address or the person's name should
404      * appear in the body of the link.</p>
405      *
406      * @param showAddress set to <code>false</code> if the name of the
407      * person should appear in the link, otherwise the email address
408      * itself
409      * will appear.
410      */

411     public final void setShowAddress(final boolean showAddress) {
412         this.showAddress = showAddress;
413     }
414
415     /**
416      * <p>Stores the current page context. This is needed to create the
417      * <em>JavaScript</em> window.</p>
418      *
419      * @return the current value of pageContext.
420      */

421     public final PageContext JavaDoc getPageContext() {
422         return pageContext;
423     }
424
425     /**
426      * <p>Stores the current page context. This is needed to create the
427      * <em>JavaScript</em> window.</p>
428      *
429      * @param pageContext the new value of pageContext.
430      */

431     public final void setPageContext(final PageContext JavaDoc pageContext) {
432         this.pageContext = pageContext;
433     }
434 }
435
Popular Tags