2010-06-28 Alan McGovern <amcgovern@novell.com>
[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 #if NET_2_0
30
31 using System.ComponentModel;
32 #if !NET_2_1
33 using System.ComponentModel.Design.Serialization;
34 #endif
35 using System.Globalization;
36 using System.Reflection;
37
38 namespace System {
39
40         public
41 #if MOONLIGHT
42         sealed
43 #endif
44         class UriTypeConverter : TypeConverter {
45
46                 public UriTypeConverter ()
47                 {
48                 }
49
50
51                 private bool CanConvert (Type type)
52                 {
53                         if (type == typeof (string))
54                                 return true;
55                         if (type == typeof (Uri))
56                                 return true;
57 #if NET_2_1
58                         return false;
59 #else
60                         return (type == typeof (InstanceDescriptor));
61 #endif
62                 }
63
64                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
65                 {
66                         if (sourceType == null)
67                                 throw new ArgumentNullException ("sourceType");
68
69                         return CanConvert (sourceType);
70                 }
71
72                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
73                 {
74                         if (destinationType == null)
75                                 return false;
76
77                         return CanConvert (destinationType);
78                 }
79
80                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
81                 {
82                         if (value == null) {
83 #if NET_2_1
84                                 throw new NotSupportedException (Locale.GetText ("Cannot convert from value."));
85 #else
86                                 throw new ArgumentNullException ("value");
87 #endif
88                         }
89
90                         Console.WriteLine ("Converting: '{0}' of type '{1}'", value, value.GetType ().Name);
91                         if (!CanConvertFrom (context, value.GetType ()))
92                                 throw new NotSupportedException (Locale.GetText ("Cannot convert from value."));
93
94                         if (value is Uri)
95                                 return value;
96
97                         string s = (value as string);
98                         if (s != null)
99 #if NET_2_1
100                                 if (s == "")
101                                         return null;
102 #endif
103                                 return new Uri (s, UriKind.RelativeOrAbsolute);
104 #if !NET_2_1
105                         InstanceDescriptor id = (value as InstanceDescriptor);
106                         if (id != null) {
107                                 return id.Invoke ();
108                         }
109 #endif
110                         return base.ConvertFrom (context, culture, value);
111                 }
112
113                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
114                 {
115                         if (!CanConvertTo (context, destinationType))
116                                 throw new NotSupportedException (Locale.GetText ("Cannot convert to destination type."));
117
118                         Uri uri = (value as Uri);
119                         if (uri != null) {
120                                 if (destinationType == typeof (string))
121                                         return uri.ToString ();
122                                 if (destinationType == typeof (Uri))
123                                         return uri;
124 #if !NET_2_1
125                                 if (destinationType == typeof (InstanceDescriptor)) {
126                                         ConstructorInfo ci = typeof (Uri).GetConstructor (new Type [2] { typeof (string), typeof (UriKind) });
127                                         return new InstanceDescriptor (ci , new object [] { uri.ToString (), uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative });
128                                 }
129 #else
130                                 throw new NotSupportedException (Locale.GetText ("Cannot convert to destination type."));
131 #endif
132                         } else {
133 #if NET_2_1
134                                 throw new NotSupportedException (Locale.GetText ("Cannot convert to destination type."));
135 #endif
136                         }
137
138                         return base.ConvertTo (context, culture, value, destinationType);
139                 }
140
141 #if !NET_2_1
142                 public override bool IsValid (ITypeDescriptorContext context, object value)
143                 {
144                         if (value == null)
145                                 return false;
146
147                         // LAMESPEC: docs says that a string is valid only if we can create an URI
148                         // from it. However all strings seems to be accepted (see unit tests)
149                         return ((value is string) || (value is Uri));
150                 }
151 #endif
152         }
153 }
154
155 #endif