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