ResourceSet.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // System.Resources.ResourceSet.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Dick Porter ([email protected])
  7. // Andreas Nahr ([email protected])
  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. using System.Collections;
  32. using System.IO;
  33. using System.Globalization;
  34. using System.Runtime.InteropServices;
  35. using System.Security.Permissions;
  36. namespace System.Resources
  37. {
  38. [Serializable]
  39. public class ResourceSet : IDisposable
  40. #if (NET_1_1)
  41. , IEnumerable
  42. #endif
  43. {
  44. protected IResourceReader Reader;
  45. protected Hashtable Table;
  46. // Constructors
  47. protected ResourceSet () {}
  48. public ResourceSet (IResourceReader reader)
  49. {
  50. if (reader == null)
  51. throw new ArgumentNullException ("The reader is null.");
  52. Reader = reader;
  53. }
  54. [SecurityPermission (SecurityAction.LinkDemand, SerializationFormatter = true)]
  55. public ResourceSet (Stream stream)
  56. {
  57. if(stream==null) {
  58. throw new ArgumentNullException("stream is null");
  59. }
  60. if(!stream.CanRead) {
  61. throw new ArgumentException("stream is not readable");
  62. }
  63. Reader = new ResourceReader (stream);
  64. }
  65. public ResourceSet (String fileName)
  66. {
  67. if(fileName==null) {
  68. throw new ArgumentNullException("filename is null");
  69. }
  70. Reader = new ResourceReader (fileName);
  71. }
  72. public virtual void Close ()
  73. {
  74. Dispose (true);
  75. }
  76. public void Dispose()
  77. {
  78. Dispose (true);
  79. }
  80. protected virtual void Dispose (bool disposing)
  81. {
  82. if (disposing) {
  83. if(Reader!=null) {
  84. Reader.Close();
  85. }
  86. }
  87. Reader = null;
  88. Table = null;
  89. }
  90. public virtual Type GetDefaultReader ()
  91. {
  92. return (typeof (ResourceReader));
  93. }
  94. public virtual Type GetDefaultWriter ()
  95. {
  96. return (typeof (ResourceWriter));
  97. }
  98. #if (NET_1_1)
  99. [ComVisible (false)]
  100. public virtual IDictionaryEnumerator GetEnumerator ()
  101. {
  102. if (Table == null)
  103. ReadResources ();
  104. return Table.GetEnumerator();
  105. }
  106. IEnumerator IEnumerable.GetEnumerator ()
  107. {
  108. return this.GetEnumerator ();
  109. }
  110. #endif
  111. public virtual object GetObject (string name)
  112. {
  113. if (name == null)
  114. throw new ArgumentNullException ("The name parameter is null.");
  115. if (Reader == null)
  116. throw new InvalidOperationException ("The ResourceSet has been closed.");
  117. if (Table == null) {
  118. ReadResources ();
  119. }
  120. return(Table[name]);
  121. }
  122. public virtual object GetObject (string name, bool ignoreCase)
  123. {
  124. if (name == null)
  125. throw new ArgumentNullException ("The name parameter is null.");
  126. if (Reader == null)
  127. throw new InvalidOperationException ("ResourceSet has been closed.");
  128. if (Table == null)
  129. ReadResources ();
  130. if (ignoreCase) {
  131. foreach (DictionaryEntry de in Table) {
  132. string key = (string) de.Key;
  133. if (String.Compare (key, name, true, CultureInfo.InvariantCulture) == 0)
  134. return de.Value;
  135. }
  136. return null;
  137. } else
  138. return Table[name];
  139. }
  140. public virtual string GetString (string name)
  141. {
  142. Object o = GetObject (name);
  143. if (o == null)
  144. return null;
  145. if (o is string)
  146. return (string) o;
  147. throw new InvalidOperationException("Not a string");
  148. }
  149. public virtual string GetString (string name, bool ignoreCase)
  150. {
  151. Object o = GetObject (name, ignoreCase);
  152. if (o == null)
  153. return null;
  154. if (o is string)
  155. return (string) o;
  156. throw new InvalidOperationException("Not a string");
  157. }
  158. protected virtual void ReadResources ()
  159. {
  160. if (Reader == null)
  161. throw new InvalidOperationException ("ResourceSet is closed.");
  162. IDictionaryEnumerator i = Reader.GetEnumerator();
  163. if (Table == null)
  164. Table = new Hashtable ();
  165. i.Reset ();
  166. while (i.MoveNext ())
  167. Table.Add (i.Key, i.Value);
  168. }
  169. }
  170. }