2008-05-08 Miguel de Icaza <miguel@novell.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 // 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                 [NonSerialized]
59                 private bool disposed;
60
61                 // Constructors
62                 protected ResourceSet ()
63                 {
64                         Table = new Hashtable ();
65                 }
66
67                 public ResourceSet (IResourceReader reader)
68                 {
69                         if (reader == null)
70                                 throw new ArgumentNullException ("reader");
71                         Reader = reader;
72                 }
73
74                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
75                 public ResourceSet (Stream stream)
76                 {
77                         Reader = new ResourceReader (stream);
78                 }
79
80                 internal ResourceSet (IntPtrStream stream)
81                 {
82                         Reader = new ResourceReader (stream);
83                 }
84                 
85                 public ResourceSet (string fileName)
86                 {
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                         disposed = true;
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 #if NET_1_1
126                 [ComVisible (false)]
127                 public virtual IDictionaryEnumerator GetEnumerator ()
128                 {
129                         if (disposed)
130 #if NET_2_0
131                                 throw new ObjectDisposedException ("ResourceSet is closed.");
132 #else
133                                 throw new InvalidOperationException ("ResourceSet is closed.");
134 #endif
135                         if (Table == null)
136                                 ReadResources ();
137                         return Table.GetEnumerator();
138                 }
139
140                 IEnumerator IEnumerable.GetEnumerator ()
141                 {
142                         return this.GetEnumerator ();
143                 }
144 #endif
145
146                 private object GetObjectInternal (string name, bool ignoreCase)
147                 {
148                         if (name == null)
149                                 throw new ArgumentNullException ("name");
150                         if (disposed)
151 #if NET_2_0
152                                 throw new ObjectDisposedException ("ResourceSet is closed.");
153 #else
154                                 throw new InvalidOperationException ("ResourceSet is closed.");
155 #endif
156                         if (Table == null)
157                                 ReadResources ();
158
159                         if (ignoreCase) {
160                                 foreach (DictionaryEntry de in Table) {
161                                         string key = (string) de.Key;
162                                         if (String.Compare (key, name, true, CultureInfo.InvariantCulture) == 0)
163                                                 return de.Value;
164                                 }
165                                 return null;
166                         } else
167                                 return Table [name];
168                 }
169
170                 public virtual object GetObject (string name)
171                 {
172                         return GetObjectInternal (name, false);
173                 }
174
175                 public virtual object GetObject (string name, bool ignoreCase)
176                 {
177                         return GetObjectInternal (name, ignoreCase);
178                 }
179
180                 private string GetStringInternal (string name, bool ignoreCase)
181                 {
182                         object value = GetObject (name, ignoreCase);
183                         if (value == null)
184                                 return null;
185
186                         string s = (value as string);
187                         if (s == null)
188                                 throw new InvalidOperationException (string.Format (
189                                         "Resource '{0}' is not a String. Use " +
190                                         "GetObject instead.", name));
191
192                         return s;
193                 }
194
195                 public virtual string GetString (string name)
196                 {
197                         return GetStringInternal (name, false);
198                 }
199
200                 public virtual string GetString (string name, bool ignoreCase)
201                 {
202                         return GetStringInternal (name, ignoreCase);
203                 }
204
205                 protected virtual void ReadResources ()
206                 {
207                         if (Reader == null)
208 #if NET_2_0
209                                 throw new ObjectDisposedException ("ResourceSet is closed.");
210 #else
211                                 throw new InvalidOperationException ("ResourceSet is closed.");
212 #endif
213                         
214                         IDictionaryEnumerator i = Reader.GetEnumerator();
215
216                         if (Table == null)
217                                 Table = new Hashtable ();
218                         i.Reset ();
219
220                         while (i.MoveNext ()) 
221                                 Table.Add (i.Key, i.Value);
222                 }
223
224 #if NET_2_0
225                 internal UnmanagedMemoryStream GetStream (string name, bool ignoreCase)
226                 {
227                         if (Reader == null)
228                                 throw new ObjectDisposedException ("ResourceSet is closed.");
229
230                         IDictionaryEnumerator i = Reader.GetEnumerator();
231                         i.Reset ();
232                         while (i.MoveNext ()){
233                                 if (String.Compare (name, (string) i.Key, ignoreCase) == 0)
234                                         return ((ResourceReader.ResourceEnumerator) i).ValueAsStream;
235                         }
236                         return null;
237                 }
238 #endif
239         }
240 }