Ejemplo n.º 1
0
        private string ProcessExpression(StringBuilder expressionBuffer, ViewDataContainer ViewData)
        {
            if (expressionBuffer.Length > 0)
            {
                string expression = expressionBuffer.ToString();
                expressionBuffer.Clear();

                string[] parts = expression.Split(':');
                if (!ViewData.TryGet(parts[parts.Length - 1], out string value))
                {
                    throw new Exception("A view expression referred to a key that does not exist in the view data.");
                }
                for (int i = parts.Length - 2; i >= 0; i--)
                {
                    value = PerformExpressionMethod(parts[i], value);
                }
                return(value);
            }
            else
            {
                throw new Exception("View contained an empty expression.");
            }
        }
Ejemplo n.º 2
0
        private string ProcessExpression(StringBuilder expressionBuffer, ViewDataContainer ViewData)
        {
            if (expressionBuffer.Length > 0)
            {
                string expression = expressionBuffer.ToString();
                expressionBuffer.Clear();

                string[] parts = expression.Split(':');
                string   value = ViewData.Get(parts[parts.Length - 1]);
                if (string.IsNullOrEmpty(value))
                {
                    return("");
                }
                for (int i = parts.Length - 2; i >= 0; i--)
                {
                    value = PerformExpressionMethod(parts[i], value);
                }
                return(value);
            }
            else
            {
                throw new Exception("View contained an empty expression.");
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Processes the specified text as a view and sets this result body. Do not call this unless the constructor you used says to do so.
 /// </summary>
 /// <param name="viewText">The view's text.</param>
 /// <param name="ViewData">A ViewDataContainer containing values for expressions found within the view.</param>
 /// <returns></returns>
 public void ProcessView(string viewText, ViewDataContainer ViewData)
 {
     if (ViewData != null)
     {
         StringBuilder  sb = new StringBuilder(viewText.Length);
         StringBuilder  expressionBuffer = new StringBuilder();
         ViewParseState state            = ViewParseState.HTML;
         bool           expressionStartedWithParenthesis = false;
         foreach (char c in viewText)
         {
             if (state == ViewParseState.HTML)
             {
                 if (c == '@')
                 {
                     state = ViewParseState.Expression;
                 }
                 else
                 {
                     sb.Append(c);
                 }
             }
             else if (state == ViewParseState.Expression)
             {
                 if (c == '@')
                 {
                     if (expressionBuffer.Length == 0)
                     {
                         // This was a sequence of two adjacent '@' characters, which is output as one literal '@' character.
                         state = ViewParseState.HTML;
                         sb.Append(c);
                     }
                     else
                     {
                         // New '@' ends the previous expression and starts a new one.
                         sb.Append(ProcessExpression(expressionBuffer, ViewData));
                     }
                 }
                 else if (expressionBuffer.Length == 0 && c == '(')
                 {
                     // Expressions can be within parenthesis in order
                     // to mark their end to allow for cases like:
                     //    "@(AppRoot)images/icon.jpg"
                     expressionStartedWithParenthesis = true;
                 }
                 else if ((c >= 'a' && c <= 'z') ||
                          (c >= 'A' && c <= 'Z') ||
                          (c >= '0' && c <= '9') ||
                          c == '_' ||
                          c == ':')
                 {
                     // This character gets added to the expression.
                     expressionBuffer.Append(c);
                 }
                 else
                 {
                     if (expressionStartedWithParenthesis && c != ')')
                     {
                         throw new Exception("Expression began with opening parenthesis but did not end with closing parenthesis.");
                     }
                     // This character ends the expression.
                     state = ViewParseState.HTML;
                     sb.Append(ProcessExpression(expressionBuffer, ViewData));
                     if (!expressionStartedWithParenthesis)
                     {
                         sb.Append(c);
                     }
                     expressionStartedWithParenthesis = false;
                 }
             }
         }
         if (state == ViewParseState.Expression)
         {
             sb.Append(ProcessExpression(expressionBuffer, ViewData));
         }
         viewText = sb.ToString();
     }
     BodyStr = viewText;
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Contructs a ViewResult from the specified file.
        /// </summary>
        /// <param name="filePath">Path to a text file containing the view content.</param>
        /// <param name="ViewData">A ViewDataContainer containing values for expressions found within the view.</param>
        public ViewResult(string filePath, ViewDataContainer ViewData) : base(null)
        {
            string text = File.ReadAllText(filePath);

            ProcessView(text, ViewData);
        }
Ejemplo n.º 5
0
 public ViewBagContainer(ViewDataContainer ViewData)
 {
     this.ViewData = ViewData;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Processes the specified text as a view and sets this result body.
 /// </summary>
 /// <param name="viewText">The view's text.</param>
 /// <param name="ViewData">A ViewDataContainer containing values for expressions found within the view.</param>
 /// <returns></returns>
 public void ProcessView(string viewText, ViewDataContainer ViewData)
 {
     if (ViewData != null)
     {
         StringBuilder  sb = new StringBuilder(viewText.Length);
         StringBuilder  expressionBuffer = new StringBuilder();
         ViewParseState state            = ViewParseState.HTML;
         foreach (char c in viewText)
         {
             if (state == ViewParseState.HTML)
             {
                 if (c == '@')
                 {
                     state = ViewParseState.Expression;
                 }
                 else
                 {
                     sb.Append(c);
                 }
             }
             else if (state == ViewParseState.Expression)
             {
                 if (c == '@')
                 {
                     if (expressionBuffer.Length == 0)
                     {
                         // This was a sequence of two adjacent '@' characters, which is output as one literal '@' character.
                         state = ViewParseState.HTML;
                         sb.Append(c);
                     }
                     else
                     {
                         // New '@' ends the previous expression and starts a new one.
                         sb.Append(ProcessExpression(expressionBuffer, ViewData));
                     }
                 }
                 else if ((c >= 'a' && c <= 'z') ||
                          (c >= 'A' && c <= 'Z') ||
                          (c >= '0' && c <= '9') ||
                          c == '_' ||
                          c == ':')
                 {
                     // This character gets added to the expression.
                     expressionBuffer.Append(c);
                 }
                 else
                 {
                     // This character ends the expression.
                     state = ViewParseState.HTML;
                     sb.Append(ProcessExpression(expressionBuffer, ViewData));
                     sb.Append(c);
                 }
             }
         }
         if (state == ViewParseState.Expression)
         {
             sb.Append(ProcessExpression(expressionBuffer, ViewData));
         }
         viewText = sb.ToString();
     }
     Body = Encoding.UTF8.GetBytes(viewText);
 }