1
2
3
4
5 package net.sourceforge.jsdp;
6
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.HashMap;
10 import java.util.Iterator;
11 import java.util.LinkedList;
12 import java.util.List;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 public class SessionDescription implements Description {
40
41
42 private static final long serialVersionUID = -2804972731894656668L;
43
44
45 protected Version v;
46
47
48 protected Origin o;
49
50
51 protected SessionName s;
52
53
54 protected Information i;
55
56
57 protected Uri u;
58
59
60 protected ArrayList emails;
61
62
63 protected ArrayList phones;
64
65
66 protected Connection c;
67
68
69 protected HashMap bandwiths;
70
71
72 protected ArrayList timeDescriptions;
73
74
75 protected TimeZone z;
76
77
78 protected Key k;
79
80
81 protected HashMap attributes;
82
83
84 protected ArrayList mediaDescriptions;
85
86
87
88
89 protected SessionDescription() {
90
91 super();
92
93 this.emails = new ArrayList();
94 this.phones = new ArrayList();
95 this.bandwiths = new HashMap();
96 this.timeDescriptions = new ArrayList();
97 this.attributes = new HashMap();
98 this.mediaDescriptions = new ArrayList();
99 }
100
101
102
103
104
105
106
107 protected SessionDescription(final SessionDescription sd) {
108
109 super();
110
111 this.v = (Version) sd.v.clone();
112 this.o = (Origin) sd.o.clone();
113 this.s = (SessionName) sd.s.clone();
114 this.i = (Information) sd.i.clone();
115 this.u = (Uri) sd.u.clone();
116 this.emails = (ArrayList) sd.emails.clone();
117 this.phones = (ArrayList) sd.phones.clone();
118 this.c = (Connection) sd.c.clone();
119 this.bandwiths = (HashMap) sd.bandwiths.clone();
120 this.timeDescriptions = (ArrayList) sd.timeDescriptions.clone();
121 this.z = (TimeZone) sd.z.clone();
122 this.attributes = (HashMap) sd.attributes.clone();
123 this.mediaDescriptions = (ArrayList) sd.mediaDescriptions.clone();
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 public SessionDescription(final Version v, final Origin o, final SessionName s, final TimeDescription td) throws IllegalArgumentException {
141
142 this();
143
144 setVersion(v);
145 setOrigin(o);
146 setSessionName(s);
147
148 addTimeDescription(td);
149 }
150
151
152
153
154
155
156
157
158
159 public void addAttribute(final Attribute field) throws IllegalArgumentException {
160
161 if (field == null) {
162 throw new IllegalArgumentException("An attribute field cannot be null");
163 }
164
165
166 String name = field.getName();
167
168
169 if (attributes.containsKey(name)) {
170
171 List values = null;
172 Object maybe = attributes.get(name);
173
174
175 if (maybe instanceof List) {
176
177 values = (List) maybe;
178 }
179
180 else {
181
182
183 Attribute previous = (Attribute) maybe;
184
185
186
187
188
189 values = new LinkedList();
190
191
192 values.add(previous);
193
194 attributes.put(field.getName(), values);
195 }
196
197 values.add(field);
198
199 }
200
201 else {
202
203 attributes.put(field.getName(), field);
204 }
205 }
206
207
208
209
210
211
212
213
214
215 public void addBandwith(final Bandwith bandwith) throws IllegalArgumentException {
216
217 if (bandwith == null) {
218 throw new IllegalArgumentException("A bandwith field cannot be null");
219 }
220
221 bandwiths.put(bandwith.getModifier(), bandwith);
222 }
223
224
225
226
227
228
229
230
231 public void addEmail(final Email field) throws IllegalArgumentException {
232
233 if (field == null) {
234 throw new IllegalArgumentException("An email field cannot be null");
235 }
236
237 emails.add(field);
238 }
239
240
241
242
243
244
245
246
247
248
249
250
251 public void addMediaDescription(final MediaDescription md) throws IllegalArgumentException, SDPException {
252
253 if (md == null) {
254 throw new IllegalArgumentException("A media description cannot be null");
255 }
256
257 if (!hasConnection() && !md.hasConnection()) {
258 throw new SDPException("This media description must have a connection field");
259 }
260
261
262 mediaDescriptions.add(md);
263 }
264
265
266
267
268
269
270
271
272 public void addPhone(final Phone field) throws IllegalArgumentException {
273
274 if (field == null) {
275 throw new IllegalArgumentException("A phone field cannot be null");
276 }
277
278
279 phones.add(field);
280 }
281
282
283
284
285
286
287
288
289 public void addTimeDescription(final TimeDescription td) throws IllegalArgumentException {
290
291 if (td == null) {
292 throw new IllegalArgumentException("A time description cannot be null");
293 }
294
295 timeDescriptions.add(td);
296 }
297
298
299
300
301 public void clearAttributes() {
302
303 attributes.clear();
304 }
305
306
307
308
309 public void clearBandwiths() {
310
311 this.bandwiths.clear();
312 }
313
314
315
316
317 public void clearEmails() {
318
319 this.emails.clear();
320 }
321
322
323
324
325 public void clearMediaDescriptions() {
326
327 this.mediaDescriptions.clear();
328 }
329
330
331
332
333 public void clearPhones() {
334
335 this.phones.clear();
336 }
337
338
339
340
341
342
343 public Object clone() {
344
345 return new SessionDescription(this);
346 }
347
348
349
350
351
352
353
354 public Attribute[] getAttributes() {
355
356 Object maybe = null;
357 List values = null;
358
359 Collection result = new LinkedList();
360
361 for (Iterator i = attributes.values().iterator(); i.hasNext();) {
362
363 maybe = i.next();
364
365 if (maybe instanceof Attribute) {
366
367 result.add(maybe);
368 }
369 else {
370
371 values = (List) maybe;
372
373 for (Iterator j = values.iterator(); j.hasNext();) {
374
375 result.add(j.next());
376 }
377 }
378 }
379
380 return (Attribute[]) result.toArray(new Attribute[result.size()]);
381 }
382
383
384
385
386
387
388
389
390
391 public Attribute getAttribute(final String name) {
392
393 Attribute result = null;
394 Object maybe = attributes.get(name);
395
396 if (maybe != null) {
397
398 if (maybe instanceof List) {
399
400 List values = (List) maybe;
401
402 result = (Attribute) values.get(0);
403 }
404 else {
405
406 result = (Attribute) maybe;
407 }
408 }
409
410 return result;
411 }
412
413
414
415
416
417
418
419
420
421 public Attribute getAttribute(final String name, final int index) {
422
423 Attribute result = null;
424 Object maybe = attributes.get(name);
425
426 if ((maybe != null) && (maybe instanceof List)) {
427
428 List values = (List) maybe;
429
430 result = (Attribute) values.get(index);
431 }
432
433 return result;
434 }
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449 public Attribute[] getAttributes(final String name) {
450
451 Attribute[] result;
452 Object maybe = attributes.get(name);
453
454 if (maybe != null) {
455
456 if (maybe instanceof List) {
457
458 List values = (List) maybe;
459
460 result = (Attribute[]) values.toArray(new Attribute[values.size()]);
461 }
462 else {
463
464 result = new Attribute[1];
465 result[0] = (Attribute) maybe;
466 }
467 }
468 else {
469 result = new Attribute[0];
470 }
471
472 return result;
473 }
474
475
476
477
478
479
480
481
482 public int getAttributesCount(final String name) {
483
484 int result = 0;
485 Object maybe = attributes.get(name);
486
487 if (maybe != null) {
488
489 if (maybe instanceof List) {
490
491 List values = (List) maybe;
492
493 result = values.size();
494 }
495 else {
496
497 result = 1;
498 }
499 }
500
501 return result;
502 }
503
504
505
506
507
508
509
510
511
512 public Bandwith getBandwith(final String modifier) {
513
514 return (Bandwith) bandwiths.get(modifier);
515 }
516
517
518
519
520
521
522
523 public Bandwith[] getBandwiths() {
524
525 Bandwith[] values = new Bandwith[bandwiths.size()];
526 Iterator iterator = bandwiths.values().iterator();
527
528 for (int j = 0; j < values.length; j++) {
529 values[j] = (Bandwith) iterator.next();
530 }
531
532 return values;
533 }
534
535
536
537
538
539
540 public Connection getConnection() {
541
542 return c;
543 }
544
545
546
547
548
549
550
551 public Email[] getEmails() {
552
553 return (Email[]) emails.toArray(new Email[emails.size()]);
554 }
555
556
557
558
559
560
561 public Information getInformation() {
562
563 return i;
564 }
565
566
567
568
569
570
571 public Key getKey() {
572
573 return k;
574 }
575
576
577
578
579
580
581
582 public MediaDescription[] getMediaDescriptions() {
583
584 return (MediaDescription[]) mediaDescriptions.toArray(new MediaDescription[mediaDescriptions.size()]);
585 }
586
587
588
589
590
591
592 public int getMediaDescriptionsCount() {
593
594 return mediaDescriptions.size();
595 }
596
597
598
599
600
601
602 public Origin getOrigin() {
603
604 return o;
605 }
606
607
608
609
610
611
612
613 public Phone[] getPhones() {
614
615 return (Phone[]) phones.toArray(new Phone[phones.size()]);
616 }
617
618
619
620
621
622
623 public SessionName getSessionName() {
624
625 return s;
626 }
627
628
629
630
631
632
633
634 public TimeDescription[] getTimeDescriptions() {
635
636 return (TimeDescription[]) timeDescriptions.toArray(new TimeDescription[timeDescriptions.size()]);
637 }
638
639
640
641
642
643
644 public TimeZone getTimeZone() {
645
646 return z;
647 }
648
649
650
651
652
653
654 public Uri getUri() {
655
656 return u;
657 }
658
659
660
661
662
663
664 public Version getVersion() {
665
666 return v;
667 }
668
669
670
671
672
673
674
675
676
677 public boolean hasAttribute(String name) {
678
679 boolean result = false;
680
681 if (name != null) {
682 result = attributes.containsKey(name);
683 }
684
685 return result;
686 }
687
688
689
690
691
692
693
694 public boolean hasConnection() {
695
696 return c != null;
697 }
698
699
700
701
702
703
704
705 public boolean hasEmails() {
706
707 return (emails.size() > 0);
708 }
709
710
711
712
713
714
715
716 public boolean hasInformation() {
717
718 return i != null;
719 }
720
721
722
723
724
725
726
727 public boolean hasKey() {
728
729 return k != null;
730 }
731
732
733
734
735
736
737
738 public boolean hasPhones() {
739
740 return (phones.size() > 0);
741 }
742
743
744
745
746
747
748
749 public boolean hasTimeZone() {
750
751 return z != null;
752 }
753
754
755
756
757
758
759
760 public boolean hasUri() {
761
762 return u != null;
763 }
764
765
766
767
768
769
770
771
772
773
774 public Attribute removeAttribute(final String name) {
775
776 Attribute a = null;
777 Object maybe = attributes.get(name);
778
779 if (maybe != null) {
780
781 if (maybe instanceof List) {
782
783 List values = (List) maybe;
784
785
786 a = (Attribute) values.remove(0);
787
788
789 if (values.isEmpty()) {
790 attributes.remove(name);
791 }
792 }
793 else {
794 a = (Attribute) attributes.remove(name);
795 }
796 }
797
798 return a;
799 }
800
801
802
803
804
805
806
807
808
809 public Attribute removeAttribute(final String name, final int index) {
810
811 Attribute a = null;
812 Object maybe = attributes.get(name);
813
814 if ((maybe != null) && (maybe instanceof List)) {
815
816 List values = (List) maybe;
817
818 a = (Attribute) values.remove(index);
819 }
820
821 return a;
822 }
823
824
825
826
827
828
829 public Attribute[] removeAttributes(final String name) {
830
831
832 Attribute[] result = getAttributes(name);
833
834
835 attributes.remove(name);
836
837 return result;
838 }
839
840
841
842
843
844
845
846
847
848 public Bandwith removeBandwith(final String modifier) {
849
850 return (Bandwith) bandwiths.remove(modifier);
851 }
852
853
854
855
856
857
858
859
860
861 public void setAttributes(Attribute[] fields) {
862
863 if (fields == null) {
864 throw new IllegalArgumentException("Attribute fields cannot be null");
865 }
866
867 int length = fields.length;
868 HashMap backup = (HashMap) attributes.clone();
869
870 try {
871
872
873 attributes.clear();
874
875
876 for (int i = 0; i < length; i++) {
877 addAttribute(fields[i]);
878 }
879 }
880 catch (IllegalArgumentException exception) {
881
882
883 attributes = backup;
884
885
886 throw exception;
887 }
888 }
889
890
891
892
893
894
895
896
897
898 public void setBandwiths(Bandwith[] fields) {
899
900 if (fields == null) {
901 throw new IllegalArgumentException("Bandwith fields cannot be null");
902 }
903
904 int length = fields.length;
905 HashMap backup = (HashMap) bandwiths.clone();
906
907 try {
908
909
910 bandwiths.clear();
911
912
913 for (int i = 0; i < length; i++) {
914 addBandwith(fields[i]);
915 }
916 }
917 catch (IllegalArgumentException exception) {
918
919
920 bandwiths = backup;
921
922
923 throw exception;
924 }
925 }
926
927
928
929
930
931
932 public void setConnection(final Connection c) {
933
934
935 this.c = c;
936 }
937
938
939
940
941
942
943
944
945 public void setEmails(final Email[] fields) throws IllegalArgumentException {
946
947 if (fields == null) {
948 throw new IllegalArgumentException("Email fields cannot be null");
949 }
950
951 int length = fields.length;
952 ArrayList backup = (ArrayList) emails.clone();
953
954 try {
955
956
957 emails.clear();
958
959
960 for (int i = 0; i < length; i++) {
961
962 addEmail(fields[i]);
963 }
964 }
965 catch (IllegalArgumentException exception) {
966
967
968 emails = backup;
969
970
971 throw exception;
972 }
973 }
974
975
976
977
978
979
980 public void setInformation(final Information i) {
981
982 this.i = i;
983 }
984
985
986
987
988
989
990 public void setKey(final Key k) {
991
992 this.k = k;
993 }
994
995
996
997
998
999
1000
1001
1002
1003 public void setMediaDescriptions(final MediaDescription[] descriptions) throws IllegalArgumentException, SDPException {
1004
1005 if (descriptions == null) {
1006 throw new IllegalArgumentException("Media descriptions cannot be null");
1007 }
1008
1009 int length = descriptions.length;
1010 ArrayList backup = (ArrayList) mediaDescriptions.clone();
1011
1012 try {
1013
1014
1015 mediaDescriptions.clear();
1016
1017
1018 for (int i = 0; i < length; i++) {
1019
1020 addMediaDescription(descriptions[i]);
1021 }
1022 }
1023 catch (IllegalArgumentException exception) {
1024
1025
1026 mediaDescriptions = backup;
1027
1028
1029 throw exception;
1030 }
1031 catch (SDPException sdpException) {
1032
1033
1034 mediaDescriptions = backup;
1035
1036
1037 throw sdpException;
1038 }
1039 }
1040
1041
1042
1043
1044
1045
1046
1047
1048 public void setOrigin(final Origin o) throws IllegalArgumentException {
1049
1050 if (o == null) {
1051 throw new IllegalArgumentException("Origin field cannot be null");
1052 }
1053
1054 this.o = o;
1055 }
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065 public void setPhones(final Phone[] fields) throws IllegalArgumentException {
1066
1067 if (fields == null) {
1068 throw new IllegalArgumentException("Phone fields cannot be null");
1069 }
1070
1071 int length = fields.length;
1072 ArrayList backup = (ArrayList) phones.clone();
1073
1074 try {
1075
1076
1077 phones.clear();
1078
1079
1080 for (int i = 0; i < length; i++) {
1081
1082 addPhone(fields[i]);
1083 }
1084 }
1085 catch (IllegalArgumentException exception) {
1086
1087
1088 phones = backup;
1089
1090
1091 throw exception;
1092 }
1093 }
1094
1095
1096
1097
1098
1099
1100
1101
1102 public void setSessionName(final SessionName s) throws IllegalArgumentException {
1103
1104 if (s == null) {
1105 throw new IllegalArgumentException("Session name field cannot be null");
1106 }
1107
1108 this.s = s;
1109 }
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119 public void setTimeDescriptions(final TimeDescription[] descriptions) throws IllegalArgumentException {
1120
1121 if (descriptions == null) {
1122 throw new IllegalArgumentException("Time descriptions cannot be null");
1123 }
1124
1125 int length = descriptions.length;
1126 ArrayList backup = (ArrayList) timeDescriptions.clone();
1127
1128 try {
1129
1130
1131 timeDescriptions.clear();
1132
1133
1134 for (int i = 0; i < length; i++) {
1135
1136 addTimeDescription(descriptions[i]);
1137 }
1138 }
1139 catch (IllegalArgumentException exception) {
1140
1141
1142 timeDescriptions = backup;
1143
1144
1145 throw exception;
1146 }
1147 }
1148
1149
1150
1151
1152
1153
1154 public void setTimeZone(final TimeZone z) {
1155
1156 this.z = z;
1157 }
1158
1159
1160
1161
1162
1163
1164 public void setUri(final Uri u) {
1165
1166 this.u = u;
1167 }
1168
1169
1170
1171
1172
1173
1174
1175
1176 public void setVersion(final Version v) throws IllegalArgumentException {
1177
1178 if (v == null) {
1179 throw new IllegalArgumentException("Version field cannot be null");
1180 }
1181
1182 this.v = v;
1183 }
1184
1185
1186
1187
1188
1189
1190 public String toString() {
1191
1192
1193 StringBuffer result = new StringBuffer(v.toString());
1194 result.append(Field.END_OF_FIELD);
1195
1196
1197 result.append(o.toString());
1198 result.append(Field.END_OF_FIELD);
1199
1200
1201 result.append(s.toString());
1202 result.append(Field.END_OF_FIELD);
1203
1204
1205 if (i != null) {
1206 result.append(i.toString());
1207 result.append(Field.END_OF_FIELD);
1208 }
1209
1210
1211 if (u != null) {
1212 result.append(u.toString());
1213 result.append(Field.END_OF_FIELD);
1214 }
1215
1216
1217 for (Iterator iterator = emails.iterator(); iterator.hasNext();) {
1218 result.append(iterator.next());
1219 result.append(Field.END_OF_FIELD);
1220 }
1221
1222
1223 for (Iterator iterator = phones.iterator(); iterator.hasNext();) {
1224 result.append(iterator.next());
1225 result.append(Field.END_OF_FIELD);
1226 }
1227
1228
1229 if (c != null) {
1230 result.append(c.toString());
1231 result.append(Field.END_OF_FIELD);
1232 }
1233
1234 int size = 0;
1235
1236 Bandwith[] myBandwiths = getBandwiths();
1237 size = myBandwiths.length;
1238
1239
1240 for (int i = 0; i < size; i++) {
1241 result.append(myBandwiths[i]);
1242 result.append(Field.END_OF_FIELD);
1243 }
1244
1245
1246 for (Iterator i = timeDescriptions.iterator(); i.hasNext();) {
1247 result.append(i.next());
1248
1249
1250
1251
1252 }
1253
1254
1255 if (z != null) {
1256 result.append(z.toString());
1257 result.append(Field.END_OF_FIELD);
1258 }
1259
1260
1261 if (k != null) {
1262 result.append(k.toString());
1263 result.append(Field.END_OF_FIELD);
1264 }
1265
1266 Attribute[] myAttributes = getAttributes();
1267 size = myAttributes.length;
1268
1269
1270 for (int i = 0; i < size; i++) {
1271 result.append(myAttributes[i]);
1272 result.append(Field.END_OF_FIELD);
1273 }
1274
1275
1276 for (Iterator i = mediaDescriptions.iterator(); i.hasNext();) {
1277 result.append(i.next());
1278
1279
1280
1281
1282 }
1283
1284 return result.toString();
1285 }
1286 }