ResXResourceReader.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // System.Resources.ResXResourceReader.cs
  3. //
  4. // Authors:
  5. // Duncan Mak <[email protected]>
  6. // Nick Drochak <[email protected]>
  7. // Paolo Molaro <[email protected]>
  8. //
  9. // 2001 (C) Ximian Inc, http://www.ximian.com
  10. //
  11. // TODO: Finish this
  12. using System.Collections;
  13. using System.Resources;
  14. using System.IO;
  15. using System.Xml;
  16. namespace System.Resources
  17. {
  18. public sealed class ResXResourceReader : IResourceReader, IDisposable
  19. {
  20. Stream stream;
  21. XmlTextReader reader;
  22. Hashtable hasht;
  23. // Constructors
  24. public ResXResourceReader (Stream stream)
  25. {
  26. if (stream == null)
  27. throw new ArgumentNullException ("Value cannot be null.");
  28. if (!stream.CanRead)
  29. throw new ArgumentException ("Stream was not readable.");
  30. this.stream = stream;
  31. reader = new XmlTextReader (stream);
  32. if (!IsStreamValid()){
  33. throw new ArgumentException("Stream is not a valid .resources file! It was possibly truncated.");
  34. }
  35. load_data ();
  36. }
  37. public ResXResourceReader (string fileName)
  38. {
  39. if (fileName == null)
  40. throw new ArgumentException ("Path cannot be null.");
  41. if (String.Empty == fileName)
  42. throw new ArgumentException("Empty path name is not legal.");
  43. if (!System.IO.File.Exists (fileName))
  44. throw new FileNotFoundException ("Could not find file " + Path.GetFullPath(fileName));
  45. stream = new FileStream (fileName, FileMode.Open);
  46. reader = new XmlTextReader (stream);
  47. if (!IsStreamValid()){
  48. throw new ArgumentException("Stream is not a valid .resources file! It was possibly truncated.");
  49. }
  50. load_data ();
  51. }
  52. static string get_attr (XmlTextReader reader, string name) {
  53. if (!reader.HasAttributes)
  54. return null;
  55. for (int i = 0; i < reader.AttributeCount; i++) {
  56. reader.MoveToAttribute (i);
  57. if (reader.Name == name) {
  58. string v = reader.Value;
  59. reader.MoveToElement ();
  60. return v;
  61. }
  62. }
  63. reader.MoveToElement ();
  64. return null;
  65. }
  66. static string get_value (XmlTextReader reader, string name) {
  67. bool gotelement = false;
  68. while (reader.Read ()) {
  69. if (reader.NodeType == XmlNodeType.Element && reader.Name == name) {
  70. gotelement = true;
  71. break;
  72. }
  73. }
  74. if (!gotelement)
  75. return null;
  76. while (reader.Read ()) {
  77. if (reader.NodeType == XmlNodeType.Text) {
  78. string v = reader.Value;
  79. return v;
  80. }
  81. }
  82. return null;
  83. }
  84. private bool IsStreamValid() {
  85. bool gotroot = false;
  86. bool gotmime = false;
  87. while (reader.Read ()) {
  88. if (reader.NodeType == XmlNodeType.Element && reader.Name == "root") {
  89. gotroot = true;
  90. break;
  91. }
  92. }
  93. if (!gotroot)
  94. return false;
  95. while (reader.Read ()) {
  96. if (reader.NodeType == XmlNodeType.Element && reader.Name == "resheader") {
  97. string v = get_attr (reader, "name");
  98. if (v != null && v == "resmimetype") {
  99. v = get_value (reader, "value");
  100. if (v == "text/microsoft-resx") {
  101. gotmime = true;
  102. break;
  103. }
  104. }
  105. }
  106. }
  107. return gotmime;
  108. }
  109. private void load_data ()
  110. {
  111. hasht = new Hashtable ();
  112. while (reader.Read ()) {
  113. if (reader.NodeType == XmlNodeType.Element && reader.Name == "data") {
  114. string n = get_attr (reader, "name");
  115. if (n != null) {
  116. string v = get_value (reader, "value");
  117. hasht [n] = v;
  118. }
  119. }
  120. }
  121. }
  122. public void Close ()
  123. {
  124. stream.Close ();
  125. stream = null;
  126. }
  127. public IDictionaryEnumerator GetEnumerator () {
  128. if (null == stream){
  129. throw new InvalidOperationException("ResourceReader is closed.");
  130. }
  131. else {
  132. return hasht.GetEnumerator ();
  133. }
  134. }
  135. IEnumerator IEnumerable.GetEnumerator ()
  136. {
  137. return ((IResourceReader) this).GetEnumerator();
  138. }
  139. [MonoTODO]
  140. void IDisposable.Dispose ()
  141. {
  142. // FIXME: is this all we need to do?
  143. Close();
  144. }
  145. } // public sealed class ResXResourceReader
  146. } // namespace System.Resources