* roottypes.cs: Rename from tree.cs.
[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 using System.ComponentModel.Design.Serialization;
33 using System.Globalization;
34 using System.Reflection;
35
36 namespace System {
37
38         public class UriTypeConverter : TypeConverter {
39
40                 public UriTypeConverter ()
41                 {
42                 }
43
44
45                 private bool CanConvert (Type type)
46                 {
47                         if (type == typeof (string))
48                                 return true;
49                         if (type == typeof (Uri))
50                                 return true;
51                         return (type == typeof (InstanceDescriptor));
52                 }
53
54                 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
55                 {
56                         if (sourceType == null)
57                                 throw new ArgumentNullException ("sourceType");
58
59                         return CanConvert (sourceType);
60                 }
61
62                 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
63                 {
64                         if (destinationType == null)
65                                 return false;
66
67                         return CanConvert (destinationType);
68                 }
69
70                 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
71                 {
72                         if (value == null)
73                                 throw new ArgumentNullException ("value");
74
75                         if (!CanConvertTo (context, value.GetType ()))
76                                 throw new NotSupportedException (Locale.GetText ("Cannot convert from value."));
77
78                         if (value is Uri)
79                                 return value;
80
81                         string s = (value as string);
82                         if (s != null)
83                                 return new Uri (s);
84
85                         InstanceDescriptor id = (value as InstanceDescriptor);
86                         if (id != null) {
87                                 return id.Invoke ();
88                         }
89
90                         return base.ConvertFrom (context, culture, value);
91                 }
92
93                 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
94                 {
95                         if (!CanConvertTo (context, destinationType))
96                                 throw new NotSupportedException (Locale.GetText ("Cannot convert to destination type."));
97
98                         Uri uri = (value as Uri);
99                         if (uri != null) {
100                                 if (destinationType == typeof (string))
101                                         return uri.AbsoluteUri;
102                                 if (destinationType == typeof (Uri))
103                                         return uri;
104                                 if (destinationType == typeof (InstanceDescriptor)) {
105                                         ConstructorInfo ci = typeof (Uri).GetConstructor (new Type [1] { typeof (string) });
106                                         return new InstanceDescriptor (ci , new object [] { uri.AbsoluteUri });
107                                 }
108                         }
109
110                         return base.ConvertTo (context, culture, value, destinationType);
111                 }
112
113                 public override bool IsValid (ITypeDescriptorContext context, object value)
114                 {
115                         if (value == null)
116                                 return false;
117
118                         // LAMESPEC: docs says that a string is valid only if we can create an URI
119                         // from it. However all strings seems to be accepted (see unit tests)
120                         return ((value is string) || (value is Uri));
121                 }
122         }
123 }
124
125 #endif