2 // System.UriTypeConverter class
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.ComponentModel;
31 using System.ComponentModel.Design.Serialization;
33 using System.Globalization;
34 using System.Reflection;
39 class UriTypeConverter : TypeConverter {
41 public UriTypeConverter ()
46 private bool CanConvert (Type type)
48 if (type == typeof (string))
50 if (type == typeof (Uri))
55 return (type == typeof (InstanceDescriptor));
59 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
61 if (sourceType == null)
62 throw new ArgumentNullException ("sourceType");
64 return CanConvert (sourceType);
67 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
69 if (destinationType == null)
72 return CanConvert (destinationType);
75 public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
79 throw new NotSupportedException (Locale.GetText ("Cannot convert from value."));
81 throw new ArgumentNullException ("value");
85 if (!CanConvertFrom (context, value.GetType ()))
86 throw new NotSupportedException (Locale.GetText ("Cannot convert from value."));
91 string s = (value as string);
97 return new Uri (s, UriKind.RelativeOrAbsolute);
101 InstanceDescriptor id = (value as InstanceDescriptor);
106 return base.ConvertFrom (context, culture, value);
109 public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
111 if (!CanConvertTo (context, destinationType))
112 throw new NotSupportedException (Locale.GetText ("Cannot convert to destination type."));
114 Uri uri = (value as Uri);
116 if (destinationType == typeof (string))
117 return uri.ToString ();
118 if (destinationType == typeof (Uri))
121 if (destinationType == typeof (InstanceDescriptor)) {
122 ConstructorInfo ci = typeof (Uri).GetConstructor (new Type [2] { typeof (string), typeof (UriKind) });
123 return new InstanceDescriptor (ci , new object [] { uri.ToString (), uri.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative });
126 throw new NotSupportedException (Locale.GetText ("Cannot convert to destination type."));
131 throw new NotSupportedException (Locale.GetText ("Cannot convert to destination type."));
133 return base.ConvertTo (context, culture, value, destinationType);
138 public override bool IsValid (ITypeDescriptorContext context, object value)
143 // LAMESPEC: docs says that a string is valid only if we can create an URI
144 // from it. However all strings seems to be accepted (see unit tests)
145 return ((value is string) || (value is Uri));