ResXResourceReader.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. basic_setup ();
  32. }
  33. public ResXResourceReader (string fileName)
  34. {
  35. if (fileName == null)
  36. throw new ArgumentException ("Path cannot be null.");
  37. if (String.Empty == fileName)
  38. throw new ArgumentException("Empty path name is not legal.");
  39. if (!System.IO.File.Exists (fileName))
  40. throw new FileNotFoundException ("Could not find file " + Path.GetFullPath(fileName));
  41. stream = new FileStream (fileName, FileMode.Open);
  42. basic_setup ();
  43. }
  44. void basic_setup () {
  45. reader = new XmlTextReader (stream);
  46. hasht = new Hashtable ();
  47. if (!IsStreamValid()){
  48. throw new ArgumentException("Stream is not a valid .resx 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 (String.Compare (reader.Name, name, true) == 0) {
  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 && String.Compare (reader.Name, name, true) == 0) {
  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 && String.Compare (reader.Name, "root", true) == 0) {
  89. gotroot = true;
  90. break;
  91. }
  92. }
  93. if (!gotroot)
  94. return false;
  95. while (reader.Read ()) {
  96. if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "resheader", true) == 0) {
  97. string v = get_attr (reader, "name");
  98. if (v != null && String.Compare (v, "resmimetype", true) == 0) {
  99. v = get_value (reader, "value");
  100. if (String.Compare (v, "text/microsoft-resx", true) == 0) {
  101. gotmime = true;
  102. break;
  103. }
  104. }
  105. } else if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "data", true) == 0) {
  106. /* resheader apparently can appear anywhere, so we collect
  107. * the data even if we haven't validated yet.
  108. */
  109. string n = get_attr (reader, "name");
  110. if (n != null) {
  111. string v = get_value (reader, "value");
  112. hasht [n] = v;
  113. }
  114. }
  115. }
  116. return gotmime;
  117. }
  118. private void load_data ()
  119. {
  120. while (reader.Read ()) {
  121. if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "data", true) == 0) {
  122. string n = get_attr (reader, "name");
  123. if (n != null) {
  124. string v = get_value (reader, "value");
  125. hasht [n] = v;
  126. }
  127. }
  128. }
  129. }
  130. public void Close ()
  131. {
  132. stream.Close ();
  133. stream = null;
  134. }
  135. public IDictionaryEnumerator GetEnumerator () {
  136. if (null == stream){
  137. throw new InvalidOperationException("ResourceReader is closed.");
  138. }
  139. else {
  140. return hasht.GetEnumerator ();
  141. }
  142. }
  143. IEnumerator IEnumerable.GetEnumerator ()
  144. {
  145. return ((IResourceReader) this).GetEnumerator();
  146. }
  147. [MonoTODO]
  148. void IDisposable.Dispose ()
  149. {
  150. // FIXME: is this all we need to do?
  151. Close();
  152. }
  153. } // public sealed class ResXResourceReader
  154. } // namespace System.Resources