⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sgmlreader.cs

📁 Freetextbox是优秀的在线编辑器
💻 CS
📖 第 1 页 / 共 4 页
字号:
namespace FreeTextBoxControls.Support.Sgml
{
    using FreeTextBoxControls;
    using System;
    using System.IO;
    using System.Reflection;
    using System.Text;
    using System.Xml;

    public class SgmlReader : XmlReader
    {
        static SgmlReader()
        {
            SgmlReader.declterm = " \t\r\n><";
            SgmlReader.tagterm = " \t\r\n=/><";
            SgmlReader.aterm = " \t\r\n=/>";
            SgmlReader.avterm = " \t\r\n>";
            SgmlReader.cdataterm = "\t\r\n[<>";
            SgmlReader.dtterm = " \t\r\n>";
            SgmlReader.piterm = " \t\r\n?";
        }

        public SgmlReader()
        {
            this.folding = FreeTextBoxControls.Support.Sgml.CaseFolding.None;
            this.stripDocType = true;
            this.Init();
            this.nametable = new System.Xml.NameTable();
        }

        public override void Close()
        {
            if (this.current != null)
            {
                this.current.Close();
                this.current = null;
            }
            if (this.log != null)
            {
                this.log.Close();
                this.log = null;
            }
        }

        private void ExpandEntity(StringBuilder sb, char terminator)
        {
            char ch1 = this.current.ReadChar();
            if (ch1 == '#')
            {
                string text1 = this.current.ExpandCharEntity();
                sb.Append(text1);
                ch1 = this.current.ReadChar();
            }
            else
            {
                this.name.Length = 0;
                while ((ch1 != 0xffff) && ((char.IsLetter(ch1) || (ch1 == '_')) || (ch1 == '-')))
                {
                    this.name.Append(ch1);
                    ch1 = this.current.ReadChar();
                }
                string text2 = this.name.ToString();
                if ((this.dtd != null) && (text2 != ""))
                {
                    Entity entity1 = this.dtd.FindEntity(text2);
                    if (entity1 != null)
                    {
                        if (entity1.Internal)
                        {
                            sb.Append(entity1.Literal);
                            if (ch1 != terminator)
                            {
                                ch1 = this.current.ReadChar();
                            }
                            return;
                        }
                        Entity entity2 = new Entity(text2, entity1.PublicId, entity1.Uri, this.current.Proxy);
                        entity1.Open(this.current, new Uri(entity1.Uri));
                        this.current = entity2;
                        this.current.ReadChar();
                        return;
                    }
                    this.Log("Undefined entity '{0}'", new string[] { text2 });
                }
                sb.Append("&");
                sb.Append(text2);
                if (ch1 != terminator)
                {
                    sb.Append(ch1);
                    ch1 = this.current.ReadChar();
                }
            }
        }

        public override string GetAttribute(int i)
        {
            if ((this.state != FreeTextBoxControls.Support.Sgml.State.Attr) && (this.state != FreeTextBoxControls.Support.Sgml.State.AttrValue))
            {
                FreeTextBoxControls.Support.Sgml.Attribute attribute1 = this.node.GetAttribute(i);
                if (attribute1 != null)
                {
                    return attribute1.Value;
                }
            }
            throw new IndexOutOfRangeException();
        }

        public override string GetAttribute(string name)
        {
            if ((this.state != FreeTextBoxControls.Support.Sgml.State.Attr) && (this.state != FreeTextBoxControls.Support.Sgml.State.AttrValue))
            {
                int num1 = this.node.GetAttribute(name);
                if (num1 >= 0)
                {
                    return this.GetAttribute(num1);
                }
            }
            return null;
        }

        public override string GetAttribute(string name, string namespaceURI)
        {
            return this.GetAttribute(name);
        }

        public Encoding GetEncoding()
        {
            if (this.current == null)
            {
                this.OpenInput();
            }
            return this.current.GetEncoding();
        }

        private void Init()
        {
            this.state = FreeTextBoxControls.Support.Sgml.State.Initial;
            this.stack = new FreeTextBoxControls.Support.Sgml.HWStack(10);
            this.node = this.Push(null, XmlNodeType.Document, null);
            this.node.IsEmpty = false;
            this.sb = new StringBuilder();
            this.name = new StringBuilder();
            this.poptodepth = 0;
            this.current = null;
            this.partial = '\0';
            this.endTag = null;
            this.a = null;
            this.apos = 0;
            this.newnode = null;
            this.rootCount = 0;
        }

        private void LazyLoadDtd(Uri baseUri)
        {
            if (this.dtd == null)
            {
                if ((this.syslit == null) || (this.syslit == ""))
                {
                    if ((this.docType != null) && (this.docType.ToLower() == "html"))
                    {
                        Assembly assembly1 = typeof(FreeTextBox).Assembly;
                        string text1 = "FreeTextBoxControls.Support.SgmlReader.Resources.Html.dtd";
                        Stream stream1 = assembly1.GetManifestResourceStream(text1);
                        try
                        {
                            StreamReader reader1 = new StreamReader(stream1);
                            this.dtd = SgmlDtd.Parse(baseUri, "HTML", null, reader1, null, this.proxy, this.nametable);
                        }
                        catch (Exception exception1)
                        {
                            throw new Exception(exception1.ToString() + "//" + text1 + @"\\");
                        }
                    }
                }
                else
                {
                    if (baseUri != null)
                    {
                        baseUri = new Uri(baseUri, this.syslit);
                    }
                    else if (this.baseUri != null)
                    {
                        baseUri = new Uri(this.baseUri, this.syslit);
                    }
                    else
                    {
                        baseUri = new Uri(new Uri(Directory.GetCurrentDirectory() + @"\"), this.syslit);
                    }
                    this.dtd = SgmlDtd.Parse(baseUri, this.docType, this.pubid, baseUri.AbsoluteUri, this.subset, this.proxy, this.nametable);
                }
                if ((this.dtd != null) && (this.dtd.Name != null))
                {
                    switch (this.CaseFolding)
                    {
                        case FreeTextBoxControls.Support.Sgml.CaseFolding.ToUpper:
                            this.rootElementName = this.dtd.Name.ToUpper();
                            break;

                        case FreeTextBoxControls.Support.Sgml.CaseFolding.ToLower:
                            this.rootElementName = this.dtd.Name.ToLower();
                            break;

                        default:
                            this.rootElementName = this.dtd.Name;
                            break;
                    }
                    this.isHtml = this.dtd.Name.ToLower() == "html";
                }
            }
        }

        private void Log(string msg, char ch)
        {
            this.Log(msg, new string[] { ch.ToString() });
        }

        private void Log(string msg, params string[] args)
        {
            if (this.ErrorLog != null)
            {
                string text1 = string.Format(msg, args);
                if (this.lastError != this.current)
                {
                    text1 = text1 + "    " + this.current.Context();
                    this.lastError = this.current;
                    this.ErrorLog.WriteLine("### Error:" + text1);
                }
                else
                {
                    string text2 = "";
                    if (this.current.ResolvedUri != null)
                    {
                        text2 = this.current.ResolvedUri.AbsolutePath;
                    }
                    this.ErrorLog.WriteLine(string.Concat(new object[] { "### Error in ", text2, "#", this.current.Name, ", line ", this.current.Line, ", position ", this.current.LinePosition, ": ", text1 }));
                }
            }
        }

        public override string LookupNamespace(string prefix)
        {
            return null;
        }

        public override void MoveToAttribute(int i)
        {
            FreeTextBoxControls.Support.Sgml.Attribute attribute1 = this.node.GetAttribute(i);
            if (attribute1 == null)
            {
                throw new IndexOutOfRangeException();
            }
            this.apos = i;
            this.a = attribute1;
            this.node.CurrentState = this.state;
            this.state = FreeTextBoxControls.Support.Sgml.State.Attr;
        }

        public override bool MoveToAttribute(string name)
        {
            int num1 = this.node.GetAttribute(name);
            if (num1 >= 0)
            {
                this.MoveToAttribute(num1);
                return true;
            }
            return false;
        }

        public override bool MoveToAttribute(string name, string ns)
        {
            return this.MoveToAttribute(name);
        }

        public override bool MoveToElement()
        {
            if ((this.state == FreeTextBoxControls.Support.Sgml.State.Attr) || (this.state == FreeTextBoxControls.Support.Sgml.State.AttrValue))
            {
                this.state = this.node.CurrentState;
                this.a = null;
                return true;
            }
            return (this.node.NodeType == XmlNodeType.Element);
        }

        public override bool MoveToFirstAttribute()
        {
            if (this.node.AttributeCount > 0)
            {
                this.MoveToAttribute(0);
                return true;
            }
            return false;
        }

        public override bool MoveToNextAttribute()
        {
            if ((this.state != FreeTextBoxControls.Support.Sgml.State.Attr) && (this.state != FreeTextBoxControls.Support.Sgml.State.AttrValue))
            {
                return this.MoveToFirstAttribute();
            }
            if (this.apos < (this.node.AttributeCount - 1))
            {
                this.MoveToAttribute((int) (this.apos + 1));
                return true;
            }
            return false;
        }

        private void OpenInput()
        {
            this.LazyLoadDtd(this.baseUri);
            if (this.Href != null)
            {
                this.current = new Entity("#document", null, this.href, this.proxy);
            }
            else
            {
                if (this.inputStream == null)
                {
                    throw new InvalidOperationException("You must specify input either via Href or InputStream properties");
                }
                this.current = new Entity("#document", null, this.inputStream, this.proxy);
            }
            this.current.Html = this.IsHtml;
            this.current.Open(null, this.baseUri);
            if (this.current.ResolvedUri != null)
            {
                this.baseUri = this.current.ResolvedUri;
            }
        }

        private bool ParseAspNet()
        {
            string text1 = "<%" + this.current.ScanToEnd(this.sb, "AspNet", "%>") + "%>";
            this.Push(null, XmlNodeType.CDATA, text1);
            return true;
        }

        private bool ParseCData()
        {
            bool flag1 = this.current.IsWhitespace;
            this.sb.Length = 0;
            char ch1 = this.current.Lastchar;
            if (this.partial != '\0')
            {
                this.Pop();
                switch (this.partial)
                {
                    case ' ':
                        goto Label_02DD;

                    case '!':
                        this.partial = ' ';
                        return this.ParseComment();

                    case '/':
                        this.state = FreeTextBoxControls.Support.Sgml.State.EndTag;
                        return true;

                    case '?':
                        this.partial = ' ';
                        return this.ParsePI();
                }
            }
            else
            {
                ch1 = this.current.ReadChar();
            }
        Label_02DD:
            while (ch1 != 0xffff)
            {
                if (ch1 == '<')
                {
                    ch1 = this.current.ReadChar();
                    switch (ch1)
                    {
                        case '!':
                            ch1 = this.current.ReadChar();
                            if (ch1 == '-')
                            {
                                if (flag1)
                                {
                                    this.partial = ' ';
                                    return this.ParseComment();
                                }
                                this.partial = '!';
                                break;
                            }
                            this.sb.Append('<');
                            this.sb.Append('!');
                            this.sb.Append(ch1);
                            flag1 = false;
                            goto Label_02D0;

                        case '?':
                            this.current.ReadChar();
                            if (flag1)
                            {
                                this.partial = ' ';
                                return this.ParsePI();
                            }
                            this.partial = '?';
                            break;

                        case '/':
                        {
                            string text1 = this.sb.ToString();
                            if (this.ParseEndTag() && (this.endTag == this.node.Name))
                            {
                                if (flag1 || (text1 == ""))
                                {
                                    return true;
                                }
                                this.partial = '/';
                                this.sb.Length = 0;
                                this.sb.Append(text1);
                                this.state = FreeTextBoxControls.Support.Sgml.State.CData;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -