예제 #1
0
        public void Parse(TextElement baseitem, string text)
        {
            this.Text = text;
            this.Evulator.IsParseMode = true;
            TextElement currenttag = null;

            if (baseitem == null)
            {
                currenttag = this.Evulator.Elements;
            }
            else
            {
                currenttag = baseitem;
            }
            currenttag.BaseEvulator = this.Evulator;
            for (int i = 0; i < TextLength; i++)
            {
                TextElement tag = this.ParseTag(i, currenttag);
                if (tag == null)
                {
                    i = this.pos;
                    continue;
                }

                if (!tag.SlashUsed)
                {
                    currenttag.AddElement(tag);
                    if (tag.DirectClosed)
                    {
                        this.Evulator.OnTagClosed(tag);
                    }
                }
                if (tag.SlashUsed)
                {
                    TextElement prevtag = this.GetNotClosedPrevTag(tag);
                    //$alltags = $this->GetNotClosedPrevTagUntil($tag, $tag->elemName);
                    // int total = 0;
                    /** @var TextElement $baseitem */
                    TextElement previtem = null;
                    while (prevtag != null)
                    {
                        if (!prevtag.NameEquals(tag.ElemName, true))
                        {
                            var elem = new TextElement
                            {
                                ElemName     = prevtag.ElemName,
                                ElemAttr     = prevtag.ElemAttr.CloneWCS(),
                                AutoAdded    = true,
                                BaseEvulator = this.Evulator
                            };
                            prevtag.Closed = true;

                            if (previtem != null)
                            {
                                previtem.Parent = elem;
                                elem.AddElement(previtem);
                            }
                            else
                            {
                                currenttag = elem;
                            }
                            previtem = elem;
                        }
                        else
                        {
                            if (prevtag.ElemName != tag.ElemName)
                            {
                                prevtag.AliasName = tag.ElemName;
                                //Alias
                            }
                            if (previtem != null)
                            {
                                previtem.Parent = prevtag.Parent;
                                previtem.Parent.AddElement(previtem);
                            }
                            else
                            {
                                currenttag = prevtag.Parent;
                            }
                            prevtag.Closed = true;
                            break;
                        }
                        prevtag = this.GetNotClosedPrevTag(prevtag);
                    }
                    if (prevtag == null && this.Evulator.ThrowExceptionIFPrevIsNull)
                    {
                        this.Evulator.IsParseMode = false;
                        throw new Exception("Syntax Error");
                    }
                }
                else if (!tag.Closed)
                {
                    currenttag = tag;
                }


                i = this.pos;
            }
            this.pos                  = 0;
            this.in_noparse           = false;
            this.Evulator.IsParseMode = false;
        }
예제 #2
0
        private TextElement ParseTag(int start, TextElement parent = null)
        {
            bool        inspec     = false;
            TextElement tagElement = new TextElement
            {
                Parent       = parent,
                BaseEvulator = this.Evulator
            };
            bool istextnode = false;
            bool intag      = false;

            for (int i = start; i < this.TextLength; i++)
            {
                if (this.Evulator.NoParseEnabled && this.in_noparse)
                {
                    istextnode             = true;
                    tagElement.ElemName    = "#text";
                    tagElement.ElementType = TextElementType.TextNode;
                    tagElement.Closed      = true;
                }
                else
                {
                    var cur = this.Text[i];
                    if (!inspec)
                    {
                        if (cur == this.Evulator.LeftTag)
                        {
                            if (intag)
                            {
                                this.Evulator.IsParseMode = false;
                                throw new Exception("Syntax Error");
                            }
                            intag = true;
                            continue;
                        }
                        else if (this.Evulator.DecodeAmpCode && cur == '&')
                        {
                            string ampcode = this.DecodeAmp(i + 1, false);
                            i = this.pos;
                            if (ampcode.StartsWith("&"))
                            {
                                this.Evulator.IsParseMode = false;
                                throw new Exception("Syntax Error");
                            }
                            tagElement.AutoClosed  = true;
                            tagElement.Closed      = true;
                            tagElement.Value       = ampcode;
                            tagElement.ElementType = TextElementType.EntityReferenceNode;
                            tagElement.ElemName    = "#text";
                            return(tagElement);
                        }
                        else
                        {
                            if (!intag)
                            {
                                istextnode             = true;
                                tagElement.ElemName    = "#text";
                                tagElement.ElementType = TextElementType.TextNode;
                                tagElement.Closed      = true;
                            }
                        }
                    }
                    if (!inspec && cur == this.Evulator.RightTag)
                    {
                        if (!intag)
                        {
                            this.Evulator.IsParseMode = false;
                            throw new Exception("Syntax Error");
                        }
                        intag = false;
                    }
                }
                this.pos = i;
                if (!intag || istextnode)
                {
                    tagElement.Value = this.ParseInner();
                    if (!this.in_noparse && tagElement.ElementType == TextElementType.TextNode && string.IsNullOrEmpty(tagElement.Value))
                    {
                        return(null);
                    }
                    intag = false;
                    if (this.in_noparse)
                    {
                        parent.AddElement(tagElement);
                        var elem = new TextElement
                        {
                            Parent    = parent,
                            ElemName  = this.Evulator.NoParseTag,
                            SlashUsed = true
                        };
                        this.in_noparse = false;
                        return(elem);
                    }
                    return(tagElement);
                }
                else
                {
                    this.ParseTagHeader(ref tagElement);
                    intag = false;
                    if (this.Evulator.NoParseEnabled && tagElement.ElemName == this.Evulator.NoParseTag)
                    {
                        this.in_noparse = true;
                    }
                    return(tagElement);
                }
            }
            return(tagElement);
        }