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