1 //Copyright 2010 Microsoft Corporation
2 //
3 //Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
4 //You may obtain a copy of the License at
5 //
6 //http://www.apache.org/licenses/LICENSE-2.0
7 //
8 //Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
9 //"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 //See the License for the specific language governing permissions and limitations under the License.
11 
12 
13 namespace System.Data.Services.Parsing
14 {
15     using System;
16     using System.Diagnostics;
17     using System.Text;
18     using System.Xml;
19     using System.Data.Services.Client;
20 
21     internal static class WebConvert
22     {
23         private const string HexValues = "0123456789ABCDEF";
24 
25         private const string XmlHexEncodePrefix = "0x";
26 
27 
ConvertByteArrayToKeyString(byte[] byteArray)28         internal static string ConvertByteArrayToKeyString(byte[] byteArray)
29         {
30             StringBuilder hexBuilder = new StringBuilder(3 + byteArray.Length * 2);
31             hexBuilder.Append(XmlConstants.XmlBinaryPrefix);
32             hexBuilder.Append("'");
33             for (int i = 0; i < byteArray.Length; i++)
34             {
35                 hexBuilder.Append(HexValues[byteArray[i] >> 4]);
36                 hexBuilder.Append(HexValues[byteArray[i] & 0x0F]);
37             }
38 
39             hexBuilder.Append("'");
40             return hexBuilder.ToString();
41         }
42 
43 
IsKeyTypeQuoted(Type type)44         internal static bool IsKeyTypeQuoted(Type type)
45         {
46             Debug.Assert(type != null, "type != null");
47             return type == typeof(System.Xml.Linq.XElement) || type == typeof(string);
48         }
49 
TryKeyPrimitiveToString(object value, out string result)50         internal static bool TryKeyPrimitiveToString(object value, out string result)
51         {
52             Debug.Assert(value != null, "value != null");
53             if (value.GetType() == typeof(byte[]))
54             {
55                 result = ConvertByteArrayToKeyString((byte[])value);
56             }
57             else
58             {
59                 if (!TryXmlPrimitiveToString(value, out result))
60                 {
61                     return false;
62                 }
63 
64                 Debug.Assert(result != null, "result != null");
65                 if (value.GetType() == typeof(DateTime))
66                 {
67                     result = XmlConstants.LiteralPrefixDateTime + "'" + result + "'";
68                 }
69                 else if (value.GetType() == typeof(Decimal))
70                 {
71                     result = result + XmlConstants.XmlDecimalLiteralSuffix;
72                 }
73                 else if (value.GetType() == typeof(Guid))
74                 {
75                     result = XmlConstants.LiteralPrefixGuid + "'" + result + "'";
76                 }
77                 else if (value.GetType() == typeof(Int64))
78                 {
79                     result = result + XmlConstants.XmlInt64LiteralSuffix;
80                 }
81                 else if (value.GetType() == typeof(Single))
82                 {
83                     result = result + XmlConstants.XmlSingleLiteralSuffix;
84                 }
85                 else if (value.GetType() == typeof(double))
86                 {
87                     result = AppendDecimalMarkerToDouble(result);
88                 }
89                 else if (IsKeyTypeQuoted(value.GetType()))
90                 {
91                     result = "'" + result.Replace("'", "''") + "'";
92                 }
93             }
94 
95             return true;
96         }
97 
TryXmlPrimitiveToString(object value, out string result)98         internal static bool TryXmlPrimitiveToString(object value, out string result)
99         {
100             Debug.Assert(value != null, "value != null");
101             result = null;
102 
103             Type valueType = value.GetType();
104             valueType = Nullable.GetUnderlyingType(valueType) ?? valueType;
105 
106             if (typeof(String) == valueType)
107             {
108                 result = (string)value;
109             }
110             else if (typeof(Boolean) == valueType)
111             {
112                 result = XmlConvert.ToString((bool)value);
113             }
114             else if (typeof(Byte) == valueType)
115             {
116                 result = XmlConvert.ToString((byte)value);
117             }
118             else if (typeof(DateTime) == valueType)
119             {
120                 result = XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.RoundtripKind);
121             }
122             else if (typeof(Decimal) == valueType)
123             {
124                 result = XmlConvert.ToString((decimal)value);
125             }
126             else if (typeof(Double) == valueType)
127             {
128                 result = XmlConvert.ToString((double)value);
129             }
130             else if (typeof(Guid) == valueType)
131             {
132                 result = value.ToString();
133             }
134             else if (typeof(Int16) == valueType)
135             {
136                 result = XmlConvert.ToString((Int16)value);
137             }
138             else if (typeof(Int32) == valueType)
139             {
140                 result = XmlConvert.ToString((Int32)value);
141             }
142             else if (typeof(Int64) == valueType)
143             {
144                 result = XmlConvert.ToString((Int64)value);
145             }
146             else if (typeof(SByte) == valueType)
147             {
148                 result = XmlConvert.ToString((SByte)value);
149             }
150             else if (typeof(Single) == valueType)
151             {
152                 result = XmlConvert.ToString((Single)value);
153             }
154             else if (typeof(byte[]) == valueType)
155             {
156                 byte[] byteArray = (byte[])value;
157                 result = Convert.ToBase64String(byteArray);
158             }
159             #if !ASTORIA_LIGHT
160             else if (ClientConvert.IsBinaryValue(value))
161             {
162                 return ClientConvert.TryKeyBinaryToString(value, out result);
163             }
164             #endif
165             else if (typeof(System.Xml.Linq.XElement) == valueType)
166             {
167                 result = ((System.Xml.Linq.XElement)value).ToString(System.Xml.Linq.SaveOptions.None);
168             }
169             else
170             {
171                 result = null;
172                 return false;
173             }
174 
175             Debug.Assert(result != null, "result != null");
176             return true;
177         }
178 
AppendDecimalMarkerToDouble(string input)179         private static string AppendDecimalMarkerToDouble(string input)
180         {
181             foreach (char c in input)
182             {
183                 if (!Char.IsDigit(c))
184                 {
185                     return input;
186                 }
187             }
188 
189             return input + ".0";
190         }
191     }
192 }
193