1
2
3
4
5 package net.sourceforge.jsdp;
6
7 import java.util.Vector;
8 import java.util.regex.Pattern;
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 public class Media implements Field {
27
28
29 private static final long serialVersionUID = 8009552820375695269L;
30
31
32 private static final Pattern mediaPattern = Pattern.compile("\\w+");
33
34
35 private static final Pattern protocolPattern = Pattern.compile("[\\w/]+");
36
37
38 protected String media;
39
40
41 protected int port;
42
43
44 protected int portsCount;
45
46
47 protected String protocol;
48
49
50 protected Vector formats;
51
52
53
54
55 protected Media() {
56
57 super();
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public Media(final String media, final int port, final int portsCount, final String protocol, final String format) throws SDPException {
77
78 super();
79
80 setMediaType(media);
81 setPort(port);
82 setPortsCount(portsCount);
83 setProtocol(protocol);
84
85 formats = new Vector(3);
86 addMediaFormat(format);
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 public Media(final String media, final int port, final String protocol, final String format) throws SDPException {
103
104 this(media, port, 1, protocol, format);
105 }
106
107
108
109
110
111
112
113
114
115
116 public static Media parse(final String field) throws SDPParseException {
117
118 if (!field.startsWith("m=")) {
119 throw new SDPParseException("The string \"" + field + "\" isn't a media field");
120 }
121
122 Media m = null;
123
124
125 String[] values = field.substring(2).split(" ");
126
127 try {
128
129
130 if (values.length < 4) {
131 throw new SDPException("Some media field parameters are missing");
132 }
133
134 String portInfo = values[1];
135 String[] portInfoValues = portInfo.split("/");
136
137 if (portInfoValues.length == 1) {
138 m = new Media(values[0], Integer.parseInt(values[1]), values[2], values[3]);
139 }
140 else if (portInfoValues.length == 2) {
141 m = new Media(values[0], Integer.parseInt(portInfoValues[0]), Integer.parseInt(portInfoValues[1]), values[2], values[3]);
142 }
143
144
145 try {
146 for (int i = 4; i < values.length; i++) {
147 m.addMediaFormat(values[i]);
148 }
149 }
150 catch (SDPException invalidMediaFormat) {
151
152
153
154
155 }
156 }
157 catch (SDPException parseException) {
158 throw new SDPParseException("The string \"" + field + "\" isn't a valid origin field", parseException);
159 }
160
161 return m;
162 }
163
164
165
166
167
168
169
170
171 public void addMediaFormat(final String format) throws SDPException {
172
173 if (!mediaPattern.matcher(format).matches()) {
174 throw new SDPException("Invalid media format: " + format);
175 }
176
177 formats.add(format);
178 }
179
180
181
182
183
184
185 public Object clone() {
186
187 Media field = new Media();
188
189 field.media = new String(media);
190 field.port = port;
191 field.portsCount = portsCount;
192 field.protocol = new String(protocol);
193 field.formats = (Vector) formats.clone();
194
195 return field;
196 }
197
198
199
200
201
202
203 public String[] getMediaFormats() {
204
205 return (String[]) formats.toArray(new String[formats.size()]);
206 }
207
208
209
210
211
212
213 public String getMediaType() {
214
215 return media;
216 }
217
218
219
220
221
222
223 public int getPort() {
224
225 return port;
226 }
227
228
229
230
231
232
233 public int getPortsCount() {
234
235 return portsCount;
236 }
237
238
239
240
241
242
243 public String getProtocol() {
244
245 return protocol;
246 }
247
248
249
250
251
252
253 public char getType() {
254
255 return Field.MEDIA_FIELD;
256 }
257
258
259
260
261
262
263
264
265
266
267 public void setMediaFormats(final String[] formats) throws SDPException {
268
269 if (formats == null) {
270 throw new IllegalArgumentException("The media formats cannot be null");
271 }
272
273
274 Vector elements = (Vector) this.formats.clone();
275
276
277 this.formats.clear();
278
279 try {
280 for (int i = 0; i < formats.length; i++) {
281 addMediaFormat(formats[i]);
282 }
283 }
284 catch (SDPException invalidFormat) {
285
286
287 this.formats = elements;
288
289
290 throw new SDPException("Invalid media formats", invalidFormat);
291 }
292 }
293
294
295
296
297
298
299
300
301 public void setMediaType(final String media) throws SDPException {
302
303 if (!mediaPattern.matcher(media).matches()) {
304 throw new SDPException("Invalid media type: " + media);
305 }
306
307 this.media = media;
308 }
309
310
311
312
313
314
315
316
317 public void setPort(final int port) throws SDPException {
318
319 if (port < 0) {
320 throw new SDPException("The transport port must be greater than 0");
321 }
322
323 this.port = port;
324 }
325
326
327
328
329
330
331
332
333 public void setPortsCount(final int n) throws SDPException {
334
335 if (n < 1) {
336 throw new SDPException("The ports count must be greater than 0");
337 }
338
339 portsCount = n;
340 }
341
342
343
344
345
346
347
348
349 public void setProtocol(final String protocol) throws SDPException {
350
351 if (!protocolPattern.matcher(protocol).matches()) {
352 throw new SDPException("Invalid protocol: " + protocol);
353 }
354
355 this.protocol = protocol;
356 }
357
358
359
360
361
362
363
364
365 public String toString() {
366
367 StringBuffer result = new StringBuffer(getType() + "=");
368
369 result.append(media);
370 result.append(" " + port);
371
372 if (portsCount > 1) {
373 result.append("/" + portsCount);
374 }
375
376 result.append(" " + protocol);
377 for (int i = 0; i < formats.size(); i++) {
378 result.append(" " + formats.get(i));
379 }
380
381 return result.toString();
382 }
383 }