001    /**
002     * Copyright (c) 2000-present 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 As of 6.2.0, replaced by {@link
029     *             com.liferay.portal.format.USAPhoneNumberFormatImpl}
030     */
031    @Deprecated
032    public class USAPhoneNumberFormat implements PhoneNumberFormat {
033    
034            @Override
035            public String format(String phoneNumber) {
036                    if (Validator.isNull(phoneNumber)) {
037                            return StringPool.BLANK;
038                    }
039    
040                    if (phoneNumber.length() > 10) {
041                            StringBundler sb = new StringBundler(8);
042    
043                            sb.append(StringPool.OPEN_PARENTHESIS);
044                            sb.append(phoneNumber.substring(0, 3));
045                            sb.append(") ");
046                            sb.append(phoneNumber.substring(3, 6));
047                            sb.append(StringPool.DASH);
048                            sb.append(phoneNumber.substring(6, 10));
049                            sb.append(" x");
050                            sb.append(phoneNumber.substring(10));
051    
052                            return sb.toString();
053                    }
054                    else if (phoneNumber.length() == 10) {
055                            StringBundler sb = new StringBundler(6);
056    
057                            sb.append(StringPool.OPEN_PARENTHESIS);
058                            sb.append(phoneNumber.substring(0, 3));
059                            sb.append(") ");
060                            sb.append(phoneNumber.substring(3, 6));
061                            sb.append(StringPool.DASH);
062                            sb.append(phoneNumber.substring(6));
063    
064                            return sb.toString();
065                    }
066                    else if (phoneNumber.length() == 7) {
067                            return phoneNumber.substring(0, 3).concat(StringPool.DASH).concat(
068                                    phoneNumber.substring(3));
069                    }
070    
071                    return phoneNumber;
072            }
073    
074            @Override
075            public String strip(String phoneNumber) {
076                    return StringUtil.extractDigits(phoneNumber);
077            }
078    
079            @Override
080            public boolean validate(String phoneNumber) {
081                    if (Validator.isNull(phoneNumber)) {
082                            return false;
083                    }
084    
085                    return phoneNumber.matches(
086                            PropsUtil.get(PropsKeys.PHONE_NUMBER_FORMAT_USA_REGEXP));
087            }
088    
089    }