Ejemplo n.º 1
0
        private TemplateElement CreateTag(Match match)
        {
            Group  param = match.Groups["param"];
            string name  = match.Groups["name"].Value;

            bool closed = false;

            TemplateElementTypes type = TemplateElementTypes.Tag;

            name = name.ToLower();

            switch (name)
            {
            case "if":
                type = TemplateElementTypes.IfExpression;
                break;

            case "else":
                if (SourceTemplate.IndexOf("if ", param.Index, param.Length, StringComparison.OrdinalIgnoreCase) == param.Index)
                {
                    type = TemplateElementTypes.ElseIfExpression;
                }
                else
                {
                    type = TemplateElementTypes.ElseExpression;
                }
                break;

            case "load":
                type = TemplateElementTypes.LoadExpression;
                break;

            //case "pre-include":
            //    type = TemplateElementTypes.PreIncludeExpression;
            //    break;

            case "loop":
                type = TemplateElementTypes.LoopExpression;
                break;

            case "ajaxpanel":
                type = TemplateElementTypes.AjaxPanel;
                break;
            }

            closed = match.Groups["close"].Success ||
                     type == TemplateElementTypes.ElseExpression ||
                     type == TemplateElementTypes.ElseIfExpression ||
                     type == TemplateElementTypes.LoadExpression;
            //|| type == TemplateElementTypes.PreIncludeExpression;

            Hashtable items = new Hashtable(2);

            items.Add(KEY_NAME, name);
            items.Add(KEY_CLOSED, closed);

            int index         = match.Index + match.Length;
            int contentLength = closed ? match.Length : SourceTemplate.Length - index;

            if (closed)
            {
                items.Add("END", index);
            }

            TemplateElement result = new TemplateElement(this.Document, type, index, contentLength, items);

            if (type == TemplateElementTypes.IfExpression || type == TemplateElementTypes.ElseIfExpression)
            {
                result.ChildNodes.Insert(0, CreateConditionExpression(param, type == TemplateElementTypes.ElseIfExpression));
            }
            else if (type == TemplateElementTypes.LoopExpression)
            {
                result.ChildNodes.Insert(0, CreateLoopExpressionParam(param));
            }
            else
            {
                result.ChildNodes.Insert(0, CreateAttributeList(param));
            }

            return(result);
        }
Ejemplo n.º 2
0
        private void ParseTemplateTag(int index, int length)
        {
            int tagBegin = SourceTemplate.IndexOf(TAG_BEGIN, index, length);

            if (tagBegin >= 0)
            {
                ParseTemplateCode(index, tagBegin - index, true, false);

                length = length - (tagBegin - index);
                index  = tagBegin;

                Match match = null;

                if ((match = TemplateTagBeginRegex.Match(SourceTemplate, index, length)).Success)
                {
                    TemplateElement tag = CreateTag(match);

                    ChildNodes.Add(tag);

                    //if (tag.Type == TemplateElementTypes.PreIncludeExpression)
                    //{
                    //    if (tag.ChildNodes[0].ChildNodes.Count >= 1)
                    //    {
                    //        TemplateElement element = tag.ChildNodes[0].ChildNodes[0];

                    //        if (element.ChildNodes[0].Type == TemplateElementTypes.Literal)
                    //        {
                    //            string path = element.ChildNodes[0].Text;

                    //            if (path.StartsWith("/") == false && path.StartsWith("~/") == false)
                    //            {
                    //                path = this.Document.TemplateFile.Owner.VirtualPath + path;
                    //            }

                    //            path = HttpContext.Current.Server.MapPath(path);

                    //            if (File.Exists(path))
                    //            {
                    //                string content = File.ReadAllText(path);

                    //                TemplateElement doc = CreateDocument(content, null);

                    //                tag.ChildNodes.Add(doc);

                    //                this.Document.TemplateFile.AddWatcher(path);
                    //            }
                    //        }
                    //    }
                    //}

                    if (tag.Items.Contains("END") == false)
                    {
                        throw new TemplateTagNotCloseException(tag.Items["name"].ToString(), TemplateFile.FilePath, SourceTemplate, index);
                    }

                    int tagEndIndex = (int)tag.Items["END"];

                    if (tagEndIndex > 0)
                    {
                        int leavingLength = SourceTemplate.Length - tagEndIndex;

                        if (leavingLength > 0)
                        {
                            ParseTemplateTag(tagEndIndex, leavingLength);
                        }
                    }
                }
                else if ((match = TemplateTagEndRegex.Match(SourceTemplate, index, length)).Success)
                {
                    if ((Type == TemplateElementTypes.Tag || Type == TemplateElementTypes.IfExpression || Type == TemplateElementTypes.LoopExpression || Type == TemplateElementTypes.AjaxPanel) &&
                        StringUtil.EqualsIgnoreCase(match.Groups["name"].Value, (string)Items[KEY_NAME]))
                    {
                        Length = match.Index - Index;

                        Items["END"] = match.Index + match.Length;
                    }
                    else
                    {
                        ChildNodes.Add(CreateLiteral(match.Index, match.Length));

                        int tagEnd = match.Index + match.Length;

                        ParseTemplateTag(tagEnd, length - (tagEnd - index));
                    }
                }
                else
                {
                    ChildNodes.Add(CreateLiteral(tagBegin, TAG_BEGIN.Length));

                    ParseTemplateTag(tagBegin + TAG_BEGIN.Length, length - TAG_BEGIN.Length);
                }
            }
            else
            {
                ParseTemplateCode(index, length, true, false);
            }
        }