1 //---------------------------------------------------------------- 2 // Copyright (c) Microsoft Corporation. All rights reserved. 3 //---------------------------------------------------------------- 4 5 namespace System.ServiceModel.Syndication 6 { 7 using System; 8 using System.Collections.Generic; 9 using System.Collections.ObjectModel; 10 using System.Text; 11 using System.Xml; 12 using System.Runtime.Serialization; 13 using System.Xml.Serialization; 14 using System.Diagnostics.CodeAnalysis; 15 using System.Runtime.CompilerServices; 16 17 // NOTE: This class implements Clone so if you add any members, please update the copy ctor 18 [TypeForwardedFrom("System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")] 19 public class SyndicationItem : IExtensibleSyndicationObject 20 { 21 Collection<SyndicationPerson> authors; 22 Uri baseUri; 23 Collection<SyndicationCategory> categories; 24 SyndicationContent content; 25 Collection<SyndicationPerson> contributors; 26 TextSyndicationContent copyright; 27 ExtensibleSyndicationObject extensions = new ExtensibleSyndicationObject(); 28 string id; 29 DateTimeOffset lastUpdatedTime; 30 Collection<SyndicationLink> links; 31 DateTimeOffset publishDate; 32 SyndicationFeed sourceFeed; 33 TextSyndicationContent summary; 34 TextSyndicationContent title; 35 SyndicationItem()36 public SyndicationItem() 37 : this(null, null, null) 38 { 39 } 40 SyndicationItem(string title, string content, Uri itemAlternateLink)41 public SyndicationItem(string title, string content, Uri itemAlternateLink) 42 : this(title, content, itemAlternateLink, null, DateTimeOffset.MinValue) 43 { 44 } 45 SyndicationItem(string title, string content, Uri itemAlternateLink, string id, DateTimeOffset lastUpdatedTime)46 public SyndicationItem(string title, string content, Uri itemAlternateLink, string id, DateTimeOffset lastUpdatedTime) 47 : this(title, (content != null) ? new TextSyndicationContent(content) : null, itemAlternateLink, id, lastUpdatedTime) 48 { 49 } 50 SyndicationItem(string title, SyndicationContent content, Uri itemAlternateLink, string id, DateTimeOffset lastUpdatedTime)51 public SyndicationItem(string title, SyndicationContent content, Uri itemAlternateLink, string id, DateTimeOffset lastUpdatedTime) 52 { 53 if (title != null) 54 { 55 this.Title = new TextSyndicationContent(title); 56 } 57 this.content = content; 58 if (itemAlternateLink != null) 59 { 60 this.Links.Add(SyndicationLink.CreateAlternateLink(itemAlternateLink)); 61 } 62 this.id = id; 63 this.lastUpdatedTime = lastUpdatedTime; 64 } 65 SyndicationItem(SyndicationItem source)66 protected SyndicationItem(SyndicationItem source) 67 { 68 if (source == null) 69 { 70 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("source"); 71 } 72 this.extensions = source.extensions.Clone(); 73 this.authors = FeedUtils.ClonePersons(source.authors); 74 this.categories = FeedUtils.CloneCategories(source.categories); 75 this.content = (source.content != null) ? source.content.Clone() : null; 76 this.contributors = FeedUtils.ClonePersons(source.contributors); 77 this.copyright = FeedUtils.CloneTextContent(source.copyright); 78 this.id = source.id; 79 this.lastUpdatedTime = source.lastUpdatedTime; 80 this.links = FeedUtils.CloneLinks(source.links); 81 this.publishDate = source.publishDate; 82 if (source.SourceFeed != null) 83 { 84 this.sourceFeed = source.sourceFeed.Clone(false); 85 this.sourceFeed.Items = new Collection<SyndicationItem>(); 86 } 87 this.summary = FeedUtils.CloneTextContent(source.summary); 88 this.baseUri = source.baseUri; 89 this.title = FeedUtils.CloneTextContent(source.title); 90 } 91 92 public Dictionary<XmlQualifiedName, string> AttributeExtensions 93 { 94 get { return this.extensions.AttributeExtensions; } 95 } 96 97 public Collection<SyndicationPerson> Authors 98 { 99 get 100 { 101 if (this.authors == null) 102 { 103 this.authors = new NullNotAllowedCollection<SyndicationPerson>(); 104 } 105 return this.authors; 106 } 107 } 108 109 public Uri BaseUri 110 { 111 get { return this.baseUri; } 112 set { this.baseUri = value; } 113 } 114 115 public Collection<SyndicationCategory> Categories 116 { 117 get 118 { 119 if (this.categories == null) 120 { 121 this.categories = new NullNotAllowedCollection<SyndicationCategory>(); 122 } 123 return this.categories; 124 } 125 } 126 127 public SyndicationContent Content 128 { 129 get { return content; } 130 set { content = value; } 131 } 132 133 public Collection<SyndicationPerson> Contributors 134 { 135 get 136 { 137 if (this.contributors == null) 138 { 139 this.contributors = new NullNotAllowedCollection<SyndicationPerson>(); 140 } 141 return this.contributors; 142 } 143 } 144 145 public TextSyndicationContent Copyright 146 { 147 get { return this.copyright; } 148 set { this.copyright = value; } 149 } 150 151 public SyndicationElementExtensionCollection ElementExtensions 152 { 153 get { return this.extensions.ElementExtensions; } 154 } 155 156 public string Id 157 { 158 get { return id; } 159 set { id = value; } 160 } 161 162 public DateTimeOffset LastUpdatedTime 163 { 164 get { return lastUpdatedTime; } 165 set { lastUpdatedTime = value; } 166 } 167 168 public Collection<SyndicationLink> Links 169 { 170 get 171 { 172 if (this.links == null) 173 { 174 this.links = new NullNotAllowedCollection<SyndicationLink>(); 175 } 176 return this.links; 177 } 178 } 179 180 public DateTimeOffset PublishDate 181 { 182 get { return publishDate; } 183 set { publishDate = value; } 184 } 185 186 public SyndicationFeed SourceFeed 187 { 188 get { return this.sourceFeed; } 189 set { this.sourceFeed = value; } 190 } 191 192 public TextSyndicationContent Summary 193 { 194 get { return this.summary; } 195 set { this.summary = value; } 196 } 197 198 public TextSyndicationContent Title 199 { 200 get { return this.title; } 201 set { this.title = value; } 202 } 203 Load(XmlReader reader)204 public static SyndicationItem Load(XmlReader reader) 205 { 206 return Load<SyndicationItem>(reader); 207 } 208 209 public static TSyndicationItem Load<TSyndicationItem>(XmlReader reader) 210 where TSyndicationItem : SyndicationItem, new () 211 { 212 if (reader == null) 213 { 214 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader"); 215 } 216 Atom10ItemFormatter<TSyndicationItem> atomSerializer = new Atom10ItemFormatter<TSyndicationItem>(); 217 if (atomSerializer.CanRead(reader)) 218 { 219 atomSerializer.ReadFrom(reader); 220 return atomSerializer.Item as TSyndicationItem; 221 } 222 Rss20ItemFormatter<TSyndicationItem> rssSerializer = new Rss20ItemFormatter<TSyndicationItem>(); 223 if (rssSerializer.CanRead(reader)) 224 { 225 rssSerializer.ReadFrom(reader); 226 return rssSerializer.Item as TSyndicationItem; 227 } 228 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.GetString(SR.UnknownItemXml, reader.LocalName, reader.NamespaceURI))); 229 } 230 231 232 [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "0#permalink", Justification = "permalink is a term defined in the RSS format")] 233 [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Permalink", Justification = "permalink is a term defined in the RSS format")] AddPermalink(Uri permalink)234 public void AddPermalink(Uri permalink) 235 { 236 if (permalink == null) 237 { 238 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("permalink"); 239 } 240 this.Id = permalink.AbsoluteUri; 241 this.Links.Add(SyndicationLink.CreateAlternateLink(permalink)); 242 } 243 Clone()244 public virtual SyndicationItem Clone() 245 { 246 return new SyndicationItem(this); 247 } 248 GetAtom10Formatter()249 public Atom10ItemFormatter GetAtom10Formatter() 250 { 251 return new Atom10ItemFormatter(this); 252 } 253 GetRss20Formatter()254 public Rss20ItemFormatter GetRss20Formatter() 255 { 256 return GetRss20Formatter(true); 257 } 258 GetRss20Formatter(bool serializeExtensionsAsAtom)259 public Rss20ItemFormatter GetRss20Formatter(bool serializeExtensionsAsAtom) 260 { 261 return new Rss20ItemFormatter(this, serializeExtensionsAsAtom); 262 } 263 SaveAsAtom10(XmlWriter writer)264 public void SaveAsAtom10(XmlWriter writer) 265 { 266 this.GetAtom10Formatter().WriteTo(writer); 267 } 268 SaveAsRss20(XmlWriter writer)269 public void SaveAsRss20(XmlWriter writer) 270 { 271 this.GetRss20Formatter().WriteTo(writer); 272 } 273 CreateCategory()274 protected internal virtual SyndicationCategory CreateCategory() 275 { 276 return new SyndicationCategory(); 277 } 278 CreateLink()279 protected internal virtual SyndicationLink CreateLink() 280 { 281 return new SyndicationLink(); 282 } 283 CreatePerson()284 protected internal virtual SyndicationPerson CreatePerson() 285 { 286 return new SyndicationPerson(); 287 } 288 TryParseAttribute(string name, string ns, string value, string version)289 protected internal virtual bool TryParseAttribute(string name, string ns, string value, string version) 290 { 291 return false; 292 } 293 TryParseContent(XmlReader reader, string contentType, string version, out SyndicationContent content)294 protected internal virtual bool TryParseContent(XmlReader reader, string contentType, string version, out SyndicationContent content) 295 { 296 content = null; 297 return false; 298 } 299 TryParseElement(XmlReader reader, string version)300 protected internal virtual bool TryParseElement(XmlReader reader, string version) 301 { 302 return false; 303 } 304 WriteAttributeExtensions(XmlWriter writer, string version)305 protected internal virtual void WriteAttributeExtensions(XmlWriter writer, string version) 306 { 307 this.extensions.WriteAttributeExtensions(writer); 308 } 309 WriteElementExtensions(XmlWriter writer, string version)310 protected internal virtual void WriteElementExtensions(XmlWriter writer, string version) 311 { 312 this.extensions.WriteElementExtensions(writer); 313 } 314 LoadElementExtensions(XmlReader readerOverUnparsedExtensions, int maxExtensionSize)315 internal void LoadElementExtensions(XmlReader readerOverUnparsedExtensions, int maxExtensionSize) 316 { 317 this.extensions.LoadElementExtensions(readerOverUnparsedExtensions, maxExtensionSize); 318 } 319 LoadElementExtensions(XmlBuffer buffer)320 internal void LoadElementExtensions(XmlBuffer buffer) 321 { 322 this.extensions.LoadElementExtensions(buffer); 323 } 324 } 325 } 326 327 328