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
42 public class ResourceSet : IDisposable, IEnumerable
45 protected IResourceReader Reader;
46 protected Hashtable Table;
48 [NonSerialized] Hashtable table_nocase;
51 protected ResourceSet ()
53 Table = new Hashtable ();
54 resources_read = true;
57 public ResourceSet (IResourceReader reader)
60 throw new ArgumentNullException ("reader");
61 Table = new Hashtable ();
65 internal bool IsDisposed {
71 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
72 public ResourceSet (Stream stream)
74 Table = new Hashtable ();
75 Reader = new ResourceReader (stream);
78 internal ResourceSet (UnmanagedMemoryStream stream)
80 Table = new Hashtable ();
81 Reader = new ResourceReader (stream);
84 public ResourceSet (string fileName)
86 Table = new Hashtable ();
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));
126 public virtual IDictionaryEnumerator GetEnumerator ()
129 throw new ObjectDisposedException ("ResourceSet is closed.");
131 return Table.GetEnumerator();
134 IEnumerator IEnumerable.GetEnumerator ()
136 return this.GetEnumerator ();
139 private object GetObjectInternal (string name, bool ignoreCase)
142 throw new ArgumentNullException ("name");
144 throw new ObjectDisposedException ("ResourceSet is closed.");
151 if (table_nocase == null) {
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);
162 return table_nocase [name];
165 public virtual object GetObject (string name)
167 return GetObjectInternal (name, false);
170 public virtual object GetObject (string name, bool ignoreCase)
172 return GetObjectInternal (name, ignoreCase);
175 private string GetStringInternal (string name, bool ignoreCase)
177 object value = GetObject (name, ignoreCase);
181 string s = (value as string);
183 throw new InvalidOperationException (string.Format (
184 "Resource '{0}' is not a String. Use " +
185 "GetObject instead.", name));
190 public virtual string GetString (string name)
192 return GetStringInternal (name, false);
195 public virtual string GetString (string name, bool ignoreCase)
197 return GetStringInternal (name, ignoreCase);
200 protected virtual void ReadResources ()
206 throw new ObjectDisposedException ("ResourceSet is closed.");
211 IDictionaryEnumerator i = Reader.GetEnumerator();
213 while (i.MoveNext ())
214 Table.Add (i.Key, i.Value);
215 resources_read = true;
219 internal UnmanagedMemoryStream GetStream (string name, bool ignoreCase)
222 throw new ObjectDisposedException ("ResourceSet is closed.");
224 IDictionaryEnumerator i = Reader.GetEnumerator();
226 while (i.MoveNext ()){
227 if (String.Compare (name, (string) i.Key, ignoreCase) == 0)
228 return ((ResourceReader.ResourceEnumerator) i).ValueAsStream;