[tests] Separate MONO_PATH directories by PLATFORM_PATH_SEPARATOR
[mono.git] / mcs / class / System.ServiceModel.Web / System.ServiceModel.Syndication / SyndicationItem.cs
1 //
2 // SyndicationItem.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2007 Novell, Inc (http://www.novell.com)
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.Collections.ObjectModel;
31 using System.IO;
32 using System.Runtime.Serialization;
33 using System.Text;
34 using System.Xml;
35
36 namespace System.ServiceModel.Syndication
37 {
38         public class SyndicationItem : ISyndicationElement
39         {
40                 public static SyndicationItem Load (XmlReader reader)
41                 {
42                         return Load<SyndicationItem> (reader);
43                 }
44
45                 public static TSyndicationItem Load<TSyndicationItem> (XmlReader reader) where TSyndicationItem : SyndicationItem, new()
46                 {
47                         return SyndicationVersions.LoadItem<TSyndicationItem> (reader);
48                 }
49
50                 SyndicationExtensions extensions = new SyndicationExtensions ();
51                 Collection<SyndicationCategory> categories;
52                 Collection<SyndicationPerson> authors, contributors;
53                 Collection<SyndicationLink> links;
54                 Uri base_uri;
55                 TextSyndicationContent copyright, summary, title;
56                 SyndicationContent content;
57                 string id;
58                 DateTimeOffset last_updated_time, published_date;
59                 SyndicationFeed source_feed;
60
61                 public SyndicationItem ()
62                 {
63                 }
64
65                 public SyndicationItem (string title, string content, Uri feedAlternateLink)
66                         : this (title, content, feedAlternateLink, null, default (DateTimeOffset))
67                 {
68                 }
69
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)
73                 {
74                 }
75
76                 public SyndicationItem (string title, SyndicationContent content, Uri feedAlternateLink, string id,
77                                         DateTimeOffset lastUpdatedTime)
78                 {
79                         Title = title != null ? new TextSyndicationContent (title) : null;
80                         Content = content;
81                         if (feedAlternateLink != null)
82                                 AddPermalink (feedAlternateLink);
83                         Id = id;
84                         LastUpdatedTime = lastUpdatedTime;
85                 }
86
87                 protected SyndicationItem (SyndicationItem source)
88                 {
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 ();
99                         id = source.id;
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);
103                 }
104
105                 Collection<T> Copy<T> (Collection<T> source)
106                 {
107                         if (source == null)
108                                 return source;
109                         Collection<T> ret = new Collection<T> ();
110                         foreach (object item in source)
111                                 ret.Add ((T) CreateCopy (item));
112                         return ret;
113                 }
114
115                 object CreateCopy (object source)
116                 {
117                         if (source == null)
118                                 return null;
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 ();
126                 }
127
128                 public Dictionary<XmlQualifiedName, string> AttributeExtensions {
129                         get { return extensions.Attributes; }
130                 }
131
132                 public SyndicationElementExtensionCollection ElementExtensions {
133                         get { return extensions.Elements; }
134                 }
135
136                 public Collection<SyndicationPerson> Authors {
137                         get {
138                                 if (authors == null)
139                                         authors = new Collection<SyndicationPerson> ();
140                                 return authors;
141                         }
142                 }
143
144                 public Collection<SyndicationCategory> Categories {
145                         get {
146                                 if (categories == null)
147                                         categories = new Collection<SyndicationCategory> ();
148                                 return categories;
149                         }
150                 }
151
152                 public Collection<SyndicationPerson> Contributors {
153                         get {
154                                 if (contributors == null)
155                                         contributors = new Collection<SyndicationPerson> ();
156                                 return contributors;
157                         }
158                 }
159
160                 public Collection<SyndicationLink> Links {
161                         get {
162                                 if (links == null)
163                                         links = new Collection<SyndicationLink> ();
164                                 return links;
165                         }
166                 }
167
168                 public Uri BaseUri {
169                         get { return base_uri; }
170                         set { base_uri = value; }
171                 }
172
173                 public TextSyndicationContent Copyright {
174                         get { return copyright; }
175                         set { copyright = value; }
176                 }
177
178                 public SyndicationContent Content {
179                         get { return content; }
180                         set { content = value; }
181                 }
182
183                 public string Id {
184                         get { return id; }
185                         set { id = value; }
186                 }
187
188                 public DateTimeOffset LastUpdatedTime {
189                         get { return last_updated_time; }
190                         set { last_updated_time = value; }
191                 }
192
193                 public DateTimeOffset PublishDate {
194                         get { return published_date; }
195                         set { published_date = value; }
196                 }
197
198                 public SyndicationFeed SourceFeed {
199                         get { return source_feed; }
200                         set { source_feed = value; }
201                 }
202
203                 public TextSyndicationContent Summary {
204                         get { return summary; }
205                         set { summary = value; }
206                 }
207
208                 public TextSyndicationContent Title {
209                         get { return title; }
210                         set { title = value; }
211                 }
212
213                 public void AddPermalink (Uri link)
214                 {
215                         if (link == null)
216                                 throw new ArgumentNullException ("link");
217                         Links.Add (SyndicationLink.CreateAlternateLink (link));
218                 }
219
220                 public virtual SyndicationItem Clone ()
221                 {
222                         return new SyndicationItem (this);
223                 }
224
225                 protected internal virtual SyndicationCategory CreateCategory ()
226                 {
227                         return new SyndicationCategory ();
228                 }
229
230                 protected internal virtual SyndicationLink CreateLink ()
231                 {
232                         return new SyndicationLink ();
233                 }
234
235                 protected internal virtual SyndicationPerson CreatePerson ()
236                 {
237                         return new SyndicationPerson ();
238                 }
239
240                 public Atom10ItemFormatter GetAtom10Formatter ()
241                 {
242                         return new Atom10ItemFormatter (this);
243                 }
244
245                 public Rss20ItemFormatter GetRss20Formatter ()
246                 {
247                         return GetRss20Formatter (true);
248                 }
249
250                 public Rss20ItemFormatter GetRss20Formatter (bool serializeExtensionsAsAtom)
251                 {
252                         return new Rss20ItemFormatter (this, serializeExtensionsAsAtom);
253                 }
254
255                 public void SaveAsAtom10 (XmlWriter writer)
256                 {
257                         if (writer == null)
258                                 throw new ArgumentNullException ("writer");
259                         GetAtom10Formatter ().WriteTo (writer);
260                 }
261
262                 public void SaveAsRss20 (XmlWriter writer)
263                 {
264                         if (writer == null)
265                                 throw new ArgumentNullException ("writer");
266                         GetRss20Formatter ().WriteTo (writer);
267                 }
268
269                 protected internal virtual bool TryParseContent (XmlReader reader, string contentType, string version, out SyndicationContent content)
270                 {
271                         content = null;
272                         return false;
273                 }
274
275                 protected internal virtual bool TryParseAttribute (string name, string ns, string value, string version)
276                 {
277                         return false;
278                 }
279
280                 protected internal virtual bool TryParseElement (XmlReader reader, string version)
281                 {
282                         return false;
283                 }
284
285                 protected internal virtual void WriteAttributeExtensions (XmlWriter writer, string version)
286                 {
287                         extensions.WriteAttributeExtensions (writer, version);
288                 }
289
290                 protected internal virtual void WriteElementExtensions (XmlWriter writer, string version)
291                 {
292                         extensions.WriteElementExtensions (writer, version);
293                 }
294         }
295 }