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.util.format;
016    
017    import com.liferay.portal.kernel.format.PhoneNumberFormat;
018    import com.liferay.portal.kernel.util.PropsKeys;
019    import com.liferay.portal.kernel.util.PropsUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    
025    /**
026     * @author     Brian Wing Shun Chan
027     * @author     Manuel de la Peña
028     * @deprecated {@link com.liferay.portal.format.USAPhoneNumberFormatImpl}
029     */
030    public class USAPhoneNumberFormat implements PhoneNumberFormat {
031    
032            public String format(String phoneNumber) {
033                    if (Validator.isNull(phoneNumber)) {
034                            return StringPool.BLANK;
035                    }
036    
037                    if (phoneNumber.length() > 10) {
038                            StringBundler sb = new StringBundler(8);
039    
040                            sb.append(StringPool.OPEN_PARENTHESIS);
041                            sb.append(phoneNumber.substring(0, 3));
042                            sb.append(") ");
043                            sb.append(phoneNumber.substring(3, 6));
044                            sb.append(StringPool.DASH);
045                            sb.append(phoneNumber.substring(6, 10));
046                            sb.append(" x");
047                            sb.append(phoneNumber.substring(10));
048    
049                            return sb.toString();
050                    }
051                    else if (phoneNumber.length() == 10) {
052                            StringBundler sb = new StringBundler(6);
053    
054                            sb.append(StringPool.OPEN_PARENTHESIS);
055                            sb.append(phoneNumber.substring(0, 3));
056                            sb.append(") ");
057                            sb.append(phoneNumber.substring(3, 6));
058                            sb.append(StringPool.DASH);
059                            sb.append(phoneNumber.substring(6));
060    
061                            return sb.toString();
062                    }
063                    else if (phoneNumber.length() == 7) {
064                            return phoneNumber.substring(0, 3).concat(StringPool.DASH).concat(
065                                    phoneNumber.substring(3));
066                    }
067    
068                    return phoneNumber;
069            }
070    
071            public String strip(String phoneNumber) {
072                    return StringUtil.extractDigits(phoneNumber);
073            }
074    
075            public boolean validate(String phoneNumber) {
076                    if (Validator.isNull(phoneNumber)) {
077                            return false;
078                    }
079    
080                    return phoneNumber.matches(
081                            PropsUtil.get(PropsKeys.PHONE_NUMBER_FORMAT_USA_REGEXP));
082            }
083    
084    }