예제 #1
0
 /// <summary>
 ///     Closes connection to file.
 /// </summary>
 /// <remarks>
 ///     This method also releases any resources held while reading.
 /// </remarks>
 public void Close()
 {
     this.textInput = null;
     this.image     = null;
     this.cloud     = null;
     this.channel   = null;
     this.source    = null;
     this.enclosure = null;
     this.category  = null;
     this.item      = null;
     if (this.reader != null)
     {
         this.reader.Close();
         this.reader = null;
     }
     this.elementText  = null;
     this.xmlNodeStack = null;
 }
예제 #2
0
 /// <summary>
 ///     Writes an RSS channel
 /// </summary>
 /// <exception cref="InvalidOperationException">RssWriter has been closed, and can not be written to.</exception>
 /// <exception cref="ArgumentNullException">Channel must be instanciated with data, before calling Write.</exception>
 /// <param name="channel"> RSS channel to write </param>
 public void Write(RssChannel.RssChannel channel)
 {
     this.writeChannel(channel);
 }
예제 #3
0
        private void writeChannel(RssChannel.RssChannel channel)
        {
            if (this.writer == null)
            {
                throw new InvalidOperationException("RssWriter has been closed, and can not be written to.");
            }
            if (channel == null)
            {
                throw new ArgumentNullException("Channel must be instanciated with data to be written.");
            }

            if (this.wroteChannel)
            {
                this.writer.WriteEndElement();
            }
            else
            {
                this.wroteChannel = true;
            }

            this.BeginDocument();

            this.writer.WriteStartElement("channel");
            WriteElement("title", channel.Title, true);
            WriteElement("description", channel.Description, true);
            WriteElement("link", channel.Link, true);
            if (channel.Image != null)
            {
                this.writer.WriteStartElement("image");
                WriteElement("title", channel.Image.Title, true);
                WriteElement("url", channel.Image.Url, true);
                WriteElement("link", channel.Image.Link, true);
                switch (this.rssVersion)
                {
                case RssVersion.RSS091:
                case RssVersion.RSS092:
                case RssVersion.RSS20:
                    WriteElement("description", channel.Image.Description, false);
                    WriteElement("width", channel.Image.Width, false);
                    WriteElement("height", channel.Image.Height, false);
                    break;
                }
                this.writer.WriteEndElement();
            }
            switch (this.rssVersion)
            {
            case RssVersion.RSS091:
            case RssVersion.RSS092:
            case RssVersion.RSS20:
                WriteElement("language", channel.Language, this.rssVersion == RssVersion.RSS091);
                WriteElement("copyright", channel.Copyright, false);
                WriteElement("managingEditor", channel.ManagingEditor, false);
                WriteElement("webMaster", channel.WebMaster, false);
                WriteElement("pubDate", channel.PubDate, false);
                WriteElement("lastBuildDate", channel.LastBuildDate, false);
                if (channel.Docs != RssDefault.String)
                {
                    WriteElement("docs", channel.Docs, false);
                }
                else
                {
                    switch (this.rssVersion)
                    {
                    case RssVersion.RSS091:
                        this.WriteElement("docs", "http://my.netscape.com/publish/formats/rss-spec-0.91.html",
                                          false);
                        break;

                    case RssVersion.RSS092:
                        this.WriteElement("docs", "http://backend.userland.com/rss092", false);
                        break;

                    case RssVersion.RSS20:
                        this.WriteElement("docs", "http://backend.userland.com/rss", false);
                        break;
                    }
                }
                WriteElement("rating", channel.Rating, false);
                string[] Days = { "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday" };
                for (int i = 0; i <= 6; i++)
                {
                    if (channel.SkipDays[i])
                    {
                        this.writer.WriteStartElement("skipDays");
                        for (int i2 = 0; i2 <= 6; i2++)
                        {
                            if (channel.SkipDays[i2])
                            {
                                WriteElement("day", Days[i2], false);
                            }
                        }
                        this.writer.WriteEndElement();
                        break;
                    }
                }
                for (int i = 0; i <= 23; i++)
                {
                    if (channel.SkipHours[i])
                    {
                        this.writer.WriteStartElement("skipHours");
                        for (int i2 = 0; i2 <= 23; i2++)
                        {
                            if (channel.SkipHours[i2])
                            {
                                this.WriteElement("hour", i2 + 1, false);
                            }
                        }
                        this.writer.WriteEndElement();
                        break;
                    }
                }
                break;
            }
            switch (this.rssVersion)
            {
            case RssVersion.RSS092:
            case RssVersion.RSS20:
                if (channel.Categories != null)
                {
                    foreach (RssCategory category in channel.Categories)
                    {
                        if (category.Name != RssDefault.String)
                        {
                            this.writer.WriteStartElement("category");
                            WriteAttribute("domain", category.Domain, false);
                            this.writer.WriteString(category.Name);
                            this.writer.WriteEndElement();
                        }
                    }
                }
                if (channel.Cloud != null)
                {
                    this.writer.WriteStartElement("cloud");
                    WriteElement("domain", channel.Cloud.Domain, false);
                    WriteElement("port", channel.Cloud.Port, false);
                    WriteElement("path", channel.Cloud.Path, false);
                    WriteElement("registerProcedure", channel.Cloud.RegisterProcedure, false);
                    if (channel.Cloud.Protocol != RssCloudProtocol.Empty)
                    {
                        WriteElement("Protocol", channel.Cloud.Protocol, false);
                    }
                    this.writer.WriteEndElement();
                }
                break;
            }
            if (this.rssVersion == RssVersion.RSS20)
            {
                if (channel.Generator != RssDefault.String)
                {
                    WriteElement("generator", channel.Generator, false);
                }
                else
                {
                    this.WriteElement("generator", "RSS.NET: http://www.rssdotnet.com/", false);
                }
                WriteElement("ttl", channel.TimeToLive, false);

                // RSS Modules
                foreach (RssModule rssModule in this._rssModules)
                {
                    if (rssModule.IsBoundTo(channel.GetHashCode()))
                    {
                        foreach (RssModuleItem rssModuleItem in rssModule.ChannelExtensions)
                        {
                            if (rssModuleItem.SubElements.Count == 0)
                            {
                                WriteElement(rssModule.NamespacePrefix + ":" + rssModuleItem.Name, rssModuleItem.Text,
                                             rssModuleItem.IsRequired);
                            }
                            else
                            {
                                this.writeSubElements(rssModuleItem.SubElements, rssModule.NamespacePrefix);
                            }
                        }
                    }
                }
            }
            if (channel.TextInput != null)
            {
                this.writer.WriteStartElement("textinput");
                WriteElement("title", channel.TextInput.Title, true);
                WriteElement("description", channel.TextInput.Description, true);
                WriteElement("name", channel.TextInput.Name, true);
                WriteElement("link", channel.TextInput.Link, true);
                this.writer.WriteEndElement();
            }
            foreach (RssItem.RssItem item in channel.Items)
            {
                this.writeItem(item, channel.GetHashCode());
            }
            this.writer.Flush();
        }
예제 #4
0
 /// <summary>
 ///     Closes connection to file.
 /// </summary>
 /// <remarks>
 ///     This method also releases any resources held while reading.
 /// </remarks>
 public void Close()
 {
     this.textInput = null;
     this.image = null;
     this.cloud = null;
     this.channel = null;
     this.source = null;
     this.enclosure = null;
     this.category = null;
     this.item = null;
     if (this.reader != null)
     {
         this.reader.Close();
         this.reader = null;
     }
     this.elementText = null;
     this.xmlNodeStack = null;
 }
예제 #5
0
        /// <summary>
        ///     Reads the next RssElement from the stream.
        /// </summary>
        /// <returns> An RSS Element </returns>
        /// <exception cref="InvalidOperationException">RssReader has been closed, and can not be read.</exception>
        /// <exception cref="System.IO.FileNotFoundException">RSS file not found.</exception>
        /// <exception cref="System.Xml.XmlException">Invalid XML syntax in RSS file.</exception>
        /// <exception cref="System.IO.EndOfStreamException">Unable to read an RssElement. Reached the end of the stream.</exception>
        public RssElement Read()
        {
            bool readData = false;
            RssElement rssElement = null;
            int lineNumber = -1;
            int linePosition = -1;

            if (this.reader == null)
                throw new InvalidOperationException("RssReader has been closed, and can not be read.");

            do
            {
                bool pushElement = true;
                try
                {
                    readData = this.reader.Read();
                }
                catch (EndOfStreamException e)
                {
                    throw new EndOfStreamException("Unable to read an RssElement. Reached the end of the stream.", e);
                }
                catch (XmlException e)
                {
                    if (lineNumber != -1 || linePosition != -1)
                        if (this.reader.LineNumber == lineNumber && this.reader.LinePosition == linePosition)
                            throw this.exceptions.LastException;

                    lineNumber = this.reader.LineNumber;
                    linePosition = this.reader.LinePosition;

                    this.exceptions.Add(e); // just add to list of exceptions and continue :)
                }
                if (readData)
                {
                    string readerName = this.reader.Name.ToLower();
                    switch (this.reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            {
                                //if (reader.IsEmptyElement)
                                //	break;
                                // doesnt take empty elements into account :/
                                this.elementText = new StringBuilder();
                                switch (readerName)
                                {
                                    case "item":
                                        // is this the end of the channel element? (absence of </channel> before <item>)
                                        if (!this.wroteChannel)
                                        {
                                            this.wroteChannel = true;
                                            rssElement = this.channel; // return RssChannel
                                            readData = false;
                                        }
                                        this.item = new RssItem.RssItem(); // create new RssItem
                                        this.channel.Items.Add(this.item);
                                        break;
                                    case "source":
                                        this.source = new RssSource();
                                        this.item.Source = this.source;
                                        for (int i = 0; i < this.reader.AttributeCount; i++)
                                        {
                                            this.reader.MoveToAttribute(i);
                                            switch (this.reader.Name.ToLower())
                                            {
                                                case "url":
                                                    try
                                                    {
                                                        this.source.Url = new Uri(this.reader.Value);
                                                    }
                                                    catch (Exception e)
                                                    {
                                                        this.exceptions.Add(e);
                                                    }
                                                    break;
                                            }
                                        }
                                        break;
                                    case "enclosure":
                                        this.enclosure = new RssEnclosure();
                                        this.item.Enclosure = this.enclosure;
                                        for (int i = 0; i < this.reader.AttributeCount; i++)
                                        {
                                            this.reader.MoveToAttribute(i);
                                            switch (this.reader.Name.ToLower())
                                            {
                                                case "url":
                                                    try
                                                    {
                                                        this.enclosure.Url = new Uri(this.reader.Value);
                                                    }
                                                    catch (Exception e)
                                                    {
                                                        this.exceptions.Add(e);
                                                    }
                                                    break;
                                                case "length":
                                                    try
                                                    {
                                                        this.enclosure.Length = int.Parse(this.reader.Value);
                                                    }
                                                    catch (Exception e)
                                                    {
                                                        this.exceptions.Add(e);
                                                    }
                                                    break;
                                                case "type":
                                                    this.enclosure.Type = this.reader.Value;
                                                    break;
                                            }
                                        }
                                        break;
                                    case "guid":
                                        this.guid = new RssGuid();
                                        this.item.Guid = this.guid;
                                        for (int i = 0; i < this.reader.AttributeCount; i++)
                                        {
                                            this.reader.MoveToAttribute(i);
                                            switch (this.reader.Name.ToLower())
                                            {
                                                case "ispermalink":
                                                    try
                                                    {
                                                        this.guid.PermaLink = bool.Parse(this.reader.Value);
                                                    }
                                                    catch (Exception e)
                                                    {
                                                        this.exceptions.Add(e);
                                                    }
                                                    break;
                                            }
                                        }
                                        break;
                                    case "category":
                                        this.category = new RssCategory();
                                        if ((string) this.xmlNodeStack.Peek() == "channel")
                                            this.channel.Categories.Add(this.category);
                                        else
                                            this.item.Categories.Add(this.category);
                                        for (int i = 0; i < this.reader.AttributeCount; i++)
                                        {
                                            this.reader.MoveToAttribute(i);
                                            switch (this.reader.Name.ToLower())
                                            {
                                                case "url":
                                                    goto case "domain";
                                                case "domain":
                                                    this.category.Domain = this.reader.Value;
                                                    break;
                                            }
                                        }
                                        break;
                                    case "channel":
                                        this.channel = new RssChannel.RssChannel();
                                        this.textInput = null;
                                        this.image = null;
                                        this.cloud = null;
                                        this.source = null;
                                        this.enclosure = null;
                                        this.category = null;
                                        this.item = null;
                                        break;
                                    case "image":
                                        this.image = new RssImage();
                                        this.channel.Image = this.image;
                                        break;
                                    case "textinput":
                                        this.textInput = new RssTextInput();
                                        this.channel.TextInput = this.textInput;
                                        break;
                                    case "cloud":
                                        pushElement = false;
                                        this.cloud = new RssCloud();
                                        this.channel.Cloud = this.cloud;
                                        for (int i = 0; i < this.reader.AttributeCount; i++)
                                        {
                                            this.reader.MoveToAttribute(i);
                                            switch (this.reader.Name.ToLower())
                                            {
                                                case "domain":
                                                    this.cloud.Domain = this.reader.Value;
                                                    break;
                                                case "port":
                                                    try
                                                    {
                                                        this.cloud.Port = ushort.Parse(this.reader.Value);
                                                    }
                                                    catch (Exception e)
                                                    {
                                                        this.exceptions.Add(e);
                                                    }
                                                    break;
                                                case "path":
                                                    this.cloud.Path = this.reader.Value;
                                                    break;
                                                case "registerprocedure":
                                                    this.cloud.RegisterProcedure = this.reader.Value;
                                                    break;
                                                case "protocol":
                                                    switch (this.reader.Value.ToLower())
                                                    {
                                                        case "xml-rpc":
                                                            this.cloud.Protocol = RssCloudProtocol.XmlRpc;
                                                            break;
                                                        case "soap":
                                                            this.cloud.Protocol = RssCloudProtocol.Soap;
                                                            break;
                                                        case "http-post":
                                                            this.cloud.Protocol = RssCloudProtocol.HttpPost;
                                                            break;
                                                        default:
                                                            this.cloud.Protocol = RssCloudProtocol.Empty;
                                                            break;
                                                    }
                                                    break;
                                            }
                                        }
                                        break;
                                    case "rss":
                                        for (int i = 0; i < this.reader.AttributeCount; i++)
                                        {
                                            this.reader.MoveToAttribute(i);
                                            if (this.reader.Name.ToLower() == "version")
                                                switch (this.reader.Value)
                                                {
                                                    case "0.91":
                                                        this.rssVersion = RssVersion.RSS091;
                                                        break;
                                                    case "0.92":
                                                        this.rssVersion = RssVersion.RSS092;
                                                        break;
                                                    case "2.0":
                                                        this.rssVersion = RssVersion.RSS20;
                                                        break;
                                                    default:
                                                        this.rssVersion = RssVersion.NotSupported;
                                                        break;
                                                }
                                        }
                                        break;
                                    case "rdf":
                                        for (int i = 0; i < this.reader.AttributeCount; i++)
                                        {
                                            this.reader.MoveToAttribute(i);
                                            if (this.reader.Name.ToLower() == "version")
                                                switch (this.reader.Value)
                                                {
                                                    case "0.90":
                                                        this.rssVersion = RssVersion.RSS090;
                                                        break;
                                                    case "1.0":
                                                        this.rssVersion = RssVersion.RSS10;
                                                        break;
                                                    default:
                                                        this.rssVersion = RssVersion.NotSupported;
                                                        break;
                                                }
                                        }
                                        break;
                                }
                                if (pushElement)
                                    this.xmlNodeStack.Push(readerName);
                                break;
                            }
                        case XmlNodeType.EndElement:
                            {
                                if (this.xmlNodeStack.Count == 1)
                                    break;
                                string childElementName = (string) this.xmlNodeStack.Pop();
                                string parentElementName = (string) this.xmlNodeStack.Peek();
                                switch (childElementName) // current element
                                {
                                        // item classes
                                    case "item":
                                        rssElement = this.item;
                                        readData = false;
                                        break;
                                    case "source":
                                        this.source.Name = this.elementText.ToString();
                                        rssElement = this.source;
                                        readData = false;
                                        break;
                                    case "enclosure":
                                        rssElement = this.enclosure;
                                        readData = false;
                                        break;
                                    case "guid":
                                        this.guid.Name = this.elementText.ToString();
                                        rssElement = this.guid;
                                        readData = false;
                                        break;
                                    case "category": // parent is either item or channel
                                        this.category.Name = this.elementText.ToString();
                                        rssElement = this.category;
                                        readData = false;
                                        break;
                                        // channel classes
                                    case "channel":
                                        if (this.wroteChannel)
                                            this.wroteChannel = false;
                                        else
                                        {
                                            this.wroteChannel = true;
                                            rssElement = this.channel;
                                            readData = false;
                                        }
                                        break;
                                    case "textinput":
                                        rssElement = this.textInput;
                                        readData = false;
                                        break;
                                    case "image":
                                        rssElement = this.image;
                                        readData = false;
                                        break;
                                    case "cloud":
                                        rssElement = this.cloud;
                                        readData = false;
                                        break;
                                }
                                switch (parentElementName) // parent element
                                {
                                    case "item":
                                        switch (childElementName)
                                        {
                                            case "title":
                                                this.item.Title = this.elementText.ToString();
                                                break;
                                            case "link":
                                                this.item.Link = new Uri(this.elementText.ToString());
                                                break;
                                            case "description":
                                                this.item.Description = this.elementText.ToString();
                                                break;
                                            case "author":
                                                this.item.Author = this.elementText.ToString();
                                                break;
                                            case "comments":
                                                this.item.Comments = this.elementText.ToString();
                                                break;
                                            case "pubdate":
                                                try
                                                {
                                                    this.item.PubDate = DateTime.Parse(this.elementText.ToString());
                                                }
                                                catch (Exception e)
                                                {
                                                    try
                                                    {
                                                        string tmp = this.elementText.ToString();
                                                        tmp = tmp.Substring(0, tmp.Length - 5);
                                                        tmp += "GMT";
                                                        this.item.PubDate = DateTime.Parse(tmp);
                                                    }
                                                    catch
                                                    {
                                                        this.exceptions.Add(e);
                                                    }
                                                }
                                                break;
                                        }
                                        break;
                                    case "channel":
                                        switch (childElementName)
                                        {
                                            case "title":
                                                this.channel.Title = this.elementText.ToString();
                                                break;
                                            case "link":
                                                try
                                                {
                                                    this.channel.Link = new Uri(this.elementText.ToString());
                                                }
                                                catch (Exception e)
                                                {
                                                    this.exceptions.Add(e);
                                                }
                                                break;
                                            case "description":
                                                this.channel.Description = this.elementText.ToString();
                                                break;
                                            case "language":
                                                this.channel.Language = this.elementText.ToString();
                                                break;
                                            case "copyright":
                                                this.channel.Copyright = this.elementText.ToString();
                                                break;
                                            case "managingeditor":
                                                this.channel.ManagingEditor = this.elementText.ToString();
                                                break;
                                            case "webmaster":
                                                this.channel.WebMaster = this.elementText.ToString();
                                                break;
                                            case "rating":
                                                this.channel.Rating = this.elementText.ToString();
                                                break;
                                            case "pubdate":
                                                try
                                                {
                                                    this.channel.PubDate = DateTime.Parse(this.elementText.ToString());
                                                }
                                                catch (Exception e)
                                                {
                                                    this.exceptions.Add(e);
                                                }
                                                break;
                                            case "lastbuilddate":
                                                try
                                                {
                                                    this.channel.LastBuildDate =
                                                        DateTime.Parse(this.elementText.ToString());
                                                }
                                                catch (Exception e)
                                                {
                                                    this.exceptions.Add(e);
                                                }
                                                break;
                                            case "generator":
                                                this.channel.Generator = this.elementText.ToString();
                                                break;
                                            case "docs":
                                                this.channel.Docs = this.elementText.ToString();
                                                break;
                                            case "ttl":
                                                try
                                                {
                                                    this.channel.TimeToLive = int.Parse(this.elementText.ToString());
                                                }
                                                catch (Exception e)
                                                {
                                                    this.exceptions.Add(e);
                                                }
                                                break;
                                        }
                                        break;
                                    case "image":
                                        switch (childElementName)
                                        {
                                            case "url":
                                                try
                                                {
                                                    this.image.Url = new Uri(this.elementText.ToString());
                                                }
                                                catch (Exception e)
                                                {
                                                    this.exceptions.Add(e);
                                                }
                                                break;
                                            case "title":
                                                this.image.Title = this.elementText.ToString();
                                                break;
                                            case "link":
                                                try
                                                {
                                                    this.image.Link = new Uri(this.elementText.ToString());
                                                }
                                                catch (Exception e)
                                                {
                                                    this.exceptions.Add(e);
                                                }
                                                break;
                                            case "description":
                                                this.image.Description = this.elementText.ToString();
                                                break;
                                            case "width":
                                                try
                                                {
                                                    this.image.Width = Byte.Parse(this.elementText.ToString());
                                                }
                                                catch (Exception e)
                                                {
                                                    this.exceptions.Add(e);
                                                }
                                                break;
                                            case "height":
                                                try
                                                {
                                                    this.image.Height = Byte.Parse(this.elementText.ToString());
                                                }
                                                catch (Exception e)
                                                {
                                                    this.exceptions.Add(e);
                                                }
                                                break;
                                        }
                                        break;
                                    case "textinput":
                                        switch (childElementName)
                                        {
                                            case "title":
                                                this.textInput.Title = this.elementText.ToString();
                                                break;
                                            case "description":
                                                this.textInput.Description = this.elementText.ToString();
                                                break;
                                            case "name":
                                                this.textInput.Name = this.elementText.ToString();
                                                break;
                                            case "link":
                                                try
                                                {
                                                    this.textInput.Link = new Uri(this.elementText.ToString());
                                                }
                                                catch (Exception e)
                                                {
                                                    this.exceptions.Add(e);
                                                }
                                                break;
                                        }
                                        break;
                                    case "skipdays":
                                        if (childElementName == "day")
                                            switch (this.elementText.ToString().ToLower())
                                            {
                                                case "monday":
                                                    this.channel.SkipDays[0] = true;
                                                    break;
                                                case "tuesday":
                                                    this.channel.SkipDays[1] = true;
                                                    break;
                                                case "wednesday":
                                                    this.channel.SkipDays[2] = true;
                                                    break;
                                                case "thursday":
                                                    this.channel.SkipDays[3] = true;
                                                    break;
                                                case "friday":
                                                    this.channel.SkipDays[4] = true;
                                                    break;
                                                case "saturday":
                                                    this.channel.SkipDays[5] = true;
                                                    break;
                                                case "sunday":
                                                    this.channel.SkipDays[6] = true;
                                                    break;
                                            }
                                        break;
                                    case "skiphours":
                                        if (childElementName == "hour")
                                            this.channel.SkipHours[Byte.Parse(this.elementText.ToString().ToLower())] =
                                                true;
                                        break;
                                }
                                break;
                            }
                        case XmlNodeType.Text:
                            this.elementText.Append(this.reader.Value);
                            break;
                        case XmlNodeType.CDATA:
                            this.elementText.Append(this.reader.Value);
                            break;
                    }
                }
            } while (readData);
            return rssElement;
        }
예제 #6
0
 /// <summary>
 ///     Searches for the specified RssChannel and returns the zero-based index of the first occurrence within the entire RssChannelCollection.
 /// </summary>
 /// <param name="rssChannel"> The RssChannel to locate in the RssChannelCollection. </param>
 /// <returns> The zero-based index of the first occurrence of RssChannel within the entire RssChannelCollection, if found; otherwise, -1. </returns>
 public int IndexOf(RssChannel.RssChannel rssChannel)
 {
     return this.List.IndexOf(rssChannel);
 }
예제 #7
0
 /// <summary>
 ///     Copies the entire RssChannelCollection to a compatible one-dimensional <see cref="Array" />, starting at the specified index of the target array.
 /// </summary>
 /// <param name="array"> The one-dimensional RssChannel Array that is the destination of the elements copied from RssChannelCollection. The Array must have zero-based indexing. </param>
 /// <param name="index"> The zero-based index in array at which copying begins. </param>
 /// <exception cref="ArgumentNullException">array is a null reference (Nothing in Visual Basic).</exception>
 /// <exception cref="ArgumentOutOfRangeException">index is less than zero.</exception>
 /// <exception cref="ArgumentException">array is multidimensional. -or- index is equal to or greater than the length of array.-or-The number of elements in the source RssChannelCollection is greater than the available space from index to the end of the destination array.</exception>
 public void CopyTo(RssChannel.RssChannel[] array, int index)
 {
     this.List.CopyTo(array, index);
 }
예제 #8
0
 /// <summary>
 ///     Determines whether the RssChannelCollection contains a specific element.
 /// </summary>
 /// <param name="rssChannel"> The RssChannel to locate in the RssChannelCollection. </param>
 /// <returns> true if the RssChannelCollection contains the specified value; otherwise, false. </returns>
 public bool Contains(RssChannel.RssChannel rssChannel)
 {
     return this.List.Contains(rssChannel);
 }
예제 #9
0
 /// <summary>
 ///     Adds a specified channel to this collection.
 /// </summary>
 /// <param name="channel"> The channel to add. </param>
 /// <returns> The zero-based index of the added channel. </returns>
 public int Add(RssChannel.RssChannel channel)
 {
     return this.List.Add(channel);
 }
예제 #10
0
 /// <summary>
 ///     Removes a specified channel from this collection.
 /// </summary>
 /// <param name="channel"> The channel to remove. </param>
 public void Remove(RssChannel.RssChannel channel)
 {
     this.List.Remove(channel);
 }
예제 #11
0
 /// <summary>
 ///     Inserts a channel into this collection at a specified index.
 /// </summary>
 /// <param name="index"> The zero-based index of the collection at which to insert the channel. </param>
 /// <param name="channel"> The channel to insert into this collection. </param>
 public void Insert(int index, RssChannel.RssChannel channel)
 {
     this.List.Insert(index, channel);
 }
예제 #12
0
        /// <summary>
        ///     Reads the next RssElement from the stream.
        /// </summary>
        /// <returns> An RSS Element </returns>
        /// <exception cref="InvalidOperationException">RssReader has been closed, and can not be read.</exception>
        /// <exception cref="System.IO.FileNotFoundException">RSS file not found.</exception>
        /// <exception cref="System.Xml.XmlException">Invalid XML syntax in RSS file.</exception>
        /// <exception cref="System.IO.EndOfStreamException">Unable to read an RssElement. Reached the end of the stream.</exception>
        public RssElement Read()
        {
            bool       readData     = false;
            RssElement rssElement   = null;
            int        lineNumber   = -1;
            int        linePosition = -1;

            if (this.reader == null)
            {
                throw new InvalidOperationException("RssReader has been closed, and can not be read.");
            }

            do
            {
                bool pushElement = true;
                try
                {
                    readData = this.reader.Read();
                }
                catch (EndOfStreamException e)
                {
                    throw new EndOfStreamException("Unable to read an RssElement. Reached the end of the stream.", e);
                }
                catch (XmlException e)
                {
                    if (lineNumber != -1 || linePosition != -1)
                    {
                        if (this.reader.LineNumber == lineNumber && this.reader.LinePosition == linePosition)
                        {
                            throw this.exceptions.LastException;
                        }
                    }

                    lineNumber   = this.reader.LineNumber;
                    linePosition = this.reader.LinePosition;

                    this.exceptions.Add(e); // just add to list of exceptions and continue :)
                }
                if (readData)
                {
                    string readerName = this.reader.Name.ToLower();
                    switch (this.reader.NodeType)
                    {
                    case XmlNodeType.Element:
                    {
                        //if (reader.IsEmptyElement)
                        //	break;
                        // doesnt take empty elements into account :/
                        this.elementText = new StringBuilder();
                        switch (readerName)
                        {
                        case "item":
                            // is this the end of the channel element? (absence of </channel> before <item>)
                            if (!this.wroteChannel)
                            {
                                this.wroteChannel = true;
                                rssElement        = this.channel;      // return RssChannel
                                readData          = false;
                            }
                            this.item = new RssItem.RssItem();             // create new RssItem
                            this.channel.Items.Add(this.item);
                            break;

                        case "source":
                            this.source      = new RssSource();
                            this.item.Source = this.source;
                            for (int i = 0; i < this.reader.AttributeCount; i++)
                            {
                                this.reader.MoveToAttribute(i);
                                switch (this.reader.Name.ToLower())
                                {
                                case "url":
                                    try
                                    {
                                        this.source.Url = new Uri(this.reader.Value);
                                    }
                                    catch (Exception e)
                                    {
                                        this.exceptions.Add(e);
                                    }
                                    break;
                                }
                            }
                            break;

                        case "enclosure":
                            this.enclosure      = new RssEnclosure();
                            this.item.Enclosure = this.enclosure;
                            for (int i = 0; i < this.reader.AttributeCount; i++)
                            {
                                this.reader.MoveToAttribute(i);
                                switch (this.reader.Name.ToLower())
                                {
                                case "url":
                                    try
                                    {
                                        this.enclosure.Url = new Uri(this.reader.Value);
                                    }
                                    catch (Exception e)
                                    {
                                        this.exceptions.Add(e);
                                    }
                                    break;

                                case "length":
                                    try
                                    {
                                        this.enclosure.Length = int.Parse(this.reader.Value);
                                    }
                                    catch (Exception e)
                                    {
                                        this.exceptions.Add(e);
                                    }
                                    break;

                                case "type":
                                    this.enclosure.Type = this.reader.Value;
                                    break;
                                }
                            }
                            break;

                        case "guid":
                            this.guid      = new RssGuid();
                            this.item.Guid = this.guid;
                            for (int i = 0; i < this.reader.AttributeCount; i++)
                            {
                                this.reader.MoveToAttribute(i);
                                switch (this.reader.Name.ToLower())
                                {
                                case "ispermalink":
                                    try
                                    {
                                        this.guid.PermaLink = bool.Parse(this.reader.Value);
                                    }
                                    catch (Exception e)
                                    {
                                        this.exceptions.Add(e);
                                    }
                                    break;
                                }
                            }
                            break;

                        case "category":
                            this.category = new RssCategory();
                            if ((string)this.xmlNodeStack.Peek() == "channel")
                            {
                                this.channel.Categories.Add(this.category);
                            }
                            else
                            {
                                this.item.Categories.Add(this.category);
                            }
                            for (int i = 0; i < this.reader.AttributeCount; i++)
                            {
                                this.reader.MoveToAttribute(i);
                                switch (this.reader.Name.ToLower())
                                {
                                case "url":
                                    goto case "domain";

                                case "domain":
                                    this.category.Domain = this.reader.Value;
                                    break;
                                }
                            }
                            break;

                        case "channel":
                            this.channel   = new RssChannel.RssChannel();
                            this.textInput = null;
                            this.image     = null;
                            this.cloud     = null;
                            this.source    = null;
                            this.enclosure = null;
                            this.category  = null;
                            this.item      = null;
                            break;

                        case "image":
                            this.image         = new RssImage();
                            this.channel.Image = this.image;
                            break;

                        case "textinput":
                            this.textInput         = new RssTextInput();
                            this.channel.TextInput = this.textInput;
                            break;

                        case "cloud":
                            pushElement        = false;
                            this.cloud         = new RssCloud();
                            this.channel.Cloud = this.cloud;
                            for (int i = 0; i < this.reader.AttributeCount; i++)
                            {
                                this.reader.MoveToAttribute(i);
                                switch (this.reader.Name.ToLower())
                                {
                                case "domain":
                                    this.cloud.Domain = this.reader.Value;
                                    break;

                                case "port":
                                    try
                                    {
                                        this.cloud.Port = ushort.Parse(this.reader.Value);
                                    }
                                    catch (Exception e)
                                    {
                                        this.exceptions.Add(e);
                                    }
                                    break;

                                case "path":
                                    this.cloud.Path = this.reader.Value;
                                    break;

                                case "registerprocedure":
                                    this.cloud.RegisterProcedure = this.reader.Value;
                                    break;

                                case "protocol":
                                    switch (this.reader.Value.ToLower())
                                    {
                                    case "xml-rpc":
                                        this.cloud.Protocol = RssCloudProtocol.XmlRpc;
                                        break;

                                    case "soap":
                                        this.cloud.Protocol = RssCloudProtocol.Soap;
                                        break;

                                    case "http-post":
                                        this.cloud.Protocol = RssCloudProtocol.HttpPost;
                                        break;

                                    default:
                                        this.cloud.Protocol = RssCloudProtocol.Empty;
                                        break;
                                    }
                                    break;
                                }
                            }
                            break;

                        case "rss":
                            for (int i = 0; i < this.reader.AttributeCount; i++)
                            {
                                this.reader.MoveToAttribute(i);
                                if (this.reader.Name.ToLower() == "version")
                                {
                                    switch (this.reader.Value)
                                    {
                                    case "0.91":
                                        this.rssVersion = RssVersion.RSS091;
                                        break;

                                    case "0.92":
                                        this.rssVersion = RssVersion.RSS092;
                                        break;

                                    case "2.0":
                                        this.rssVersion = RssVersion.RSS20;
                                        break;

                                    default:
                                        this.rssVersion = RssVersion.NotSupported;
                                        break;
                                    }
                                }
                            }
                            break;

                        case "rdf":
                            for (int i = 0; i < this.reader.AttributeCount; i++)
                            {
                                this.reader.MoveToAttribute(i);
                                if (this.reader.Name.ToLower() == "version")
                                {
                                    switch (this.reader.Value)
                                    {
                                    case "0.90":
                                        this.rssVersion = RssVersion.RSS090;
                                        break;

                                    case "1.0":
                                        this.rssVersion = RssVersion.RSS10;
                                        break;

                                    default:
                                        this.rssVersion = RssVersion.NotSupported;
                                        break;
                                    }
                                }
                            }
                            break;
                        }
                        if (pushElement)
                        {
                            this.xmlNodeStack.Push(readerName);
                        }
                        break;
                    }

                    case XmlNodeType.EndElement:
                    {
                        if (this.xmlNodeStack.Count == 1)
                        {
                            break;
                        }
                        string childElementName  = (string)this.xmlNodeStack.Pop();
                        string parentElementName = (string)this.xmlNodeStack.Peek();
                        switch (childElementName)         // current element
                        {
                        // item classes
                        case "item":
                            rssElement = this.item;
                            readData   = false;
                            break;

                        case "source":
                            this.source.Name = this.elementText.ToString();
                            rssElement       = this.source;
                            readData         = false;
                            break;

                        case "enclosure":
                            rssElement = this.enclosure;
                            readData   = false;
                            break;

                        case "guid":
                            this.guid.Name = this.elementText.ToString();
                            rssElement     = this.guid;
                            readData       = false;
                            break;

                        case "category":             // parent is either item or channel
                            this.category.Name = this.elementText.ToString();
                            rssElement         = this.category;
                            readData           = false;
                            break;

                        // channel classes
                        case "channel":
                            if (this.wroteChannel)
                            {
                                this.wroteChannel = false;
                            }
                            else
                            {
                                this.wroteChannel = true;
                                rssElement        = this.channel;
                                readData          = false;
                            }
                            break;

                        case "textinput":
                            rssElement = this.textInput;
                            readData   = false;
                            break;

                        case "image":
                            rssElement = this.image;
                            readData   = false;
                            break;

                        case "cloud":
                            rssElement = this.cloud;
                            readData   = false;
                            break;
                        }
                        switch (parentElementName)         // parent element
                        {
                        case "item":
                            switch (childElementName)
                            {
                            case "title":
                                this.item.Title = this.elementText.ToString();
                                break;

                            case "link":
                                this.item.Link = new Uri(this.elementText.ToString());
                                break;

                            case "description":
                                this.item.Description = this.elementText.ToString();
                                break;

                            case "author":
                                this.item.Author = this.elementText.ToString();
                                break;

                            case "comments":
                                this.item.Comments = this.elementText.ToString();
                                break;

                            case "pubdate":
                                try
                                {
                                    this.item.PubDate = DateTime.Parse(this.elementText.ToString());
                                }
                                catch (Exception e)
                                {
                                    try
                                    {
                                        string tmp = this.elementText.ToString();
                                        tmp  = tmp.Substring(0, tmp.Length - 5);
                                        tmp += "GMT";
                                        this.item.PubDate = DateTime.Parse(tmp);
                                    }
                                    catch
                                    {
                                        this.exceptions.Add(e);
                                    }
                                }
                                break;
                            }
                            break;

                        case "channel":
                            switch (childElementName)
                            {
                            case "title":
                                this.channel.Title = this.elementText.ToString();
                                break;

                            case "link":
                                try
                                {
                                    this.channel.Link = new Uri(this.elementText.ToString());
                                }
                                catch (Exception e)
                                {
                                    this.exceptions.Add(e);
                                }
                                break;

                            case "description":
                                this.channel.Description = this.elementText.ToString();
                                break;

                            case "language":
                                this.channel.Language = this.elementText.ToString();
                                break;

                            case "copyright":
                                this.channel.Copyright = this.elementText.ToString();
                                break;

                            case "managingeditor":
                                this.channel.ManagingEditor = this.elementText.ToString();
                                break;

                            case "webmaster":
                                this.channel.WebMaster = this.elementText.ToString();
                                break;

                            case "rating":
                                this.channel.Rating = this.elementText.ToString();
                                break;

                            case "pubdate":
                                try
                                {
                                    this.channel.PubDate = DateTime.Parse(this.elementText.ToString());
                                }
                                catch (Exception e)
                                {
                                    this.exceptions.Add(e);
                                }
                                break;

                            case "lastbuilddate":
                                try
                                {
                                    this.channel.LastBuildDate =
                                        DateTime.Parse(this.elementText.ToString());
                                }
                                catch (Exception e)
                                {
                                    this.exceptions.Add(e);
                                }
                                break;

                            case "generator":
                                this.channel.Generator = this.elementText.ToString();
                                break;

                            case "docs":
                                this.channel.Docs = this.elementText.ToString();
                                break;

                            case "ttl":
                                try
                                {
                                    this.channel.TimeToLive = int.Parse(this.elementText.ToString());
                                }
                                catch (Exception e)
                                {
                                    this.exceptions.Add(e);
                                }
                                break;
                            }
                            break;

                        case "image":
                            switch (childElementName)
                            {
                            case "url":
                                try
                                {
                                    this.image.Url = new Uri(this.elementText.ToString());
                                }
                                catch (Exception e)
                                {
                                    this.exceptions.Add(e);
                                }
                                break;

                            case "title":
                                this.image.Title = this.elementText.ToString();
                                break;

                            case "link":
                                try
                                {
                                    this.image.Link = new Uri(this.elementText.ToString());
                                }
                                catch (Exception e)
                                {
                                    this.exceptions.Add(e);
                                }
                                break;

                            case "description":
                                this.image.Description = this.elementText.ToString();
                                break;

                            case "width":
                                try
                                {
                                    this.image.Width = Byte.Parse(this.elementText.ToString());
                                }
                                catch (Exception e)
                                {
                                    this.exceptions.Add(e);
                                }
                                break;

                            case "height":
                                try
                                {
                                    this.image.Height = Byte.Parse(this.elementText.ToString());
                                }
                                catch (Exception e)
                                {
                                    this.exceptions.Add(e);
                                }
                                break;
                            }
                            break;

                        case "textinput":
                            switch (childElementName)
                            {
                            case "title":
                                this.textInput.Title = this.elementText.ToString();
                                break;

                            case "description":
                                this.textInput.Description = this.elementText.ToString();
                                break;

                            case "name":
                                this.textInput.Name = this.elementText.ToString();
                                break;

                            case "link":
                                try
                                {
                                    this.textInput.Link = new Uri(this.elementText.ToString());
                                }
                                catch (Exception e)
                                {
                                    this.exceptions.Add(e);
                                }
                                break;
                            }
                            break;

                        case "skipdays":
                            if (childElementName == "day")
                            {
                                switch (this.elementText.ToString().ToLower())
                                {
                                case "monday":
                                    this.channel.SkipDays[0] = true;
                                    break;

                                case "tuesday":
                                    this.channel.SkipDays[1] = true;
                                    break;

                                case "wednesday":
                                    this.channel.SkipDays[2] = true;
                                    break;

                                case "thursday":
                                    this.channel.SkipDays[3] = true;
                                    break;

                                case "friday":
                                    this.channel.SkipDays[4] = true;
                                    break;

                                case "saturday":
                                    this.channel.SkipDays[5] = true;
                                    break;

                                case "sunday":
                                    this.channel.SkipDays[6] = true;
                                    break;
                                }
                            }
                            break;

                        case "skiphours":
                            if (childElementName == "hour")
                            {
                                this.channel.SkipHours[Byte.Parse(this.elementText.ToString().ToLower())] =
                                    true;
                            }
                            break;
                        }
                        break;
                    }

                    case XmlNodeType.Text:
                        this.elementText.Append(this.reader.Value);
                        break;

                    case XmlNodeType.CDATA:
                        this.elementText.Append(this.reader.Value);
                        break;
                    }
                }
            } while (readData);
            return(rssElement);
        }