001/* 002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * Vilogia - Mail address formatting 019 * 020 */ 021 022package org.nuxeo.ecm.platform.mail.jsf.converter; 023 024import java.util.regex.Matcher; 025import java.util.regex.Pattern; 026 027import javax.faces.component.UIComponent; 028import javax.faces.context.FacesContext; 029import javax.faces.convert.Converter; 030 031import static java.util.regex.Pattern.*; 032 033/** 034 * Simple mail address converter: most of the addresses imported from the POP3 or IMAP mailbox simply return the string 035 * "null <[email protected]>". To avoid a list of nulls this converter removes the "null" aliases and only keep 036 * the mail address. Also return a mailto: link to the sender. 037 * 038 * @author <a href="mailto:[email protected]">Christophe Capon</a> 039 */ 040public class MailAddressConverter implements Converter { 041 042 private static final String EMAIL_REGEXP = "(.*)<([A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4})>"; 043 044 private static final Pattern pattern = compile(EMAIL_REGEXP, CASE_INSENSITIVE); 045 046 public Object getAsObject(FacesContext ctx, UIComponent uiComp, String inStr) { 047 return inStr; 048 } 049 050 public String getAsString(FacesContext ctx, UIComponent uiComp, Object inObj) { 051 if (null == inObj) { 052 return null; 053 } 054 055 if (inObj instanceof String) { 056 String inStr = (String) inObj; 057 Matcher m = pattern.matcher(inStr); 058 059 if (m.matches()) { 060 String alias = m.group(1); 061 String email = m.group(2); 062 063 if (alias.trim().toLowerCase().equals("null")) { 064 alias = email; 065 } 066 067 return String.format("<a href=\"mailto:%s\">%s</a>", email, alias); 068 069 } else { 070 return inStr; 071 } 072 } else { 073 return inObj.toString(); 074 } 075 } 076 077}