Beispiel #1
0
        private static void Main()
        {
            var n      = DateTime.Now;
            var tempDi = new DirectoryInfo(string.Format("ExampleOutput-{0:00}-{1:00}-{2:00}-{3:00}{4:00}{5:00}", n.Year - 2000, n.Month, n.Day, n.Hour, n.Minute, n.Second));

            tempDi.Create();

            var settings = new WmlComparerSettings();
            var result   = WmlComparer.Compare(
                new WmlDocument("../../Source1.docx"),
                new WmlDocument("../../Source2.docx"),
                settings);

            result.SaveAs(Path.Combine(tempDi.FullName, "Compared.docx"));

            var revisions = WmlComparer.GetRevisions(result, settings);

            foreach (var rev in revisions)
            {
                Console.WriteLine("Author: " + rev.Author);
                Console.WriteLine("Revision type: " + rev.RevisionType);
                Console.WriteLine("Revision text: " + rev.Text);
                Console.WriteLine();
            }
        }
Beispiel #2
0
        public void Sample1()
        {
            var settings = new WmlComparerSettings();
            var result   = WmlComparer.Compare(
                new WmlDocument(GetFilePath("Sample1/Source1.docx")),
                new WmlDocument(GetFilePath("Sample1/Source2.docx")),
                settings);

            result.SaveAs(Path.Combine(TempDir, "Compared.docx"));

            var revisions = WmlComparer.GetRevisions(result, settings);

            foreach (var rev in revisions)
            {
                Log.WriteLine("Author: " + rev.Author);
                Log.WriteLine("Revision type: " + rev.RevisionType);
                Log.WriteLine("Revision text: " + rev.Text);
            }
        }
        public void DoTest(string name)
        {
            var sourceFi           = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, name));
            var baselineAcceptedFi = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, name.Replace(".docx", "-Accepted.docx")));
            var baselineRejectedFi = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, name.Replace(".docx", "-Rejected.docx")));

            WmlDocument sourceWml         = new WmlDocument(sourceFi.FullName);
            WmlDocument afterRejectingWml = RevisionProcessor.RejectRevisions(sourceWml);
            WmlDocument afterAcceptingWml = RevisionProcessor.AcceptRevisions(sourceWml);

            var processedAcceptedFi = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, sourceFi.Name.Replace(".docx", "-Accepted.docx")));

            afterAcceptingWml.SaveAs(processedAcceptedFi.FullName);

            var processedRejectedFi = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, sourceFi.Name.Replace(".docx", "-Rejected.docx")));

            afterRejectingWml.SaveAs(processedRejectedFi.FullName);

            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // Copy source files to temp dir
            if (m_CopySourceFilesToTempDir)
            {
                while (true)
                {
                    try
                    {
                        ////////// CODE TO REPEAT UNTIL SUCCESS //////////
                        var sourceDocxCopiedToDestFi = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, sourceFi.Name));
                        if (!sourceDocxCopiedToDestFi.Exists)
                        {
                            sourceWml.SaveAs(sourceDocxCopiedToDestFi.FullName);
                        }
                        //////////////////////////////////////////////////
                        break;
                    }
                    catch (IOException)
                    {
                        System.Threading.Thread.Sleep(50);
                    }
                }
            }

            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // create batch file to copy properly processed documents to the TestFiles directory.
            while (true)
            {
                try
                {
                    ////////// CODE TO REPEAT UNTIL SUCCESS //////////
                    var batchFileName = "Copy-Gen-Files-To-TestFiles.bat";
                    var batchFi       = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, batchFileName));
                    var batch         = "";
                    batch += "copy " + processedAcceptedFi.FullName + " " + baselineAcceptedFi.FullName + Environment.NewLine;
                    batch += "copy " + processedRejectedFi.FullName + " " + baselineRejectedFi.FullName + Environment.NewLine;
                    if (batchFi.Exists)
                    {
                        File.AppendAllText(batchFi.FullName, batch);
                    }
                    else
                    {
                        File.WriteAllText(batchFi.FullName, batch);
                    }
                    //////////////////////////////////////////////////
                    break;
                }
                catch (IOException)
                {
                    System.Threading.Thread.Sleep(50);
                }
            }

            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // Open Windows Explorer
            if (m_OpenTempDirInExplorer)
            {
                while (true)
                {
                    try
                    {
                        ////////// CODE TO REPEAT UNTIL SUCCESS //////////
                        var semaphorFi = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, "z_ExplorerOpenedSemaphore.txt"));
                        if (!semaphorFi.Exists)
                        {
                            File.WriteAllText(semaphorFi.FullName, "");
                            TestUtil.Explorer(TestUtil.TempDir);
                        }
                        //////////////////////////////////////////////////
                        break;
                    }
                    catch (IOException)
                    {
                        System.Threading.Thread.Sleep(50);
                    }
                }
            }

            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // Use WmlComparer to see if accepted baseline is same as processed
            if (baselineAcceptedFi.Exists)
            {
                var baselineAcceptedWml = new WmlDocument(baselineAcceptedFi.FullName);
                WmlComparerSettings wmlComparerSettings = new WmlComparerSettings();
                WmlDocument         result = WmlComparer.Compare(baselineAcceptedWml, afterAcceptingWml, wmlComparerSettings);
                var revisions = WmlComparer.GetRevisions(result, wmlComparerSettings);
                if (revisions.Any())
                {
                    Assert.True(false, "Regression Error: Accepted baseline document did not match processed document");
                }
            }
            else
            {
                Assert.True(false, "No Accepted baseline document");
            }

            ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            // Use WmlComparer to see if rejected baseline is same as processed
            if (baselineRejectedFi.Exists)
            {
                var baselineRejectedWml = new WmlDocument(baselineRejectedFi.FullName);
                WmlComparerSettings wmlComparerSettings = new WmlComparerSettings();
                WmlDocument         result = WmlComparer.Compare(baselineRejectedWml, afterRejectingWml, wmlComparerSettings);
                var revisions = WmlComparer.GetRevisions(result, wmlComparerSettings);
                if (revisions.Any())
                {
                    Assert.True(false, "Regression Error: Rejected baseline document did not match processed document");
                }
            }
            else
            {
                Assert.True(false, "No Rejected baseline document");
            }
        }
        //[InlineData("", "", 0)]
        //[InlineData("", "", 0)]
        //[InlineData("", "", 0)]
        //[InlineData("", "", 0)]
        //[InlineData("", "", 0)]
        //[InlineData("", "", 0)]
        //[InlineData("", "", 0)]
        //[InlineData("", "", 0)]

        public void WC003_Compare(string name1, string name2, int revisionCount)
        {
            FileInfo source1Docx = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, name1));
            FileInfo source2Docx = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, name2));

            var source1CopiedToDestDocx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, source1Docx.Name));
            var source2CopiedToDestDocx = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, source2Docx.Name));

            if (!source1CopiedToDestDocx.Exists)
            {
                File.Copy(source1Docx.FullName, source1CopiedToDestDocx.FullName);
            }
            if (!source2CopiedToDestDocx.Exists)
            {
                File.Copy(source2Docx.FullName, source2CopiedToDestDocx.FullName);
            }

            /************************************************************************************************************************/

            if (s_OpenWord)
            {
                FileInfo source1DocxForWord = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, name1));
                FileInfo source2DocxForWord = new FileInfo(Path.Combine(TestUtil.SourceDir.FullName, name2));

                var source1CopiedToDestDocxForWord = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, source1Docx.Name.Replace(".docx", "-For-Word.docx")));
                var source2CopiedToDestDocxForWord = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, source2Docx.Name.Replace(".docx", "-For-Word.docx")));
                if (!source1CopiedToDestDocxForWord.Exists)
                {
                    File.Copy(source1Docx.FullName, source1CopiedToDestDocxForWord.FullName);
                }
                if (!source2CopiedToDestDocxForWord.Exists)
                {
                    File.Copy(source2Docx.FullName, source2CopiedToDestDocxForWord.FullName);
                }

                FileInfo wordExe = new FileInfo(@"C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE");
                var      path    = new DirectoryInfo(@"C:\Users\Eric\Documents\WindowsPowerShellModules\Open-Xml-PowerTools\TestFiles");
                WordRunner.RunWord(wordExe, source2CopiedToDestDocxForWord);
                WordRunner.RunWord(wordExe, source1CopiedToDestDocxForWord);
            }

            /************************************************************************************************************************/

            var before = source1CopiedToDestDocx.Name.Replace(".docx", "");
            var after  = source2CopiedToDestDocx.Name.Replace(".docx", "");
            var docxWithRevisionsFi = new FileInfo(Path.Combine(TestUtil.TempDir.FullName, before + "-COMPARE-" + after + ".docx"));

            WmlDocument         source1Wml  = new WmlDocument(source1CopiedToDestDocx.FullName);
            WmlDocument         source2Wml  = new WmlDocument(source2CopiedToDestDocx.FullName);
            WmlComparerSettings settings    = new WmlComparerSettings();
            WmlDocument         comparedWml = WmlComparer.Compare(source1Wml, source2Wml, settings);

            comparedWml.SaveAs(docxWithRevisionsFi.FullName);

            using (MemoryStream ms = new MemoryStream())
            {
                ms.Write(comparedWml.DocumentByteArray, 0, comparedWml.DocumentByteArray.Length);
                using (WordprocessingDocument wDoc = WordprocessingDocument.Open(ms, true))
                {
                    OpenXmlValidator validator = new OpenXmlValidator();
                    var errors = validator.Validate(wDoc).Where(e => !ExpectedErrors.Contains(e.Description));
                    if (errors.Count() > 0)
                    {
                        var ind = "  ";
                        var sb  = new StringBuilder();
                        foreach (var err in errors)
                        {
#if true
                            sb.Append("Error" + Environment.NewLine);
                            sb.Append(ind + "ErrorType: " + err.ErrorType.ToString() + Environment.NewLine);
                            sb.Append(ind + "Description: " + err.Description + Environment.NewLine);
                            sb.Append(ind + "Part: " + err.Part.Uri.ToString() + Environment.NewLine);
                            sb.Append(ind + "XPath: " + err.Path.XPath + Environment.NewLine);
#else
                            sb.Append("            \"" + err.Description + "\"," + Environment.NewLine);
#endif
                        }
                        var sbs = sb.ToString();
                        Assert.Equal("", sbs);
                    }
                }
            }

            /************************************************************************************************************************/

            if (s_OpenWord)
            {
                FileInfo wordExe = new FileInfo(@"C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE");
                WordRunner.RunWord(wordExe, docxWithRevisionsFi);
            }

            /************************************************************************************************************************/

            WmlDocument revisionWml = new WmlDocument(docxWithRevisionsFi.FullName);
            var         revisions   = WmlComparer.GetRevisions(revisionWml);
            Assert.Equal(revisionCount, revisions.Count());
        }