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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2004-2005 Novell, Inc.
23 // Duncan Mak duncan@ximian.com
24 // Nick Drochak ndrochak@gol.com
25 // Paolo Molaro lupus@ximian.com
26 // Peter Bartok pbartok@novell.com
32 using System.Collections;
33 using System.ComponentModel;
34 using System.ComponentModel.Design;
35 using System.Globalization;
37 using System.Resources;
38 using System.Runtime.Serialization.Formatters.Binary;
41 namespace System.Resources
43 public class ResXResourceReader : IResourceReader, IDisposable
45 #region Local Variables
46 private Stream stream;
47 private XmlTextReader reader;
48 private Hashtable hasht;
49 private ITypeResolutionService typeresolver;
50 #endregion // Local Variables
52 #region Constructors & Destructor
53 public ResXResourceReader (Stream stream)
56 throw new ArgumentNullException ("Value cannot be null.");
59 throw new ArgumentException ("Stream was not readable.");
65 public ResXResourceReader (Stream stream, ITypeResolutionService typeresolver) : this(stream) {
66 this.typeresolver = typeresolver;
69 public ResXResourceReader (string fileName)
71 stream = File.OpenRead (fileName);
75 public ResXResourceReader (string fileName, ITypeResolutionService typeresolver) : this(fileName) {
76 this.typeresolver = typeresolver;
79 public ResXResourceReader(TextReader reader) {
80 this.reader = new XmlTextReader(reader);
81 this.hasht = new Hashtable();
86 public ResXResourceReader (TextReader reader, ITypeResolutionService typeresolver) : this(reader) {
87 this.typeresolver = typeresolver;
90 ~ResXResourceReader() {
93 #endregion // Constructors & Destructor
96 #region Private Methods
98 reader = new XmlTextReader (stream);
99 hasht = new Hashtable ();
101 if (!IsStreamValid()){
102 throw new ArgumentException("Stream is not a valid .resx file! It was possibly truncated.");
107 static string get_attr (XmlTextReader reader, string name) {
108 if (!reader.HasAttributes)
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 ();
118 reader.MoveToElement ();
123 static string get_value (XmlTextReader reader, string name)
125 return get_value (reader, name, true);
128 // Returns the value of the next XML element with the specified
129 // name from the reader. canBeCdata == true specifies that
130 // the element may be a CDATA node as well.
131 static string get_value (XmlTextReader reader, string name, bool canBeCdata) {
132 bool gotelement = false;
133 while (reader.Read ()) {
134 if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, name, true) == 0) {
138 if (canBeCdata && reader.NodeType == XmlNodeType.CDATA)
143 while (reader.Read ()) {
144 if (reader.NodeType == XmlNodeType.Text || reader.NodeType == XmlNodeType.CDATA) {
145 string v = reader.Value;
148 else if (reader.NodeType == XmlNodeType.EndElement && reader.Value == string.Empty)
150 string v = reader.Value;
157 private bool IsStreamValid() {
158 bool gotroot = false;
159 bool gotmime = false;
161 while (reader.Read ()) {
162 if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "root", true) == 0) {
169 while (reader.Read ()) {
170 if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "resheader", true) == 0) {
171 string v = get_attr (reader, "name");
172 if (v != null && String.Compare (v, "resmimetype", true) == 0) {
173 v = get_value (reader, "value");
174 if (String.Compare (v, "text/microsoft-resx", true) == 0) {
179 } else if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "data", true) == 0) {
180 /* resheader apparently can appear anywhere, so we collect
181 * the data even if we haven't validated yet.
189 private void load_data ()
191 while (reader.Read ()) {
192 if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "data", true) == 0)
198 void parse_data_node ()
200 string name = get_attr (reader, "name");
201 string type_name = get_attr (reader, "type");
202 string mime_type = get_attr (reader, "mimetype");
207 Type type = type_name == null ? null : ResolveType (type_name);
209 if (type_name != null && type == null)
210 throw new ArgumentException (String.Format (
211 "The type '{0}' of the element '{1}' could not be resolved.", type_name, name));
213 if (type == typeof (ResXNullRef)) {
218 string value = get_value (reader, "value");
222 TypeConverter c = TypeDescriptor.GetConverter (type);
226 } else if (c.CanConvertFrom (typeof (string))) {
227 obj = c.ConvertFromInvariantString (value);
228 } else if (c.CanConvertFrom (typeof (byte[]))) {
229 obj = c.ConvertFrom (Convert.FromBase64String (value));
231 // the type must be a byte[]
232 obj = Convert.FromBase64String(value);
234 } else if (mime_type != null && mime_type != String.Empty) {
235 if (mime_type == ResXResourceWriter.BinSerializedObjectMimeType) {
236 byte [] data = Convert.FromBase64String (value);
237 BinaryFormatter f = new BinaryFormatter ();
238 using (MemoryStream s = new MemoryStream (data)) {
239 obj = f.Deserialize (s);
251 private Type ResolveType (string type) {
252 if (typeresolver == null) {
253 return Type.GetType(type);
255 return typeresolver.GetType(type);
258 #endregion // Private Methods
260 #region Public Methods
263 if (stream != null) {
268 if (reader != null) {
274 public IDictionaryEnumerator GetEnumerator () {
276 throw new InvalidOperationException("ResourceReader is closed.");
279 return hasht.GetEnumerator ();
283 IEnumerator IEnumerable.GetEnumerator ()
285 return ((IResourceReader) this).GetEnumerator();
288 void IDisposable.Dispose ()
291 GC.SuppressFinalize(this);
294 protected virtual void Dispose(bool disposing) {
300 public static ResXResourceReader FromFileContents(string fileContents) {
301 return new ResXResourceReader(new StringReader(fileContents));
304 public static ResXResourceReader FromFileContents(string fileContents, ITypeResolutionService typeResolver) {
305 return new ResXResourceReader(new StringReader(fileContents), typeResolver);
308 #endregion // Public Methods
310 } // public sealed class ResXResourceReader
311 } // namespace System.Resources