/// <summary> /// Write contracts from dictionary to XML file. /// </summary> static void WriteContracts(IDictionary<string, XContract[]> contracts, Options options, DocTracker docTracker) { Contract.Requires(options != null); Contract.Requires(docTracker != null); Contract.Requires(contracts != null); string xmlDocName = options.xmlFile; #region If an XML file isn't provided, create a new one. if (string.IsNullOrEmpty(xmlDocName) || !File.Exists(xmlDocName)) { docTracker.WriteLine("XML file not provided. Creating a new one."); #region Trim assembly name var trimmedAssemblyName = options.assembly; trimmedAssemblyName = Path.GetFileNameWithoutExtension(trimmedAssemblyName); #endregion if (string.IsNullOrEmpty(xmlDocName)) { xmlDocName = trimmedAssemblyName + ".xml"; } Contract.Assert(xmlDocName != null); #region Create new document XmlDocument doc = new XmlDocument(); doc.AppendChild(doc.CreateXmlDeclaration("1.0", null, null)); var docNode = doc.AppendChild(doc.CreateElement("doc")); var assemblyNode = docNode.AppendChild(doc.CreateElement("assembly")); var nameNode = assemblyNode.AppendChild(doc.CreateElement("name")); nameNode.AppendChild(doc.CreateTextNode(trimmedAssemblyName)); docNode.AppendChild(doc.CreateElement("members")); #endregion doc.Save(xmlDocName); } #endregion #region Traverse the XML file and create a new XML file with contracts var xmlDocTempName = xmlDocName + ".temp"; // xmlDocName may have a path, so don't prepend! using (var reader = XmlReader.Create(xmlDocName)) { docTracker.WriteLine("Reading {0}...", xmlDocName); var settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineHandling = NewLineHandling.None; using (var writer = XmlWriter.Create(xmlDocTempName, settings)) { docTracker.WriteLine("Writing to {0} ...", xmlDocTempName); XmlTraverser xmlTraverser = new XmlTraverser(contracts, docTracker, options); xmlTraverser.Transform(reader, writer); writer.Flush(); } } #endregion #region Rename the output XML file if (String.IsNullOrEmpty(options.outputFile)) { File.Replace(xmlDocTempName, xmlDocName, xmlDocName + ".old"); } else { var outFile = options.outputFile; var ext = Path.GetExtension(outFile); if (ext != "xml") outFile = Path.ChangeExtension(outFile, ".xml"); if (File.Exists(outFile)) { File.Replace(xmlDocTempName, outFile, xmlDocName + ".old"); } else { File.Copy(xmlDocTempName, outFile); } } #endregion }
/// <summary> /// Write contracts from dictionary to XML file. /// </summary> static void WriteContracts(IDictionary <string, XContract[]> contracts, Options options, DocTracker docTracker) { Contract.Requires(options != null); Contract.Requires(docTracker != null); Contract.Requires(contracts != null); string xmlDocName = options.xmlFile; #region If an XML file isn't provided, create a new one. if (string.IsNullOrEmpty(xmlDocName) || !File.Exists(xmlDocName)) { docTracker.WriteLine("XML file not provided. Creating a new one."); #region Trim assembly name var trimmedAssemblyName = options.assembly; trimmedAssemblyName = Path.GetFileNameWithoutExtension(trimmedAssemblyName); #endregion if (string.IsNullOrEmpty(xmlDocName)) { xmlDocName = trimmedAssemblyName + ".xml"; } Contract.Assert(xmlDocName != null); #region Create new document XmlDocument doc = new XmlDocument(); doc.AppendChild(doc.CreateXmlDeclaration("1.0", null, null)); var docNode = doc.AppendChild(doc.CreateElement("doc")); var assemblyNode = docNode.AppendChild(doc.CreateElement("assembly")); var nameNode = assemblyNode.AppendChild(doc.CreateElement("name")); nameNode.AppendChild(doc.CreateTextNode(trimmedAssemblyName)); docNode.AppendChild(doc.CreateElement("members")); #endregion doc.Save(xmlDocName); } #endregion #region Traverse the XML file and create a new XML file with contracts var xmlDocTempName = xmlDocName + ".temp"; // xmlDocName may have a path, so don't prepend! using (var reader = XmlReader.Create(xmlDocName)) { docTracker.WriteLine("Reading {0}...", xmlDocName); var settings = new XmlWriterSettings(); settings.Indent = true; settings.NewLineHandling = NewLineHandling.None; using (var writer = XmlWriter.Create(xmlDocTempName, settings)) { docTracker.WriteLine("Writing to {0} ...", xmlDocTempName); XmlTraverser xmlTraverser = new XmlTraverser(contracts, docTracker, options); xmlTraverser.Transform(reader, writer); writer.Flush(); } } #endregion #region Rename the output XML file if (String.IsNullOrEmpty(options.outputFile)) { File.Replace(xmlDocTempName, xmlDocName, xmlDocName + ".old"); } else { var outFile = options.outputFile; var ext = Path.GetExtension(outFile); if (ext != "xml") { outFile = Path.ChangeExtension(outFile, ".xml"); } if (File.Exists(outFile)) { File.Replace(xmlDocTempName, outFile, xmlDocName + ".old"); } else { File.Copy(xmlDocTempName, outFile); } } #endregion }