| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| PhoneNumberFormatter |
|
| 5.0;5 |
| 1 | /** | |
| 2 | * Copyright 2005-2011 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.opensource.org/licenses/ecl2.php | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | // begin Kuali Foundation modification | |
| 17 | package org.kuali.rice.core.web.format; | |
| 18 | // end Kuali Foundation modification | |
| 19 | ||
| 20 | // begin Kuali Foundation modification | |
| 21 | import org.kuali.rice.core.api.util.RiceKeyConstants; | |
| 22 | ||
| 23 | ||
| 24 | /** | |
| 25 | * begin Kuali Foundation modification | |
| 26 | * This class is used to format phone number objects. | |
| 27 | * end Kuali Foundation modification | |
| 28 | */ | |
| 29 | 0 | public class PhoneNumberFormatter extends Formatter { |
| 30 | // begin Kuali Foundation modification | |
| 31 | private static final long serialVersionUID = 241458864711484787L; | |
| 32 | // end Kuali Foundation modification | |
| 33 | ||
| 34 | // begin Kuali Foundation modification | |
| 35 | // removed: PHONE_NUMBER_ERROR_KEY, PARSE_MSG, FORMAT_MSG | |
| 36 | // todo: foreign phone numbers can be different lengths | |
| 37 | // end Kuali Foundation modification | |
| 38 | static final int NUM_DIGITS = 10; | |
| 39 | ||
| 40 | /** | |
| 41 | * begin Kuali Foundation modification | |
| 42 | * Removes formatting characters from the provided phone number and returns just the digits. Very lenient about formatting, but | |
| 43 | * requires a ten-digit number. | |
| 44 | * end Kuali Foundation modification | |
| 45 | */ | |
| 46 | protected Object convertToObject(String target) { | |
| 47 | 0 | String digits = target.replaceAll("[^0-9]", ""); |
| 48 | 0 | if (digits.length() != NUM_DIGITS) |
| 49 | // begin Kuali Foundation modification | |
| 50 | 0 | throw new FormatException("parsing", RiceKeyConstants.ERROR_PHONE_NUMBER, target); |
| 51 | // end Kuali Foundation modification | |
| 52 | ||
| 53 | 0 | return digits; |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * Returns its argument formatted as a phone number in the style: | |
| 58 | * <p> | |
| 59 | * | |
| 60 | * <pre> | |
| 61 | * (999) 999-9999 | |
| 62 | * </pre> | |
| 63 | */ | |
| 64 | public Object format(Object value) { | |
| 65 | 0 | if (value == null) |
| 66 | 0 | return null; |
| 67 | 0 | if (!(value instanceof String)) |
| 68 | // begin Kuali Foundation modification | |
| 69 | 0 | throw new FormatException("formatting", RiceKeyConstants.ERROR_PHONE_NUMBER, value.toString()); |
| 70 | // end Kuali Foundation modification | |
| 71 | ||
| 72 | // begin Kuali Foundation modification | |
| 73 | 0 | String digits = (String) value; |
| 74 | 0 | if (digits.length() != NUM_DIGITS) |
| 75 | 0 | throw new FormatException("formatting", RiceKeyConstants.ERROR_PHONE_NUMBER, value.toString()); |
| 76 | // end Kuali Foundation modification | |
| 77 | ||
| 78 | 0 | StringBuffer buf = new StringBuffer("("); |
| 79 | 0 | buf.append(digits.substring(0, 3)); |
| 80 | 0 | buf.append(") "); |
| 81 | 0 | buf.append(digits.substring(3, 6)); |
| 82 | 0 | buf.append("-"); |
| 83 | 0 | buf.append(digits.substring(6)); |
| 84 | ||
| 85 | 0 | return buf.toString(); |
| 86 | } | |
| 87 | } |