Mark tests as not working under TARGET_JVM
[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 #if NET_2_0
30 using System.Drawing;
31 #endif
32 using System.IO;
33 using System.Reflection;
34 using System.Text;
35
36 namespace System.Resources {
37         [Serializable]
38         [TypeConverter(typeof(ResXFileRef.Converter))]
39         public class ResXFileRef {
40                 public class Converter : TypeConverter {
41                         public Converter() {
42                         }
43
44                         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
45                                 return sourceType == typeof(string);
46                         }
47
48                         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
49                                 return destinationType == typeof(string);
50                         }
51
52                         public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
53                                 byte[]          buffer;
54
55                                 if ( !(value is String)) {
56                                         return null;
57                                 }
58
59                                 string [] parts = ResXFileRef.Parse ((string) value);
60 #if NET_2_0
61                                 if (parts.Length == 1)
62                                         throw new ArgumentException ("value");
63 #endif
64
65                                 Type type = Type.GetType (parts [1]);
66 #if NET_2_0
67                                 if (type == typeof(string)) {
68                                         Encoding encoding;
69                                         if (parts.Length > 2) {
70                                                 encoding = Encoding.GetEncoding (parts [2]);
71                                         } else {
72                                                 encoding = Encoding.Default;
73                                         }
74
75                                         using (TextReader reader = new StreamReader(parts [0], encoding)) {
76                                                 return reader.ReadToEnd();
77                                         }
78                                 }
79 #endif
80
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);
84                                 }
85
86                                 if (type == typeof(System.Byte[])) 
87                                         return buffer;
88
89 #if NET_2_0
90                                 if (type == typeof (Bitmap) && Path.GetExtension (parts [0]) == ".ico") {
91                                         MemoryStream ms = new MemoryStream (buffer);
92                                         return new Icon (ms).ToBitmap ();
93                                 }
94 #endif
95
96                                 return Activator.CreateInstance(type, BindingFlags.CreateInstance
97                                         | BindingFlags.Public | BindingFlags.Instance, null, 
98                                         new object[] { new MemoryStream (buffer) }, culture);
99                         }
100
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);
104                                 }
105
106                                 return ((ResXFileRef)value).ToString();
107                         }
108                 }
109
110                 private string filename;
111                 private string typename;
112                 private Encoding textFileEncoding;
113
114                 public ResXFileRef (string fileName, string typeName)
115                 {
116 #if NET_2_0
117                         if (fileName == null)
118                                 throw new ArgumentNullException ("fileName");
119                         if (typeName == null)
120                                 throw new ArgumentNullException ("typeName");
121 #endif
122
123                         this.filename = fileName;
124                         this.typename = typeName;
125                 }
126
127 #if NET_2_0
128                 public
129 #else
130                 internal
131 #endif
132                 ResXFileRef (string fileName, string typeName, Encoding textFileEncoding)
133                         : this (fileName, typeName) 
134                 {
135                         this.textFileEncoding = textFileEncoding;
136                 }
137
138 #if NET_2_0
139                 public
140 #else
141                 internal
142 #endif
143                 string FileName {
144                         get { return filename; }
145                 }
146
147 #if NET_2_0
148                 public
149 #else
150                 internal
151 #endif
152                 Encoding TextFileEncoding {
153                         get { return textFileEncoding; }
154                 }
155
156 #if NET_2_0
157                 public
158 #else
159                 internal
160 #endif
161                 string TypeName {
162                         get { return typename; }
163                 }
164
165                 public override string ToString() {
166                         StringBuilder sb = new StringBuilder ();
167                         if (filename != null) {
168                                 sb.Append (filename);
169                         }
170                         sb.Append (';');
171                         if (typename != null) {
172                                 sb.Append (typename);
173                         }
174                         if (textFileEncoding != null) {
175                                 sb.Append (';');
176                                 sb.Append (textFileEncoding.WebName);
177                         }
178                         return sb.ToString ();
179                 }
180
181                 internal static string [] Parse (string fileRef)
182                 {
183                         // we cannot return ResXFileRef, as that would mean we'd have to
184                         // instantiate the encoding, and we do not always need this
185
186                         if (fileRef == null)
187                                 throw new ArgumentNullException ("fileRef");
188
189                         return fileRef.Split (';');
190                 }
191         }
192 }