001    /**
002     * Copyright (c) 2000-2010 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.util.format;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class USAPhoneNumberFormat implements PhoneNumberFormat {
025    
026            public String format(String phoneNumber) {
027                    if (phoneNumber == null) {
028                            return StringPool.BLANK;
029                    }
030    
031                    if (phoneNumber.length() > 10) {
032                            StringBundler sb = new StringBundler(8);
033    
034                            sb.append(StringPool.OPEN_PARENTHESIS);
035                            sb.append(phoneNumber.substring(0, 3));
036                            sb.append(") ");
037                            sb.append(phoneNumber.substring(3, 6));
038                            sb.append(StringPool.DASH);
039                            sb.append(phoneNumber.substring(6, 10));
040                            sb.append(" x");
041                            sb.append(phoneNumber.substring(10));
042    
043                            return sb.toString();
044                    }
045                    else if (phoneNumber.length() == 10) {
046                            StringBundler sb = new StringBundler(6);
047    
048                            sb.append(StringPool.OPEN_PARENTHESIS);
049                            sb.append(phoneNumber.substring(0, 3));
050                            sb.append(") ");
051                            sb.append(phoneNumber.substring(3, 6));
052                            sb.append(StringPool.DASH);
053                            sb.append(phoneNumber.substring(6));
054    
055                            return sb.toString();
056                    }
057                    else if (phoneNumber.length() == 7) {
058                            return phoneNumber.substring(0, 3).concat(StringPool.DASH).concat(
059                                    phoneNumber.substring(3));
060                    }
061                    else {
062                            return phoneNumber;
063                    }
064            }
065    
066            public String strip(String phoneNumber) {
067                    return StringUtil.extractDigits(phoneNumber);
068            }
069    
070    }