PageRenderTime 24ms CodeModel.GetById 18ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/DataStore/XMLDocumentBehavior.cs

#
C# | 144 lines | 79 code | 21 blank | 44 comment | 8 complexity | 963d0421c27c0a87b926c4679f19f8f3 MD5 | raw file
  1namespace BlogEngine.Core.DataStore
  2{
  3    using System;
  4    using System.Configuration;
  5    using System.IO;
  6    using System.Xml;
  7    using System.Xml.Serialization;
  8
  9    using BlogEngine.Core.Providers;
 10
 11    /// <summary>
 12    /// Class to encapsulate saving and retreaving 
 13    ///     XML documents to and from data storage
 14    /// </summary>
 15    public class XmlDocumentBehavior : ISettingsBehavior
 16    {
 17        #region Constants and Fields
 18
 19        /// <summary>
 20        ///     The _section.
 21        /// </summary>
 22        private static readonly BlogProviderSection Section =
 23            (BlogProviderSection)ConfigurationManager.GetSection("BlogEngine/blogProvider");
 24
 25        #endregion
 26
 27        #region Implemented Interfaces
 28
 29        #region ISettingsBehavior
 30
 31        /// <summary>
 32        /// Gets settings from data store
 33        /// </summary>
 34        /// <param name="extensionType">
 35        /// Extension Type
 36        /// </param>
 37        /// <param name="extensionId">
 38        /// Extension ID
 39        /// </param>
 40        /// <returns>
 41        /// Settings as Stream
 42        /// </returns>
 43        public object GetSettings(ExtensionType extensionType, string extensionId)
 44        {
 45            WidgetData widgetData;
 46            var xml = new XmlDocument();
 47
 48            if (Section.DefaultProvider == "XmlBlogProvider")
 49            {
 50                var stm = (Stream)BlogService.LoadFromDataStore(extensionType, extensionId);
 51                if (stm != null)
 52                {
 53                    var x = new XmlSerializer(typeof(XmlDocument));
 54                    xml = (XmlDocument)x.Deserialize(stm);
 55                    stm.Close();
 56                }
 57            }
 58            else
 59            {
 60                var o = BlogService.LoadFromDataStore(extensionType, extensionId);
 61                if (!string.IsNullOrEmpty((string)o))
 62                {
 63                    var serializer = new XmlSerializer(typeof(WidgetData));
 64                    using (var reader = new StringReader((string)o))
 65                    {
 66                        widgetData = (WidgetData)serializer.Deserialize(reader);
 67                    }
 68
 69                    if (widgetData.Settings.Length > 0)
 70                    {
 71                        xml.InnerXml = widgetData.Settings;
 72                    }
 73                }
 74            }
 75
 76            return xml;
 77        }
 78
 79        /// <summary>
 80        /// Saves XML document to data storage
 81        /// </summary>
 82        /// <param name="extensionType">
 83        /// Extension Type
 84        /// </param>
 85        /// <param name="extensionId">
 86        /// Extension ID
 87        /// </param>
 88        /// <param name="settings">
 89        /// Settings as XML document
 90        /// </param>
 91        /// <returns>
 92        /// True if saved
 93        /// </returns>
 94        public bool SaveSettings(ExtensionType extensionType, string extensionId, object settings)
 95        {
 96            var xml = (XmlDocument)settings;
 97
 98            if (Section.DefaultProvider == "XmlBlogProvider")
 99            {
100                BlogService.SaveToDataStore(extensionType, extensionId, xml);
101            }
102            else
103            {
104                var wd = new WidgetData { Settings = xml.InnerXml };
105                BlogService.SaveToDataStore(extensionType, extensionId, wd);
106            }
107
108            return true;
109        }
110
111        #endregion
112
113        #endregion
114    }
115
116    /// <summary>
117    /// Wrap around xml document
118    /// </summary>
119    [Serializable]
120    public class WidgetData
121    {
122        #region Constructors and Destructors
123
124        /// <summary>
125        ///     Initializes a new instance of the <see cref = "WidgetData" /> class.
126        /// </summary>
127        public WidgetData()
128        {
129            this.Settings = string.Empty;
130        }
131
132        #endregion
133
134        #region Properties
135
136        /// <summary>
137        ///     Gets or sets the settings.
138        /// </summary>
139        /// <value>The settings.</value>
140        public string Settings { get; set; }
141
142        #endregion
143    }
144}