PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/CRMCustom/CRMCustom.GUI/acm/acmresources/resource.cs

#
C# | 95 lines | 70 code | 15 blank | 10 comment | 3 complexity | 6f155f1ccca408095a30ecb28cb9db38 MD5 | raw file
  1. //---------------------------------------------------------------------------------
  2. // Microsoft (R) Windows Azure Platform AppFabric SDK
  3. // Software Development Kit
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  8. // EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
  9. // OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  10. //---------------------------------------------------------------------------------
  11. namespace Microsoft.AccessControl.Samples.AcmTool.Resources
  12. {
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Diagnostics;
  16. using System.Globalization;
  17. using System.Reflection;
  18. using System.Security.Cryptography;
  19. using System.Text;
  20. using System.Xml;
  21. using System.Xml.Serialization;
  22. [XmlRoot(Namespace = Constants.ResourceNameSpace)]
  23. public abstract class Resource
  24. {
  25. protected Resource()
  26. {
  27. }
  28. [XmlElement(IsNullable = true)]
  29. public string DisplayName { get; set; }
  30. public string Id { get; set; }
  31. [XmlIgnore]
  32. public string Version { get; set; }
  33. internal static Resource DeserializeFromXml(XmlReader reader)
  34. {
  35. Resource resource;
  36. reader.Read();
  37. string typeName = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", typeof(Resource).Namespace, reader.Name);
  38. XmlSerializer xs = new XmlSerializer(Assembly.GetExecutingAssembly().GetType(typeName));
  39. resource = (Resource)xs.Deserialize(reader);
  40. return resource;
  41. }
  42. internal static string SerializeToXmlString(Resource resource)
  43. {
  44. XmlSerializer xs = new XmlSerializer(resource.GetType());
  45. StringBuilder sb = new StringBuilder(256);
  46. XmlWriterSettings writerSettings = new XmlWriterSettings();
  47. writerSettings.Indent = true;
  48. writerSettings.IndentChars = " ";
  49. writerSettings.OmitXmlDeclaration = true;
  50. using (XmlWriter writer = XmlTextWriter.Create(sb, writerSettings))
  51. {
  52. xs.Serialize(writer, resource);
  53. }
  54. return sb.ToString();
  55. }
  56. internal abstract void ParseAndValidate(Dictionary<string, string> options);
  57. protected static void ValidateOptionExists(string option, Dictionary<string, string> options)
  58. {
  59. if (!options.ContainsKey(option))
  60. {
  61. throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, Constants.ErrorMissingOption1, option));
  62. }
  63. }
  64. internal static void ValidateOptionValueExists(string optionName, Dictionary<string, string> options)
  65. {
  66. String optionValue;
  67. if (!options.TryGetValue(optionName, out optionValue) || String.IsNullOrEmpty(optionValue))
  68. {
  69. throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, Constants.ErrorMissingOption1, optionName));
  70. }
  71. }
  72. protected static string GenerateKey()
  73. {
  74. RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
  75. byte[] key = new byte[32];
  76. rng.GetBytes(key);
  77. return Convert.ToBase64String(key);
  78. }
  79. }
  80. }