Exemple #1
0
 public Node(Placehold begin, Placehold end, string html)
 {
     ID                 = begin.ID;
     Children           = new List <Node>();
     this.PlaceholdType = begin.PlaceholdType;
     InnerHtml          = html.Substring(begin.EndPosition, end.StartPosition - begin.EndPosition);
     OuterHtml          = html.Substring(begin.StartPosition, end.EndPosition - begin.StartPosition);
     UniqueSign         = string.Format("<^*?{0}?*^>", Guid.NewGuid().ToString());
 }
Exemple #2
0
        private static List <Node> AnalyseTemplate(string html)
        {
            string          pattern          = @"<%\s*(?<place>IF|TABLE_ROWS)_(?<tag>BEGIN|END)\s+ID\s*=(?<id>[\s\S]+?)\s*%>";
            MatchCollection matchCollectioin = Regex.Matches(html, pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);

            Stack <Placehold> stack = new Stack <Placehold>(matchCollectioin.Count);
            List <KeyValuePair <int, Node> > stackNode = new List <KeyValuePair <int, Node> >(matchCollectioin.Count / 2);
            StringBuilder htmlCopy = new StringBuilder(html);
            int           cut_len  = 0;

            foreach (Match matchField in matchCollectioin)
            {
                if (matchField.Success && matchField.Groups["id"] != null && !string.IsNullOrWhiteSpace(matchField.Groups["id"].Value))
                {
                    string    tag   = matchField.Groups["tag"].Value.ToUpper();
                    string    place = matchField.Groups["place"].Value.ToUpper();
                    Placehold p     = new Placehold();
                    if (tag == "BEGIN")
                    {
                        p.TagType = TagType.Begin;
                    }
                    else
                    {
                        p.TagType = TagType.End;
                    }
                    if (place == "IF")
                    {
                        p.PlaceholdType = PlaceholdType.If;
                    }
                    else
                    {
                        p.PlaceholdType = PlaceholdType.Table;
                    }
                    p.ID   = matchField.Groups["id"].Value.Trim();
                    p.Word = matchField.Groups[0].Value;
                    int t_start = htmlCopy.ToString().IndexOf(p.Word);
                    p.StartPosition = t_start + cut_len;
                    cut_len         = cut_len + p.Word.Length;
                    htmlCopy.Remove(t_start, p.Word.Length);
                    if (p.TagType == TagType.Begin)
                    {
                        stack.Push(p);
                    }
                    else
                    {
                        if (stack.Count <= 0)
                        {
                            throw new ApplicationException("The tag '" + p.Word + "' has not a matched Begin tag.");
                        }
                        Placehold tmp = stack.Pop();
                        if (tmp.TagType == TagType.Begin && tmp.PlaceholdType == p.PlaceholdType && tmp.ID == p.ID)
                        {
                            Node n = new Node(tmp, p, html);
                            stackNode.Add(new KeyValuePair <int, Node>(stack.Count, n));
                        }
                        else
                        {
                            throw new ApplicationException("The tag '" + p.Word + "' is not matched with the Begin tag '" + tmp.Word + "'");
                        }
                    }
                }
            }

            List <Node> nodeList = new List <Node>(stackNode.Count(k => k.Key == 0));

            for (int i = 0; i < stackNode.Count; i++)
            {
                KeyValuePair <int, Node> n = stackNode[i];
                if (n.Key == 0)
                {
                    nodeList.Add(n.Value);
                }
                else
                {
                    for (int j = i + 1; j < stackNode.Count; j++)
                    {
                        if (stackNode[j].Key == n.Key - 1)
                        {
                            stackNode[j].Value.Children.Add(n.Value);
                            break;
                        }
                    }
                }
            }
            return(nodeList);
        }