Merge pull request #167 from konrad-kruczynski/send_async_fix
[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         [ComVisible (true)]
42         public class ResourceSet : IDisposable, IEnumerable
43         {
44 #if !MOONLIGHT
45                 [NonSerialized]
46 #endif
47                 protected IResourceReader Reader;
48                 protected Hashtable Table;
49                 bool resources_read;
50                 [NonSerialized] Hashtable table_nocase;
51
52                 // Constructors
53                 protected ResourceSet ()
54                 {
55                         Table = new Hashtable ();
56                         resources_read = true;
57                 }
58
59                 public ResourceSet (IResourceReader reader)
60                 {
61                         if (reader == null)
62                                 throw new ArgumentNullException ("reader");
63                         Table = new Hashtable ();
64                         Reader = reader;
65                 }
66                 
67                 internal bool IsDisposed {
68                         get {
69                                 return Table == null;
70                         }
71                 }
72
73                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
74                 public ResourceSet (Stream stream)
75                 {
76                         Table = new Hashtable ();
77                         Reader = new ResourceReader (stream);
78                 }
79
80                 internal ResourceSet (UnmanagedMemoryStream stream)
81                 {
82                         Table = new Hashtable ();
83                         Reader = new ResourceReader (stream);
84                 }
85                 
86                 public ResourceSet (string fileName)
87                 {
88                         Table = new Hashtable ();
89                         Reader = new ResourceReader (fileName);
90                 }
91
92                 public virtual void Close ()
93                 {
94                         Dispose ();
95                 }
96
97                 public void Dispose()
98                 {
99                         Dispose (true);
100
101                         // If we are explicitly disposed, we can avoid finalization.
102                         GC.SuppressFinalize (this);
103                 }
104
105                 protected virtual void Dispose (bool disposing)
106                 {
107                         if (disposing) {
108                                 if(Reader != null)
109                                         Reader.Close();
110                         }
111
112                         Reader = null;
113                         Table = null;
114                         table_nocase = null;
115                 }
116
117                 public virtual Type GetDefaultReader ()
118                 {
119                         return (typeof (ResourceReader));
120                 }
121
122                 public virtual Type GetDefaultWriter ()
123                 {
124                         return (typeof (ResourceWriter));
125                 }
126
127                 [ComVisible (false)]
128                 public virtual IDictionaryEnumerator GetEnumerator ()
129                 {
130                         if (IsDisposed)
131                                 throw new ObjectDisposedException ("ResourceSet is closed.");
132                         ReadResources ();
133                         return Table.GetEnumerator();
134                 }
135
136                 IEnumerator IEnumerable.GetEnumerator ()
137                 {
138                         return this.GetEnumerator ();
139                 }
140
141                 private object GetObjectInternal (string name, bool ignoreCase)
142                 {
143                         if (name == null)
144                                 throw new ArgumentNullException ("name");
145                         if (IsDisposed)
146                                 throw new ObjectDisposedException ("ResourceSet is closed.");
147
148                         ReadResources ();
149
150                         if (!ignoreCase)
151                                 return Table [name];
152
153                         if (table_nocase == null) {
154                                 lock (Table) {
155                                         if (table_nocase == null) {
156                                                 Hashtable ht = new Hashtable (StringComparer.InvariantCultureIgnoreCase);
157                                                 foreach (DictionaryEntry de in Table) {
158                                                         ht.Add (de.Key, de.Value);
159                                                 }
160                                                 table_nocase = ht;
161                                         }
162                                 }
163                         }
164                         return table_nocase [name];
165                 }
166
167                 public virtual object GetObject (string name)
168                 {
169                         return GetObjectInternal (name, false);
170                 }
171
172                 public virtual object GetObject (string name, bool ignoreCase)
173                 {
174                         return GetObjectInternal (name, ignoreCase);
175                 }
176
177                 private string GetStringInternal (string name, bool ignoreCase)
178                 {
179                         object value = GetObject (name, ignoreCase);
180                         if (value == null)
181                                 return null;
182
183                         string s = (value as string);
184                         if (s == null)
185                                 throw new InvalidOperationException (string.Format (
186                                         "Resource '{0}' is not a String. Use " +
187                                         "GetObject instead.", name));
188
189                         return s;
190                 }
191
192                 public virtual string GetString (string name)
193                 {
194                         return GetStringInternal (name, false);
195                 }
196
197                 public virtual string GetString (string name, bool ignoreCase)
198                 {
199                         return GetStringInternal (name, ignoreCase);
200                 }
201
202                 protected virtual void ReadResources ()
203                 {
204                         if (resources_read)
205                                 return;
206
207                         if (IsDisposed)
208                                 throw new ObjectDisposedException ("ResourceSet is closed.");
209                         lock (Table) {
210                                 if (resources_read)
211                                         return;
212
213                                 IDictionaryEnumerator i = Reader.GetEnumerator();
214                                 i.Reset ();
215                                 while (i.MoveNext ()) 
216                                         Table.Add (i.Key, i.Value);
217                                 resources_read = true;
218                         }
219                 }
220
221                 internal UnmanagedMemoryStream GetStream (string name, bool ignoreCase)
222                 {
223                         if (IsDisposed)
224                                 throw new ObjectDisposedException ("ResourceSet is closed.");
225
226                         IDictionaryEnumerator i = Reader.GetEnumerator();
227                         i.Reset ();
228                         while (i.MoveNext ()){
229                                 if (String.Compare (name, (string) i.Key, ignoreCase) == 0)
230                                         return ((ResourceReader.ResourceEnumerator) i).ValueAsStream;
231                         }
232                         return null;
233                 }
234         }
235 }