A face only a mother could love...
[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;
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         
81         public class Channel {
82                 XmlDocument doc;
83                 XmlNode channel;
84                 XmlText title, link, description, language, pubDate, lastBuildDate;
85                 XmlText managingEditor, webMaster;
86                 
87                 XmlText MakeTextElement (string name)
88                 {
89                         XmlNode node = doc.CreateElement (name);
90                         XmlText text = doc.CreateTextNode ("");
91                         
92                         channel.AppendChild (node);
93                         node.AppendChild (text);
94                         
95                         return text;
96                 }
97                 
98                 public Channel (XmlDocument doc, XmlNode node)
99                 {
100                         this.channel = node;
101                         this.doc = doc;
102                         
103                         title = MakeTextElement ("title");
104                         link = MakeTextElement ("link");
105                         description = MakeTextElement ("description");
106                 }
107
108                 public Item NewItem ()
109                 {
110                         XmlNode node = doc.CreateElement ("item");
111                         Item item;
112
113                         channel.AppendChild (node);
114                         item = new Item (doc, node);
115
116                         return item;
117                 }
118                 
119                 public string Title {
120                         get {
121                                 return title.Value;
122                         }
123                         
124                         set {
125                                 title.Value = value;
126                         }
127                 }
128                 
129                 public string Link {
130                         get {
131                                 return link.Value;
132                         }
133
134                         set {
135                                 link.Value = value;
136                         }
137                 }
138
139                 public string Description {
140                         get {
141                                 return description.Value;
142                         }
143
144                         set {
145                                 description.Value = value;
146                         }
147                 }
148
149 #region Optional Values
150                 public string ManagingEditor {
151                         get {
152                                 if (managingEditor == null)
153                                         return null;
154                         
155                                 return managingEditor.Value;
156                         }
157
158                         set {
159                                 if (managingEditor == null)
160                                         managingEditor = MakeTextElement ("managingEditor");
161
162                                 managingEditor.Value = value;
163                         }
164                 }
165
166                 public string WebMaster {
167                         get {
168                                 if (webMaster == null)
169                                         return null;
170                         
171                                 return webMaster.Value;
172                         }
173
174                         set {
175                                 if (webMaster == null)
176                                         webMaster = MakeTextElement ("webMaster");
177                                 webMaster.Value = value;
178                         }
179                 }
180
181                 public string PubDate {
182                         get {
183                                 if (pubDate == null)
184                                         return null;
185
186                                 return pubDate.Value;
187                         }
188
189                         set {
190                                 if (pubDate == null)
191                                         pubDate = MakeTextElement ("pubDate");
192                                 pubDate.Value = value;
193                         }
194                 }
195
196                 public string LastBuildDate {
197                         get {
198                                 if (lastBuildDate == null)
199                                         return null;
200
201                                 return lastBuildDate.Value;
202                         }
203
204                         set {
205                                 if (lastBuildDate == null)
206                                         lastBuildDate = MakeTextElement ("lastBuildDate");
207                                 lastBuildDate.Value = value;
208                         }
209                 }
210
211                 public string Language {
212                         get {
213                                 if (language == null)
214                                         return null;
215
216                                 return language.Value;
217                         }
218
219                         set {
220                                 if (language == null)
221                                         language = MakeTextElement ("language");
222                                 language.Value = value;
223                         }
224                 }
225 #endregion
226         }
227  
228         class RSS {
229                 XmlDocument doc;
230                 XmlNode rss;
231                 
232                 const string rss_base =
233                 "<?xml version=\"1.0\"?> <rss version=\"0.92\"></rss>";
234                 
235                 public RSS ()
236                 {
237                         doc = new XmlDocument ();
238                         
239                         doc.LoadXml (rss_base);
240                         rss = doc.DocumentElement;
241                 }
242                 
243                 public Channel NewChannel (string title, string url)
244                 {
245                         XmlNode node = doc.CreateElement ("channel");
246                         Channel c;
247                         
248                         rss.AppendChild (node);
249                         c = new Channel (doc, node);
250                         
251                         return c;
252                 }
253                 
254                 public XmlDocument XmlDocument {
255                         get {
256                                 return doc;
257                         }
258                 }
259         }
260 }
261
262