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