Beispiel #1
0
        /// <summary>
        /// Load the templates from a stream (which needs to be in the appropriate Json format)
        /// </summary>
        /// <param name="data">The loader object</param>
        /// <returns>Success Or Failure</returns>
        public Boolean Load(BlogViewTemplateLoader data)
        {
            // Must have something to work with so loop the items and load their content up
            data.Items.ForEach(item =>
            {
                // Add the content to the dictionary
                Templates.Add(item.Id, new HtmlContentBuilder().Append(item.Content));
            });

            // Successful?
            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Load the templates from a stream (which needs to be in the appropriate Json format)
        /// </summary>
        /// <param name="stream">A stream object that contains the loader object</param>
        /// <returns>Success Or Failure</returns>
        public Boolean Load(Stream stream)
        {
            BlogViewTemplateLoader templateLoader = null; // Define the template loader outside the try

            try
            {
                Templates = new Dictionary <BlogViewTemplatePart, IHtmlContent>(); // Empty list of templates by default

                // Convert stream to string
                using (StreamReader reader = new StreamReader(stream))
                {
                    // Parse the XML stream as an XDocument to query
                    XDocument doc = XDocument.Parse(reader.ReadToEnd());

                    // Query the XDocument and pull out the new template loader item
                    // for each node of type "Item" in the XDocument
                    templateLoader = new BlogViewTemplateLoader()
                    {
                        Items = doc.Descendants("item").Select(item =>
                                                               new BlogViewTemplateLoaderItem()
                        {
                            // Get the Id by analysing the attributes portion
                            Id = (BlogViewTemplatePart)item.Attribute("id").Value.
                                 GetValueFromDescription <BlogViewTemplatePart>(),

                            // Get the content by selecting all sub-nodes and pulling the CData value out for the first item it finds
                            Content = item.Elements("content").Select(
                                node => node.Value
                                ).First() ?? ""
                        }).ToList()
                    };

                    // Nothing to work with? Raise the error
                    if (templateLoader == null || templateLoader.Items == null || templateLoader.Items.Count == 0)
                    {
                        throw new CastObjectBlogException();
                    }
                }
            }
            catch (Exception ex)
            {
                // Decide whether or not to pass the origional or inner exception to the user
                throw BlogException.Passthrough(ex, new HtmlTemplateLoadFailureBlogException(ex));
            }

            // Call the loader with the object and not the stream
            return(Load(templateLoader));
        }