Given an html path, this can give it as xhtml, let you make changes, and then give it back as html when you're done.
Inheritance: IDisposable
        private static void CarefullyWriteOutResultingXml(HtmlFileForMerging oursXml, NodeMergeResult result)
        {
//REVIEW: it's not clear whether we need all this fancy xml cannonicalization, when we're going to run
            //it through html tidy to make html anyhow. This is just from the code we copied from.
            //Note it also is doing something careful with unicode.

            using (var writer = XmlWriter.Create(oursXml.GetPathToXHtml(), CanonicalXmlSettings.CreateXmlWriterSettings()))
            {
                var nameSpaceManager = new XmlNamespaceManager(new NameTable());
                //nameSpaceManager.AddNamespace("palaso", "urn://palaso.org/ldmlExtensions/v1");

                var readerSettings = new XmlReaderSettings
                {
                    NameTable        = nameSpaceManager.NameTable,
                    IgnoreWhitespace = true,
                    ConformanceLevel = ConformanceLevel.Auto,
                    ValidationType   = ValidationType.None,
                    XmlResolver      = null,
                    CloseInput       = true,
                    DtdProcessing    = DtdProcessing.Prohibit,
                };
                using (
                    var nodeReader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(result.MergedNode.OuterXml)),
                                                      readerSettings))
                {
                    writer.WriteNode(nodeReader, false);
                }
            }
        }
        /// <summary>
        /// Do a 3-file merge, placing the result over the "ours" file and returning an error status
        /// </summary>
        /// <remarks>Implementations can exit with an exception, which the caller will catch and deal with.
        /// The must not have any UI, no interaction with the user.</remarks>
        public void Do3WayMerge(MergeOrder mergeOrder)
        {
            Guard.AgainstNull(mergeOrder, "mergeOrder");

            if (mergeOrder == null)
            {
                throw new ArgumentNullException("mergeOrder");
            }

            var merger = new XmlMerger(mergeOrder.MergeSituation);

            SetupElementStrategies(merger);

            merger.EventListener = mergeOrder.EventListener;

            using (var oursXml = new HtmlFileForMerging(mergeOrder.pathToOurs))
                using (var theirsXml = new HtmlFileForMerging(mergeOrder.pathToTheirs))
                    using (var ancestorXml = new HtmlFileForMerging(mergeOrder.pathToCommonAncestor))
                    {
                        var result = merger.MergeFiles(oursXml.GetPathToXHtml(), theirsXml.GetPathToXHtml(), ancestorXml.GetPathToXHtml());

                        CarefullyWriteOutResultingXml(oursXml, result);

                        //now convert back to html
                        oursXml.SaveHtml();
                    }
        }
 public void GetPathToXHtml_IsXml()
 {
     const string html = @"<html><head></head><body><p></body></html>";
     using(var htmlFile = new TempFile(html))
     {
         var xmlPath = new HtmlFileForMerging(htmlFile.Path).GetPathToXHtml();
         Assert.IsTrue(File.Exists(xmlPath));
         AssertThatXmlIn.File(xmlPath).HasSpecifiedNumberOfMatchesForXpath("//p",1);
     }
 }
 public void SaveHtml_IsHtml()
 {
     const string html = @"<html><head></head><body><p>one</body></html>";
     using (var htmlFile = new TempFile(html))
     {
         var htmlFileForMerging = new HtmlFileForMerging(htmlFile.Path);
         var xmlPath = htmlFileForMerging.GetPathToXHtml();
         var xml = File.ReadAllText(xmlPath);
         xml = xml.Replace("one","one<br/>two");
         File.WriteAllText(xmlPath, xml);
         htmlFileForMerging.SaveHtml();
         var newHtml = File.ReadAllText(htmlFile.Path);
         Assert.IsTrue(newHtml.StartsWith("<!DOCTYPE html>"));
         Assert.IsTrue(newHtml.Contains("two"));
     }
 }
        /// <summary>
        /// Do a 3-file merge, placing the result over the "ours" file and returning an error status
        /// </summary>
        /// <remarks>Implementations can exit with an exception, which the caller will catch and deal with.
        /// The must not have any UI, no interaction with the user.</remarks>
        public void Do3WayMerge(MergeOrder mergeOrder)
        {
            Guard.AgainstNull(mergeOrder, "mergeOrder");

            if (mergeOrder == null)
                throw new ArgumentNullException("mergeOrder");

            var merger = new XmlMerger(mergeOrder.MergeSituation);
            SetupElementStrategies(merger);

            merger.EventListener = mergeOrder.EventListener;

            using(var oursXml = new HtmlFileForMerging(mergeOrder.pathToOurs))
            using(var theirsXml = new HtmlFileForMerging(mergeOrder.pathToTheirs))
            using (var ancestorXml = new HtmlFileForMerging(mergeOrder.pathToCommonAncestor))
            {
                var result = merger.MergeFiles(oursXml.GetPathToXHtml(), theirsXml.GetPathToXHtml(), ancestorXml.GetPathToXHtml());

                CarefullyWriteOutResultingXml(oursXml, result);

                //now convert back to html
                oursXml.SaveHtml();
            }
        }
        private static void CarefullyWriteOutResultingXml(HtmlFileForMerging oursXml, NodeMergeResult result)
        {
            //REVIEW: it's not clear whether we need all this fancy xml cannonicalization, when we're going to run
            //it through html tidy to make html anyhow. This is just from the code we copied from.
            //Note it also is doing something careful with unicode.

            using (var writer = XmlWriter.Create(oursXml.GetPathToXHtml(), CanonicalXmlSettings.CreateXmlWriterSettings()))
            {
                var nameSpaceManager = new XmlNamespaceManager(new NameTable());
                //nameSpaceManager.AddNamespace("palaso", "urn://palaso.org/ldmlExtensions/v1");

                var readerSettings = new XmlReaderSettings
                                        {
                                            NameTable = nameSpaceManager.NameTable,
                                            IgnoreWhitespace = true,
                                            ConformanceLevel = ConformanceLevel.Auto,
                                            ValidationType = ValidationType.None,
                                            XmlResolver = null,
                                            CloseInput = true,
                                            DtdProcessing = DtdProcessing.Prohibit,
                                        };
                using (
                    var nodeReader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(result.MergedNode.OuterXml)),
                                                      readerSettings))
                {
                    writer.WriteNode(nodeReader, false);
                }
            }
        }