1
2
3
4
5 package net.sourceforge.jsdp;
6
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
9
10 import net.sourceforge.jsdp.util.Address;
11 import net.sourceforge.jsdp.util.Resource;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class Connection implements Field {
31
32
33 private static final long serialVersionUID = 1555839237745744131L;
34
35
36 public static final String IP4 = Address.IP4;
37
38
39 public static final String IP6 = Address.IP6;
40
41
42 private static final Pattern fieldPattern = Pattern.compile("IN (IP4|IP6) ((.+))");
43
44
45 protected Resource resource;
46
47
48
49
50 protected Connection() {
51
52 super();
53 }
54
55
56
57
58
59
60
61
62
63
64 public Connection(final String resource) throws SDPException {
65
66 super();
67 this.resource = new Resource(resource);
68 }
69
70
71
72
73
74
75
76
77
78
79 public static Connection parse(final String field) throws SDPParseException {
80
81 if (!field.startsWith("c=")) {
82 throw new SDPParseException("The string \"" + field + "\" isn't a connection field");
83 }
84
85 Connection c = null;
86
87
88 Matcher matcher = fieldPattern.matcher(field.substring(2));
89
90
91 if (matcher.matches()) {
92 try {
93
94
95 String type = matcher.group(1);
96
97 c = new Connection(matcher.group(2));
98
99 if (!c.getAddressType().equals(type)) {
100 throw new SDPParseException("The address " + c.getAddress() + " isn't an " + type + " address");
101 }
102 }
103 catch (SDPException parseException) {
104 throw new SDPParseException("The string \"" + field + "\" isn't a valid connection field", parseException);
105 }
106 }
107 else {
108 throw new SDPParseException("The string \"" + field + "\" isn't a valid connection field");
109 }
110
111 return c;
112 }
113
114
115
116
117
118
119 public Object clone() {
120
121 Connection field = new Connection();
122 field.resource = (Resource) resource.clone();
123
124 return field;
125 }
126
127
128
129
130
131
132 public String getAddress() {
133
134 return resource.getAddress();
135 }
136
137
138
139
140
141
142 public String getAddressType() {
143
144 return resource.getAddressType();
145 }
146
147
148
149
150
151
152
153 public String getNetType() {
154
155 return Address.IN;
156 }
157
158
159
160
161
162
163 public char getType() {
164
165 return Field.CONNECTION_FIELD;
166 }
167
168
169
170
171
172
173
174
175
176 public void setAddress(final String address) throws SDPException {
177
178 resource.setAddress(address);
179 }
180
181
182
183
184
185
186
187
188
189
190
191 public void setAddress(final String address, final int ttl) throws SDPException {
192
193 resource.setAddress(address);
194 resource.setTTL(ttl);
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 public void setAddress(final String address, final int ttl, final int addresses) throws SDPException {
211
212 resource.setAddress(address);
213 resource.setTTL(ttl);
214 resource.setAddresses(addresses);
215 }
216
217
218
219
220
221
222
223 public String toString() {
224
225 return getType() + "=" + Address.IN + " " + resource.toString();
226 }
227 }