001    /**
002     * Copyright (c) 2000-2013 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            @Override
043            public Object getAsObject(
044                    FacesContext facesContext, UIComponent uiComponent, String value) {
045    
046                    if (value == null) {
047                            return null;
048                    }
049    
050                    StringBuilder integerChars = new StringBuilder(value.length());
051                    StringBuilder invalidChars = new StringBuilder(value.length());
052    
053                    for (int i = 0; i < value.length(); i++) {
054                            char curChar = value.charAt(i);
055    
056                            if (Character.isDigit(curChar)) {
057                                    integerChars.append(curChar);
058                            }
059                            else if ((curChar != '-') && (curChar != '(') &&
060                                             (curChar != ')') && (curChar != '.') &&
061                                             (curChar != '+') && (curChar != ' ')) {
062    
063                                    invalidChars.append(curChar);
064                            }
065                    }
066    
067                    if (invalidChars.length() > 0) {
068                            ExternalContext externalContext = facesContext.getExternalContext();
069    
070                            Locale locale = externalContext.getRequestLocale();
071    
072                            String summary = LanguageUtil.get(
073                                    locale, "the-following-are-invalid-characters");
074    
075                            summary += " " + invalidChars.toString();
076    
077                            FacesMessage facesMessage = new FacesMessage(
078                                    FacesMessage.SEVERITY_ERROR, summary, null);
079    
080                            throw new ConverterException(facesMessage);
081                    }
082                    else if (integerChars.length() == 10) {
083                            StringBuilder unitedStatesPhoneNumber = new StringBuilder(
084                                    _unitedStatesFormat.length());
085    
086                            int integerDigitIndex = 0;
087    
088                            for (int i = 0; i < _unitedStatesFormat.length(); i++) {
089                                    char curChar = _unitedStatesFormat.charAt(i);
090    
091                                    if (curChar == '#') {
092                                            unitedStatesPhoneNumber.append(
093                                                    integerChars.charAt(integerDigitIndex++));
094                                    }
095                                    else {
096                                            unitedStatesPhoneNumber.append(curChar);
097                                    }
098                            }
099    
100                            return unitedStatesPhoneNumber.toString();
101                    }
102    
103                    return value;
104            }
105    
106            @Override
107            public String getAsString(
108                            FacesContext facesContext, UIComponent uiComponent, Object value)
109                    throws ConverterException {
110    
111                    // ICE-1537
112    
113                    return (String)value;
114            }
115    
116            public String getUnitedStatesFormat() {
117                    return _unitedStatesFormat;
118            }
119    
120            @Override
121            public boolean isTransient() {
122                    return _transient;
123            }
124    
125            @Override
126            public void restoreState(FacesContext facesContext, Object obj) {
127                    Object[] values = (Object[])obj;
128    
129                    _unitedStatesFormat = (String)values[0];
130            }
131    
132            @Override
133            public Object saveState(FacesContext facesContext) {
134                    Object[] values = new Object[1];
135    
136                    values[0] = _unitedStatesFormat;
137    
138                    return values;
139            }
140    
141            @Override
142            public void setTransient(boolean value) {
143                    _transient = value;
144            }
145    
146            public void setUnitedStatesFormat(String unitedStatesFormat) {
147                    _unitedStatesFormat = unitedStatesFormat;
148            }
149    
150            private boolean _transient;
151            private String _unitedStatesFormat;
152    
153    }