Merge pull request #900 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / System.ServiceModel.Web / System.ServiceModel.Dispatcher / QueryStringConverter.cs
1 //
2 // QueryStringConverter.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 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 using System;
29 using System.ComponentModel;
30 using System.Globalization;
31 using System.ServiceModel;
32 using System.ServiceModel.Description;
33
34 namespace System.ServiceModel.Dispatcher
35 {
36         public class QueryStringConverter
37         {
38                 // "Service Operation Parameters and URLs"
39                 // http://msdn2.microsoft.com/en-us/library/bb412172.aspx
40                 public virtual bool CanConvert (Type type)
41                 {
42                         switch (Type.GetTypeCode (type)) {
43                         case TypeCode.DBNull:
44                         case TypeCode.Empty:
45                                 return false;
46                         case TypeCode.Object:
47                                 if (type == typeof (TimeSpan))
48                                         return true;
49                                 if (type == typeof (DateTimeOffset))
50                                         return true;
51                                 if (type == typeof (Guid))
52                                         return true;
53                                 if (type == typeof (object))
54                                         return true;
55 //                              if (type.GetCustomAttributes (typeof (TypeConverterAttribute), true).Length > 0)
56 //                                      return true;
57                                 return false;
58                         default:
59                                 return true;
60                         }
61                 }
62
63                 public virtual object ConvertStringToValue (string parameter, Type parameterType)
64                 {
65                         if (parameterType == null)
66                                 throw new ArgumentNullException ("parameterType");
67
68                         if (!CanConvert (parameterType))
69                                 throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
70
71                         if (parameterType.IsEnum)
72                                 return Enum.Parse(parameterType, parameter, true);
73
74                         switch (Type.GetTypeCode (parameterType)) {
75                         case TypeCode.String:
76                                 return parameter;
77                         case TypeCode.Char:
78                                 return parameter != null ? Char.Parse (parameter) : default (char);
79                         case TypeCode.SByte:
80                                 return parameter != null ? SByte.Parse (parameter, CultureInfo.InvariantCulture): default (sbyte);
81                         case TypeCode.Byte:
82                                 return parameter != null ? Byte.Parse (parameter, CultureInfo.InvariantCulture): default (byte);
83                         case TypeCode.Int16:
84                                 return parameter != null ? Int16.Parse (parameter, CultureInfo.InvariantCulture): default (short);
85                         case TypeCode.Int32:
86                                 return parameter != null ? Int32.Parse (parameter, CultureInfo.InvariantCulture): default (int);
87                         case TypeCode.Int64:
88                                 return parameter != null ? Int64.Parse (parameter, CultureInfo.InvariantCulture): default (long);
89                         case TypeCode.UInt16:
90                                 return parameter != null ? UInt16.Parse (parameter, CultureInfo.InvariantCulture): default (ushort);
91                         case TypeCode.UInt32:
92                                 return parameter != null ? UInt32.Parse (parameter, CultureInfo.InvariantCulture): default (uint);
93                         case TypeCode.UInt64:
94                                 return parameter != null ? UInt64.Parse (parameter, CultureInfo.InvariantCulture): default (ulong);
95                         case TypeCode.DateTime:
96                                 return parameter != null ? DateTime.Parse (parameter, CultureInfo.InvariantCulture): default (DateTime);
97                         case TypeCode.Boolean:
98                                 return parameter != null ? Boolean.Parse (parameter): default (bool);
99                         case TypeCode.Single:
100                                 return parameter != null ? Single.Parse (parameter, CultureInfo.InvariantCulture): default (float);
101                         case TypeCode.Double:
102                                 return parameter != null ? Double.Parse (parameter, CultureInfo.InvariantCulture): default (double);
103                         case TypeCode.Decimal:
104                                 return parameter != null ? Decimal.Parse (parameter, CultureInfo.InvariantCulture): default (decimal);
105                         case TypeCode.Object:
106                                 if (parameterType == typeof (TimeSpan))
107                                         return TimeSpan.Parse (parameter);
108                                 if (parameterType == typeof (DateTimeOffset))
109                                         return DateTimeOffset.Parse (parameter, CultureInfo.InvariantCulture);
110                                 if (parameterType == typeof (Guid))
111                                         return new Guid (parameter);
112                                 break;
113                         }
114                         throw new NotSupportedException (String.Format ("Cannot convert parameter string '{0}' to parameter type '{1}'", parameter, parameterType));
115                 }
116
117                 public virtual string ConvertValueToString (object parameter, Type parameterType)
118                 {
119                         if (parameterType == null)
120                                 throw new ArgumentNullException ("parameterType");
121                         if (parameterType.IsValueType && parameter == null)
122                                 throw new ArgumentNullException ("parameter");
123
124                         if (parameter == null)
125                                 return null;
126
127                         if (parameter.GetType () != parameterType)
128                                 throw new InvalidCastException (String.Format ("This QueryStringConverter does not support cast from {0} to {1}", parameter.GetType (), parameterType));
129
130                         if (!CanConvert (parameterType))
131                                 throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
132
133                         if (parameter is IFormattable)
134                                 ((IFormattable) parameter).ToString (null, CultureInfo.InvariantCulture);
135                         return parameter.ToString ();
136                 }
137         }
138 }