PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/GitCommands/Settings/GitExtSettingsCache.cs

https://github.com/vbjay/gitextensions
C# | 90 lines | 76 code | 14 blank | 0 comment | 3 complexity | 5dc59f9f5217af1be4ff424e9359d336 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.Xml;
  6. using System.Xml.Serialization;
  7. namespace GitCommands.Settings
  8. {
  9. public class GitExtSettingsCache : FileSettingsCache
  10. {
  11. private readonly XmlSerializableDictionary<string, string> EncodedNameMap = new XmlSerializableDictionary<string, string>();
  12. public GitExtSettingsCache(string aSettingsFilePath, bool autoSave = true)
  13. : base(aSettingsFilePath, autoSave)
  14. {
  15. }
  16. public static GitExtSettingsCache FromCache(string aSettingsFilePath)
  17. {
  18. Lazy<GitExtSettingsCache> createSettingsCache = new Lazy<GitExtSettingsCache>(() =>
  19. {
  20. return new GitExtSettingsCache(aSettingsFilePath, true);
  21. });
  22. return FileSettingsCache.FromCache(aSettingsFilePath, createSettingsCache);
  23. }
  24. public static GitExtSettingsCache Create(string aSettingsFilePath, bool allowCache = true)
  25. {
  26. if (allowCache)
  27. return FromCache(aSettingsFilePath);
  28. else
  29. return new GitExtSettingsCache(aSettingsFilePath, false);
  30. }
  31. protected override void ClearImpl()
  32. {
  33. base.ClearImpl();
  34. EncodedNameMap.Clear();
  35. }
  36. protected override void WriteSettings(string fileName)
  37. {
  38. using (System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter(fileName, Encoding.UTF8))
  39. {
  40. xtw.Formatting = Formatting.Indented;
  41. xtw.WriteStartDocument();
  42. xtw.WriteStartElement("dictionary");
  43. EncodedNameMap.WriteXml(xtw);
  44. xtw.WriteEndElement();
  45. }
  46. }
  47. protected override void ReadSettings(string fileName)
  48. {
  49. XmlReaderSettings rSettings = new XmlReaderSettings
  50. {
  51. IgnoreWhitespace = true
  52. };
  53. using (System.Xml.XmlReader xr = XmlReader.Create(fileName, rSettings))
  54. {
  55. EncodedNameMap.ReadXml(xr);
  56. }
  57. }
  58. protected override void SetValueImpl(string key, string value)
  59. {
  60. if (value == null)
  61. {
  62. EncodedNameMap.Remove(key);
  63. }
  64. else
  65. {
  66. EncodedNameMap[key] = value;
  67. }
  68. }
  69. protected override string GetValueImpl(string key)
  70. {
  71. string value = null;
  72. EncodedNameMap.TryGetValue(key, out value);
  73. return value;
  74. }
  75. }
  76. }