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