2 // System.Resources.ResourceSet.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Dick Porter (dick@ximian.com)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
9 // (C) 2001, 2002 Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
34 using System.Globalization;
35 using System.Runtime.InteropServices;
36 using System.Security.Permissions;
38 namespace System.Resources
44 public class ResourceSet : IDisposable
55 protected IResourceReader Reader;
56 protected Hashtable Table;
59 private bool disposed;
62 protected ResourceSet ()
64 Table = new Hashtable ();
67 public ResourceSet (IResourceReader reader)
70 throw new ArgumentNullException ("reader");
74 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
75 public ResourceSet (Stream stream)
77 Reader = new ResourceReader (stream);
80 internal ResourceSet (IntPtrStream stream)
82 Reader = new ResourceReader (stream);
85 public ResourceSet (string fileName)
87 Reader = new ResourceReader (fileName);
90 public virtual void Close ()
99 // If we are explicitly disposed, we can avoid finalization.
100 GC.SuppressFinalize (this);
103 protected virtual void Dispose (bool disposing)
115 public virtual Type GetDefaultReader ()
117 return (typeof (ResourceReader));
120 public virtual Type GetDefaultWriter ()
122 return (typeof (ResourceWriter));
127 public virtual IDictionaryEnumerator GetEnumerator ()
131 throw new ObjectDisposedException ("ResourceSet is closed.");
133 throw new InvalidOperationException ("ResourceSet is closed.");
137 return Table.GetEnumerator();
140 IEnumerator IEnumerable.GetEnumerator ()
142 return this.GetEnumerator ();
146 private object GetObjectInternal (string name, bool ignoreCase)
149 throw new ArgumentNullException ("name");
152 throw new ObjectDisposedException ("ResourceSet is closed.");
154 throw new InvalidOperationException ("ResourceSet is closed.");
160 foreach (DictionaryEntry de in Table) {
161 string key = (string) de.Key;
162 if (String.Compare (key, name, true, CultureInfo.InvariantCulture) == 0)
170 public virtual object GetObject (string name)
172 return GetObjectInternal (name, false);
175 public virtual object GetObject (string name, bool ignoreCase)
177 return GetObjectInternal (name, ignoreCase);
180 private string GetStringInternal (string name, bool ignoreCase)
182 object value = GetObject (name, ignoreCase);
186 string s = (value as string);
188 throw new InvalidOperationException (string.Format (
189 "Resource '{0}' is not a String. Use " +
190 "GetObject instead.", name));
195 public virtual string GetString (string name)
197 return GetStringInternal (name, false);
200 public virtual string GetString (string name, bool ignoreCase)
202 return GetStringInternal (name, ignoreCase);
205 protected virtual void ReadResources ()
209 throw new ObjectDisposedException ("ResourceSet is closed.");
211 throw new InvalidOperationException ("ResourceSet is closed.");
214 IDictionaryEnumerator i = Reader.GetEnumerator();
217 Table = new Hashtable ();
220 while (i.MoveNext ())
221 Table.Add (i.Key, i.Value);
225 internal UnmanagedMemoryStream GetStream (string name, bool ignoreCase)
228 throw new ObjectDisposedException ("ResourceSet is closed.");
230 IDictionaryEnumerator i = Reader.GetEnumerator();
232 while (i.MoveNext ()){
233 if (String.Compare (name, (string) i.Key, ignoreCase) == 0)
234 return ((ResourceReader.ResourceEnumerator) i).ValueAsStream;