ResXResourceReader.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. else if (reader.NodeType == XmlNodeType.EndElement && reader.Value == string.Empty)
  82. {
  83. string v = reader.Value;
  84. return v;
  85. }
  86. }
  87. return null;
  88. }
  89. private bool IsStreamValid() {
  90. bool gotroot = false;
  91. bool gotmime = false;
  92. while (reader.Read ()) {
  93. if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "root", true) == 0) {
  94. gotroot = true;
  95. break;
  96. }
  97. }
  98. if (!gotroot)
  99. return false;
  100. while (reader.Read ()) {
  101. if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "resheader", true) == 0) {
  102. string v = get_attr (reader, "name");
  103. if (v != null && String.Compare (v, "resmimetype", true) == 0) {
  104. v = get_value (reader, "value");
  105. if (String.Compare (v, "text/microsoft-resx", true) == 0) {
  106. gotmime = true;
  107. break;
  108. }
  109. }
  110. } else if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "data", true) == 0) {
  111. /* resheader apparently can appear anywhere, so we collect
  112. * the data even if we haven't validated yet.
  113. */
  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. return gotmime;
  122. }
  123. private void load_data ()
  124. {
  125. while (reader.Read ()) {
  126. if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "data", true) == 0) {
  127. string n = get_attr (reader, "name");
  128. if (n != null) {
  129. string v = get_value (reader, "value");
  130. hasht [n] = v;
  131. }
  132. }
  133. }
  134. }
  135. public void Close ()
  136. {
  137. stream.Close ();
  138. stream = null;
  139. }
  140. public IDictionaryEnumerator GetEnumerator () {
  141. if (null == stream){
  142. throw new InvalidOperationException("ResourceReader is closed.");
  143. }
  144. else {
  145. return hasht.GetEnumerator ();
  146. }
  147. }
  148. IEnumerator IEnumerable.GetEnumerator ()
  149. {
  150. return ((IResourceReader) this).GetEnumerator();
  151. }
  152. [MonoTODO]
  153. void IDisposable.Dispose ()
  154. {
  155. // FIXME: is this all we need to do?
  156. Close();
  157. }
  158. } // public sealed class ResXResourceReader
  159. } // namespace System.Resources