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