1
2
3
4
5 package net.sourceforge.jsdp;
6
7 import java.util.regex.Pattern;
8
9
10
11
12
13
14
15
16
17
18
19
20
21 public class Phone implements Field {
22
23
24 private static final long serialVersionUID = 4792028746712967047L;
25
26
27 private static final Pattern phoneMatcher = Pattern.compile("\\+([1-9])+[ -](\\d)+([ -](\\d)+)*");
28
29
30 protected String name;
31
32
33 protected String phoneNumber;
34
35
36
37
38 private Phone() {
39
40 super();
41 }
42
43
44
45
46
47
48
49
50 public Phone(final String number) throws SDPException {
51
52 super();
53 setPhoneNumber(number);
54 }
55
56
57
58
59
60
61
62
63
64
65 public static Phone parse(final String field) throws SDPParseException {
66
67 if (!field.startsWith("p=")) {
68 throw new SDPParseException("The string \"" + field + "\" isn't a phone field");
69 }
70
71 Phone p = null;
72
73 try {
74
75 p = new Phone(field.substring(2));
76 }
77 catch (SDPException parseException) {
78 throw new SDPParseException("The string \"" + field + "\" isn't a valid phone field", parseException);
79 }
80
81 return p;
82 }
83
84
85
86
87
88
89 public Object clone() {
90
91 Phone field = new Phone();
92
93 field.phoneNumber = this.phoneNumber;
94 if (this.name != null) {
95 field.name = new String(this.name);
96 }
97
98 return field;
99 }
100
101
102
103
104
105
106 public String getName() {
107
108 return name;
109 }
110
111
112
113
114
115
116 public String getPhoneNumber() {
117
118 return phoneNumber;
119 }
120
121
122
123
124
125
126 public char getType() {
127
128 return Field.PHONE_FIELD;
129 }
130
131
132
133
134
135
136
137
138 public void setName(final String name) throws SDPException {
139
140
141 this.name = name;
142 }
143
144
145
146
147
148
149
150
151 public void setPhoneNumber(final String number) throws SDPException {
152
153 if (!phoneMatcher.matcher(number).matches()) {
154 throw new SDPException("Invalid phone number");
155 }
156
157 this.phoneNumber = number;
158 }
159
160
161
162
163
164
165
166 public String toString() {
167
168
169 StringBuffer result = new StringBuffer(getType() + "=");
170
171 if (name != null) {
172 result.append(name);
173 result.append("<" + phoneNumber + ">");
174 }
175 else {
176 result.append(phoneNumber);
177 }
178
179 return result.toString();
180 }
181 }