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) 2005 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok (pbartok@novell.com)
24 // Gert Driesen (drieseng@users.sourceforge.net)
28 using System.ComponentModel;
33 using System.Reflection;
36 namespace System.Resources {
38 [TypeConverter(typeof(ResXFileRef.Converter))]
39 public class ResXFileRef {
40 public class Converter : TypeConverter {
44 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
45 return sourceType == typeof(string);
48 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
49 return destinationType == typeof(string);
52 public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
55 if ( !(value is String)) {
59 string [] parts = ResXFileRef.Parse ((string) value);
61 if (parts.Length == 1)
62 throw new ArgumentException ("value");
65 Type type = Type.GetType (parts [1]);
67 if (type == typeof(string)) {
69 if (parts.Length > 2) {
70 encoding = Encoding.GetEncoding (parts [2]);
72 encoding = Encoding.Default;
75 using (TextReader reader = new StreamReader(parts [0], encoding)) {
76 return reader.ReadToEnd();
81 using (FileStream file = new FileStream (parts [0], FileMode.Open, FileAccess.Read, FileShare.Read)) {
82 buffer = new byte [file.Length];
83 file.Read(buffer, 0, (int) file.Length);
86 if (type == typeof(System.Byte[]))
90 if (type == typeof (Bitmap) && Path.GetExtension (parts [0]) == ".ico") {
91 MemoryStream ms = new MemoryStream (buffer);
92 return new Icon (ms).ToBitmap ();
96 return Activator.CreateInstance(type, BindingFlags.CreateInstance
97 | BindingFlags.Public | BindingFlags.Instance, null,
98 new object[] { new MemoryStream (buffer) }, culture);
101 public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
102 if (destinationType != typeof(String)) {
103 return base.ConvertTo (context, culture, value, destinationType);
106 return ((ResXFileRef)value).ToString();
110 private string filename;
111 private string typename;
112 private Encoding textFileEncoding;
114 public ResXFileRef (string fileName, string typeName)
117 if (fileName == null)
118 throw new ArgumentNullException ("fileName");
119 if (typeName == null)
120 throw new ArgumentNullException ("typeName");
123 this.filename = fileName;
124 this.typename = typeName;
132 ResXFileRef (string fileName, string typeName, Encoding textFileEncoding)
133 : this (fileName, typeName)
135 this.textFileEncoding = textFileEncoding;
144 get { return filename; }
152 Encoding TextFileEncoding {
153 get { return textFileEncoding; }
162 get { return typename; }
165 public override string ToString() {
166 StringBuilder sb = new StringBuilder ();
167 if (filename != null) {
168 sb.Append (filename);
171 if (typename != null) {
172 sb.Append (typename);
174 if (textFileEncoding != null) {
176 sb.Append (textFileEncoding.WebName);
178 return sb.ToString ();
181 internal static string [] Parse (string fileRef)
183 // we cannot return ResXFileRef, as that would mean we'd have to
184 // instantiate the encoding, and we do not always need this
187 throw new ArgumentNullException ("fileRef");
189 return fileRef.Split (';');