XML Serialization isn't completely working yet ;(
[mono.git] / doc / web / render-team-page.cs
1 //
2 // RenderTeamPage.cs - Renders an HTML page with team member information from an XML file
3 //
4 // Author: Duncan Mak (duncan@ximian.com)
5 //
6 // (C) Copyright 2003, Ximian Inc.
7 //
8
9 using System;
10 using System.Collections;
11 using System.IO;
12 using System.Text;
13 using System.Xml;
14
15 class Write {
16
17         static Contributor [] list;
18
19         static void Main (string [] args)
20         {
21                 if (args.Length != 2) {
22                         Console.WriteLine ("write.exe <input.xml> <output.html>");
23                         Environment.Exit (0);
24                 }
25
26                 string input = args [0];
27                 string output = args [1];
28                 XmlDocument document = new XmlDocument ();
29                 document.Load (input);
30
31                 XmlNodeList contributors = document.SelectNodes ("/contributors/contributor");
32                 list = new Contributor [contributors.Count];
33
34                 Page p = new Page ();
35
36                 int count = 0;
37                 foreach (XmlNode n in contributors) {
38                         list [count] = new Contributor (n, p.Document);
39                         count ++;
40                 }
41
42                 Array.Sort (list, new ContributorComparer ());
43
44                 int length = list.Length % 2 == 0 ? list.Length : list.Length + 1;
45
46                 int i = 0;
47                 while (i < length) {
48                         try {
49                                 p.AddRow (list [i].RenderHtml (), list [i + 1].RenderHtml ());
50                         } catch (IndexOutOfRangeException) {
51                                 p.AddRow (list [i].RenderHtml (), null);
52                         }
53                         i += 2;
54                 }
55
56                 p.Write (output);
57         }
58 }
59
60 public class ContributorComparer : IComparer
61 {
62         public int Compare (object x, object y)
63         {
64                 return String.Compare (x as string , y as string);
65         }
66 }
67
68 class Contributor {
69
70         public Name name;
71         public string email;
72         public string image;
73         public string location;
74         public string organization;
75         public string description;
76         public string[] tasks;
77
78         public XmlDocument document;
79
80         public Contributor (XmlNode node, XmlDocument document)
81         {
82
83                 name         = GetName (node);
84                 image        = GetImage (node);
85                 email        = GetField (node, "e-mail");
86                 location     = GetField (node, "location");
87                 organization = GetField (node, "organization");
88                 description  = GetField (node, "description");
89                 tasks        = GetTasks (node);
90
91                 this.document = document;
92         }
93
94         public override string ToString ()
95         {
96                 return name.ToString ();
97         }
98
99         public static string GetImage (XmlNode node)
100         {
101                 string result = GetField (node, "image");
102
103                 if (result == String.Empty)
104                         return "none.png";
105
106                 else
107                         return result;
108         }
109
110         public static string GetField (XmlNode node, string selector)
111         {
112                 XmlNode result = node.SelectSingleNode (selector);
113
114                 if (result == null)
115                         return String.Empty;
116
117                 return result.InnerText;
118         }
119
120         public static Name GetName (XmlNode node)
121         {
122                 string first_name = GetField (node, "name/first-name");
123                 string last_name = GetField (node, "name/last-name");
124
125                 return new Name (first_name, last_name);
126         }
127
128         public static string [] GetTasks (XmlNode node)
129         {
130                 XmlNodeList nodes = node.SelectNodes ("tasks/task");
131
132                 string [] result = new string [nodes.Count];
133
134                 int i = 0;
135                 foreach (XmlNode n in nodes) {
136                         result [i] = n.InnerText;
137
138                         i++;
139                 }
140
141                 return result;
142         }
143
144         public XmlElement RenderHtml ()
145         {
146                 XmlElement root = document.CreateElement ("TD");
147                 XmlElement table = document.CreateElement ("TABLE");
148                 table.SetAttribute ("width", "100%");
149                 XmlElement tr = document.CreateElement ("TR");
150                 XmlElement td = document.CreateElement ("TD");
151                 td.SetAttribute ("bgcolor", "#c3cda7");
152                 td.SetAttribute ("valign", "top");
153                 tr.AppendChild (td);
154                 table.AppendChild (tr);
155                 root.AppendChild (table);
156
157                 XmlElement img = document.CreateElement ("IMG");
158                 img.SetAttribute ("align", "top");
159                 img.SetAttribute ("border", "0");
160                 img.SetAttribute ("height", "48");
161                 img.SetAttribute ("width", "48");
162                 img.SetAttribute ("src", "team/" + image);
163                 td.AppendChild (img);
164
165                 td = document.CreateElement ("TD");
166                 td.SetAttribute ("bgcolor", "#c3cda7");
167                 td.SetAttribute ("valign", "bottom");
168                 tr.AppendChild (td);
169
170                 td.AppendChild (name.ToXml (document));
171                 td.AppendChild (document.CreateElement ("BR"));
172                 td.AppendChild (RenderEmail ());
173
174                 tr = document.CreateElement ("TR");
175                 table.AppendChild (tr);
176                 td = document.CreateElement ("TD");
177                 td.SetAttribute ("bgcolor", "#f5f8e4");
178                 td.SetAttribute ("valign", "top");
179                 tr.AppendChild (td);
180                 td.AppendChild (RenderLabel ("Location: "));
181
182                 td = document.CreateElement ("TD");
183                 td.SetAttribute ("bgcolor", "#f5f8e4");
184                 td.SetAttribute ("valign", "top");
185                 tr.AppendChild (td);
186                 td.AppendChild (document.CreateTextNode (location));
187
188                 tr = document.CreateElement ("TR");
189                 table.AppendChild (tr);
190                 td = document.CreateElement ("TD");
191                 td.SetAttribute ("bgcolor", "#f5f8e4");
192                 td.SetAttribute ("valign", "top");
193                 tr.AppendChild (td);
194                 td.AppendChild (RenderLabel ("Description: "));
195
196                 td = document.CreateElement ("TD");
197                 td.SetAttribute ("bgcolor", "#f5f8e4");
198                 td.SetAttribute ("valign", "top");
199                 tr.AppendChild (td);
200                 td.AppendChild (document.CreateTextNode (description));
201
202                 tr = document.CreateElement ("TR");
203                 table.AppendChild (tr);
204                 td = document.CreateElement ("TD");
205                 td.SetAttribute ("bgcolor", "#f5f8e4");
206                 td.SetAttribute ("valign", "top");
207                 tr.AppendChild (td);
208                 td.AppendChild (RenderLabel ("Tasks: "));
209
210                 td = document.CreateElement ("TD");
211                 td.SetAttribute ("bgcolor", "#f5f8e4");
212                 td.SetAttribute ("valign", "top");
213                 tr.AppendChild (td);
214                 td.AppendChild (RenderTasks ());
215
216                 return root;
217         }
218
219         public XmlNode RenderTasks ()
220         {
221
222                 XmlElement element = document.CreateElement ("OL");
223                 element.SetAttribute ("type", "I");
224
225                 foreach (string task in tasks) {
226                         XmlElement li = document.CreateElement ("LI");
227                         li.AppendChild (document.CreateTextNode (task));
228                         element.AppendChild (li);
229                 }
230
231                 return element;
232         }
233
234         public XmlNode RenderEmail ()
235         {
236                 XmlElement a = document.CreateElement ("A");
237                 a.SetAttribute ("href", "mailto:" + email);
238                 XmlElement font = document.CreateElement ("FONT");
239                 font.SetAttribute ("size", "3");
240                 XmlText t = document.CreateTextNode (email);
241                 a.AppendChild (font);
242                 font.AppendChild (t);
243
244                 return a;
245         }
246
247         public XmlNode RenderLabel (string label)
248         {
249                 string text = String.Format ("{0}: ", label);
250                 XmlElement element = document.CreateElement ("B");
251                 XmlText t = document.CreateTextNode (label );
252                 element.AppendChild (t);
253
254                 return element;
255         }
256 }
257
258 class Page {
259
260         XmlDocument document;
261         XmlElement tbody;
262
263         public Page ()
264         {
265                 document = new XmlDocument ();
266
267                 XmlElement table = document.CreateElement ("TABLE");
268                 document.AppendChild (table);
269
270                 tbody = document.CreateElement ("TBODY");
271                 table.AppendChild (tbody);
272         }
273
274         public XmlDocument Document {
275                 get { return document; }
276         }
277
278         public void AddRow (XmlNode left, XmlNode right)
279         {
280                 if (left == null && right == null)
281                         return;
282
283                 XmlElement tr = document.CreateElement ("TR");
284                 tbody.AppendChild (tr);
285                 tr.AppendChild (left);
286
287                 if (right == null)
288                         tr.AppendChild (document.CreateElement ("TD"));
289                 else {
290                         tr.SetAttribute ("valign", "top");
291                         tr.AppendChild (right);
292                 }
293         }
294
295         public void Write (TextWriter text_writer)
296         {
297                 XmlTextWriter writer = new XmlTextWriter (text_writer);
298                 writer.Formatting = Formatting.Indented;
299
300                 document.WriteContentTo (writer);
301
302                 writer.Flush ();
303         }
304
305         public void Write (string filename)
306         {
307                 XmlTextWriter writer = new XmlTextWriter (filename, Encoding.Default);
308                 writer.Formatting = Formatting.Indented;
309
310                 document.WriteContentTo (writer);
311                 writer.Flush ();
312         }
313 }
314
315
316 class Name {
317
318         string first_name;
319         string last_name;
320
321         public Name (string a, string b)
322         {
323                 this.first_name = a;
324                 this.last_name = b;
325         }
326
327         public override string ToString ()
328         {
329                 if (first_name == null && last_name == null)
330                         return String.Empty;
331
332                 return first_name + " " + last_name;
333         }
334
335         public XmlNode ToXml (XmlDocument document)
336         {
337                 XmlElement element = document.CreateElement ("FONT");
338                 element.SetAttribute ("size", "3");
339                 XmlElement b = document.CreateElement ("B");
340                 XmlText t = document.CreateTextNode (ToString ());
341                 b.AppendChild (t);
342                 element.AppendChild (b);
343
344                 return element;
345         }
346 }