Inheritance: TextReader
Esempio n. 1
0
        /// <summary>
        ///   Begins processing an entity.
        /// </summary>
        /// <param name = "parent">The parent of this entity.</param>
        /// <param name = "baseUri">The base Uri for processing this entity within.</param>
        public void Open(Entity parent, Uri baseUri)
        {
            this.m_parent = parent;
            if (parent != null)
            {
                this.m_isHtml = parent.IsHtml;
            }
            this.m_line = 1;
            if (this.m_isInternal)
            {
                if (this.m_literal != null)
                {
                    this.m_stm = new StringReader(this.m_literal);
                }
            }
            else if (this.m_uri == null)
            {
                this.Error("Unresolvable entity '{0}'", this.m_name);
            }
            else
            {
                if (baseUri != null)
                {
                    this.m_resolvedUri = new Uri(baseUri, this.m_uri);
                }
                else
                {
                    this.m_resolvedUri = new Uri(this.m_uri);
                }

                Stream stream = null;
                Encoding e = Encoding.Default;
                switch (this.m_resolvedUri.Scheme)
                {
                    case "file":
                        {
                            string path = this.m_resolvedUri.LocalPath;
                            stream = new FileStream(path, FileMode.Open, FileAccess.Read);
                        }
                        break;
                    default:
                        //Console.WriteLine("Fetching:" + ResolvedUri.AbsoluteUri);
                        HttpWebRequest wr = (HttpWebRequest) WebRequest.Create(this.ResolvedUri);
                        wr.UserAgent = "Mozilla/4.0 (compatible;);";
                        wr.Timeout = 10000; // in case this is running in an ASPX page.
                        if (this.m_proxy != null)
                        {
                            wr.Proxy = new WebProxy(this.m_proxy);
                        }
                        wr.PreAuthenticate = false;
                        // Pass the credentials of the process.
                        wr.Credentials = CredentialCache.DefaultCredentials;

                        WebResponse resp = wr.GetResponse();
                        Uri actual = resp.ResponseUri;
                        if (!string.Equals(actual.AbsoluteUri, this.m_resolvedUri.AbsoluteUri, StringComparison.OrdinalIgnoreCase))
                        {
                            this.m_resolvedUri = actual;
                        }
                        string contentType = resp.ContentType.ToLowerInvariant();
                        string mimeType = contentType;
                        int i = contentType.IndexOf(';');
                        if (i >= 0)
                        {
                            mimeType = contentType.Substring(0, i);
                        }

                        if (StringUtilities.EqualsIgnoreCase(mimeType, "text/html"))
                        {
                            this.m_isHtml = true;
                        }

                        i = contentType.IndexOf("charset");
                        e = Encoding.Default;
                        if (i >= 0)
                        {
                            int j = contentType.IndexOf("=", i);
                            int k = contentType.IndexOf(";", j);
                            if (k < 0)
                            {
                                k = contentType.Length;
                            }

                            if (j > 0)
                            {
                                j++;
                                string charset = contentType.Substring(j, k - j).Trim();
                                try
                                {
                                    e = Encoding.GetEncoding(charset);
                                }
                                catch (ArgumentException) {}
                            }
                        }

                        stream = resp.GetResponseStream();
                        break;
                }

                this.m_weOwnTheStream = true;
                HtmlStream html = new HtmlStream(stream, e);
                this.m_encoding = html.Encoding;
                this.m_stm = html;
            }
        }