ResourceSet.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // System.Resources.ResourceSet.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Dick Porter ([email protected])
  7. //
  8. // (C) 2001, 2002 Ximian, Inc. http://www.ximian.com
  9. //
  10. using System.Collections;
  11. using System.IO;
  12. namespace System.Resources
  13. {
  14. [Serializable]
  15. public class ResourceSet : IDisposable
  16. {
  17. protected IResourceReader Reader;
  18. protected Hashtable Table;
  19. // Constructors
  20. protected ResourceSet () {}
  21. public ResourceSet (IResourceReader reader)
  22. {
  23. if (reader == null)
  24. throw new ArgumentNullException ("The reader is null.");
  25. Reader = reader;
  26. }
  27. public ResourceSet (Stream stream)
  28. {
  29. if(stream==null) {
  30. throw new ArgumentNullException("stream is null");
  31. }
  32. if(!stream.CanRead) {
  33. throw new ArgumentException("stream is not readable");
  34. }
  35. Reader = new ResourceReader (stream);
  36. }
  37. public ResourceSet (String fileName)
  38. {
  39. if(fileName==null) {
  40. throw new ArgumentNullException("filename is null");
  41. }
  42. Reader = new ResourceReader (fileName);
  43. }
  44. public virtual void Close ()
  45. {
  46. Dispose (true);
  47. }
  48. public void Dispose()
  49. {
  50. Dispose (true);
  51. }
  52. protected virtual void Dispose (bool disposing)
  53. {
  54. if (disposing) {
  55. if(Reader!=null) {
  56. Reader.Close();
  57. }
  58. }
  59. Reader = null;
  60. Table = null;
  61. }
  62. public virtual Type GetDefaultReader ()
  63. {
  64. return (typeof (ResourceReader));
  65. }
  66. public virtual Type GetDefaultWriter ()
  67. {
  68. return (typeof (ResourceWriter));
  69. }
  70. public virtual object GetObject (string name)
  71. {
  72. if (name == null)
  73. throw new ArgumentNullException ("The name parameter is null.");
  74. if (Reader == null)
  75. throw new InvalidOperationException ("The ResourceSet has been closed.");
  76. if (Table == null) {
  77. ReadResources ();
  78. }
  79. return(Table[name]);
  80. }
  81. public virtual object GetObject (string name, bool ignoreCase)
  82. {
  83. if (name == null)
  84. throw new ArgumentNullException ("The name parameter is null.");
  85. if (Reader == null)
  86. throw new InvalidOperationException ("ResourceSet has been closed.");
  87. if (Table == null)
  88. ReadResources ();
  89. if (ignoreCase) {
  90. foreach (DictionaryEntry de in Table) {
  91. string key = (string) de.Key;
  92. if (String.Compare (key, name, true) == 0)
  93. return de.Value;
  94. }
  95. return null;
  96. } else
  97. return Table[name];
  98. }
  99. public virtual string GetString (string name)
  100. {
  101. Object o = GetObject (name);
  102. if (o is string)
  103. return (string) o;
  104. throw new InvalidOperationException("Not a string");
  105. }
  106. public virtual string GetString (string name, bool ignoreCase)
  107. {
  108. Object o = GetObject (name, ignoreCase);
  109. if (o is string)
  110. return (string) o;
  111. throw new InvalidOperationException("Not a string");
  112. }
  113. protected virtual void ReadResources ()
  114. {
  115. IDictionaryEnumerator i = Reader.GetEnumerator();
  116. if (Table == null)
  117. Table = new Hashtable ();
  118. i.Reset ();
  119. while (i.MoveNext ())
  120. Table.Add (i.Key, i.Value);
  121. }
  122. }
  123. }