using System; using System.Collections; using System.IO; using System.Xml; namespace CosmosDocumentifier { /// /// Summary description for HTMLify. /// public class HTMLify { public HTMLify() { } public static string HTMLIFY_DEFAULT_NAME = "Cosmos Feature Documentation"; public static XmlDocument GetXML(string fileName) { try { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(fileName); return xmlDocument; } catch(XmlException) { return null; } } public static string GenerateHTML(XmlDocument xmlDocument) { return GenerateHTMLFromRoot(xmlDocument.DocumentElement, HTMLIFY_DEFAULT_NAME); } public static string GenerateHTML(XmlDocument xmlDocument, string name) { return GenerateHTMLFromRoot(xmlDocument.DocumentElement, name); } public static string GenerateHTMLFromRoot(XmlElement xmlRoot) { return GenerateHTMLFromRoot(xmlRoot, HTMLIFY_DEFAULT_NAME); } public static string GetNodeThingy(XmlNode xmlNode, string identifier) { string str = null; if((xmlNode.Attributes != null) && (xmlNode.Attributes.Count > 0)) { try { str = xmlNode.Attributes[identifier].Value; } catch(Exception) { } } if((str == null) && (xmlNode.ChildNodes != null)) { foreach(XmlNode childNode in xmlNode.ChildNodes) { if(childNode.Name == identifier) { str = childNode.InnerText; break; } } } if(str != null) { str = str.Replace("\\n ", "\n"); str = str.Replace("\\n", "\n"); } return str; } public static string GetNodeId(XmlNode xmlNode) { return GetNodeThingy(xmlNode, "id"); } public static string GetNodeName(XmlNode xmlNode) { return GetNodeThingy(xmlNode, "name"); } public static string GetNodeAuthor(XmlNode xmlNode) { return GetNodeThingy(xmlNode, "author"); } public static string GetNodeInterface(XmlNode xmlNode) { return GetNodeThingy(xmlNode, "interface"); } public static string GetNodeDescription(XmlNode xmlNode) { return GetNodeThingy(xmlNode, "description"); } public static string GenerateHTMLFromRoot(XmlElement xmlRoot, string name) { StringWriter stringWriter = new StringWriter(); stringWriter.Write("\n\n\t{0}\n\n\n", name); if((xmlRoot.ChildNodes != null) && (xmlRoot.ChildNodes.Count > 0)) { stringWriter.Write("

Feature group list

\n"); stringWriter.Write("\n"); foreach(XmlNode xmlNode in xmlRoot.ChildNodes) { stringWriter.Write("

{1}

\n", GetNodeId(xmlNode), GetNodeName(xmlNode)); string description = GetNodeDescription(xmlNode); PrintField(stringWriter, "Author", GetNodeAuthor(xmlNode)); PrintField(stringWriter, "Interface", GetNodeInterface(xmlNode)); if(description != null) { stringWriter.Write("Description :
\n

{0}

\n", description); } if(xmlNode.ChildNodes != null) { XmlNode featureNode = null; foreach(XmlNode childNode in xmlNode.ChildNodes) { if(childNode.Name == "Features") { featureNode = childNode; break; } } if(featureNode != null) { stringWriter.Write("\n"); } else { stringWriter.Write("

No features specified.

\n"); } } else { stringWriter.Write("

No features specified.

\n"); } } } stringWriter.Write("\n"); return stringWriter.ToString(); } public static void PrintField(StringWriter stringWriter, string fieldName, string fieldValue) { if ( fieldValue != null ) { stringWriter.Write(fieldName); stringWriter.Write(" : "); stringWriter.Write(fieldValue); stringWriter.Write("\n"); } } } }