5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.Generic;
30 using System.Collections.ObjectModel;
32 using System.Runtime.Serialization;
36 namespace System.ServiceModel.Syndication
38 public class SyndicationItem : ISyndicationElement
40 public static SyndicationItem Load (XmlReader reader)
42 return Load<SyndicationItem> (reader);
45 public static TSyndicationItem Load<TSyndicationItem> (XmlReader reader) where TSyndicationItem : SyndicationItem, new()
47 return SyndicationVersions.LoadItem<TSyndicationItem> (reader);
50 SyndicationExtensions extensions = new SyndicationExtensions ();
51 Collection<SyndicationCategory> categories;
52 Collection<SyndicationPerson> authors, contributors;
53 Collection<SyndicationLink> links;
55 TextSyndicationContent copyright, summary, title;
56 SyndicationContent content;
58 DateTimeOffset last_updated_time, published_date;
59 SyndicationFeed source_feed;
61 public SyndicationItem ()
65 public SyndicationItem (string title, string content, Uri feedAlternateLink)
66 : this (title, content, feedAlternateLink, null, default (DateTimeOffset))
70 public SyndicationItem (string title, string content, Uri feedAlternateLink, string id,
71 DateTimeOffset lastUpdatedTime)
72 : this (title, content != null ? SyndicationContent.CreatePlaintextContent (content) : null, feedAlternateLink, id, lastUpdatedTime)
76 public SyndicationItem (string title, SyndicationContent content, Uri feedAlternateLink, string id,
77 DateTimeOffset lastUpdatedTime)
79 Title = title != null ? new TextSyndicationContent (title) : null;
81 if (feedAlternateLink != null)
82 AddPermalink (feedAlternateLink);
84 LastUpdatedTime = lastUpdatedTime;
87 protected SyndicationItem (SyndicationItem source)
89 extensions = source.extensions.Clone ();
90 categories = Copy<SyndicationCategory> (source.categories);
91 authors = Copy<SyndicationPerson> (source.authors);
92 contributors = Copy<SyndicationPerson> (source.contributors);
93 links = Copy<SyndicationLink> (source.links);
94 base_uri = source.base_uri; // copy by reference !!
95 copyright = source.copyright == null ? null : source.copyright.Clone () as TextSyndicationContent;
96 summary = source.summary == null ? null : source.summary.Clone () as TextSyndicationContent;
97 title = source.title == null ? null : source.title.Clone () as TextSyndicationContent;
98 content = source.content == null ? null : source.content.Clone ();
100 last_updated_time = source.last_updated_time;
101 published_date = source.published_date;
102 source_feed = source.source_feed == null ? null : source.source_feed.Clone (false);
105 Collection<T> Copy<T> (Collection<T> source)
109 Collection<T> ret = new Collection<T> ();
110 foreach (object item in source)
111 ret.Add ((T) CreateCopy (item));
115 object CreateCopy (object source)
119 if (source is SyndicationPerson)
120 return ((SyndicationPerson) source).Clone ();
121 if (source is SyndicationLink)
122 return ((SyndicationLink) source).Clone ();
123 if (source is SyndicationCategory)
124 return ((SyndicationCategory) source).Clone ();
125 throw new InvalidOperationException ();
128 public Dictionary<XmlQualifiedName, string> AttributeExtensions {
129 get { return extensions.Attributes; }
132 public SyndicationElementExtensionCollection ElementExtensions {
133 get { return extensions.Elements; }
136 public Collection<SyndicationPerson> Authors {
139 authors = new Collection<SyndicationPerson> ();
144 public Collection<SyndicationCategory> Categories {
146 if (categories == null)
147 categories = new Collection<SyndicationCategory> ();
152 public Collection<SyndicationPerson> Contributors {
154 if (contributors == null)
155 contributors = new Collection<SyndicationPerson> ();
160 public Collection<SyndicationLink> Links {
163 links = new Collection<SyndicationLink> ();
169 get { return base_uri; }
170 set { base_uri = value; }
173 public TextSyndicationContent Copyright {
174 get { return copyright; }
175 set { copyright = value; }
178 public SyndicationContent Content {
179 get { return content; }
180 set { content = value; }
188 public DateTimeOffset LastUpdatedTime {
189 get { return last_updated_time; }
190 set { last_updated_time = value; }
193 public DateTimeOffset PublishDate {
194 get { return published_date; }
195 set { published_date = value; }
198 public SyndicationFeed SourceFeed {
199 get { return source_feed; }
200 set { source_feed = value; }
203 public TextSyndicationContent Summary {
204 get { return summary; }
205 set { summary = value; }
208 public TextSyndicationContent Title {
209 get { return title; }
210 set { title = value; }
213 public void AddPermalink (Uri link)
216 throw new ArgumentNullException ("link");
217 Links.Add (SyndicationLink.CreateAlternateLink (link));
220 public virtual SyndicationItem Clone ()
222 return new SyndicationItem (this);
225 protected internal virtual SyndicationCategory CreateCategory ()
227 return new SyndicationCategory ();
230 protected internal virtual SyndicationLink CreateLink ()
232 return new SyndicationLink ();
235 protected internal virtual SyndicationPerson CreatePerson ()
237 return new SyndicationPerson ();
240 public Atom10ItemFormatter GetAtom10Formatter ()
242 return new Atom10ItemFormatter (this);
245 public Rss20ItemFormatter GetRss20Formatter ()
247 return GetRss20Formatter (true);
250 public Rss20ItemFormatter GetRss20Formatter (bool serializeExtensionsAsAtom)
252 return new Rss20ItemFormatter (this, serializeExtensionsAsAtom);
255 public void SaveAsAtom10 (XmlWriter writer)
258 throw new ArgumentNullException ("writer");
259 GetAtom10Formatter ().WriteTo (writer);
262 public void SaveAsRss20 (XmlWriter writer)
265 throw new ArgumentNullException ("writer");
266 GetRss20Formatter ().WriteTo (writer);
269 protected internal virtual bool TryParseContent (XmlReader reader, string contentType, string version, out SyndicationContent content)
275 protected internal virtual bool TryParseAttribute (string name, string ns, string value, string version)
280 protected internal virtual bool TryParseElement (XmlReader reader, string version)
285 protected internal virtual void WriteAttributeExtensions (XmlWriter writer, string version)
287 extensions.WriteAttributeExtensions (writer, version);
290 protected internal virtual void WriteElementExtensions (XmlWriter writer, string version)
292 extensions.WriteElementExtensions (writer, version);