Click here to Skip to main content
15,897,226 members
Articles / Web Development / ASP.NET

Introduction to Bellevue View Engine - Part 1

Rate me:
Please Sign up or sign in to vote.
3.43/5 (4 votes)
18 Mar 2010CPOL8 min read 26K   126   10  
Prototype of a new template engine for ASP.NET MVC Framework that respects HTML and uses CSS-like syntax for model binding.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ope.Bellevue.Rendering;

namespace Ope.Bellevue.Parsing
{
    /// <summary>
    /// Result of the parse process from Html file to a Rendering Tree.
    /// </summary>
    public abstract class ParsedViewBase
    {
        TemplateContainer _Templates = new TemplateContainer();
        List<ElementDeclarationList> _ElementDeclarations = new List<ElementDeclarationList>();
        List<RenderingInstructions> _Rendering = new List<RenderingInstructions>();
        List<string> _Warnings = new List<string>();
        
        /// <summary>
        /// The Html document where the rulesets in Bellevue script hav been distributed to different elements.
        /// </summary>
        public HtmlAgilityPack.HtmlDocument Html { get; set; }

        public List<ElementDeclarationList> ElementDeclarations
        {
            get
            {
                return _ElementDeclarations;
            }
        }

        public TemplateContainer Templates
        {
            get
            {
                return _Templates;
            }
        }

        internal List<RenderingInstructions> Rendering {
            get { return _Rendering; }
        }

        public void AddRendering(HtmlAgilityPack.HtmlNode referenceNode, RenderingInstruction instruction)
        {
            // Determine location
            HtmlAgilityPack.HtmlNode marker;
            switch (instruction.Position)
            {
                case RelativePosition.ReplaceOuter:
                    marker = referenceNode;
                    break;

                case RelativePosition.Append:
                    marker = referenceNode.OwnerDocument.CreateElement("bellevuemarker");
                    referenceNode.AppendChild(marker);
                    break;

                case RelativePosition.Insert:
                    marker = referenceNode.OwnerDocument.CreateElement("bellevuemarker");
                    if (referenceNode.ChildNodes.Count > 0)
                    {
                        referenceNode.InsertBefore(marker, referenceNode.FirstChild);
                    }
                    else
                    {
                        referenceNode.AppendChild(marker);
                    }
                    break;

                case RelativePosition.InsertBefore:
                    marker = referenceNode.OwnerDocument.CreateElement("bellevuemarker");
                    referenceNode.ParentNode.InsertBefore(marker, referenceNode);
                    break;

                case RelativePosition.InsertAfter:
                    marker = referenceNode.OwnerDocument.CreateElement("bellevuemarker");
                    referenceNode.ParentNode.InsertAfter(marker, referenceNode);
                    break;

                default:
                    referenceNode.InnerHtml = "";
                    marker = referenceNode.OwnerDocument.CreateElement("bellevuemarker");
                    referenceNode.AppendChild(marker);
                    break;
            }

            RenderingInstructions instructions;
            int instructionId = marker.GetAttributeValue("renderingInstructions", -1);
            if (instructionId < 0)
            {
                instructionId = _Rendering.Count;
                marker.SetAttributeValue("renderingInstructions", instructionId.ToString());
                instructions = new RenderingInstructions();
                _Rendering.Add(instructions);
            }
            else
            {
                instructions = _Rendering[instructionId];
            }
            instructions.Add(instruction);
        }

        public void AddRendering(string attributeName, HtmlAgilityPack.HtmlNode node, RenderingInstruction instruction)
        {
            if (node.GetAttributeValue(attributeName, null) == null)
            {
                node.SetAttributeValue(attributeName, "");
            }
            AddRendering(node.Attributes[attributeName], instruction);
        }

        public void AddRendering(HtmlAgilityPack.HtmlAttribute attribute, RenderingInstruction instruction)
        {
            string attrValue = attribute.Value ?? "";
            RenderingInstructions instructions;
            if (attrValue.StartsWith("#") && attrValue.EndsWith("#"))
            {
                int instructionId = int.Parse(attrValue.Substring(1, attrValue.Length - 2));
                instructions = _Rendering[instructionId];
            }
            else
            {
                int instructionId = _Rendering.Count;
                attribute.Value = "#" + instructionId + "#";
                instructions = new RenderingInstructions();
                instructions.OriginalText = attrValue;
                _Rendering.Add(instructions);
            }
            instructions.Add(instruction);
        }

        /// <summary>
        /// Warnings on issues that occured during parsin or rendering.
        /// </summary>
        public IEnumerable<string> Warnings
        {
            get
            {
                return _Warnings;
            }
        }

        /// <summary>
        /// Adds a new generic warning
        /// </summary>
        /// <param name="warning">The warning message.</param>
        public void AddWarning(string warning)
        {
            _Warnings.Add(warning);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder OPE AG
Switzerland Switzerland
Olli is a .Net developer and architect.

He specializes in Asp.net MVC and other web technologies, XML and lately Domain Specific Languages.

Olli is originally from Finland, but currently works for his own one-man-initiative OPE AG (www.ope.ag) from Switzerland. He has over 10 years of experience as one of the founding partners and Chief Technology Officer of Quartal group of companies (www.quartal.com).

Comments and Discussions