01: /*
02: * Copyright 2004 Jonathan M. Lehr
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * 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.core.web.format;
17:
18: // end Kuali Foundation modification
19:
20: // begin Kuali Foundation modification
21: import org.kuali.RiceKeyConstants;
22:
23: /**
24: * begin Kuali Foundation modification
25: * This class is used to format phone number objects.
26: * end Kuali Foundation modification
27: */
28: 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: String digits = target.replaceAll("[^0-9]", "");
47: if (digits.length() != NUM_DIGITS)
48: // begin Kuali Foundation modification
49: throw new FormatException("parsing",
50: RiceKeyConstants.ERROR_PHONE_NUMBER, target);
51: // end Kuali Foundation modification
52:
53: 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: if (value == null)
66: return null;
67: if (!(value instanceof String))
68: // begin Kuali Foundation modification
69: throw new FormatException("formatting",
70: RiceKeyConstants.ERROR_PHONE_NUMBER, value
71: .toString());
72: // end Kuali Foundation modification
73:
74: // begin Kuali Foundation modification
75: String digits = (String) value;
76: if (digits.length() != NUM_DIGITS)
77: throw new FormatException("formatting",
78: RiceKeyConstants.ERROR_PHONE_NUMBER, value
79: .toString());
80: // end Kuali Foundation modification
81:
82: StringBuffer buf = new StringBuffer("(");
83: buf.append(digits.substring(0, 3));
84: buf.append(") ");
85: buf.append(digits.substring(3, 6));
86: buf.append("-");
87: buf.append(digits.substring(6));
88:
89: return buf.toString();
90: }
91: }
|