2004-06-19 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Mail / MailAddressCollection.cs
1 //
2 // System.Web.Mail.MailAddressCollection.cs
3 //
4 // Author(s):
5 //   Per Arneng <pt99par@student.bth.se>
6 //
7 //
8 using System;
9 using System.Text;
10 using System.Collections;
11
12 namespace System.Web.Mail {
13
14     // represents a collection of MailAddress objects
15     internal class MailAddressCollection : IEnumerable {
16         
17         protected ArrayList data = new ArrayList();
18         
19         public MailAddress this[ int index ] {
20             get { return this.Get( index ); }
21         }
22
23         public int Count { get { return data.Count; } }
24         
25         public void Add( MailAddress addr ) { data.Add( addr ); }
26         public MailAddress Get( int index ) { return (MailAddress)data[ index ]; }
27
28         public IEnumerator GetEnumerator() {
29             return data.GetEnumerator();
30         }
31     
32      
33         public override string ToString() {
34             
35             StringBuilder builder = new StringBuilder();
36             for( int i = 0; i <data.Count ; i++ ) {
37                 MailAddress addr = this.Get( i );
38                 
39                 builder.Append( addr );
40                 
41                 if( i != ( data.Count - 1 ) ) builder.Append( ",\r\n  " );
42             }
43
44             return builder.ToString(); 
45         }
46
47         public static MailAddressCollection Parse( string str ) {
48             
49             if( str == null ) throw new ArgumentNullException("Null is not allowed as an address string");
50             
51             MailAddressCollection list = new MailAddressCollection();
52             
53             string[] parts = str.Split( new char[] { ',' , ';' } );
54             
55             foreach( string part in parts ) {
56                 MailAddress add = MailAddress.Parse (part);
57                 if (add == null)
58                         continue;
59
60                 list.Add (add);
61             }
62         
63             return list;
64         }
65         
66     }
67
68 }