2004-04-01 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / class / corlib / System.Resources / ResourceSet.cs
1 //
2 // System.Resources.ResourceSet.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //      Dick Porter (dick@ximian.com)
7 //      Andreas Nahr (ClassDevelopment@A-SoftTech.com)
8 //
9 // (C) 2001, 2002 Ximian, Inc.          http://www.ximian.com
10 //
11
12 using System.Collections;
13 using System.IO;
14 using System.Globalization;
15 using System.Runtime.InteropServices;
16
17 namespace System.Resources
18 {
19         [Serializable]
20         public class ResourceSet : IDisposable
21
22 #if (NET_1_1)
23                                                 , IEnumerable
24 #endif
25
26         {
27
28                 protected IResourceReader Reader;
29                 protected Hashtable Table;
30
31                 // Constructors
32                 protected ResourceSet () {}
33
34                 public ResourceSet (IResourceReader reader)
35                 {
36                         if (reader == null)
37                                 throw new ArgumentNullException ("The reader is null.");
38                         Reader = reader;
39                 }
40
41                 public ResourceSet (Stream stream)
42                 {
43                         if(stream==null) {
44                                 throw new ArgumentNullException("stream is null");
45                         }
46
47                         if(!stream.CanRead) {
48                                 throw new ArgumentException("stream is not readable");
49                         }
50                         
51                         Reader = new ResourceReader (stream);
52                 }
53
54                 public ResourceSet (String fileName)
55                 {
56                         if(fileName==null) {
57                                 throw new ArgumentNullException("filename is null");
58                         }
59                         
60                         Reader = new ResourceReader (fileName);
61                 }
62
63                 public virtual void Close ()
64                 {
65                         Dispose (true);
66                 }
67
68                 public void Dispose()
69                 {
70                         Dispose (true);
71                 }
72
73                 protected virtual void Dispose (bool disposing)
74                 {
75                         if (disposing) {
76                                 if(Reader!=null) {
77                                         Reader.Close();
78                                 }
79                         }
80
81                         Reader = null;
82                         Table = null;
83                 }
84
85                 public virtual Type GetDefaultReader ()
86                 {
87                         return (typeof (ResourceReader));
88                 } 
89                 public virtual Type GetDefaultWriter ()
90                 {
91                         return (typeof (ResourceWriter));
92                 }
93
94 #if (NET_1_1)
95
96                 [ComVisible (false)]
97                 public virtual IDictionaryEnumerator GetEnumerator ()
98                 {
99                         if (Table == null)
100                                 ReadResources ();
101                         return Table.GetEnumerator(); 
102                 }
103
104                 IEnumerator IEnumerable.GetEnumerator ()
105                 {
106                         return this.GetEnumerator (); 
107                 }
108
109 #endif
110
111                 public virtual object GetObject (string name)
112                 {
113                         if (name == null)
114                                 throw new ArgumentNullException ("The name parameter is null.");
115                         if (Reader == null)
116                                 throw new InvalidOperationException ("The ResourceSet has been closed.");
117
118                         if (Table == null) { 
119                                 ReadResources ();
120                         }
121                         
122                         return(Table[name]);
123                 }
124
125                 public virtual object GetObject (string name, bool ignoreCase)
126                 {
127                         if (name == null)
128                                 throw new ArgumentNullException ("The name parameter is null.");
129                         if (Reader == null)
130                                 throw new InvalidOperationException ("ResourceSet has been closed.");
131                         if (Table == null)
132                                 ReadResources ();
133
134                         if (ignoreCase) {
135                                 foreach (DictionaryEntry de in Table) {
136                                         string key = (string) de.Key;
137                                         if (String.Compare (key, name, true, CultureInfo.InvariantCulture) == 0)
138                                                 return de.Value;
139                                 }
140                                 return null;
141                         } else
142                                 return Table[name];
143                 }
144
145                 public virtual string GetString (string name)
146                 {
147                         Object o = GetObject (name);
148                         if (o == null)
149                                 return null;
150                         if (o is string)
151                                 return (string) o;
152                         throw new InvalidOperationException("Not a string");
153                 }
154
155                 public virtual string GetString (string name, bool ignoreCase)
156                 {
157                         Object o = GetObject (name, ignoreCase);
158                         if (o == null)
159                                 return null;
160                         if (o is string)
161                                 return (string) o;
162                         throw new InvalidOperationException("Not a string");
163                 }
164
165                 protected virtual void ReadResources ()
166                 {
167                         IDictionaryEnumerator i = Reader.GetEnumerator();
168
169                         if (Table == null)
170                                 Table = new Hashtable ();
171                         i.Reset ();
172
173                         while (i.MoveNext ()) 
174                                 Table.Add (i.Key, i.Value);
175                 }
176         }
177 }