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;
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>");
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];
40 foreach (XmlNode n in contributors) {
41 list [count] = new Contributor (n, p.Document);
45 Array.Sort (list, new ContributorComparer ());
47 int length = list.Length % 2 == 0 ? list.Length : list.Length + 1;
52 p.AddRow (list [i].RenderHtml (), list [i + 1].RenderHtml ());
53 } catch (IndexOutOfRangeException) {
54 p.AddRow (list [i].RenderHtml (), null);
63 public class ContributorComparer : IComparer
65 public int Compare (object x, object y)
67 return String.Compare (x as string , y as string);
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)
113 public static string GetField (XmlNode node, string selector)
115 XmlNode result = node.SelectSingleNode (selector, Write.nsmgr);
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];
138 foreach (XmlNode n in nodes) {
139 result [i] = n.InnerText;
147 public XmlElement RenderHtml ()
149 XmlElement root = document.CreateElement ("TD");
150 XmlElement table = document.CreateElement ("TABLE");
151 table.SetAttribute ("width", "100%");
152 XmlElement tr = document.CreateElement ("TR");
153 XmlElement td = document.CreateElement ("TD");
154 td.SetAttribute ("bgcolor", "#c3cda7");
155 td.SetAttribute ("valign", "top");
157 table.AppendChild (tr);
158 root.AppendChild (table);
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);
168 td = document.CreateElement ("TD");
169 td.SetAttribute ("bgcolor", "#c3cda7");
170 td.SetAttribute ("valign", "bottom");
173 td.AppendChild (name.ToXml (document));
174 td.AppendChild (document.CreateElement ("BR"));
175 td.AppendChild (RenderEmail ());
177 tr = document.CreateElement ("TR");
178 table.AppendChild (tr);
179 td = document.CreateElement ("TD");
180 td.SetAttribute ("bgcolor", "#f5f8e4");
181 td.SetAttribute ("valign", "top");
183 td.AppendChild (RenderLabel ("Location: "));
185 td = document.CreateElement ("TD");
186 td.SetAttribute ("bgcolor", "#f5f8e4");
187 td.SetAttribute ("valign", "top");
189 td.AppendChild (document.CreateTextNode (location));
191 tr = document.CreateElement ("TR");
192 table.AppendChild (tr);
193 td = document.CreateElement ("TD");
194 td.SetAttribute ("bgcolor", "#f5f8e4");
195 td.SetAttribute ("valign", "top");
197 td.AppendChild (RenderLabel ("Description: "));
199 td = document.CreateElement ("TD");
200 td.SetAttribute ("bgcolor", "#f5f8e4");
201 td.SetAttribute ("valign", "top");
203 td.AppendChild (document.CreateTextNode (description));
205 tr = document.CreateElement ("TR");
206 table.AppendChild (tr);
207 td = document.CreateElement ("TD");
208 td.SetAttribute ("bgcolor", "#f5f8e4");
209 td.SetAttribute ("valign", "top");
211 td.AppendChild (RenderLabel ("Tasks: "));
213 td = document.CreateElement ("TD");
214 td.SetAttribute ("bgcolor", "#f5f8e4");
215 td.SetAttribute ("valign", "top");
217 td.AppendChild (RenderTasks ());
222 public XmlNode RenderTasks ()
225 XmlElement element = document.CreateElement ("OL");
226 element.SetAttribute ("type", "I");
228 foreach (string task in tasks) {
229 XmlElement li = document.CreateElement ("LI");
230 li.AppendChild (document.CreateTextNode (task));
231 element.AppendChild (li);
237 public XmlNode RenderEmail ()
239 XmlElement a = document.CreateElement ("A");
240 a.SetAttribute ("href", "mailto:" + email);
241 XmlElement font = document.CreateElement ("FONT");
242 font.SetAttribute ("size", "3");
243 XmlText t = document.CreateTextNode (email);
244 a.AppendChild (font);
245 font.AppendChild (t);
250 public XmlNode RenderLabel (string label)
252 string text = String.Format ("{0}: ", label);
253 XmlElement element = document.CreateElement ("B");
254 XmlText t = document.CreateTextNode (label );
255 element.AppendChild (t);
263 XmlDocument document;
268 document = new XmlDocument ();
270 XmlElement table = document.CreateElement ("TABLE");
271 document.AppendChild (table);
273 tbody = document.CreateElement ("TBODY");
274 table.AppendChild (tbody);
277 public XmlDocument Document {
278 get { return document; }
281 public void AddRow (XmlNode left, XmlNode right)
283 if (left == null && right == null)
286 XmlElement tr = document.CreateElement ("TR");
287 tbody.AppendChild (tr);
288 tr.AppendChild (left);
291 tr.AppendChild (document.CreateElement ("TD"));
293 tr.SetAttribute ("valign", "top");
294 tr.AppendChild (right);
298 public void Write (TextWriter text_writer)
300 XmlTextWriter writer = new XmlTextWriter (text_writer);
301 writer.Formatting = Formatting.Indented;
303 document.WriteContentTo (writer);
308 public void Write (string filename)
310 XmlTextWriter writer = new XmlTextWriter (filename, Encoding.Default);
311 writer.Formatting = Formatting.Indented;
313 document.WriteContentTo (writer);
324 public Name (string a, string b)
330 public override string ToString ()
332 if (first_name == null && last_name == null)
335 return first_name + " " + last_name;
338 public XmlNode ToXml (XmlDocument document)
340 XmlElement element = document.CreateElement ("FONT");
341 element.SetAttribute ("size", "3");
342 XmlElement b = document.CreateElement ("B");
343 XmlText t = document.CreateTextNode (ToString ());
345 element.AppendChild (b);