Merge pull request #439 from mono-soc-2012/garyb/iconfix
[mono.git] / mcs / class / System / System / UriTypeConverter.cs
1 //
2 // System.UriTypeConverter class
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.ComponentModel;
30 #if !NET_2_1
31 using System.ComponentModel.Design.Serialization;
32 #endif
33 using System.Globalization;
34 using System.Reflection;
35
36 namespace System {
37
38         public
39 #if MOONLIGHT
40         sealed
41 #endif
42         class UriTypeConverter : TypeConverter {
43
44                 public UriTypeConverter ()
45                 {
46                 }
47
48
49                 private bool CanConvert (Type type)
50                 {
51                         if (type == typeof (string))
52                                 return true;
53                         if (type == typeof (Uri))
54                                 return true;
55 #if NET_2_1
56                         return false;
57 #else
58                         return (type == typeof (InstanceDescriptor));
59 #endif
60                 }
61
62                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
63                 {
64                         if (sourceType == null)
65                                 throw new ArgumentNullException ("sourceType");
66
67                         return CanConvert (sourceType);
68                 }
69
70                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
71                 {
72                         if (destinationType == null)
73                                 return false;
74
75                         return CanConvert (destinationType);
76                 }
77
78                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
79                 {
80                         if (value == null) {
81 #if NET_2_1
82                                 throw new NotSupportedException (Locale.GetText ("Cannot convert from value."));
83 #else
84                                 throw new ArgumentNullException ("value");
85 #endif
86                         }
87
88                         if (!CanConvertFrom (context, value.GetType ()))
89                                 throw new NotSupportedException (Locale.GetText ("Cannot convert from value."));
90
91                         if (value is Uri)
92                                 return value;
93
94                         string s = (value as string);
95                         if (s != null)
96 #if NET_2_1
97                                 if (s == "")
98                                         return null;
99 #endif
100                                 return new Uri (s, UriKind.RelativeOrAbsolute);
101 #if !NET_2_1
102                         InstanceDescriptor id = (value as InstanceDescriptor);
103                         if (id != null) {
104                                 return id.Invoke ();
105                         }
106 #endif
107                         return base.ConvertFrom (context, culture, value);
108                 }
109
110                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
111                 {
112                         if (!CanConvertTo (context, destinationType))
113                                 throw new NotSupportedException (Locale.GetText ("Cannot convert to destination type."));
114
115                         Uri uri = (value as Uri);
116                         if (uri != null) {
117                                 if (destinationType == typeof (string))
118                                         return uri.ToString ();
119                                 if (destinationType == typeof (Uri))
120                                         return uri;
121 #if !NET_2_1
122                                 if (destinationType == typeof (InstanceDescriptor)) {
123                                         ConstructorInfo ci = typeof (Uri).GetConstructor (new Type [2] { typeof (string), typeof (UriKind) });
124                                         return new InstanceDescriptor (ci , new object [] { uri.ToString (), uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative });
125                                 }
126 #else
127                                 throw new NotSupportedException (Locale.GetText ("Cannot convert to destination type."));
128 #endif
129                         } else {
130 #if NET_2_1
131                                 throw new NotSupportedException (Locale.GetText ("Cannot convert to destination type."));
132 #endif
133                         }
134
135                         return base.ConvertTo (context, culture, value, destinationType);
136                 }
137
138 #if !NET_2_1
139                 public override bool IsValid (ITypeDescriptorContext context, object value)
140                 {
141                         if (value == null)
142                                 return false;
143
144                         // LAMESPEC: docs says that a string is valid only if we can create an URI
145                         // from it. However all strings seems to be accepted (see unit tests)
146                         return ((value is string) || (value is Uri));
147                 }
148 #endif
149         }
150 }