Merge branch 'master' of http://github.com/mono/mono
[mono.git] / web / web / rss.cs
1 //
2 // RSS.cs: Some utility classes to generate RSS feeds
3 //
4 // (C) 2002 Miguel de Icaza (miguel@gnu.org)
5 //
6 //
7 using System;
8 using System.Xml;
9 using System.IO;
10
11 namespace RSS {
12
13         public class Item {
14                 XmlDocument doc;
15                 XmlNode item;
16                 XmlText title, link, description, pubdate;
17                 
18                 public Item (XmlDocument doc, XmlNode item)
19                 {
20                         this.doc = doc;
21                         this.item = item;
22                 }
23
24                 XmlText MakeTextElement (string name)
25                 {
26                         XmlNode node = doc.CreateElement (name);
27                         XmlText text = doc.CreateTextNode ("");
28                         
29                         item.AppendChild (node);
30                         node.AppendChild (text);
31                         
32                         return text;
33                 }
34
35                 public string Title {
36                         get {
37                                 if (title == null)
38                                         return null;
39
40                                 return title.Value;
41                         }
42
43                         set {
44                                 if (title == null)
45                                         title = MakeTextElement ("title");
46                                 title.Value = value;
47                         }
48                 }
49
50                 public string Link {
51                         get {
52                                 if (link == null)
53                                         return null;
54
55                                 return link.Value;
56                         }
57
58                         set {
59                                 if (link == null)
60                                         link = MakeTextElement ("link");
61                                 link.Value = value;
62                         }
63                 }
64
65                 public string Description {
66                         get {
67                                 if (description == null)
68                                         return null;
69
70                                 return description.Value;
71                         }
72
73                         set {
74                                 if (description == null)
75                                         description = MakeTextElement ("description");
76                                 description.Value = value;
77                         }
78                 }
79                 
80                 public string PubDate {
81                         get {
82                                 if (pubdate == null)
83                                         return null;
84
85                                 return pubdate.Value;
86                         }
87
88                         set {
89                                 if (pubdate == null)
90                                         pubdate = MakeTextElement ("pubDate");
91                                 pubdate.Value = value;
92                         }
93                 }
94         }
95         
96         public class Channel {
97                 XmlDocument doc;
98                 XmlNode channel;
99                 XmlText title, link, description, language, pubDate, lastBuildDate;
100                 XmlText managingEditor, webMaster;
101                 
102                 XmlText MakeTextElement (string name)
103                 {
104                         XmlNode node = doc.CreateElement (name);
105                         XmlText text = doc.CreateTextNode ("");
106                         
107                         channel.AppendChild (node);
108                         node.AppendChild (text);
109                         
110                         return text;
111                 }
112                 
113                 public Channel (XmlDocument doc, XmlNode node)
114                 {
115                         this.channel = node;
116                         this.doc = doc;
117                         
118                         title = MakeTextElement ("title");
119                         link = MakeTextElement ("link");
120                         description = MakeTextElement ("description");
121                 }
122
123                 public Item NewItem ()
124                 {
125                         XmlNode node = doc.CreateElement ("item");
126                         Item item;
127
128                         channel.AppendChild (node);
129                         item = new Item (doc, node);
130
131                         return item;
132                 }
133                 
134                 public string Title {
135                         get {
136                                 return title.Value;
137                         }
138                         
139                         set {
140                                 title.Value = value;
141                         }
142                 }
143                 
144                 public string Link {
145                         get {
146                                 return link.Value;
147                         }
148
149                         set {
150                                 link.Value = value;
151                         }
152                 }
153
154                 public string Description {
155                         get {
156                                 return description.Value;
157                         }
158
159                         set {
160                                 description.Value = value;
161                         }
162                 }
163
164 #region Optional Values
165                 public string ManagingEditor {
166                         get {
167                                 if (managingEditor == null)
168                                         return null;
169                         
170                                 return managingEditor.Value;
171                         }
172
173                         set {
174                                 if (managingEditor == null)
175                                         managingEditor = MakeTextElement ("managingEditor");
176
177                                 managingEditor.Value = value;
178                         }
179                 }
180
181                 public string WebMaster {
182                         get {
183                                 if (webMaster == null)
184                                         return null;
185                         
186                                 return webMaster.Value;
187                         }
188
189                         set {
190                                 if (webMaster == null)
191                                         webMaster = MakeTextElement ("webMaster");
192                                 webMaster.Value = value;
193                         }
194                 }
195
196                 public string PubDate {
197                         get {
198                                 if (pubDate == null)
199                                         return null;
200
201                                 return pubDate.Value;
202                         }
203
204                         set {
205                                 if (pubDate == null)
206                                         pubDate = MakeTextElement ("pubDate");
207                                 pubDate.Value = value;
208                         }
209                 }
210
211                 public string LastBuildDate {
212                         get {
213                                 if (lastBuildDate == null)
214                                         return null;
215
216                                 return lastBuildDate.Value;
217                         }
218
219                         set {
220                                 if (lastBuildDate == null)
221                                         lastBuildDate = MakeTextElement ("lastBuildDate");
222                                 lastBuildDate.Value = value;
223                         }
224                 }
225
226                 public string Language {
227                         get {
228                                 if (language == null)
229                                         return null;
230
231                                 return language.Value;
232                         }
233
234                         set {
235                                 if (language == null)
236                                         language = MakeTextElement ("language");
237                                 language.Value = value;
238                         }
239                 }
240 #endregion
241         }
242  
243         class RSS {
244                 XmlDocument doc;
245                 XmlNode rss;
246                 
247                 const string rss_base =
248                 "<?xml version=\"1.0\"?> <rss version=\"0.92\"></rss>";
249                 
250                 public RSS ()
251                 {
252                         doc = new XmlDocument ();
253                         
254                         doc.LoadXml (rss_base);
255                         rss = doc.DocumentElement;
256                 }
257                 
258                 public Channel NewChannel (string title, string url)
259                 {
260                         XmlNode node = doc.CreateElement ("channel");
261                         Channel c;
262                         
263                         rss.AppendChild (node);
264                         c = new Channel (doc, node);
265                         
266                         return c;
267                 }
268                 
269                 public XmlDocument XmlDocument {
270                         get {
271                                 return doc;
272                         }
273                 }
274         }
275 }
276
277