2010-04-08 Zoltan Varga <vargaz@gmail.com>
[mono/afaerber.git] / web / web / render-team-page.cs
blob39ad660855e551cf0cc9bf90dc630609214752cb
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 //
9 using System;
10 using System.Collections;
11 using System.IO;
12 using System.Text;
13 using System.Xml;
15 class Write {
17 static Contributor [] list;
18 static public XmlNamespaceManager nsmgr;
20 static void Main (string [] args)
22 if (args.Length != 2) {
23 Console.WriteLine ("write.exe <input.xml> <output.html>");
24 Environment.Exit (0);
27 string input = args [0];
28 string output = args [1];
29 XmlDocument document = new XmlDocument ();
30 document.Load (input);
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];
37 Page p = new Page ();
39 int count = 0;
40 foreach (XmlNode n in contributors) {
41 list [count] = new Contributor (n, p.Document);
42 count ++;
45 Array.Sort (list, new ContributorComparer ());
47 int length = list.Length % 2 == 0 ? list.Length : list.Length + 1;
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);
56 i += 2;
59 p.Write (output);
63 public class ContributorComparer : IComparer
65 public int Compare (object x, object y)
67 return String.Compare (x.ToString (), y.ToString ());
71 class Contributor {
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;
81 public XmlDocument document;
83 public Contributor (XmlNode node, XmlDocument document)
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);
94 this.document = document;
97 public override string ToString ()
99 return name.ToString ();
102 public static string GetImage (XmlNode node)
104 string result = GetField (node, "t:image");
106 if (result == String.Empty)
107 return "none.png";
109 else
110 return result;
113 public static string GetField (XmlNode node, string selector)
115 XmlNode result = node.SelectSingleNode (selector, Write.nsmgr);
117 if (result == null)
118 return String.Empty;
120 return result.InnerText;
123 public static Name GetName (XmlNode node)
125 string first_name = GetField (node, "t:name/t:first-name");
126 string last_name = GetField (node, "t:name/t:last-name");
128 return new Name (first_name, last_name);
131 public static string [] GetTasks (XmlNode node)
133 XmlNodeList nodes = node.SelectNodes ("t:tasks/t:task", Write.nsmgr);
135 string [] result = new string [nodes.Count];
137 int i = 0;
138 foreach (XmlNode n in nodes) {
139 result [i] = n.InnerText;
141 i++;
144 return result;
147 public XmlElement RenderHtml ()
149 XmlElement root = document.CreateElement ("td");
150 XmlElement table = document.CreateElement ("table");
151 table.SetAttribute ("cellPadding", "0");
152 table.SetAttribute ("border", "0");
153 XmlElement tr = document.CreateElement ("tr");
154 XmlElement td = document.CreateElement ("td");
155 td.SetAttribute ("bgcolor", "#c3cda7");
156 td.SetAttribute ("valign", "top");
157 td.SetAttribute ("width", "1%");
158 tr.AppendChild (td);
159 table.AppendChild (tr);
160 root.AppendChild (table);
162 XmlElement img = document.CreateElement ("img");
163 img.SetAttribute ("align", "top");
164 img.SetAttribute ("border", "0");
165 img.SetAttribute ("height", "48");
166 img.SetAttribute ("width", "48");
167 img.SetAttribute ("src", "team/" + image);
168 td.AppendChild (img);
170 td = document.CreateElement ("TD");
171 td.SetAttribute ("bgcolor", "#c3cda7");
172 td.SetAttribute ("valign", "bottom");
173 td.SetAttribute ("width", "100%");
174 tr.AppendChild (td);
176 td.AppendChild (name.ToXml (document));
177 td.AppendChild (document.CreateElement ("br"));
178 td.AppendChild (RenderEmail ());
180 tr = document.CreateElement ("tr");
181 table.AppendChild (tr);
182 td = document.CreateElement ("td");
183 td.SetAttribute ("bgcolor", "#f5f8e4");
184 td.SetAttribute ("valign", "top");
185 tr.AppendChild (td);
186 td.AppendChild (RenderLabel ("Location: "));
188 td = document.CreateElement ("td");
189 td.SetAttribute ("bgcolor", "#f5f8e4");
190 td.SetAttribute ("valign", "top");
191 tr.AppendChild (td);
192 td.AppendChild (document.CreateTextNode (location));
194 tr = document.CreateElement ("tr");
195 table.AppendChild (tr);
196 td = document.CreateElement ("td");
197 td.SetAttribute ("bgcolor", "#f5f8e4");
198 td.SetAttribute ("valign", "top");
199 tr.AppendChild (td);
200 td.AppendChild (RenderLabel ("Description: "));
202 td = document.CreateElement ("td");
203 td.SetAttribute ("bgcolor", "#f5f8e4");
204 td.SetAttribute ("valign", "top");
205 tr.AppendChild (td);
206 td.AppendChild (document.CreateTextNode (description));
208 tr = document.CreateElement ("tr");
209 table.AppendChild (tr);
210 td = document.CreateElement ("td");
211 td.SetAttribute ("bgcolor", "#f5f8e4");
212 td.SetAttribute ("valign", "top");
213 tr.AppendChild (td);
214 td.AppendChild (RenderLabel ("Tasks: "));
216 td = document.CreateElement ("td");
217 td.SetAttribute ("bgcolor", "#f5f8e4");
218 td.SetAttribute ("valign", "top");
219 tr.AppendChild (td);
220 td.AppendChild (RenderTasks ());
222 return root;
225 public XmlNode RenderTasks ()
228 XmlElement element = document.CreateElement ("ol");
229 element.SetAttribute ("type", "I");
231 foreach (string task in tasks) {
232 XmlElement li = document.CreateElement ("li");
233 li.AppendChild (document.CreateTextNode (task));
234 element.AppendChild (li);
237 return element;
240 public XmlNode RenderEmail ()
242 XmlElement a = document.CreateElement ("a");
243 a.SetAttribute ("href", "mailto:" + email);
244 XmlElement font = document.CreateElement ("font");
245 font.SetAttribute ("size", "3");
246 XmlText t = document.CreateTextNode (email);
247 a.AppendChild (font);
248 font.AppendChild (t);
250 return a;
253 public XmlNode RenderLabel (string label)
255 string text = String.Format ("{0}: ", label);
256 XmlElement element = document.CreateElement ("b");
257 XmlText t = document.CreateTextNode (label );
258 element.AppendChild (t);
260 return element;
264 class Page {
266 XmlDocument document;
267 XmlElement tbody;
269 public Page ()
271 document = new XmlDocument ();
273 XmlElement table = document.CreateElement ("table");
274 document.AppendChild (table);
276 tbody = document.CreateElement ("tbody");
277 table.AppendChild (tbody);
280 public XmlDocument Document {
281 get { return document; }
284 public void AddRow (XmlNode left, XmlNode right)
286 if (left == null && right == null)
287 return;
289 XmlElement tr = document.CreateElement ("tr");
290 tbody.AppendChild (tr);
291 tr.AppendChild (left);
293 if (right == null)
294 tr.AppendChild (document.CreateElement ("td"));
295 else {
296 tr.SetAttribute ("valign", "top");
297 tr.AppendChild (right);
301 public void Write (TextWriter text_writer)
303 XmlTextWriter writer = new XmlTextWriter (text_writer);
304 writer.Formatting = Formatting.Indented;
306 document.WriteContentTo (writer);
308 writer.Flush ();
311 public void Write (string filename)
313 XmlTextWriter writer = new XmlTextWriter (filename, Encoding.Default);
314 writer.Formatting = Formatting.Indented;
316 document.WriteContentTo (writer);
317 writer.Flush ();
322 class Name {
324 string first_name;
325 string last_name;
327 public Name (string a, string b)
329 this.first_name = a;
330 this.last_name = b;
333 public override string ToString ()
335 if (first_name == null && last_name == null)
336 return String.Empty;
338 return first_name + " " + last_name;
341 public XmlNode ToXml (XmlDocument document)
343 XmlElement element = document.CreateElement ("font");
344 element.SetAttribute ("size", "3");
345 XmlElement b = document.CreateElement ("B");
346 XmlText t = document.CreateTextNode (ToString ());
347 b.AppendChild (t);
348 element.AppendChild (b);
350 return element;