Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / class / Managed.Windows.Forms / System.Resources / TypeConverterFromResXHandler.cs
1 //
2 // TypeConverterFromResXHandler.cs : Handles a resource that was stored 
3 // in a resx file by means of a typeconverter associated with the 
4 // resources type.
5 //
6 // Author:
7 //      Gary Barnett (gary.barnett.mono@gmail.com)
8 // 
9 // Copyright (C) Gary Barnett (2012)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 using System;
31 using System.Reflection;
32 using System.ComponentModel.Design;
33 using System.ComponentModel;
34
35 namespace System.Resources {
36         internal class TypeConverterFromResXHandler : ResXDataNodeHandler, IWritableHandler {
37
38                 string dataString;
39                 string mime_type;
40                 string typeString;
41
42                 public TypeConverterFromResXHandler (string data, string _mime_type, string _typeString)
43                 {
44                         dataString = data;
45                         mime_type = _mime_type;
46                         typeString = _typeString;
47                 }
48
49                 #region implemented abstract members of System.Resources.ResXDataNodeHandler
50                 public override object GetValue (ITypeResolutionService typeResolver)
51                 {
52                         if (!String.IsNullOrEmpty(mime_type)
53                             && mime_type != ResXResourceWriter.ByteArraySerializedObjectMimeType)
54                                 return null;
55
56                         Type type = ResolveType (typeString, typeResolver);
57                         if (type == null)
58                                 throw new TypeLoadException();
59
60                         TypeConverter c = TypeDescriptor.GetConverter (type);
61                         if (c == null)
62                                 throw new TypeLoadException();
63
64                         return ConvertData (c);
65                 }
66
67                 public override object GetValue (AssemblyName[] assemblyNames)
68                 {
69                         if (!String.IsNullOrEmpty(mime_type)
70                             && mime_type != ResXResourceWriter.ByteArraySerializedObjectMimeType)
71                                 return null;
72
73                         Type type = ResolveType (typeString, assemblyNames);
74                         if (type == null)
75                                 throw new TypeLoadException();
76
77                         TypeConverter c = TypeDescriptor.GetConverter (type);
78                         if (c == null)
79                                 throw new TypeLoadException();
80
81                         return ConvertData (c);
82                 }
83
84                 public override string GetValueTypeName (ITypeResolutionService typeResolver)
85                 {
86                         Type type = ResolveType (typeString, typeResolver);
87
88                         if (type == null)
89                                 return typeString;
90                         else
91                                 return type.AssemblyQualifiedName;
92                 }
93
94                 public override string GetValueTypeName (AssemblyName [] assemblyNames)
95                 {
96                         Type type = ResolveType (typeString, assemblyNames);
97
98                         if (type == null)
99                                 return typeString;
100                         else
101                                 return type.AssemblyQualifiedName;
102                 }
103                 #endregion
104
105                 #region IWritableHandler implementation
106                 public string DataString {
107                         get {
108                                 return dataString;
109                         }
110                 }
111                 #endregion
112
113                 object ConvertData (TypeConverter c)
114                 {
115                         if (mime_type == ResXResourceWriter.ByteArraySerializedObjectMimeType) {
116                                 if (c.CanConvertFrom (typeof (byte [])))
117                                         return c.ConvertFrom (Convert.FromBase64String (dataString));
118                         } else if (String.IsNullOrEmpty (mime_type)) {
119                                 if (c.CanConvertFrom (typeof (string)))
120                                         return c.ConvertFromInvariantString (dataString);
121                         }
122                         else
123                                 throw new Exception ("shouldnt get here, invalid mime type");
124
125                         throw new TypeLoadException ("No converter for this type found");
126                 }
127
128         }
129 }
130