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;
31 using System.Reflection;
34 namespace System.Resources {
36 [TypeConverter(typeof(ResXFileRef.Converter))]
48 class Converter : TypeConverter {
52 public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
53 return sourceType == typeof(string);
56 public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
57 return destinationType == typeof(string);
60 public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
63 if ( !(value is String)) {
67 string [] parts = ResXFileRef.Parse ((string) value);
68 if (parts.Length == 1)
69 throw new ArgumentException ("value");
71 Type type = Type.GetType (parts [1]);
72 if (type == typeof(string)) {
74 if (parts.Length > 2) {
75 encoding = Encoding.GetEncoding (parts [2]);
77 encoding = Encoding.Default;
80 using (TextReader reader = new StreamReader(parts [0], encoding)) {
81 return reader.ReadToEnd();
85 using (FileStream file = new FileStream (parts [0], FileMode.Open, FileAccess.Read, FileShare.Read)) {
86 buffer = new byte [file.Length];
87 file.Read(buffer, 0, (int) file.Length);
90 if (type == typeof(System.Byte[]))
93 if (type == typeof (Bitmap) && Path.GetExtension (parts [0]) == ".ico") {
94 MemoryStream ms = new MemoryStream (buffer);
95 return new Icon (ms).ToBitmap ();
98 if (type == typeof (MemoryStream))
99 return new MemoryStream (buffer);
101 return Activator.CreateInstance(type, BindingFlags.CreateInstance
102 | BindingFlags.Public | BindingFlags.Instance, null,
103 new object[] { new MemoryStream (buffer) }, culture);
106 public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
107 if (destinationType != typeof(String)) {
108 return base.ConvertTo (context, culture, value, destinationType);
111 return ((ResXFileRef)value).ToString();
115 private string filename;
116 private string typename;
117 private Encoding textFileEncoding;
119 public ResXFileRef (string fileName, string typeName)
121 if (fileName == null)
122 throw new ArgumentNullException ("fileName");
123 if (typeName == null)
124 throw new ArgumentNullException ("typeName");
126 this.filename = fileName;
127 this.typename = typeName;
130 public ResXFileRef (string fileName, string typeName, Encoding textFileEncoding)
131 : this (fileName, typeName)
133 this.textFileEncoding = textFileEncoding;
136 public string FileName {
137 get { return filename; }
140 public Encoding TextFileEncoding {
141 get { return textFileEncoding; }
144 public string TypeName {
145 get { return typename; }
148 public override string ToString() {
149 StringBuilder sb = new StringBuilder ();
150 if (filename != null) {
151 sb.Append (filename);
154 if (typename != null) {
155 sb.Append (typename);
157 if (textFileEncoding != null) {
159 sb.Append (textFileEncoding.WebName);
161 return sb.ToString ();
164 internal static string [] Parse (string fileRef)
166 // we cannot return ResXFileRef, as that would mean we'd have to
167 // instantiate the encoding, and we do not always need this
170 throw new ArgumentNullException ("fileRef");
172 return fileRef.Split (';');