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