PageRenderTime 64ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/referencesource/System.Activities.DurableInstancing/System/Activities/DurableInstancing/XmlPropertyBag.cs

https://github.com/pruiz/mono
C# | 315 lines | 298 code | 14 blank | 3 comment | 91 complexity | 886cdd4674e7c4ea958e3f92f592e3e1 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //----------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------
  4. namespace System.Activities.DurableInstancing
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.IO.Compression;
  11. using System.Linq;
  12. using System.Runtime;
  13. using System.Text;
  14. using System.Xml;
  15. using System.Xml.Linq;
  16. using System.Xml.Schema;
  17. using System.Xml.Serialization;
  18. class XmlPropertyBag : Dictionary<XName, object>, IXmlSerializable
  19. {
  20. public XmlPropertyBag()
  21. {
  22. }
  23. public XmlSchema GetSchema()
  24. {
  25. return null;
  26. }
  27. public void ReadXml(XmlReader reader)
  28. {
  29. if (reader.ReadToDescendant("Property"))
  30. {
  31. do
  32. {
  33. reader.MoveToFirstAttribute();
  34. XName propertyName = XName.Get(reader.Value);
  35. reader.MoveToNextAttribute();
  36. PrimitiveType type = (PrimitiveType)Int32.Parse(reader.Value, CultureInfo.InvariantCulture);
  37. reader.MoveToNextAttribute();
  38. object value = ConvertStringToNativeType(reader.Value, type);
  39. this.Add(propertyName, value);
  40. }
  41. while (reader.ReadToNextSibling("Property"));
  42. }
  43. }
  44. public void WriteXml(XmlWriter writer)
  45. {
  46. writer.WriteStartElement("Properties");
  47. foreach (KeyValuePair<XName, object> property in this)
  48. {
  49. writer.WriteStartElement("Property");
  50. writer.WriteAttributeString("XName", property.Key.ToString());
  51. writer.WriteAttributeString("Type", ((int)GetPrimitiveType(property.Value)).ToString(CultureInfo.InvariantCulture));
  52. writer.WriteAttributeString("Value", ConvertNativeValueToString(property.Value));
  53. writer.WriteEndElement();
  54. }
  55. writer.WriteEndElement();
  56. }
  57. public static PrimitiveType GetPrimitiveType(object value)
  58. {
  59. if (value == null)
  60. {
  61. return PrimitiveType.Null;
  62. }
  63. else if (value is bool)
  64. {
  65. return PrimitiveType.Bool;
  66. }
  67. else if (value is byte)
  68. {
  69. return PrimitiveType.Byte;
  70. }
  71. else if (value is char)
  72. {
  73. return PrimitiveType.Char;
  74. }
  75. else if (value is DateTime)
  76. {
  77. return PrimitiveType.DateTime;
  78. }
  79. else if (value is DateTimeOffset)
  80. {
  81. return PrimitiveType.DateTimeOffset;
  82. }
  83. else if (value is decimal)
  84. {
  85. return PrimitiveType.Decimal;
  86. }
  87. else if (value is double)
  88. {
  89. return PrimitiveType.Double;
  90. }
  91. else if (value is float)
  92. {
  93. return PrimitiveType.Float;
  94. }
  95. else if (value is Guid)
  96. {
  97. return PrimitiveType.Guid;
  98. }
  99. else if (value is int)
  100. {
  101. return PrimitiveType.Int;
  102. }
  103. else if (value is long)
  104. {
  105. return PrimitiveType.Long;
  106. }
  107. else if (value is sbyte)
  108. {
  109. return PrimitiveType.SByte;
  110. }
  111. else if (value is short)
  112. {
  113. return PrimitiveType.Short;
  114. }
  115. else if (value is string)
  116. {
  117. return PrimitiveType.String;
  118. }
  119. else if (value is TimeSpan)
  120. {
  121. return PrimitiveType.TimeSpan;
  122. }
  123. else if (value is Type)
  124. {
  125. return PrimitiveType.Type;
  126. }
  127. else if (value is uint)
  128. {
  129. return PrimitiveType.UInt;
  130. }
  131. else if (value is ulong)
  132. {
  133. return PrimitiveType.ULong;
  134. }
  135. else if (value is Uri)
  136. {
  137. return PrimitiveType.Uri;
  138. }
  139. else if (value is ushort)
  140. {
  141. return PrimitiveType.UShort;
  142. }
  143. else if (value is XmlQualifiedName)
  144. {
  145. return PrimitiveType.XmlQualifiedName;
  146. }
  147. else
  148. {
  149. return PrimitiveType.Unavailable;
  150. }
  151. }
  152. public static object ConvertStringToNativeType(string value, PrimitiveType type)
  153. {
  154. switch (type)
  155. {
  156. case PrimitiveType.Bool:
  157. return XmlConvert.ToBoolean(value);
  158. case PrimitiveType.Byte:
  159. return XmlConvert.ToByte(value);
  160. case PrimitiveType.Char:
  161. return XmlConvert.ToChar(value);
  162. case PrimitiveType.DateTime:
  163. return XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.RoundtripKind);
  164. case PrimitiveType.DateTimeOffset:
  165. return XmlConvert.ToDateTimeOffset(value);
  166. case PrimitiveType.Decimal:
  167. return XmlConvert.ToDecimal(value);
  168. case PrimitiveType.Double:
  169. return XmlConvert.ToDouble(value);
  170. case PrimitiveType.Float:
  171. return float.Parse(value, CultureInfo.InvariantCulture);
  172. case PrimitiveType.Guid:
  173. return XmlConvert.ToGuid(value);
  174. case PrimitiveType.Int:
  175. return XmlConvert.ToInt32(value);
  176. case PrimitiveType.Long:
  177. return XmlConvert.ToInt64(value);
  178. case PrimitiveType.SByte:
  179. return XmlConvert.ToSByte(value);
  180. case PrimitiveType.Short:
  181. return XmlConvert.ToInt16(value);
  182. case PrimitiveType.String:
  183. return value;
  184. case PrimitiveType.TimeSpan:
  185. return XmlConvert.ToTimeSpan(value);
  186. case PrimitiveType.Type:
  187. return Type.GetType(value);
  188. case PrimitiveType.UInt:
  189. return XmlConvert.ToUInt32(value);
  190. case PrimitiveType.ULong:
  191. return XmlConvert.ToUInt64(value);
  192. case PrimitiveType.Uri:
  193. return new Uri(value);
  194. case PrimitiveType.UShort:
  195. return XmlConvert.ToUInt16(value);
  196. case PrimitiveType.XmlQualifiedName:
  197. return new XmlQualifiedName(value);
  198. case PrimitiveType.Null:
  199. case PrimitiveType.Unavailable:
  200. default:
  201. return null;
  202. }
  203. }
  204. public static string ConvertNativeValueToString(object value)
  205. {
  206. if (value == null)
  207. {
  208. return null;
  209. }
  210. else if (value is bool)
  211. {
  212. return XmlConvert.ToString((bool)value);
  213. }
  214. else if (value is byte)
  215. {
  216. return XmlConvert.ToString((byte)value);
  217. }
  218. else if (value is char)
  219. {
  220. return XmlConvert.ToString((char)value);
  221. }
  222. else if (value is DateTime)
  223. {
  224. return XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.RoundtripKind);
  225. }
  226. else if (value is DateTimeOffset)
  227. {
  228. return XmlConvert.ToString((DateTimeOffset)value);
  229. }
  230. else if (value is decimal)
  231. {
  232. return XmlConvert.ToString((decimal)value);
  233. }
  234. else if (value is double)
  235. {
  236. return XmlConvert.ToString((double)value);
  237. }
  238. else if (value is float)
  239. {
  240. return ((float)value).ToString("r", CultureInfo.InvariantCulture);
  241. }
  242. else if (value is Guid)
  243. {
  244. return XmlConvert.ToString((Guid)value);
  245. }
  246. else if (value is int)
  247. {
  248. return XmlConvert.ToString((int)value);
  249. }
  250. else if (value is long)
  251. {
  252. return XmlConvert.ToString((long)value);
  253. }
  254. else if (value is sbyte)
  255. {
  256. return XmlConvert.ToString((sbyte)value);
  257. }
  258. else if (value is short)
  259. {
  260. return XmlConvert.ToString((short)value);
  261. }
  262. else if (value is string)
  263. {
  264. return (string)value;
  265. }
  266. else if (value is TimeSpan)
  267. {
  268. return XmlConvert.ToString((TimeSpan)value);
  269. }
  270. else if (value is Type)
  271. {
  272. return value.ToString();
  273. }
  274. else if (value is uint)
  275. {
  276. return XmlConvert.ToString((uint)value);
  277. }
  278. else if (value is ulong)
  279. {
  280. return XmlConvert.ToString((ulong)value);
  281. }
  282. else if (value is Uri)
  283. {
  284. return ((Uri)value).ToString();
  285. }
  286. else if (value is ushort)
  287. {
  288. return XmlConvert.ToString((ushort)value);
  289. }
  290. else if (value is XmlQualifiedName)
  291. {
  292. return ((XmlQualifiedName)value).ToString();
  293. }
  294. else
  295. {
  296. Fx.AssertAndThrow("Should never reach here");
  297. return null;
  298. }
  299. }
  300. }
  301. }