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