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