-
Notifications
You must be signed in to change notification settings - Fork 940
/
ResXFileRef.cs
301 lines (269 loc) · 10.7 KB
/
ResXFileRef.cs
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
301
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable disable
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
namespace System.Resources
{
/// <summary>
/// ResX File Reference class. This allows the developer to represent
/// a link to an external resource. When the resource manager asks
/// for the value of the resource item, the external resource is loaded.
/// </summary>
[TypeConverter(typeof(Converter))]
public class ResXFileRef
{
private string fileName;
private readonly string typeName;
private Encoding textFileEncoding;
/// <summary>
/// Creates a new ResXFileRef that points to the specified file.
/// The type refered to by typeName must support a constructor
/// that accepts a System.IO.Stream as a parameter.
/// </summary>
public ResXFileRef(string fileName, string typeName)
{
this.fileName = fileName ?? throw new ArgumentNullException(nameof(fileName));
this.typeName = typeName ?? throw new ArgumentNullException(nameof(typeName));
}
/// <summary>
/// Creates a new ResXFileRef that points to the specified file.
/// The type refered to by typeName must support a constructor
/// that accepts a System.IO.Stream as a parameter.
/// </summary>
public ResXFileRef(string fileName, string typeName, Encoding textFileEncoding) : this(fileName, typeName)
{
this.textFileEncoding = textFileEncoding;
}
internal ResXFileRef Clone()
{
return new ResXFileRef(fileName, typeName, textFileEncoding);
}
public string FileName
{
get
{
return fileName;
}
}
public string TypeName
{
get
{
return typeName;
}
}
public Encoding TextFileEncoding
{
get
{
return textFileEncoding;
}
}
/// <summary>
/// path1+result = path2
/// A string which is the relative path difference between path1 and
/// path2 such that if path1 and the calculated difference are used
/// as arguments to Combine(), path2 is returned
/// </summary>
private static string PathDifference(string path1, string path2, bool compareCase)
{
int i;
int si = -1;
for (i = 0; (i < path1.Length) && (i < path2.Length); ++i)
{
if ((path1[i] != path2[i]) && (compareCase || (char.ToLower(path1[i], CultureInfo.InvariantCulture) != char.ToLower(path2[i], CultureInfo.InvariantCulture))))
{
break;
}
if (path1[i] == Path.DirectorySeparatorChar)
{
si = i;
}
}
if (i == 0)
{
return path2;
}
if ((i == path1.Length) && (i == path2.Length))
{
return string.Empty;
}
StringBuilder relPath = new StringBuilder();
for (; i < path1.Length; ++i)
{
if (path1[i] == Path.DirectorySeparatorChar)
{
relPath.Append(".." + Path.DirectorySeparatorChar);
}
}
return relPath.ToString() + path2.Substring(si + 1);
}
internal void MakeFilePathRelative(string basePath)
{
if (string.IsNullOrEmpty(basePath))
{
return;
}
fileName = PathDifference(basePath, fileName, false);
}
public override string ToString()
{
string result = string.Empty;
if (fileName.IndexOf(';') != -1 || fileName.IndexOf('\"') != -1)
{
result += ("\"" + fileName + "\";");
}
else
{
result += (fileName + ";");
}
result += typeName;
if (textFileEncoding != null)
{
result += (";" + textFileEncoding.WebName);
}
return result;
}
public class Converter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context,
Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return false;
}
public override bool CanConvertTo(ITypeDescriptorContext context,
Type destinationType)
{
return destinationType == typeof(string);
}
public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture,
object value,
Type destinationType)
{
object created = null;
if (destinationType == typeof(string))
{
created = ((ResXFileRef)value).ToString();
}
return created;
}
// "value" is the parameter name of ConvertFrom, which calls this method.
internal static string[] ParseResxFileRefString(string stringValue)
{
string[] result = null;
if (stringValue != null)
{
stringValue = stringValue.Trim();
string fileName;
string remainingString;
if (stringValue.StartsWith("\""))
{
int lastIndexOfQuote = stringValue.LastIndexOf('\"');
if (lastIndexOfQuote - 1 < 0)
{
throw new ArgumentException(nameof(stringValue));
}
fileName = stringValue.Substring(1, lastIndexOfQuote - 1); // remove the quotes in" ..... "
if (lastIndexOfQuote + 2 > stringValue.Length)
{
throw new ArgumentException(nameof(stringValue));
}
remainingString = stringValue.Substring(lastIndexOfQuote + 2);
}
else
{
int nextSemiColumn = stringValue.IndexOf(';');
if (nextSemiColumn == -1)
{
throw new ArgumentException(nameof(stringValue));
}
fileName = stringValue.Substring(0, nextSemiColumn);
if (nextSemiColumn + 1 > stringValue.Length)
{
throw new ArgumentException(nameof(stringValue));
}
remainingString = stringValue.Substring(nextSemiColumn + 1);
}
string[] parts = remainingString.Split(';');
if (parts.Length > 1)
{
result = new string[] { fileName, parts[0], parts[1] };
}
else if (parts.Length > 0)
{
result = new string[] { fileName, parts[0] };
}
else
{
result = new string[] { fileName };
}
}
return result;
}
public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture,
object value)
{
if (value is string stringValue)
{
string[] parts = ParseResxFileRefString(stringValue);
string fileName = parts[0];
Type toCreate = Type.GetType(parts[1], true);
// special case string and byte[]
if (toCreate == typeof(string))
{
// we have a string, now we need to check the encoding
Encoding textFileEncoding =
parts.Length > 2
? Encoding.GetEncoding(parts[2])
: Encoding.Default;
using (StreamReader sr = new StreamReader(fileName, textFileEncoding))
{
return sr.ReadToEnd();
}
}
// this is a regular file, we call it's constructor with a stream as a parameter
// or if it's a byte array we just return that
byte[] temp = null;
using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
Debug.Assert(fileStream != null, "Couldn't open " + fileName);
temp = new byte[fileStream.Length];
fileStream.Read(temp, 0, (int)fileStream.Length);
}
if (toCreate == typeof(byte[]))
{
return temp;
}
MemoryStream memStream = new MemoryStream(temp);
if (toCreate == typeof(MemoryStream))
{
return memStream;
}
if (toCreate == typeof(Bitmap) && fileName.EndsWith(".ico"))
{
// we special case the .ico bitmaps because GDI+ destroy the alpha channel component and
// we don't want that to happen
Icon ico = new Icon(memStream);
return ico.ToBitmap();
}
return Activator.CreateInstance(toCreate, BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance, null, new object[] { memStream }, null);
}
return null;
}
}
}
}