001: /*
002: * (c) Copyright 2007 by Volker Bergmann. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, is permitted under the terms of the
006: * GNU General Public License.
007: *
008: * For redistributing this software or a derivative work under a license other
009: * than the GPL-compatible Free Software License as defined by the Free
010: * Software Foundation or approved by OSI, you must first obtain a commercial
011: * license to this software product from Volker Bergmann.
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
014: * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
015: * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
016: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
017: * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
018: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
019: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
020: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
021: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
022: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
023: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
024: * POSSIBILITY OF SUCH DAMAGE.
025: */
026:
027: package org.databene.region;
028:
029: import java.text.FieldPosition;
030: import java.text.Format;
031: import java.text.ParseException;
032: import java.text.ParsePosition;
033:
034: /**
035: * Formats a phone code.<br/>
036: * The following pattern letters are defined:
037: * <table>
038: * <tr><th>Letter</th><th>Phone Number Component</th></tr>
039: * <tr><td>c</td><td>country code</td></tr>
040: * <tr><td>a</td><td>area code</td></tr>
041: * <tr><td>l</td><td>local</td></tr>
042: * </table>
043: * Any other character will be used 'as is'.<br/>
044: * Examples:
045: * <table>
046: * <tr><th>Pattern</th><th>Rendered as</th></tr>
047: * <tr><td>+c-a-l</td><td>+49-1234-5678</td></tr>
048: * <tr><td>0al</td><td>012345678</td></tr>
049: * <tr><td>00c(a)l</td><td>0049(1234)5678</td></tr>
050: * </table>
051: * @since 0.3.05
052: * @author Volker Bergmann
053: */
054: public class PhoneNumberFormat extends Format {
055:
056: private String pattern;
057:
058: public PhoneNumberFormat(String pattern) {
059: this .pattern = pattern;
060: }
061:
062: @Override
063: public StringBuffer format(Object obj, StringBuffer toAppendTo,
064: FieldPosition pos) {
065: PhoneNumber number = (PhoneNumber) obj;
066: for (int i = 0; i < pattern.length(); i++) {
067: char c = pattern.charAt(i);
068: switch (c) {
069: case 'c':
070: toAppendTo.append(number.getCountryCode());
071: break;
072: case 'a':
073: toAppendTo.append(number.getAreaCode());
074: break;
075: case 'l':
076: toAppendTo.append(number.getLocalNumber());
077: break;
078: default:
079: toAppendTo.append(c);
080: }
081: }
082: return toAppendTo;
083: }
084:
085: @Override
086: public Object parseObject(String source, ParsePosition pos) {
087: PhoneNumber number = new PhoneNumber();
088: for (int i = 0; i < pattern.length(); i++) {
089: char c = pattern.charAt(i);
090: switch (c) {
091: case 'c':
092: number.setCountryCode(parseDigits(source, pos));
093: break;
094: case 'a':
095: number.setAreaCode(parseDigits(source, pos));
096: break;
097: case 'l':
098: number.setLocalNumber(parseDigits(source, pos));
099: break;
100: default:
101: if (source.charAt(pos.getIndex()) != c)
102: throw new IllegalArgumentException("Pattern '"
103: + pattern + "' is not matched by String: "
104: + source);
105: pos.setIndex(pos.getIndex() + 1);
106: }
107: }
108: return number;
109: }
110:
111: @Override
112: public Object parseObject(String source) throws ParseException {
113: try {
114: return super .parseObject(source);
115: } catch (IllegalArgumentException e) {
116: throw new ParseException(e.getMessage(), -1);
117: }
118: }
119:
120: private String parseDigits(String source, ParsePosition pos) {
121: if (pos.getIndex() >= source.length())
122: throw new IllegalArgumentException(
123: "Text cannot be parsed unambiguously as phone number with pattern: "
124: + pattern);
125: int start = pos.getIndex();
126: int end;
127: for (end = start; end < source.length(); end++) {
128: char c = source.charAt(end);
129: if (!Character.isDigit(c))
130: break;
131: }
132: pos.setIndex(end);
133: return source.substring(start, end);
134: }
135: }
|