New test.
[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         public class ResourceSet : IDisposable
42
43 #if (NET_1_1)
44                                                 , IEnumerable
45 #endif
46
47         {
48
49                 protected IResourceReader Reader;
50                 protected Hashtable Table;
51
52                 // Constructors
53                 protected ResourceSet () {}
54
55                 public ResourceSet (IResourceReader reader)
56                 {
57                         if (reader == null)
58                                 throw new ArgumentNullException ("The reader is null.");
59                         Reader = reader;
60                 }
61
62                 [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
63                 public ResourceSet (Stream stream)
64                 {
65                         if(stream==null) {
66                                 throw new ArgumentNullException("stream is null");
67                         }
68
69                         if(!stream.CanRead) {
70                                 throw new ArgumentException("stream is not readable");
71                         }
72                         
73                         Reader = new ResourceReader (stream);
74                 }
75
76                 public ResourceSet (String fileName)
77                 {
78                         if(fileName==null) {
79                                 throw new ArgumentNullException("filename is null");
80                         }
81                         
82                         Reader = new ResourceReader (fileName);
83                 }
84
85                 public virtual void Close ()
86                 {
87                         Dispose (true);
88                 }
89
90                 public void Dispose()
91                 {
92                         Dispose (true);
93                 }
94
95                 protected virtual void Dispose (bool disposing)
96                 {
97                         if (disposing) {
98                                 if(Reader!=null) {
99                                         Reader.Close();
100                                 }
101                         }
102
103                         Reader = null;
104                         Table = null;
105                 }
106
107                 public virtual Type GetDefaultReader ()
108                 {
109                         return (typeof (ResourceReader));
110                 } 
111                 public virtual Type GetDefaultWriter ()
112                 {
113                         return (typeof (ResourceWriter));
114                 }
115
116 #if (NET_1_1)
117
118                 [ComVisible (false)]
119                 public virtual IDictionaryEnumerator GetEnumerator ()
120                 {
121                         if (Table == null)
122                                 ReadResources ();
123                         return Table.GetEnumerator(); 
124                 }
125
126                 IEnumerator IEnumerable.GetEnumerator ()
127                 {
128                         return this.GetEnumerator (); 
129                 }
130
131 #endif
132
133                 public virtual object GetObject (string name)
134                 {
135                         if (name == null)
136                                 throw new ArgumentNullException ("The name parameter is null.");
137                         if (Reader == null)
138                                 throw new InvalidOperationException ("The ResourceSet has been closed.");
139
140                         if (Table == null) { 
141                                 ReadResources ();
142                         }
143                         
144                         return(Table[name]);
145                 }
146
147                 public virtual object GetObject (string name, bool ignoreCase)
148                 {
149                         if (name == null)
150                                 throw new ArgumentNullException ("The name parameter is null.");
151                         if (Reader == null)
152                                 throw new InvalidOperationException ("ResourceSet has been closed.");
153                         if (Table == null)
154                                 ReadResources ();
155
156                         if (ignoreCase) {
157                                 foreach (DictionaryEntry de in Table) {
158                                         string key = (string) de.Key;
159                                         if (String.Compare (key, name, true, CultureInfo.InvariantCulture) == 0)
160                                                 return de.Value;
161                                 }
162                                 return null;
163                         } else
164                                 return Table[name];
165                 }
166
167                 public virtual string GetString (string name)
168                 {
169                         Object o = GetObject (name);
170                         if (o == null)
171                                 return null;
172                         if (o is string)
173                                 return (string) o;
174                         throw new InvalidOperationException("Not a string");
175                 }
176
177                 public virtual string GetString (string name, bool ignoreCase)
178                 {
179                         Object o = GetObject (name, ignoreCase);
180                         if (o == null)
181                                 return null;
182                         if (o is string)
183                                 return (string) o;
184                         throw new InvalidOperationException("Not a string");
185                 }
186
187                 protected virtual void ReadResources ()
188                 {
189                         if (Reader == null)
190                                 throw new InvalidOperationException ("ResourceSet is closed.");
191                         
192                         IDictionaryEnumerator i = Reader.GetEnumerator();
193
194                         if (Table == null)
195                                 Table = new Hashtable ();
196                         i.Reset ();
197
198                         while (i.MoveNext ()) 
199                                 Table.Add (i.Key, i.Value);
200                 }
201         }
202 }