Add [Category ("NotWorking")] to failing test.
[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 #if !NET_2_1
78                         case TypeCode.Char:
79                                 return parameter != null ? Char.Parse (parameter) : default (char);
80 #endif
81                         case TypeCode.SByte:
82                                 return parameter != null ? SByte.Parse (parameter, CultureInfo.InvariantCulture): default (sbyte);
83                         case TypeCode.Byte:
84                                 return parameter != null ? Byte.Parse (parameter, CultureInfo.InvariantCulture): default (byte);
85                         case TypeCode.Int16:
86                                 return parameter != null ? Int16.Parse (parameter, CultureInfo.InvariantCulture): default (short);
87                         case TypeCode.Int32:
88                                 return parameter != null ? Int32.Parse (parameter, CultureInfo.InvariantCulture): default (int);
89                         case TypeCode.Int64:
90                                 return parameter != null ? Int64.Parse (parameter, CultureInfo.InvariantCulture): default (long);
91                         case TypeCode.UInt16:
92                                 return parameter != null ? UInt16.Parse (parameter, CultureInfo.InvariantCulture): default (ushort);
93                         case TypeCode.UInt32:
94                                 return parameter != null ? UInt32.Parse (parameter, CultureInfo.InvariantCulture): default (uint);
95                         case TypeCode.UInt64:
96                                 return parameter != null ? UInt64.Parse (parameter, CultureInfo.InvariantCulture): default (ulong);
97                         case TypeCode.DateTime:
98                                 return parameter != null ? DateTime.Parse (parameter, CultureInfo.InvariantCulture): default (DateTime);
99                         case TypeCode.Boolean:
100                                 return parameter != null ? Boolean.Parse (parameter): default (bool);
101                         case TypeCode.Single:
102                                 return parameter != null ? Single.Parse (parameter, CultureInfo.InvariantCulture): default (float);
103                         case TypeCode.Double:
104                                 return parameter != null ? Double.Parse (parameter, CultureInfo.InvariantCulture): default (double);
105                         case TypeCode.Decimal:
106                                 return parameter != null ? Decimal.Parse (parameter, CultureInfo.InvariantCulture): default (decimal);
107                         case TypeCode.Object:
108                                 if (parameterType == typeof (TimeSpan))
109                                         return TimeSpan.Parse (parameter);
110                                 if (parameterType == typeof (DateTimeOffset))
111                                         return DateTimeOffset.Parse (parameter, CultureInfo.InvariantCulture);
112                                 if (parameterType == typeof (Guid))
113                                         return new Guid (parameter);
114                                 break;
115                         }
116                         throw new NotSupportedException (String.Format ("Cannot convert parameter string '{0}' to parameter type '{1}'", parameter, parameterType));
117                 }
118
119                 public virtual string ConvertValueToString (object parameter, Type parameterType)
120                 {
121                         if (parameterType == null)
122                                 throw new ArgumentNullException ("parameterType");
123                         if (parameterType.IsValueType && parameter == null)
124                                 throw new ArgumentNullException ("parameter");
125
126                         if (parameter == null)
127                                 return null;
128
129                         if (parameter.GetType () != parameterType)
130                                 throw new InvalidCastException (String.Format ("This QueryStringConverter does not support cast from {0} to {1}", parameter.GetType (), parameterType));
131
132                         if (!CanConvert (parameterType))
133                                 throw new NotSupportedException (String.Format ("Conversion from the argument parameterType '{0}' is not supported", parameterType));
134
135                         if (parameter is IFormattable)
136                                 ((IFormattable) parameter).ToString (null, CultureInfo.InvariantCulture);
137                         return parameter.ToString ();
138                 }
139         }
140 }