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