PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/GitUI/CommitTemplateItem.cs

https://github.com/eisnerd/gitextensions
C# | 121 lines | 107 code | 13 blank | 1 comment | 3 complexity | 39964cafcd987ec6e1fb560c3cebc8d6 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Xml;
  7. using System.Xml.Serialization;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.Runtime.Serialization;
  10. namespace GitUI
  11. {
  12. [Serializable()]
  13. public class CommitTemplateItem : ISerializable
  14. {
  15. private string _name;
  16. public string Name
  17. {
  18. get { return _name; }
  19. set { _name = value; }
  20. }
  21. private string _text;
  22. public string Text
  23. {
  24. get { return _text; }
  25. set { _text = value; }
  26. }
  27. private static bool _useBinaryFormatter = true;
  28. public CommitTemplateItem(string name, string text)
  29. {
  30. Name = name;
  31. Text = text;
  32. }
  33. public CommitTemplateItem()
  34. {
  35. Name = String.Empty;
  36. Text = String.Empty;
  37. }
  38. public CommitTemplateItem(SerializationInfo info, StreamingContext ctxt)
  39. {
  40. Name = (string)info.GetValue("Name", typeof(string));
  41. Text = (string)info.GetValue("Text", typeof(string));
  42. }
  43. public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
  44. {
  45. info.AddValue("Name", Name);
  46. info.AddValue("Text", Text);
  47. }
  48. public static string SerializeCommitTemplates(CommitTemplateItem[] items)
  49. {
  50. try
  51. {
  52. if (_useBinaryFormatter)
  53. {
  54. // Serialize to a base 64 string
  55. byte[] bytes;
  56. MemoryStream ws = new MemoryStream();
  57. BinaryFormatter sf = new BinaryFormatter();
  58. sf.Serialize(ws, items);
  59. bytes = ws.GetBuffer();
  60. return bytes.Length.ToString() + ":" + Convert.ToBase64String(bytes, 0, bytes.Length, Base64FormattingOptions.None);
  61. }
  62. else
  63. {
  64. var sw = new StringWriter();
  65. var serializer = new XmlSerializer(typeof(CommitTemplateItem[]));
  66. serializer.Serialize(sw, items);
  67. return sw.ToString();
  68. }
  69. }
  70. catch
  71. {
  72. return null;
  73. }
  74. }
  75. public static CommitTemplateItem[] DeserializeCommitTemplates(string serializedString)
  76. {
  77. if (string.IsNullOrEmpty(serializedString))
  78. return null;
  79. CommitTemplateItem[] commitTemplateItem = null;
  80. try
  81. {
  82. if (_useBinaryFormatter)
  83. {
  84. int p = serializedString.IndexOf(':');
  85. int length = Convert.ToInt32(serializedString.Substring(0, p));
  86. byte[] memorydata = Convert.FromBase64String(serializedString.Substring(p + 1));
  87. MemoryStream rs = new MemoryStream(memorydata, 0, length);
  88. BinaryFormatter sf = new BinaryFormatter();
  89. commitTemplateItem = (CommitTemplateItem[])sf.Deserialize(rs);
  90. }
  91. else
  92. {
  93. var serializer = new XmlSerializer(typeof(CommitTemplateItem[]));
  94. using (var stringReader = new StringReader(serializedString))
  95. using (var xmlReader = new XmlTextReader(stringReader))
  96. {
  97. commitTemplateItem = serializer.Deserialize(xmlReader) as CommitTemplateItem[];
  98. }
  99. }
  100. }
  101. catch (Exception /*e*/)
  102. {
  103. return null;
  104. }
  105. return commitTemplateItem;
  106. }
  107. }
  108. }