PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Cudafy/Cudafy/XmlExtensions.cs

#
C# | 260 lines | 164 code | 14 blank | 82 comment | 20 complexity | 7a3d91025c46836fc160c85d1a45bef5 MD5 | raw file
  1. /*
  2. CUDAfy.NET - LGPL 2.1 License
  3. Please consider purchasing a commerical license - it helps development, frees you from LGPL restrictions
  4. and provides you with support. Thank you!
  5. Copyright (C) 2011 Hybrid DSP Systems
  6. http://www.hybriddsp.com
  7. This library is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2.1 of the License, or (at your option) any later version.
  11. This library is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public
  16. License along with this library; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using System.Text;
  23. using System.IO;
  24. using System.Diagnostics;
  25. using System.Xml;
  26. using System.Xml.Linq;
  27. namespace Cudafy
  28. {
  29. /// <summary>
  30. /// Xml extension class.
  31. /// </summary>
  32. public static class XmlExtensions
  33. {
  34. /// <summary>
  35. /// Converts an XmlNode to XElement.
  36. /// </summary>
  37. /// <param name="node">The node.</param>
  38. /// <returns>XElement</returns>
  39. public static XElement GetXElement(this XmlNode node)
  40. {
  41. XDocument xDoc = new XDocument();
  42. using (XmlWriter xmlWriter = xDoc.CreateWriter())
  43. node.WriteTo(xmlWriter);
  44. return xDoc.Root;
  45. }
  46. /// <summary>
  47. /// Converts an XElement to XmlNode.
  48. /// </summary>
  49. /// <param name="element">The element.</param>
  50. /// <returns>XmlNode</returns>
  51. public static XmlNode GetXmlNode(this XElement element)
  52. {
  53. using (XmlReader xmlReader = element.CreateReader())
  54. {
  55. XmlDocument xmlDoc = new XmlDocument();
  56. xmlDoc.Load(xmlReader);
  57. return xmlDoc;
  58. }
  59. }
  60. /// <summary>
  61. /// Tries to get element value.
  62. /// </summary>
  63. /// <param name="element">The element.</param>
  64. /// <param name="elementName">Name of the element.</param>
  65. /// <returns>Value of element or null if element does not exist.</returns>
  66. public static string TryGetElementValue(this XElement element, string elementName)
  67. {
  68. XElement elem = element.Element(elementName);
  69. if (elem == null)
  70. return null;
  71. return elem.Value;
  72. }
  73. /// <summary>
  74. /// Gets the attribute value.
  75. /// </summary>
  76. /// <param name="element">The element.</param>
  77. /// <param name="attributeName">Name of the attribute.</param>
  78. /// <returns>String value.</returns>
  79. /// <exception cref="XmlException">Attribute not found.</exception>
  80. public static string GetAttributeValue(this XElement element, string attributeName)
  81. {
  82. XAttribute attr = element.Attribute(attributeName);
  83. if (attr == null)
  84. throw new XmlException(string.Format(GES.csATTRIBUTE_X_NOT_FOUND_FOR_NODE_X, attributeName, element.Name));
  85. return attr.Value;
  86. }
  87. /// <summary>
  88. /// Tries to get attribute value.
  89. /// </summary>
  90. /// <param name="element">The element.</param>
  91. /// <param name="attributeName">Name of the attribute.</param>
  92. /// <returns>String value, or null if not found.</returns>
  93. public static string TryGetAttributeValue(this XElement element, string attributeName)
  94. {
  95. XAttribute attr = element.Attribute(attributeName);
  96. if (attr == null)
  97. return null;
  98. return attr.Value;
  99. }
  100. public static TEnum TryGetAttributeEnum<TEnum>(this XElement element, string attributeName) where TEnum : struct
  101. {
  102. string enumValue = element.TryGetAttributeValue(attributeName);
  103. if (enumValue == null)
  104. return default(TEnum);
  105. TEnum result = default(TEnum);
  106. bool rc = true;
  107. #if NET35
  108. rc = Utility.TryEnumParse<TEnum>(enumValue, out result);
  109. #else
  110. rc = Enum.TryParse<TEnum>(enumValue, out result);
  111. #endif
  112. if (!rc)
  113. return default(TEnum);
  114. return result;
  115. }
  116. public static TEnum GetAttributeEnum<TEnum>(this XElement element, string attributeName) where TEnum : struct
  117. {
  118. string enumValue = element.TryGetAttributeValue(attributeName);
  119. if (enumValue == null)
  120. throw new XmlException(string.Format(GES.csATTRIBUTE_X_NOT_FOUND_FOR_NODE_X, attributeName, element.Name));
  121. TEnum result;
  122. #if NET35
  123. bool rc = Utility.TryEnumParse<TEnum>(enumValue, out result);
  124. #else
  125. bool rc = Enum.TryParse<TEnum>(enumValue, out result);
  126. #endif
  127. if (!rc)
  128. throw new XmlException(string.Format(GES.csATTRIBUTE_X_NOT_FOUND_FOR_NODE_X, attributeName, element.Name));
  129. return result;
  130. }
  131. public static string TryGetElementBase64(this XElement element, string elementName)
  132. {
  133. var value = element.TryGetElementValue(elementName);
  134. byte[] ba = Convert.FromBase64String(value);
  135. string result = UnicodeEncoding.ASCII.GetString(ba);
  136. return result;
  137. }
  138. public static string GetElementBase64(this XElement element, string elementName)
  139. {
  140. string value = element.TryGetElementBase64(elementName);
  141. if(value == null)
  142. throw new XmlException(string.Format(GES.csELEMENT_X_NOT_FOUND, elementName));
  143. return value;
  144. }
  145. /// <summary>
  146. /// Gets the attribute as Int32 value.
  147. /// </summary>
  148. /// <param name="element">The element.</param>
  149. /// <param name="attributeName">Name of the attribute.</param>
  150. /// <returns>Int32 value.</returns>
  151. /// <exception cref="XmlException">Attribute not found.</exception>
  152. public static int GetAttributeInt32Value(this XElement element, string attributeName)
  153. {
  154. XAttribute attr = element.Attribute(attributeName);
  155. if (attr == null)
  156. throw new XmlException(string.Format(GES.csATTRIBUTE_X_NOT_FOUND_FOR_NODE_X, attributeName, element.Name));
  157. int i = 0;
  158. try
  159. {
  160. i = XmlConvert.ToInt32(attr.Value);
  161. }
  162. catch (FormatException fe)
  163. {
  164. throw new XmlException(string.Format(GES.csFAILED_TO_CONVERT_ATTRIBUTE_X_TO_INT32_ERROR_X, attributeName, fe.Message));
  165. }
  166. catch (OverflowException oe)
  167. {
  168. throw new XmlException(string.Format(GES.csFAILED_TO_CONVERT_ATTRIBUTE_X_TO_INT32_ERROR_X, attributeName, oe.Message));
  169. }
  170. return i;
  171. }
  172. /// <summary>
  173. /// Tries to get attribute as Int32 value.
  174. /// </summary>
  175. /// <param name="element">The element.</param>
  176. /// <param name="attributeName">Name of the attribute.</param>
  177. /// <returns>Int32 value, or null if not found.</returns>
  178. public static int? TryGetAttributeInt32Value(this XElement element, string attributeName)
  179. {
  180. XAttribute attr = element.Attribute(attributeName);
  181. if (attr == null)
  182. return null;
  183. int i = 0;
  184. try
  185. {
  186. i = XmlConvert.ToInt32(attr.Value);
  187. return i;
  188. }
  189. catch (Exception ex)
  190. {
  191. Debug.WriteLine(string.Format(GES.csFAILED_TO_CONVERT_ATTRIBUTE_X_TO_INT32_ERROR_X, attributeName, ex.Message));
  192. return null;
  193. }
  194. }
  195. /// <summary>
  196. /// Tries the get attribute as bool value.
  197. /// </summary>
  198. /// <param name="element">The element.</param>
  199. /// <param name="attributeName">Name of the attribute.</param>
  200. /// <returns>Boolean value, or null if not found.</returns>
  201. public static bool? TryGetAttributeBoolValue(this XElement element, string attributeName)
  202. {
  203. XAttribute attr = element.Attribute(attributeName);
  204. if (attr == null)
  205. return null;
  206. bool? b = null;
  207. try
  208. {
  209. b = XmlConvert.ToBoolean(attr.Value);
  210. return b;
  211. }
  212. catch (Exception ex)
  213. {
  214. Debug.WriteLine(string.Format(GES.csFAILED_TO_CONVERT_ATTRIBUTE_X_TO_INT32_ERROR_X, attributeName, ex.Message));
  215. return null;
  216. }
  217. }
  218. /// <summary>
  219. /// Loads an XDocument from the specified stream.
  220. /// </summary>
  221. /// <param name="inStream">The input stream.</param>
  222. /// <returns>XDocument</returns>
  223. public static XDocument LoadStream(Stream inStream)
  224. {
  225. using (XmlReader xr = XmlReader.Create(inStream))
  226. {
  227. return XDocument.Load(xr);
  228. }
  229. }
  230. /// <summary>
  231. /// Saves the stream to XDocument supplied.
  232. /// </summary>
  233. /// <param name="xmlDoc">The XML doc.</param>
  234. /// <param name="outStream">The out stream.</param>
  235. public static void SaveStream(this XDocument xmlDoc, Stream outStream)
  236. {
  237. using (XmlWriter xw = XmlWriter.Create(outStream, new XmlWriterSettings() { Indent = true }))
  238. {
  239. xmlDoc.Save(xw);
  240. }
  241. }
  242. }
  243. }