Dang.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Resources / ResXResourceReader.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2004-2005 Novell, Inc.
21 //
22 // Authors:
23 //      Duncan Mak      duncan@ximian.com
24 //      Nick Drochak    ndrochak@gol.com
25 //      Paolo Molaro    lupus@ximian.com
26 //      Peter Bartok    pbartok@novell.com
27 //
28
29 // COMPLETE
30
31 using System;
32 using System.Collections;
33 using System.ComponentModel;
34 using System.ComponentModel.Design;
35 using System.Globalization;
36 using System.IO;
37 using System.Resources;
38 using System.Runtime.Serialization.Formatters.Binary;
39 using System.Xml;
40
41 namespace System.Resources
42 {
43         public class ResXResourceReader : IResourceReader, IDisposable
44         {
45                 #region Local Variables
46                 private Stream                  stream;
47                 private XmlTextReader           reader;
48                 private Hashtable               hasht;
49                 private ITypeResolutionService  typeresolver;
50                 #endregion      // Local Variables
51
52                 #region Constructors & Destructor
53                 public ResXResourceReader (Stream stream)
54                 {
55                         if (stream == null)
56                                 throw new ArgumentNullException ("Value cannot be null.");
57                         
58                         if (!stream.CanRead)
59                                 throw new ArgumentException ("Stream was not readable.");
60
61                         this.stream = stream;
62                         basic_setup ();
63                 }
64
65                 public ResXResourceReader (Stream stream, ITypeResolutionService typeresolver) : this(stream) {
66                         this.typeresolver = typeresolver;
67                 }
68                 
69                 public ResXResourceReader (string fileName)
70                 {
71                         stream = File.OpenRead (fileName);
72                         basic_setup ();
73                 }
74
75                 public ResXResourceReader (string fileName, ITypeResolutionService typeresolver) : this(fileName) {
76                         this.typeresolver = typeresolver;
77                 }
78
79                 public ResXResourceReader(TextReader reader) {
80                         this.reader = new XmlTextReader(reader);
81                         this.hasht = new Hashtable();
82
83                         load_data();
84                 }
85
86                 public ResXResourceReader (TextReader reader, ITypeResolutionService typeresolver) : this(reader) {
87                         this.typeresolver = typeresolver;
88                 }
89
90                 ~ResXResourceReader() {
91                         Dispose(false);
92                 }
93                 #endregion      // Constructors & Destructor
94
95
96                 #region Private Methods
97                 void basic_setup () {
98                         reader = new XmlTextReader (stream);
99                         hasht = new Hashtable ();
100
101                         if (!IsStreamValid()){
102                                 throw new ArgumentException("Stream is not a valid .resx file!  It was possibly truncated.");
103                         }
104                         load_data ();
105                 }
106                 
107                 static string get_attr (XmlTextReader reader, string name) {
108                         if (!reader.HasAttributes)
109                                 return null;
110                         for (int i = 0; i < reader.AttributeCount; i++) {
111                                 reader.MoveToAttribute (i);
112                                 if (String.Compare (reader.Name, name, true) == 0) {
113                                         string v = reader.Value;
114                                         reader.MoveToElement ();
115                                         return v;
116                                 }
117                         }
118                         reader.MoveToElement ();
119                         return null;
120                 }
121
122                 static string get_value (XmlTextReader reader, string name) {
123                         bool gotelement = false;
124                         while (reader.Read ()) {
125                                 if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, name, true) == 0) {
126                                         gotelement = true;
127                                         break;
128                                 }
129                         }
130                         if (!gotelement)
131                                 return null;
132                         while (reader.Read ()) {
133                                 if (reader.NodeType == XmlNodeType.Text || reader.NodeType == XmlNodeType.CDATA) {
134                                         string v = reader.Value;
135                                         return v;
136                                 }
137                                 else if (reader.NodeType == XmlNodeType.EndElement && reader.Value == string.Empty)
138                                 {
139                                         string v = reader.Value;
140                                         return v;
141                                 }
142                         }
143                         return null;
144                 }
145
146                 private bool IsStreamValid() {
147                         bool gotroot = false;
148                         bool gotmime = false;
149                         
150                         while (reader.Read ()) {
151                                 if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "root", true) == 0) {
152                                         gotroot = true;
153                                         break;
154                                 }
155                         }
156                         if (!gotroot)
157                                 return false;
158                         while (reader.Read ()) {
159                                 if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "resheader", true) == 0) {
160                                         string v = get_attr (reader, "name");
161                                         if (v != null && String.Compare (v, "resmimetype", true) == 0) {
162                                                 v = get_value (reader, "value");
163                                                 if (String.Compare (v, "text/microsoft-resx", true) == 0) {
164                                                         gotmime = true;
165                                                         break;
166                                                 }
167                                         }
168                                 } else if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "data", true) == 0) {
169                                         /* resheader apparently can appear anywhere, so we collect
170                                          * the data even if we haven't validated yet.
171                                          */
172                                         string n = get_attr (reader, "name");
173                                         if (n != null) {
174                                                 string v = get_value (reader, "value");
175                                                 hasht [n] = v;
176                                         }
177                                 }
178                         }
179                         return gotmime;
180                 }
181
182                 private void load_data ()
183                 {
184                         while (reader.Read ()) {
185                                 if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "data", true) == 0) {
186                                         string n = get_attr (reader, "name");
187                                         string t = get_attr (reader, "type");
188                                         string mt = get_attr (reader, "mimetype");
189
190                                         Type tt = t == null ? null : Type.GetType (t);
191
192                                         if (t != null && tt == null) {
193                                                 throw new SystemException ("The type `" + t +"' could not be resolved");
194                                         }
195                                         if (tt == typeof (ResXNullRef)) {
196                                                 hasht [n] = null;
197                                                 continue;
198                                         }
199                                         if (n != null) {
200                                                 object v = null;
201                                                 string val = get_value (reader, "value");
202
203                                                 if (mt != null && tt != null) {
204                                                         TypeConverter c = TypeDescriptor.GetConverter (tt);
205                                                         v = c.ConvertFrom (Convert.FromBase64String (val));
206                                                 } else if (tt != null) {
207                                                         // MS seems to handle Byte[] without any mimetype :-(
208                                                         if (t.StartsWith("System.Byte[], mscorlib")) {
209                                                                 v = Convert.FromBase64String(val);
210                                                         } else {
211                                                                 TypeConverter c = TypeDescriptor.GetConverter (tt);
212                                                                 v = c.ConvertFromInvariantString (val);
213                                                         }
214                                                 } else if (mt != null) {
215                                                         byte [] data = Convert.FromBase64String (val);
216                                                         BinaryFormatter f = new BinaryFormatter ();
217                                                         using (MemoryStream s = new MemoryStream (data)) {
218                                                                 v = f.Deserialize (s);
219                                                         }
220                                                 } else {
221                                                         v = val;
222                                                 }
223                                                 hasht [n] = v;
224                                         }
225                                 }
226                         }
227                 }
228
229                 private Type GetType(string type) {
230                         if (typeresolver == null) {
231                                 return Type.GetType(type);
232                         } else {
233                                 return typeresolver.GetType(type);
234                         }
235                 }
236                 #endregion      // Private Methods
237
238                 #region Public Methods
239                 public void Close ()
240                 {
241                         if (stream != null) {
242                                 stream.Close ();
243                                 stream = null;
244                         }
245
246                         if (reader != null) {
247                                 reader.Close();
248                                 reader = null;
249                         }
250                 }
251                 
252                 public IDictionaryEnumerator GetEnumerator () {
253                         if (null == stream){
254                                 throw new InvalidOperationException("ResourceReader is closed.");
255                         }
256                         else {
257                                 return hasht.GetEnumerator ();
258                         }
259                 }
260                 
261                 IEnumerator IEnumerable.GetEnumerator ()
262                 {
263                         return ((IResourceReader) this).GetEnumerator();
264                 }
265                 
266                 void IDisposable.Dispose ()
267                 {
268                         Dispose(true);
269                         GC.SuppressFinalize(this);
270                 }
271
272                 protected virtual void Dispose(bool disposing) {
273                         if (disposing) {
274                                 Close();
275                         }
276                 }
277
278                 public static ResXResourceReader FromFileContents(string fileContents) {
279                         return new ResXResourceReader(new StringReader(fileContents));
280                 }
281
282                 public static ResXResourceReader FromFileContents(string fileContents, ITypeResolutionService typeResolver) {
283                         return new ResXResourceReader(new StringReader(fileContents), typeResolver);
284                 }
285
286                 #endregion      // Public Methods
287                 
288         }  // public sealed class ResXResourceReader
289 } // namespace System.Resources