Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ResXResourceReader.cs « System.Resources « Managed.Windows.Forms « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 592f6ee5ffca3e7a5509d6369f78211ce72f4b15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2004-2005 Novell, Inc.
//
// Authors:
// 	Duncan Mak	duncan@ximian.com
//	Nick Drochak	ndrochak@gol.com
//	Paolo Molaro	lupus@ximian.com
//	Peter Bartok	pbartok@novell.com
//

// COMPLETE

using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;

namespace System.Resources
{
	public class ResXResourceReader : IResourceReader, IDisposable
	{
		#region Local Variables
		private Stream			stream;
		private XmlTextReader		reader;
		private Hashtable		hasht;
		private ITypeResolutionService	typeresolver;
		#endregion	// Local Variables

		#region Constructors & Destructor
		public ResXResourceReader (Stream stream)
		{
			if (stream == null)
				throw new ArgumentNullException ("Value cannot be null.");
			
			if (!stream.CanRead)
				throw new ArgumentException ("Stream was not readable.");

			this.stream = stream;
			basic_setup ();
		}

		public ResXResourceReader (Stream stream, ITypeResolutionService typeresolver) : this(stream) {
			this.typeresolver = typeresolver;
		}
		
		public ResXResourceReader (string fileName)
		{
			stream = File.OpenRead (fileName);
			basic_setup ();
		}

		public ResXResourceReader (string fileName, ITypeResolutionService typeresolver) : this(fileName) {
			this.typeresolver = typeresolver;
		}

		public ResXResourceReader(TextReader reader) {
			this.reader = new XmlTextReader(reader);
			this.hasht = new Hashtable();

			load_data();
		}

		public ResXResourceReader (TextReader reader, ITypeResolutionService typeresolver) : this(reader) {
			this.typeresolver = typeresolver;
		}

		~ResXResourceReader() {
			Dispose(false);
		}
		#endregion	// Constructors & Destructor


		#region Private Methods
		void basic_setup () {
			reader = new XmlTextReader (stream);
			hasht = new Hashtable ();

			if (!IsStreamValid()){
				throw new ArgumentException("Stream is not a valid .resx file!  It was possibly truncated.");
			}
			load_data ();
		}
		
		static string get_attr (XmlTextReader reader, string name) {
			if (!reader.HasAttributes)
				return null;
			for (int i = 0; i < reader.AttributeCount; i++) {
				reader.MoveToAttribute (i);
				if (String.Compare (reader.Name, name, true) == 0) {
					string v = reader.Value;
					reader.MoveToElement ();
					return v;
				}
			}
			reader.MoveToElement ();
			return null;
		}


		static string get_value (XmlTextReader reader, string name)
		{
			return get_value (reader, name, true);
		}

		// Returns the value of the next XML element with the specified
		// name from the reader. canBeCdata == true specifies that
		// the element may be a CDATA node as well.
		static string get_value (XmlTextReader reader, string name, bool canBeCdata) {
			bool gotelement = false;
			while (reader.Read ()) {
				if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, name, true) == 0) {
					gotelement = true;
					break;
				}
				if (canBeCdata && reader.NodeType == XmlNodeType.CDATA)
				   return reader.Value;
			}
			if (!gotelement)
				return null;
			while (reader.Read ()) {
				if (reader.NodeType == XmlNodeType.Text || reader.NodeType == XmlNodeType.CDATA) {
					string v = reader.Value;
					return v;
				}
				else if (reader.NodeType == XmlNodeType.EndElement && reader.Value == string.Empty)
				{
					string v = reader.Value;
					return v;
				}
			}
			return null;
		}

		private bool IsStreamValid() {
			bool gotroot = false;
			bool gotmime = false;
			
			while (reader.Read ()) {
				if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "root", true) == 0) {
					gotroot = true;
					break;
				}
			}
			if (!gotroot)
				return false;
			while (reader.Read ()) {
				if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "resheader", true) == 0) {
					string v = get_attr (reader, "name");
					if (v != null && String.Compare (v, "resmimetype", true) == 0) {
						v = get_value (reader, "value");
						if (String.Compare (v, "text/microsoft-resx", true) == 0) {
							gotmime = true;
							break;
						}
					}
				} else if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "data", true) == 0) {
					/* resheader apparently can appear anywhere, so we collect
					 * the data even if we haven't validated yet.
					 */
					string n = get_attr (reader, "name");
					if (n != null) {
						string v = get_value (reader, "value");
						hasht [n] = v;
					}
				}
			}
			return gotmime;
		}

		private void load_data ()
		{
			while (reader.Read ()) {
				if (reader.NodeType == XmlNodeType.Element && String.Compare (reader.Name, "data", true) == 0) {
					string n = get_attr (reader, "name");
					string t = get_attr (reader, "type");
					string mt = get_attr (reader, "mimetype");

					Type tt = t == null ? null : Type.GetType (t);

					if (t != null && tt == null) {
						throw new SystemException ("The type `" + t +"' could not be resolved");
					}
					if (tt == typeof (ResXNullRef)) {
						hasht [n] = null;
						continue;
					}
					if (n != null) {
						object v = null;
						string val = get_value (reader, "value");

						if (mt != null && tt != null) {
							TypeConverter c = TypeDescriptor.GetConverter (tt);
							v = c.ConvertFrom (Convert.FromBase64String (val));
						} else if (tt != null) {
							// MS seems to handle Byte[] without any mimetype :-(
							if (t.StartsWith("System.Byte[], mscorlib")) {
								v = Convert.FromBase64String(val);
							} else {
								TypeConverter c = TypeDescriptor.GetConverter (tt);
								v = c.ConvertFromInvariantString (val);
							}
						} else if (mt != null) {
							byte [] data = Convert.FromBase64String (val);
							BinaryFormatter f = new BinaryFormatter ();
							using (MemoryStream s = new MemoryStream (data)) {
								v = f.Deserialize (s);
							}
						} else {
							v = val;
						}
						hasht [n] = v;
					}
				}
			}
		}

		private Type GetType(string type) {
			if (typeresolver == null) {
				return Type.GetType(type);
			} else {
				return typeresolver.GetType(type);
			}
		}
		#endregion	// Private Methods

		#region Public Methods
		public void Close ()
		{
			if (stream != null) {
				stream.Close ();
				stream = null;
			}

			if (reader != null) {
				reader.Close();
				reader = null;
			}
		}
		
		public IDictionaryEnumerator GetEnumerator () {
			if (null == stream){
				throw new InvalidOperationException("ResourceReader is closed.");
			}
			else {
				return hasht.GetEnumerator ();
			}
		}
		
		IEnumerator IEnumerable.GetEnumerator ()
		{
			return ((IResourceReader) this).GetEnumerator();
		}
		
		void IDisposable.Dispose ()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing) {
			if (disposing) {
				Close();
			}
		}

		public static ResXResourceReader FromFileContents(string fileContents) {
			return new ResXResourceReader(new StringReader(fileContents));
		}

		public static ResXResourceReader FromFileContents(string fileContents, ITypeResolutionService typeResolver) {
			return new ResXResourceReader(new StringReader(fileContents), typeResolver);
		}

		#endregion	// Public Methods
		
	}  // public sealed class ResXResourceReader
} // namespace System.Resources