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.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    }