Пример #1
0
        /// <summary>
        /// Parse
        /// </summary>
        public bool Parse(Template template)
        {
            // Defintions.
            bool success = false;

            Match statement = Token.ExpStatement.Match(this._clean);
            if (statement.Success)
            {
                switch (statement.Groups["head"].Value.ToLower())
                {
                    #region If
                    case "if":
                        if (template.CurrentBlock.Loop == null)
                        {
                            // Since If was triggered, we need a new Block.
                            Block block = new Block();

                            // If parent Block is not being rendered then this Block wont be rendered either.
                            if (!template.Blocks[template.Blocks.Count-1].Render)
                            {
                                block.Render = false;
                            }

                            // Render Block if needed.
                            if (block.Render)
                            {

                                SorentoLib.Render.Condition condition = new SorentoLib.Render.Condition(template.Session, statement.Groups["body"].Value);
                                if (condition.Result)
                                {
                                    block.LastIfStatementWasTrue = true;
                                }
                                else
                                {
                                    block.Render = false;
                                }

                                // CleanUp
                                condition = null;
                            }

                            // Add Block to list.
                            template.Blocks.Add(block);

                            // Finish.
                            success = true;
                        }
                    break;
                    #endregion

                    #region ElseIf
                    case "elseif":
                        if (template.CurrentBlock.Loop == null)
                        {
                            // If inital If or previous ElseIf was true, than we dont need dont need to render.
                            if (template.Blocks[template.Blocks.Count-1].LastIfStatementWasTrue)
                            {
                                template.Blocks[template.Blocks.Count-1].Render = false;
                            }
                            else
                            {
                                SorentoLib.Render.Condition condition = new SorentoLib.Render.Condition(template.Session, statement.Groups["body"].Value);
                                if (condition.Result)
                                {
                                    template.Blocks[template.Blocks.Count-1].Render = true;
                                    template.Blocks[template.Blocks.Count-1].LastIfStatementWasTrue = true;
                                }

                                // CleanUp
                                condition = null;
                            }

                            // Finish
                            success = true;
                        }
                    break;
                    #endregion

                    #region Else
                    case "else":
                        if (template.CurrentBlock.Loop == null)
                        {
                            // If inital If or ElseIf was true, do not render.
                            if (template.Blocks[template.Blocks.Count-2].Render)
                            {
                                if (template.Blocks[template.Blocks.Count-1].LastIfStatementWasTrue)
                                {
                                    template.Blocks[template.Blocks.Count-1].Render = false;
                                }
                                else
                                {
                                    template.Blocks[template.Blocks.Count-1].Render = true;
                                }
                            }

                            // Finish.
                            success = true;
                        }
                    break;
                    #endregion

                    #region Endif
                    case "endif":
                        if (template.CurrentBlock.Loop == null)
                        {
                            // Block depth has to be more than 0.
                            if (template.Blocks.Count > 0)
                            {
                                // Remove current Block.
                                template.Blocks.RemoveAt(template.Blocks.Count-1);
                            }

                            // Finish.
                            success = true;
                        }
                    break;
                    #endregion

                    #region Foreach
                    case "foreach":
                        if (template.CurrentBlock.BeginLoop())
                        {
                            // Create BLOCK to hold FOREACH.
                            template.AddBlock();
                            template.CurrentBlock.Loop = new Foreach(statement.Groups["body"].Value);

                            // Finish.
                            success = true;
                        }
                    break;
                    #endregion

                    #region EndForeach
                    case "endforeach":
                        if (template.CurrentBlock.EndLoop(Enums.LoopType.Foreach))
                        {
                            // Create resolver
                            SorentoLib.Render.Resolver resolver = new SorentoLib.Render.Resolver(template.Session, ((Foreach)template.CurrentBlock.Loop).Source);

                            // Render FOREACH block.
                            foreach (object item in (List<object>)resolver.Result)
                            {
            //								template.Session.Page.Variables.Set(, , item.GetType().Name.ToLower());
                                template.Session.Page.Variables.Add(((Foreach)template.CurrentBlock.Loop).Destination, item);
                                Template loop = new Template(template.Session, template.CurrentBlock.Loop.Content);
                                loop.Render();
                            }

                            // Remove FOREACH block.
                            template.RemoveBlock();

                            // Cleanup
                            resolver = null;

                            // Finish
                            success = true;
                        }
                    break;
                    #endregion

                    #region Default
                    default:
                        // TODO: this if should be removed.

                        if (!success)
                        {
                            if (template.CurrentBlock.Loop == null)
                            {
                                if (statement.Groups["set"].Success)
                                {
            //Console.WriteLine(statement.Groups["head"].Value+"]");
            //Console.WriteLine(statement.Groups["value"].Value+"]");
            //									Variables.SetValue(template.Session, statement.Groups["head"].Value, Variables.ParseVariables(template.Session, statement.Groups["value"].Value));

                                    // Create resolver
                                    SorentoLib.Render.Resolver resolver = new SorentoLib.Render.Resolver(template.Session, statement.Groups["value"].Value);

                                    template.Session.Page.Variables.Add(statement.Groups["head"].Value, resolver.Result);

            //									Console.WriteLine("type:");
            //									Console.WriteLine(resolver.Result);

            //									Variables.SetValue(template.Session, statement.Groups["head"].Value, resolver.Result);

                                    // CleanUp
                                    resolver = null;

                                    // Finish
                                    success = true;
                                }
                                else
                                {
                                    // Create resolver
                                    SorentoLib.Render.Resolver resolver = new SorentoLib.Render.Resolver(template.Session, this._clean);

                                    // Return resolver results
                                    this._result = (string)resolver.Result;

                                    // Cleanup
                                    resolver = null;

                                    // Finish
                                    success = true;
                                }
                            }
                        }
                    break;
                    #endregion
                }
            }

            // Escaping problem characters.
            this._raw = Regex.Replace(this._raw, @"\\", @"\\", RegexOptions.Compiled);	// \
            this._raw = Regex.Replace(this._raw, @"\(", @"\(", RegexOptions.Compiled);	// (
            this._raw = Regex.Replace(this._raw, @"\$", @"\$", RegexOptions.Compiled);	// $
            this._raw = Regex.Replace(this._raw, @"\)", @"\)", RegexOptions.Compiled);	// )
            this._raw = Regex.Replace(this._raw, @"\|", @"\|", RegexOptions.Compiled);	// |
            this._raw = Regex.Replace(this._raw, @"\+", @"\+", RegexOptions.Compiled);	// +

            // CleanUp
            template = null;
            statement = null;

            // Finish.
            this._succces = success;
            return success;
        }
Пример #2
0
        private System.String Parser(string Line, string Token)
        {
            string result = string.Empty;

            //			Console.WriteLine (Token);

            Match statement = Template.ExpStatement.Match (Token);
            if (statement.Success)
            {
                switch (statement.Groups["head"].Value.ToLower ())
                {
                    #region If
                    case "if":
                        if (!this.CurrentBlock.InLoop)
                        {
                            // Since If was triggered, we need a new Block.
                            SorentoLib.Render.Block block = new SorentoLib.Render.Block ();

                            // If parent Block is not being rendered then this Block wont be rendered either.
                            if (!this.CurrentBlock.Render)
                            {
                                block.Render = false;
                            }

                            // Render Block if needed.
                            if (block.Render)
                            {
                                if (Condition.Evaluate (this._session, statement.Groups["body"].Value))
                                {
                                    block.LastIfStatementWasTrue = true;
                                }
                                else
                                {
                                    block.Render = false;
                                }
                            }

                            this._blocks.Add (block);
                        }
                        break;
                    #endregion

                    #region ElseIf
                    case "elseif":
                        if (!this.CurrentBlock.InLoop)
                        {
                            // If inital If or previous ElseIf was true, than we dont need dont need to render.
                            if (this.CurrentBlock.LastIfStatementWasTrue)
                            {
                                this.CurrentBlock.Render = false;
                            }

                            else
                            {
                                if (Condition.Evaluate (this._session, statement.Groups["body"].Value))
                                {
                                    this.CurrentBlock.Render = true;
                                    this.CurrentBlock.LastIfStatementWasTrue = true;
                                }
                            }
                        }
                        break;
                    #endregion

                    #region Else
                    case "else":
                        if (!this.CurrentBlock.InLoop)
                        {
                            // If inital If or ElseIf was true, do not render.
                            if (this.ParentBlock.Render)
                            {
                                if (this.CurrentBlock.LastIfStatementWasTrue)
                                {
                                    this.CurrentBlock.Render = false;
                                }

                                else
                                {
                                    this.CurrentBlock.Render = true;
                                }
                            }
                        }
                        break;
                    #endregion

                    #region Endif
                    case "endif":
                        if (!this.CurrentBlock.InLoop)
                        {
                            this.RemoveBlock ();
                        }

                        break;
                        #endregion

                    #region Foreach
                    case "foreach":
                        if ((this.CurrentBlock.BeginLoop ()) && (this.CurrentBlock.Render))
                        {
                            this.AddBlock ();
                            this.CurrentBlock.Loop = new SorentoLib.Render.Foreach (statement.Groups["body"].Value);
                            // TODO: this should probally be dealt with.
                            Line = string.Empty;
                        }
                        break;
                    #endregion

                    #region EndForeach
                    case "endforeach":
                        if ((this.CurrentBlock.EndLoop (Enums.LoopType.Foreach)) && (this.CurrentBlock.Render))
                        {
                            SorentoLib.Render.Resolver resolver = new SorentoLib.Render.Resolver (this._session);
                            resolver.Parse(((Foreach)this.CurrentBlock.Loop).Source);

            //							Console.WriteLine (resolver.Result.GetType ().IsArray);
            //							Console.WriteLine (resolver.Result.GetType ().Name);

                            switch (resolver.Result.GetType ().Name.ToLower ())
                            {
                                #region List
                                case "list`1":
                                    MethodInfo methodInfo = typeof(SorentoLib.Render.Variables).GetMethod("ConvertToListObjectNew", System.Reflection.BindingFlags.Static | BindingFlags.Public);
                                    MethodInfo genericMethodInfo = methodInfo.MakeGenericMethod(new Type[] { resolver.Result.GetType ().GetGenericArguments()[0] });
                                    List<object> returnvalue = (List<object>)genericMethodInfo.Invoke(null, new object[] { resolver.Result });

                                    foreach (System.Object item in (System.Collections.Generic.List<System.Object>)returnvalue)
                                    {
                                        this._session.Page.Variables.Add (((SorentoLib.Render.Foreach)this.CurrentBlock.Loop).Destination, item);
                                        Template loop = new Template (this._session, this.CurrentBlock.Loop.Content);
                                        loop.Render ();
                                    }

                                    break;

                                case "string[]":
                                    foreach (string item in (string[])resolver.Result)
                                    {
                                        this._session.Page.Variables.Add (((SorentoLib.Render.Foreach)this.CurrentBlock.Loop).Destination, item);
                                        Template loop = new Template (this._session, this.CurrentBlock.Loop.Content);
                                        loop.Render ();
                                    }
                                    break;
                                #endregion
                            }

                            this.RemoveBlock ();

                            resolver = null;
                        }
                        break;
                    #endregion

                    #region Write
                    case "write":
                        // TODO: This probally needs to be done differently.
            //						try
            //						{
                            if ((!this.CurrentBlock.InLoop) && (this.CurrentBlock.Render))
                            {

                                SorentoLib.Render.Resolver resolver = new SorentoLib.Render.Resolver (this._session);
                                resolver.Parse (statement.Groups["body"].Value);

                            if (resolver.Result.GetType () == typeof (bool))
                            {
                                result = resolver.Result.ToString ().ToLower ();
                            }
                            else
                            {
                                result = resolver.Result.ToString ();
                            }

                            }
            //						}
            //						catch
            //						{}
                        break;
                    #endregion

                    #region Include
                    case "include":
                        if ((!this.CurrentBlock.InLoop) && (this.CurrentBlock.Render))
                        {
                            SorentoLib.Render.Resolver resolver = new SorentoLib.Render.Resolver (this._session);
                            resolver.Parse (statement.Groups["body"].Value);

                            SorentoLib.Render.Template include;
                            include = new SorentoLib.Render.Template (this._session, (string)resolver.Result);
            //							if (((string)resolver.Result).Substring(0, 2) == "./")
            //							{
            //								include = new SorentoLib.Render.Template (this._session, ((string)resolver.Result).Substring (2, ((string)resolver.Result).Length-2));
            //							}
            //							else
            //							{
            //								include = new SorentoLib.Render.Template (this._session, Path.GetDirectoryName (this._session.Request.Environment.RequestUri) + "/" + (string)resolver.Result);
            //							}

                            include.Render ();

                            resolver = null;
                            include = null;
                        }
                        break;
                    #endregion

                    #region Render
                    case "render":
                        if ((!this.CurrentBlock.InLoop) && (this.CurrentBlock.Render))
                        {
                            SorentoLib.Render.Resolver resolver = new SorentoLib.Render.Resolver (this._session);
                            resolver.Parse (statement.Groups["body"].Value);

                            this._session.Page.Clear();
                            Template redirect = new Template(this._session, (string)resolver.Result);
                            redirect.Render ();

                            this._endrender = true;

                            resolver = null;
                        }
                        break;
                    #endregion

                    #region Redirect
                    case "redirect":
                        if ((!this.CurrentBlock.InLoop) && (this.CurrentBlock.Render))
                        {

                            SorentoLib.Render.Resolver resolver = new SorentoLib.Render.Resolver (this._session);
                            resolver.Parse (statement.Groups["body"].Value);

                            // TODO: This probally needs to be fixed.
            //							this._session.Page.Clear();

                            this._session.Page.Redirect = (string)resolver.Result;

            //							Console.WriteLine ("dczdf");

                            this._session.Page.Lines.Add(@"<meta HTTP-EQUIV=""REFRESH"" content=""0; url="+ (string)resolver.Result +@""">");

            //							HTTP/1.1 301 Moved Permanently" );
            //Header( "Location: http://www.new-url.com

                            this._endrender = true;

                            resolver = null;
                        }
                        break;
                    #endregion

                    #region Default
                    default:
                        if ((!this.CurrentBlock.InLoop) && (this.CurrentBlock.Render == true))
                        {
                            if (statement.Groups["set"].Success)
                            {
            //								Console.WriteLine (statement.Groups["value"].Value);
                                SorentoLib.Render.Resolver resolver = new SorentoLib.Render.Resolver (this._session);
                                resolver.Parse(statement.Groups["value"].Value);

                                this._session.Page.Variables.Add (statement.Groups["head"].Value, resolver.Result);
                                resolver = null;
                            }
                            else
                            {
                                SorentoLib.Render.Resolver resolver = new SorentoLib.Render.Resolver (this._session);

                                resolver.Parse(Token);

                                resolver = null;
                            }
                        }
                        break;
                    #endregion
                }
            }

            // Escaping problem characters.
            Token = Regex.Replace (Token, "\\\\", "\\\\", RegexOptions.Compiled);
            Token = Regex.Replace (Token, "\\(", "\\(", RegexOptions.Compiled);
            Token = Regex.Replace (Token, "\\$", "\\$", RegexOptions.Compiled);
            Token = Regex.Replace (Token, "\\)", "\\)", RegexOptions.Compiled);
            Token = Regex.Replace (Token, "\\|", "\\|", RegexOptions.Compiled);
            Token = Regex.Replace (Token, "\\+", "\\+", RegexOptions.Compiled);
            Token = Regex.Replace (Token, "\\[", "\\[", RegexOptions.Compiled);
            Token = Regex.Replace (Token, "\\]", "\\]", RegexOptions.Compiled);
            Token = Regex.Replace (Token, "\\*", "\\*", RegexOptions.Compiled);

            // Render Token.
            if ((!this.CurrentBlock.InLoop) && (this.CurrentBlock.Render))
            {
                Line = Regex.Replace (Line, "<%"+ Token +"%>", result);
            }

            statement = null;

            return Line;
        }