001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.faces.converter;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    
019    import java.util.Locale;
020    
021    import javax.faces.application.FacesMessage;
022    import javax.faces.component.StateHolder;
023    import javax.faces.component.UIComponent;
024    import javax.faces.context.ExternalContext;
025    import javax.faces.context.FacesContext;
026    import javax.faces.convert.Converter;
027    import javax.faces.convert.ConverterException;
028    
029    /**
030     * <p>
031     * This class is a JSF converter that can be used to convert phone numbers.
032     * Since phone numbers in the United States of America always have 10 digits,
033     * this converter provides automatic conversion of 10 digit phone numbers to a
034     * desired format. The format is specified by the unitedStatesFormat component
035     * attribute.
036     * </p>
037     *
038     * @author Neil Griffin
039     */
040    public class PhoneNumberConverter implements Converter, StateHolder {
041    
042            public boolean isTransient() {
043                    return _transient;
044            }
045    
046            public void setTransient(boolean value) {
047                    _transient = value;
048            }
049    
050            public String getUnitedStatesFormat() {
051                    return _unitedStatesFormat;
052            }
053    
054            public void setUnitedStatesFormat(String unitedStatesFormat) {
055                    _unitedStatesFormat = unitedStatesFormat;
056            }
057    
058            public Object getAsObject(
059                    FacesContext facesContext, UIComponent uiComponent, String value) {
060    
061                    if (value != null) {
062                            StringBuilder integerChars = new StringBuilder(value.length());
063                            StringBuilder invalidChars = new StringBuilder(value.length());
064    
065                            for (int i = 0; i < value.length(); i++) {
066                                    char curChar = value.charAt(i);
067    
068                                    if (Character.isDigit(curChar)) {
069                                            integerChars.append(curChar);
070                                    }
071                                    else if ((curChar != '-') && (curChar != '(') &&
072                                                     (curChar != ')') && (curChar != '.') &&
073                                                     (curChar != '+') && (curChar != ' ')) {
074    
075                                            invalidChars.append(curChar);
076                                    }
077                            }
078    
079                            if (invalidChars.length() > 0) {
080                                    ExternalContext externalContext =
081                                            facesContext.getExternalContext();
082    
083                                    Locale locale = externalContext.getRequestLocale();
084    
085                                    String summary = LanguageUtil.get(
086                                            locale, "the-following-are-invalid-characters");
087    
088                                    summary += " " + invalidChars.toString();
089    
090                                    FacesMessage facesMessage = new FacesMessage(
091                                            FacesMessage.SEVERITY_ERROR, summary, null);
092    
093                                    throw new ConverterException(facesMessage);
094                            }
095                            else if ((integerChars.length() == 10)) {
096                                    StringBuilder unitedStatesPhoneNumber = new StringBuilder(
097                                            _unitedStatesFormat.length());
098    
099                                    int integerDigitIndex = 0;
100    
101                                    for (int i = 0; i < _unitedStatesFormat.length(); i++) {
102                                            char curChar = _unitedStatesFormat.charAt(i);
103    
104                                            if (curChar == '#') {
105                                                    unitedStatesPhoneNumber.append(
106                                                            integerChars.charAt(integerDigitIndex++));
107                                            }
108                                            else {
109                                                    unitedStatesPhoneNumber.append(curChar);
110                                            }
111                                    }
112    
113                                    return unitedStatesPhoneNumber.toString();
114                            }
115                    }
116    
117                    return value;
118            }
119    
120            public String getAsString(
121                    FacesContext facesContext, UIComponent uiComponent, Object value)
122                    throws ConverterException {
123    
124                    // ICE-1537
125    
126                    return (String)value;
127            }
128    
129            public void restoreState(FacesContext facesContext, Object obj) {
130                    Object[] values = (Object[])obj;
131    
132                    _unitedStatesFormat = (String)values[0];
133            }
134    
135            public Object saveState(FacesContext facesContext) {
136                    Object[] values = new Object[1];
137    
138                    values[0] = _unitedStatesFormat;
139    
140                    return values;
141            }
142    
143            private boolean _transient;
144            private String _unitedStatesFormat;
145    
146    }