2 // RenderTeamPage.cs - Renders an HTML page with team member information from an XML file
4 // Author: Duncan Mak (duncan@ximian.com)
6 // (C) Copyright 2003, Ximian Inc.
10 using System.Collections;
17 static Contributor [] list;
19 static void Main (string [] args)
21 if (args.Length != 2) {
22 Console.WriteLine ("write.exe <input.xml> <output.html>");
26 string input = args [0];
27 string output = args [1];
28 XmlDocument document = new XmlDocument ();
29 document.Load (input);
31 XmlNodeList contributors = document.SelectNodes ("/contributors/contributor");
32 list = new Contributor [contributors.Count];
37 foreach (XmlNode n in contributors) {
38 list [count] = new Contributor (n, p.Document);
42 Array.Sort (list, new ContributorComparer ());
44 int length = list.Length % 2 == 0 ? list.Length : list.Length + 1;
49 p.AddRow (list [i].RenderHtml (), list [i + 1].RenderHtml ());
50 } catch (IndexOutOfRangeException) {
51 p.AddRow (list [i].RenderHtml (), null);
60 public class ContributorComparer : IComparer
62 public int Compare (object x, object y)
64 return String.Compare (x as string , y as string);
73 public string location;
74 public string organization;
75 public string description;
76 public string[] tasks;
78 public XmlDocument document;
80 public Contributor (XmlNode node, XmlDocument document)
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);
91 this.document = document;
94 public override string ToString ()
96 return name.ToString ();
99 public static string GetImage (XmlNode node)
101 string result = GetField (node, "image");
103 if (result == String.Empty)
110 public static string GetField (XmlNode node, string selector)
112 XmlNode result = node.SelectSingleNode (selector);
117 return result.InnerText;
120 public static Name GetName (XmlNode node)
122 string first_name = GetField (node, "name/first-name");
123 string last_name = GetField (node, "name/last-name");
125 return new Name (first_name, last_name);
128 public static string [] GetTasks (XmlNode node)
130 XmlNodeList nodes = node.SelectNodes ("tasks/task");
132 string [] result = new string [nodes.Count];
135 foreach (XmlNode n in nodes) {
136 result [i] = n.InnerText;
144 public XmlElement RenderHtml ()
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");
154 table.AppendChild (tr);
155 root.AppendChild (table);
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);
165 td = document.CreateElement ("TD");
166 td.SetAttribute ("bgcolor", "#c3cda7");
167 td.SetAttribute ("valign", "bottom");
170 td.AppendChild (name.ToXml (document));
171 td.AppendChild (document.CreateElement ("BR"));
172 td.AppendChild (RenderEmail ());
174 tr = document.CreateElement ("TR");
175 table.AppendChild (tr);
176 td = document.CreateElement ("TD");
177 td.SetAttribute ("bgcolor", "#f5f8e4");
178 td.SetAttribute ("valign", "top");
180 td.AppendChild (RenderLabel ("Location: "));
182 td = document.CreateElement ("TD");
183 td.SetAttribute ("bgcolor", "#f5f8e4");
184 td.SetAttribute ("valign", "top");
186 td.AppendChild (document.CreateTextNode (location));
188 tr = document.CreateElement ("TR");
189 table.AppendChild (tr);
190 td = document.CreateElement ("TD");
191 td.SetAttribute ("bgcolor", "#f5f8e4");
192 td.SetAttribute ("valign", "top");
194 td.AppendChild (RenderLabel ("Description: "));
196 td = document.CreateElement ("TD");
197 td.SetAttribute ("bgcolor", "#f5f8e4");
198 td.SetAttribute ("valign", "top");
200 td.AppendChild (document.CreateTextNode (description));
202 tr = document.CreateElement ("TR");
203 table.AppendChild (tr);
204 td = document.CreateElement ("TD");
205 td.SetAttribute ("bgcolor", "#f5f8e4");
206 td.SetAttribute ("valign", "top");
208 td.AppendChild (RenderLabel ("Tasks: "));
210 td = document.CreateElement ("TD");
211 td.SetAttribute ("bgcolor", "#f5f8e4");
212 td.SetAttribute ("valign", "top");
214 td.AppendChild (RenderTasks ());
219 public XmlNode RenderTasks ()
222 XmlElement element = document.CreateElement ("OL");
223 element.SetAttribute ("type", "I");
225 foreach (string task in tasks) {
226 XmlElement li = document.CreateElement ("LI");
227 li.AppendChild (document.CreateTextNode (task));
228 element.AppendChild (li);
234 public XmlNode RenderEmail ()
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);
247 public XmlNode RenderLabel (string label)
249 string text = String.Format ("{0}: ", label);
250 XmlElement element = document.CreateElement ("B");
251 XmlText t = document.CreateTextNode (label );
252 element.AppendChild (t);
260 XmlDocument document;
265 document = new XmlDocument ();
267 XmlElement table = document.CreateElement ("TABLE");
268 document.AppendChild (table);
270 tbody = document.CreateElement ("TBODY");
271 table.AppendChild (tbody);
274 public XmlDocument Document {
275 get { return document; }
278 public void AddRow (XmlNode left, XmlNode right)
280 if (left == null && right == null)
283 XmlElement tr = document.CreateElement ("TR");
284 tbody.AppendChild (tr);
285 tr.AppendChild (left);
288 tr.AppendChild (document.CreateElement ("TD"));
290 tr.SetAttribute ("valign", "top");
291 tr.AppendChild (right);
295 public void Write (TextWriter text_writer)
297 XmlTextWriter writer = new XmlTextWriter (text_writer);
298 writer.Formatting = Formatting.Indented;
300 document.WriteContentTo (writer);
305 public void Write (string filename)
307 XmlTextWriter writer = new XmlTextWriter (filename, Encoding.Default);
308 writer.Formatting = Formatting.Indented;
310 document.WriteContentTo (writer);
321 public Name (string a, string b)
327 public override string ToString ()
329 if (first_name == null && last_name == null)
332 return first_name + " " + last_name;
335 public XmlNode ToXml (XmlDocument document)
337 XmlElement element = document.CreateElement ("FONT");
338 element.SetAttribute ("size", "3");
339 XmlElement b = document.CreateElement ("B");
340 XmlText t = document.CreateTextNode (ToString ());
342 element.AppendChild (b);