Example #1
0
 public StringParser(URL url)
     : this(url.GetHTML())
 {
 }
Example #2
0
 public StringParser(URL url, Encoding encoding)
     : this(url.GetHTML(encoding))
 {
 }
Example #3
0
        /// <summary>
        /// Reads a <see cref="URL"/> address using the specified encoding.
        /// </summary>
        /// <param name="url">The <see cref="URL"/> to read.</param>
        /// <param name="encoding">The encoding used for reading.</param>
        /// <returns>An object of type <typeparamref name="T"/> or one of it derivatives, that represents the loaded page.
        /// This is read from buffer, if preloaded; or via the <see cref="Convert"/> method, if not.</returns>
        protected T ReadURL(URL url, Encoding encoding)
        {
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }

            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }

            url = translate(url);

            T item = null;

            if (itemDictionary.ContainsKey(url))
            {
                item = itemDictionary[url];
            }

            if (item == null)
            {
                if (fileDictionary.ContainsKey(url))
                {
                    string html;
                    URL    responseURL;
                    using (FileStream fs = new FileStream(this.filepath, FileMode.Open))
                        fileDictionary[url].Read(fs, out html, out responseURL);

                    item = Convert(html, url, responseURL);
                    if (item == null)
                    {
                        throw new InvalidOperationException("The " + typeof(Convert).Name + " method may not return null.");
                    }
                    item.Buffer = this;
                    if (itemDictionary.ContainsKey(url))
                    {
                        itemDictionary[url] = item;
                    }
                    else
                    {
                        itemDictionary.Add(url, item);
                    }
                }
                else
                {
                    URL    newurl;
                    string html = url.GetHTML(encoding, out newurl);
                    newurl = translate(newurl);
                    item   = Convert(html, url, newurl);
                    if (item == null)
                    {
                        throw new InvalidOperationException("The " + typeof(Convert).Name + " method may not return null.");
                    }
                    item.Buffer = this;

                    itemDictionary.Add(url, item);
                    if (url != newurl && !itemDictionary.ContainsKey(newurl))
                    {
                        T item2 = Convert(html, newurl, newurl);
                        if (item2 == null)
                        {
                            throw new InvalidOperationException("The " + typeof(Convert).Name + " method may not return null.");
                        }
                        item2.Buffer = this;
                        itemDictionary.Add(newurl, item2);
                    }
                }
            }
            return(item);
        }