001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.format;
016    
017    import com.liferay.portal.kernel.format.PhoneNumberFormat;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.util.PropsValues;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     * @author Manuel de la Peña
027     */
028    public class USAPhoneNumberFormatImpl implements PhoneNumberFormat {
029    
030            public String format(String phoneNumber) {
031                    if (Validator.isNull(phoneNumber)) {
032                            return StringPool.BLANK;
033                    }
034    
035                    if (phoneNumber.length() > 10) {
036                            StringBundler sb = new StringBundler(8);
037    
038                            sb.append(StringPool.OPEN_PARENTHESIS);
039                            sb.append(phoneNumber.substring(0, 3));
040                            sb.append(") ");
041                            sb.append(phoneNumber.substring(3, 6));
042                            sb.append(StringPool.DASH);
043                            sb.append(phoneNumber.substring(6, 10));
044                            sb.append(" x");
045                            sb.append(phoneNumber.substring(10));
046    
047                            return sb.toString();
048                    }
049                    else if (phoneNumber.length() == 10) {
050                            StringBundler sb = new StringBundler(6);
051    
052                            sb.append(StringPool.OPEN_PARENTHESIS);
053                            sb.append(phoneNumber.substring(0, 3));
054                            sb.append(") ");
055                            sb.append(phoneNumber.substring(3, 6));
056                            sb.append(StringPool.DASH);
057                            sb.append(phoneNumber.substring(6));
058    
059                            return sb.toString();
060                    }
061                    else if (phoneNumber.length() == 7) {
062                            return phoneNumber.substring(0, 3).concat(StringPool.DASH).concat(
063                                    phoneNumber.substring(3));
064                    }
065    
066                    return phoneNumber;
067            }
068    
069            public String strip(String phoneNumber) {
070                    return StringUtil.extractDigits(phoneNumber);
071            }
072    
073            public boolean validate(String phoneNumber) {
074                    if (Validator.isNull(phoneNumber)) {
075                            return false;
076                    }
077    
078                    return phoneNumber.matches(PropsValues.PHONE_NUMBER_FORMAT_USA_REGEXP);
079            }
080    
081    }