File: QueryStringConverter.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (325 lines) | stat: -rw-r--r-- 16,135 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------
#pragma warning disable 1634, 1691
namespace System.ServiceModel.Dispatcher
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Reflection;
    using System.Runtime;
    using System.Runtime.InteropServices;
    using System.ServiceModel;
    using System.Xml;

    // Thread Safety: This class is thread safe
    public class QueryStringConverter
    {
        Hashtable defaultSupportedQueryStringTypes;
        // the cache does not have a quota since it is per endpoint and is
        // bounded by the number of types in the contract at the endpoint
        Hashtable typeConverterCache;

        public QueryStringConverter()
        {
            this.defaultSupportedQueryStringTypes = new Hashtable();
            this.defaultSupportedQueryStringTypes.Add(typeof(Byte), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(SByte), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(Int16), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(Int32), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(Int64), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(UInt16), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(UInt32), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(UInt64), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(Single), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(Double), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(Boolean), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(Char), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(Decimal), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(String), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(Object), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(DateTime), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(TimeSpan), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(byte[]), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(Guid), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(Uri), null);
            this.defaultSupportedQueryStringTypes.Add(typeof(DateTimeOffset), null);
            this.typeConverterCache = new Hashtable();
        }

        public virtual bool CanConvert(Type type)
        {
            if (this.defaultSupportedQueryStringTypes.ContainsKey(type))
            {
                return true;
            }
            // otherwise check if its an enum
            if (typeof(Enum).IsAssignableFrom(type))
            {
                return true;
            }
            // check if there's a typeconverter defined on the type
            return (GetStringConverter(type) != null);
        }

        public virtual object ConvertStringToValue(string parameter, Type parameterType)
        {
            if (parameterType == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("parameterType");
            }
            switch (Type.GetTypeCode(parameterType))
            {
                case TypeCode.Byte:
                    return parameter == null ? default(Byte) : XmlConvert.ToByte(parameter);
                case TypeCode.SByte:
                    return parameter == null ? default(SByte) : XmlConvert.ToSByte(parameter);
                case TypeCode.Int16:
                    return parameter == null ? default(Int16) : XmlConvert.ToInt16(parameter);
                case TypeCode.Int32:
                    {
                        if (typeof(Enum).IsAssignableFrom(parameterType))
                        {
                            return Enum.Parse(parameterType, parameter, true);
                        }
                        else
                        {
                            return parameter == null ? default(Int32) : XmlConvert.ToInt32(parameter);
                        }
                    }
                case TypeCode.Int64:
                    return parameter == null ? default(Int64) : XmlConvert.ToInt64(parameter);
                case TypeCode.UInt16:
                    return parameter == null ? default(UInt16) : XmlConvert.ToUInt16(parameter);
                case TypeCode.UInt32:
                    return parameter == null ? default(UInt32) : XmlConvert.ToUInt32(parameter);
                case TypeCode.UInt64:
                    return parameter == null ? default(UInt64) : XmlConvert.ToUInt64(parameter);
                case TypeCode.Single:
                    return parameter == null ? default(Single) : XmlConvert.ToSingle(parameter);
                case TypeCode.Double:
                    return parameter == null ? default(Double) : XmlConvert.ToDouble(parameter);
                case TypeCode.Char:
                    return parameter == null ? default(Char) : XmlConvert.ToChar(parameter);
                case TypeCode.Decimal:
                    return parameter == null ? default(Decimal) : XmlConvert.ToDecimal(parameter);
                case TypeCode.Boolean:
                    return parameter == null ? default(Boolean) : Convert.ToBoolean(parameter, CultureInfo.InvariantCulture);
                case TypeCode.String:
                    return parameter;
                case TypeCode.DateTime:
                    return parameter == null ? default(DateTime) : DateTime.Parse(parameter, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
                default:
                    {
                        if (parameterType == typeof(TimeSpan))
                        {
                            // support the XML as well as default way of representing timespans
                            TimeSpan result;
                            if (!TimeSpan.TryParse(parameter, out result))
                            {
                                result = parameter == null ? default(TimeSpan) : XmlConvert.ToTimeSpan(parameter);
                            }
                            return result;
                        }
                        else if (parameterType == typeof(Guid))
                        {
                            return parameter == null ? default(Guid) : XmlConvert.ToGuid(parameter);
                        }
                        else if (parameterType == typeof(DateTimeOffset))
                        {
                            return (parameter == null) ? default(DateTimeOffset) : DateTimeOffset.Parse(parameter, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind | DateTimeStyles.AllowWhiteSpaces);
                        }
                        else if (parameterType == typeof(byte[]))
                        {
                            return (!string.IsNullOrEmpty(parameter)) ? Convert.FromBase64String(parameter) : new byte[] { };
                        }
                        else if (parameterType == typeof(Uri))
                        {
                            return (!string.IsNullOrEmpty(parameter)) ? new Uri(parameter, UriKind.RelativeOrAbsolute) : null;
                        }
                        else if (parameterType == typeof(object))
                        {
                            return parameter;
                        }
                        else
                        {
                            TypeConverter stringConverter = GetStringConverter(parameterType);
                            if (stringConverter == null)
                            {
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(
                                    SR2.GetString(
                                    SR2.TypeNotSupportedByQueryStringConverter,
                                    parameterType.ToString(), this.GetType().Name)));
                            }
                            return stringConverter.ConvertFromInvariantString(parameter);
                        }
                    }
            }
        }

        public virtual string ConvertValueToString(object parameter, Type parameterType)
        {
            if (parameterType == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("parameterType");
            }
            if (parameterType.IsValueType && parameter == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("parameter");
            }
            switch (Type.GetTypeCode(parameterType))
            {
                case TypeCode.Byte:
                    return XmlConvert.ToString((Byte) parameter);
                case TypeCode.SByte:
                    return XmlConvert.ToString((SByte) parameter);
                case TypeCode.Int16:
                    return XmlConvert.ToString((Int16) parameter);
                case TypeCode.Int32:
                    {
                        if (typeof(Enum).IsAssignableFrom(parameterType))
                        {
                            return Enum.Format(parameterType, parameter, "G");
                        }
                        else
                        {
                            return XmlConvert.ToString((int) parameter);
                        }
                    }
                case TypeCode.Int64:
                    return XmlConvert.ToString((Int64) parameter);
                case TypeCode.UInt16:
                    return XmlConvert.ToString((UInt16) parameter);
                case TypeCode.UInt32:
                    return XmlConvert.ToString((uint) parameter);
                case TypeCode.UInt64:
                    return XmlConvert.ToString((UInt64) parameter);
                case TypeCode.Single:
                    return XmlConvert.ToString((Single) parameter);
                case TypeCode.Double:
                    return XmlConvert.ToString((double) parameter);
                case TypeCode.Char:
                    return XmlConvert.ToString((char) parameter);
                case TypeCode.Decimal:
                    return XmlConvert.ToString((decimal) parameter);
                case TypeCode.Boolean:
                    return XmlConvert.ToString((bool) parameter);
                case TypeCode.String:
                    return (string) parameter;
                case TypeCode.DateTime:
                    return XmlConvert.ToString((DateTime) parameter, XmlDateTimeSerializationMode.RoundtripKind);
                default:
                    {
                        if (parameterType == typeof(TimeSpan))
                        {
                            return XmlConvert.ToString((TimeSpan) parameter);
                        }
                        else if (parameterType == typeof(Guid))
                        {
                            return XmlConvert.ToString((Guid) parameter);
                        }
                        else if (parameterType == typeof(DateTimeOffset))
                        {
                            return XmlConvert.ToString((DateTimeOffset) parameter);
                        }
                        else if (parameterType == typeof(byte[]))
                        {
                            return (parameter != null) ? Convert.ToBase64String((byte[]) parameter, Base64FormattingOptions.None) : null;
                        }
                        else if (parameterType == typeof(Uri) || parameterType == typeof(object))
                        {
                            // URI or object
                            return (parameter != null) ? Convert.ToString(parameter, CultureInfo.InvariantCulture) : null;
                        }
                        else
                        {
                            TypeConverter stringConverter = GetStringConverter(parameterType);
                            if (stringConverter == null)
                            {
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(
                                    SR2.GetString(
                                    SR2.TypeNotSupportedByQueryStringConverter,
                                    parameterType.ToString(), this.GetType().Name)));
                            }
                            else
                            {
                                return stringConverter.ConvertToInvariantString(parameter);
                            }
                        }
                    }
            }
        }

        // hash table is safe for multiple readers single writer
        [SuppressMessage("Reliability", "Reliability104:CaughtAndHandledExceptionsRule", Justification = "The exception is traced in the finally clause")]
        TypeConverter GetStringConverter(Type parameterType)
        {
            if (this.typeConverterCache.ContainsKey(parameterType))
            {
                return (TypeConverter) this.typeConverterCache[parameterType];
            }
            TypeConverterAttribute[] typeConverterAttrs = parameterType.GetCustomAttributes(typeof(TypeConverterAttribute), true) as TypeConverterAttribute[];
            if (typeConverterAttrs != null)
            {
                foreach (TypeConverterAttribute converterAttr in typeConverterAttrs)
                {
                    Type converterType = Type.GetType(converterAttr.ConverterTypeName, false, true);
                    if (converterType != null)
                    {
                        TypeConverter converter = null;
                        Exception handledException = null;
                        try
                        {
                            converter = (TypeConverter) Activator.CreateInstance(converterType);
                        }
                        catch (TargetInvocationException e)
                        {
                            handledException = e;
                        }
                        catch (MemberAccessException e)
                        {
                            handledException = e;
                        }
                        catch (TypeLoadException e)
                        {
                            handledException = e;
                        }
                        catch (COMException e)
                        {
                            handledException = e;
                        }
                        catch (InvalidComObjectException e)
                        {
                            handledException = e;
                        }
                        finally
                        {
                            if (handledException != null)
                            {
                                if (Fx.IsFatal(handledException))
                                {
                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(handledException);
                                }
                                DiagnosticUtility.TraceHandledException(handledException, TraceEventType.Warning);
                            }
                        }
                        if (converter == null)
                        {
                            continue;
                        }
                        if (converter.CanConvertTo(typeof(string)) && converter.CanConvertFrom(typeof(string)))
                        {
                            this.typeConverterCache.Add(parameterType, converter);
                            return converter;
                        }
                    }
                }
            }
            return null;
        }
    }
}