2007-02-19 Igor Zelmanovich <igorz@mainsoft.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Resources / ResXFileRef.cs
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:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
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.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Peter Bartok    (pbartok@novell.com)
24 //      Gert Driesen    (drieseng@users.sourceforge.net)
25 //
26
27 using System;
28 using System.ComponentModel;
29 using System.IO;
30 using System.Reflection;
31 using System.Text;
32
33 namespace System.Resources {
34         [Serializable]
35         [TypeConverter(typeof(ResXFileRef.Converter))]
36         public class ResXFileRef {
37                 public class Converter : TypeConverter {
38                         public Converter() {
39                         }
40
41                         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
42                                 return sourceType == typeof(string);
43                         }
44
45                         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
46                                 return destinationType == typeof(string);
47                         }
48
49                         public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
50                                 byte[]          buffer;
51
52                                 if ( !(value is String)) {
53                                         return null;
54                                 }
55
56                                 string [] parts = ResXFileRef.Parse ((string) value);
57 #if NET_2_0
58                                 if (parts.Length == 1)
59                                         throw new ArgumentException ("value");
60 #endif
61
62                                 Type type = Type.GetType (parts [1]);
63 #if NET_2_0
64                                 if (type == typeof(string)) {
65                                         Encoding encoding;
66                                         if (parts.Length > 2) {
67                                                 encoding = Encoding.GetEncoding (parts [2]);
68                                         } else {
69                                                 encoding = Encoding.Default;
70                                         }
71
72                                         using (TextReader reader = new StreamReader(parts [0], encoding)) {
73                                                 return reader.ReadToEnd();
74                                         }
75                                 }
76 #endif
77
78                                 using (FileStream file = new FileStream (parts [0], FileMode.Open, FileAccess.Read, FileShare.Read)) {
79                                         buffer = new byte [file.Length];
80                                         file.Read(buffer, 0, (int) file.Length);
81                                 }
82
83                                 return Activator.CreateInstance(type, BindingFlags.CreateInstance
84                                         | BindingFlags.Public | BindingFlags.Instance, null, 
85                                         new object[] { new MemoryStream (buffer) }, culture);
86                         }
87
88                         public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
89                                 if (destinationType != typeof(String)) {
90                                         return base.ConvertTo (context, culture, value, destinationType);
91                                 }
92
93                                 return ((ResXFileRef)value).ToString();
94                         }
95                 }
96
97                 private string filename;
98                 private string typename;
99                 private Encoding textFileEncoding;
100
101                 public ResXFileRef (string fileName, string typeName)
102                 {
103 #if NET_2_0
104                         if (fileName == null)
105                                 throw new ArgumentNullException ("fileName");
106                         if (typeName == null)
107                                 throw new ArgumentNullException ("typeName");
108 #endif
109
110                         this.filename = fileName;
111                         this.typename = typeName;
112                 }
113
114 #if NET_2_0
115                 public
116 #else
117                 internal
118 #endif
119                 ResXFileRef (string fileName, string typeName, Encoding textFileEncoding)
120                         : this (fileName, typeName) 
121                 {
122                         this.textFileEncoding = textFileEncoding;
123                 }
124
125 #if NET_2_0
126                 public
127 #else
128                 internal
129 #endif
130                 string FileName {
131                         get { return filename; }
132                 }
133
134 #if NET_2_0
135                 public
136 #else
137                 internal
138 #endif
139                 Encoding TextFileEncoding {
140                         get { return textFileEncoding; }
141                 }
142
143 #if NET_2_0
144                 public
145 #else
146                 internal
147 #endif
148                 string TypeName {
149                         get { return typename; }
150                 }
151
152                 public override string ToString() {
153                         StringBuilder sb = new StringBuilder ();
154                         if (filename != null) {
155                                 sb.Append (filename);
156                         }
157                         sb.Append (';');
158                         if (typename != null) {
159                                 sb.Append (typename);
160                         }
161                         if (textFileEncoding != null) {
162                                 sb.Append (';');
163                                 sb.Append (textFileEncoding.WebName);
164                         }
165                         return sb.ToString ();
166                 }
167
168                 internal static string [] Parse (string fileRef)
169                 {
170                         // we cannot return ResXFileRef, as that would mean we'd have to
171                         // instantiate the encoding, and we do not always need this
172
173                         if (fileRef == null)
174                                 throw new ArgumentNullException ("fileRef");
175
176                         return fileRef.Split (';');
177                 }
178         }
179 }