A few more changes from Jesse Jones, use SupressFinalize on
[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 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Collections;
33 using System.IO;
34 using System.Globalization;
35 using System.Runtime.InteropServices;
36 using System.Security.Permissions;
37
38 namespace System.Resources
39 {
40         [Serializable]
41 #if NET_2_0
42         [ComVisible (true)]
43 #endif
44         public class ResourceSet : IDisposable
45
46 #if (NET_1_1)
47                                                 , IEnumerable
48 #endif
49
50         {
51
52 #if NET_2_0
53                 [NonSerialized]
54 #endif
55                 protected IResourceReader Reader;
56                 protected Hashtable Table;
57
58                 // Constructors
59                 protected ResourceSet () {}
60
61                 public ResourceSet (IResourceReader reader)
62                 {
63                         if (reader == null)
64                                 throw new ArgumentNullException ("The reader is null.");
65                         Reader = reader;
66                 }
67
68                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
69                 public ResourceSet (Stream stream)
70                 {
71                         if(stream==null) {
72                                 throw new ArgumentNullException("stream is null");
73                         }
74
75                         if(!stream.CanRead) {
76                                 throw new ArgumentException("stream is not readable");
77                         }
78                         
79                         Reader = new ResourceReader (stream);
80                 }
81
82                 public ResourceSet (String fileName)
83                 {
84                         if(fileName==null) {
85                                 throw new ArgumentNullException("filename is null");
86                         }
87                         
88                         Reader = new ResourceReader (fileName);
89                 }
90
91                 public virtual void Close ()
92                 {
93                         Dispose (true);
94                 }
95
96                 public void Dispose()
97                 {
98                         Dispose (true);
99
100                         // If we are explicitly disposed, we can avoid finalization.
101                         GC.SuppressFinalize (this);
102                 }
103
104                 protected virtual void Dispose (bool disposing)
105                 {
106                         if (disposing) {
107                                 if(Reader!=null) {
108                                         Reader.Close();
109                                 }
110                         }
111
112                         Reader = null;
113                         Table = null;
114                 }
115
116                 public virtual Type GetDefaultReader ()
117                 {
118                         return (typeof (ResourceReader));
119                 } 
120                 public virtual Type GetDefaultWriter ()
121                 {
122                         return (typeof (ResourceWriter));
123                 }
124
125 #if (NET_1_1)
126
127                 [ComVisible (false)]
128                 public virtual IDictionaryEnumerator GetEnumerator ()
129                 {
130                         if (Table == null)
131                                 ReadResources ();
132                         return Table.GetEnumerator(); 
133                 }
134
135                 IEnumerator IEnumerable.GetEnumerator ()
136                 {
137                         return this.GetEnumerator (); 
138                 }
139
140 #endif
141
142                 public virtual object GetObject (string name)
143                 {
144                         if (name == null)
145                                 throw new ArgumentNullException ("The name parameter is null.");
146                         if (Reader == null)
147                                 throw new InvalidOperationException ("The ResourceSet has been closed.");
148
149                         if (Table == null) { 
150                                 ReadResources ();
151                         }
152                         
153                         return(Table[name]);
154                 }
155
156                 public virtual object GetObject (string name, bool ignoreCase)
157                 {
158                         if (name == null)
159                                 throw new ArgumentNullException ("The name parameter is null.");
160                         if (Reader == null)
161                                 throw new InvalidOperationException ("ResourceSet has been closed.");
162                         if (Table == null)
163                                 ReadResources ();
164
165                         if (ignoreCase) {
166                                 foreach (DictionaryEntry de in Table) {
167                                         string key = (string) de.Key;
168                                         if (String.Compare (key, name, true, CultureInfo.InvariantCulture) == 0)
169                                                 return de.Value;
170                                 }
171                                 return null;
172                         } else
173                                 return Table[name];
174                 }
175
176                 public virtual string GetString (string name)
177                 {
178                         Object o = GetObject (name);
179                         if (o == null)
180                                 return null;
181                         if (o is string)
182                                 return (string) o;
183                         throw new InvalidOperationException("Not a string");
184                 }
185
186                 public virtual string GetString (string name, bool ignoreCase)
187                 {
188                         Object o = GetObject (name, ignoreCase);
189                         if (o == null)
190                                 return null;
191                         if (o is string)
192                                 return (string) o;
193                         throw new InvalidOperationException("Not a string");
194                 }
195
196                 protected virtual void ReadResources ()
197                 {
198                         if (Reader == null)
199                                 throw new InvalidOperationException ("ResourceSet is closed.");
200                         
201                         IDictionaryEnumerator i = Reader.GetEnumerator();
202
203                         if (Table == null)
204                                 Table = new Hashtable ();
205                         i.Reset ();
206
207                         while (i.MoveNext ()) 
208                                 Table.Add (i.Key, i.Value);
209                 }
210
211 #if NET_2_0
212                 internal UnmanagedMemoryStream GetStream (string name)
213                 {
214                         if (Reader == null)
215                                 throw new InvalidOperationException ("ResourceSet is closed.");
216
217                         IDictionaryEnumerator i = Reader.GetEnumerator();
218                         i.Reset ();
219                         while (i.MoveNext ())
220                                 if (name == (string) i.Key)
221                                         return ((ResourceReader.ResourceEnumerator) i).ValueAsStream;
222                         return null;
223                 }
224 #endif
225         }
226 }