public void TestActualGenerationStaticTextAfterTag()
        {
            var input = AssemblyResourceHelper.GetResourceStream(this, "corruptedDocxx.docx");

            using (input)
            {
                var       output    = new MemoryStream();
                var       generator = new DocxGenerator();
                XDocument data;
                using (var dataStream = AssemblyResourceHelper.GetResourceStream(this, "DemoData2.xml"))
                {
                    data = XDocument.Load(dataStream);
                }
                generator.GenerateDocx(
                    input,
                    output,
                    data);

                var package = new DocxPackage(output);
                package.Load();
                var documentPart = package.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml;
                Assert.IsFalse(documentPart.Descendants(WordMl.SdtName).Any());
                Console.WriteLine(documentPart);
                Assert.IsTrue(documentPart.Root.Descendants(WordMl.TableCellName).All(element => element.Elements().All(el => el.Name != WordMl.TextRunName)));
            }
        }
        public void TestActualGenerationItemRepeaterElementsInParagraphs2()
        {
            MemoryStream output;

            using (var input = AssemblyResourceHelper.GetResourceStream(this, "badplan.docx"))
            {
                output = new MemoryStream();
                var       generator = new DocxGenerator();
                XDocument data;
                using (var dataStream = AssemblyResourceHelper.GetResourceStream(this, "PlanDocx.xml"))
                {
                    data = XDocument.Load(dataStream);
                }
                generator.GenerateDocx(
                    input,
                    output,
                    data, new DocxGeneratorSettings()
                {
                    MissingDataMode = MissingDataMode.ThrowException
                });
            }

            var package = new DocxPackage(output);

            package.Load();

            var documentPart = package.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml;

            Console.WriteLine(documentPart.Descendants(WordMl.TableRowName).First(tr => tr.Descendants().Any(el => el.Value == "тестовое ЭА мероприятие")));
            Assert.IsFalse(documentPart.Descendants(WordMl.SdtName).Any());
            Assert.IsFalse(documentPart.Descendants(WordMl.ParagraphName).Descendants().Any(el => el.Name == WordMl.ParagraphName));
        }
        public void TestSave()
        {
            using (var docxStream = AssemblyResourceHelper.GetResourceStream(this, "DocxPackageTest.docx"))
            {
                var memoryStream = new MemoryStream();
                memoryStream.Seek(0, SeekOrigin.Begin);
                docxStream.Seek(0, SeekOrigin.Begin);
                docxStream.CopyTo(memoryStream);
                var testPackage = new DocxPackage(memoryStream);

                testPackage.Load();

                var      documentPart = testPackage.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml;
                XElement sdt          = documentPart.Descendants(WordMl.SdtName).First();
                sdt.Remove();

                testPackage.Save();

                var expectedDocumentStream = AssemblyResourceHelper.GetResourceStream(this, "DocxPackageTest_save.docx");

                DocxPackage expectedPackagePart = new DocxPackage(expectedDocumentStream).Load();
                DocxPackage actualPackagePart   = new DocxPackage(memoryStream).Load();
                Assert.AreEqual(expectedPackagePart.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml.Root.ToString().Trim(),
                                actualPackagePart.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml.Root.ToString().Trim());
            }
        }
Пример #4
0
        public void TestCreateAfChunkPart()
        {
            using (var docxStream = AssemblyResourceHelper.GetResourceStream(this, "DocxPackageTest.docx"))
            {
                var memoryStream = new MemoryStream();
                memoryStream.Seek(0, SeekOrigin.Begin);
                docxStream.Seek(0, SeekOrigin.Begin);
                docxStream.CopyTo(memoryStream);
                var testPackage = new DocxPackagePart(memoryStream);

                const int    ExpectedAfChunkId  = 10;
                const string ExpectedHtmlString = "<html><head/><body>Test AfChunk Generator</body></html>";
                var          expectedPartPath   = string.Format("//word/afchunk{0}.dat", ExpectedAfChunkId);
                using (var package = Package.Open(memoryStream, FileMode.Open, FileAccess.ReadWrite))
                {
                    DocxPartResolver.CreateAfChunkPart(package, ExpectedAfChunkId, ExpectedHtmlString);
                    var formattedAltChunkRel = string.Format("altChunkId{0}", ExpectedAfChunkId);
                    var actualDocPart        = DocxPartResolver.GetDocumentPart(package);
                    var actualRelationship   = actualDocPart.GetRelationship(formattedAltChunkRel);
                    Assert.IsNotNull(actualRelationship);
                    Assert.AreEqual(formattedAltChunkRel, actualRelationship.Id);
                    var actualAfChunkPart = package.GetPart(PackUriHelper.ResolvePartUri(actualRelationship.SourceUri,
                                                                                         actualRelationship.TargetUri));
                    Assert.IsNotNull(actualAfChunkPart);
                    using (var afChunkStream = actualAfChunkPart.GetStream())
                    {
                        using (var reader = new StreamReader(afChunkStream))
                        {
                            Assert.AreEqual(ExpectedHtmlString, reader.ReadToEnd());
                        }
                    }
                }
            }
        }
Пример #5
0
        public void TestParseIfNested()
        {
            using (var docStream = AssemblyResourceHelper.GetResourceStream(this, "text_in_if_document.xml"))
            {
                doc = XDocument.Load(docStream);

                var           expectedRootProcessor = new RootProcessor();
                ITagProcessor expectedIfProcessor   = new IfProcessor();

                expectedIfProcessor.AddProcessor(new TextProcessor());
                var expectedIfProcessors = new List <ITagProcessor>(expectedIfProcessor.Processors);
                expectedRootProcessor.AddProcessor(expectedIfProcessor);
                var expectedRootProcessors = new List <ITagProcessor>(expectedRootProcessor.Processors);

                var actualRootProcessor = new RootProcessor();
                generalParser.Parse(actualRootProcessor, doc.Root);

                var actualRootProcessors = new List <ITagProcessor>(actualRootProcessor.Processors);
                Assert.AreEqual(expectedRootProcessors.Count, actualRootProcessors.Count);
                Assert.AreEqual(expectedRootProcessors[0].GetType(), actualRootProcessors[0].GetType());
                var actualIfProcessors = new List <ITagProcessor>(actualRootProcessors[0].Processors);
                Assert.AreEqual(expectedIfProcessors.Count, actualIfProcessors.Count);
                Assert.AreEqual(expectedIfProcessors[0].GetType(), actualIfProcessors[0].GetType());
                Assert.AreEqual(expectedIfProcessors[0].Processors.Count, expectedIfProcessors[0].Processors.Count);
            }
        }
        public void TestActualGenerationTextInTable()
        {
            MemoryStream output;

            using (var input = AssemblyResourceHelper.GetResourceStream(this, "textintable.docx"))
            {
                output = new MemoryStream();
                var       generator = new DocxGenerator();
                XDocument data;
                using (var dataStream = AssemblyResourceHelper.GetResourceStream(this, "data.xml"))
                {
                    data = XDocument.Load(dataStream);
                }
                generator.GenerateDocx(
                    input,
                    output,
                    data);
            }

            var package = new DocxPackage(output);

            package.Load();

            var documentPart = package.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml;

            Assert.IsFalse(documentPart.Descendants(WordMl.SdtName).Any());
            // documentPart.Descendants(WordMl.ParagraphName).Remove();
            Console.WriteLine(documentPart.Descendants(WordMl.TableName).First());
            Assert.IsTrue(documentPart.Descendants(WordMl.TableRowName).All(element => element.Elements().All(el => el.Name != WordMl.ParagraphName)));
        }
        public void TestInit()
        {
            var docStream = AssemblyResourceHelper.GetResourceStream(this, "ComplexIf.xml");
            var doc       = XDocument.Load(docStream);

            this.documentRoot = doc.Root.Element(WordMl.WordMlNamespace + "body");
        }
        public void TestActualGenerationIfWithTableWithParagraphs()
        {
            MemoryStream output;

            using (var input = AssemblyResourceHelper.GetResourceStream(this, "ifTtable1.docx"))
            {
                output = new MemoryStream();
                var       generator = new DocxGenerator();
                XDocument data;
                using (var dataStream = AssemblyResourceHelper.GetResourceStream(this, "data.xml"))
                {
                    data = XDocument.Load(dataStream);
                }
                generator.GenerateDocx(
                    input,
                    output,
                    data);
            }

            var package = new DocxPackage(output);

            package.Load();

            var documentPart = package.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml;

            Assert.IsFalse(documentPart.Descendants(WordMl.SdtName).Any());
        }
        public void TestSave()
        {
            using (var docxStream = AssemblyResourceHelper.GetResourceStream(this, "DocxPackageHeaderFooterTest.docx"))
            {
                var memoryStream = new MemoryStream();
                memoryStream.Seek(0, SeekOrigin.Begin);
                docxStream.Seek(0, SeekOrigin.Begin);
                docxStream.CopyTo(memoryStream);
                var package = new DocxPackage(memoryStream);

                package.Load();
                foreach (var part in package.Parts)
                {
                    XElement sdt = part.PartXml.Descendants(WordMl.SdtName).First();
                    sdt.Remove();
                }
                package.Save();

                var expectedDocumentStream = AssemblyResourceHelper.GetResourceStream(this, "DocxPackageHeaderFooterTest_saved.docx");

                DocxPackage expectedPackagePart = new DocxPackage(expectedDocumentStream).Load();
                DocxPackage actualPackagePart   = new DocxPackage(memoryStream).Load();

                foreach (var expected in expectedPackagePart.Parts)
                {
                    var actual = actualPackagePart.Parts.Single(x => x.PartUri == expected.PartUri);
                    Assert.AreEqual(expected.PartXml.Root.ToString().Trim(), actual.PartXml.Root.ToString().Trim());
                }
            }
        }
        public void TestActualGenerationDoubleItemIfWithItemText()
        {
            MemoryStream output;

            using (var input = AssemblyResourceHelper.GetResourceStream(this, "corruptedDoc.docx"))
            {
                output = new MemoryStream();
                var       generator = new DocxGenerator();
                XDocument data;
                using (var dataStream = AssemblyResourceHelper.GetResourceStream(this, "DemoData2.xml"))
                {
                    data = XDocument.Load(dataStream);
                }
                generator.GenerateDocx(
                    input,
                    output,
                    data,
                    new DocxGeneratorSettings()
                {
                    MissingDataMode = MissingDataMode.ThrowException
                });
            }

            var package = new DocxPackage(output);

            package.Load();

            var documentPart = package.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml;

            Console.WriteLine(documentPart.ToString());
            Assert.IsFalse(documentPart.Descendants(WordMl.SdtName).Any());
        }
        public void TestActualGenerationItemIfInRepeater()
        {
            MemoryStream output;

            using (var input = AssemblyResourceHelper.GetResourceStream(this, "FaultItemIf.docx"))
            {
                output = new MemoryStream();
                var       generator = new DocxGenerator();
                XDocument data;
                using (var dataStream = AssemblyResourceHelper.GetResourceStream(this, "FaultItemIfData.xml"))
                {
                    data = XDocument.Load(dataStream);
                }
                generator.GenerateDocx(
                    input,
                    output,
                    data, new DocxGeneratorSettings()
                {
                    MissingDataMode = MissingDataMode.ThrowException
                });
            }

            var package = new DocxPackage(output);

            package.Load();

            var documentPart = package.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml;

            Console.WriteLine(documentPart);
            //Console.WriteLine(documentPart);
            Assert.IsFalse(documentPart.Descendants(WordMl.SdtName).Any());
            Assert.IsFalse(documentPart.Descendants(WordMl.ParagraphName).Descendants().Any(el => el.Name == WordMl.ParagraphName));
        }
        public void TestActualGenerationItemRepeaterNestedDemoIRInParagraph()
        {
            MemoryStream output;

            using (var input = AssemblyResourceHelper.GetResourceStream(this, "ItemRepeaterNestedDemoIRParagraph.docx"))
            {
                output = new MemoryStream();
                var       generator = new DocxGenerator();
                XDocument data;
                using (var dataStream = AssemblyResourceHelper.GetResourceStream(this, "ItemRepeaterNested2IRDemoData.xml"))
                {
                    data = XDocument.Load(dataStream);
                }
                generator.GenerateDocx(
                    input,
                    output,
                    data, new DocxGeneratorSettings()
                {
                    MissingDataMode = MissingDataMode.ThrowException
                });
            }

            var package = new DocxPackage(output);

            package.Load();

            var documentPart = package.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml;

            Console.WriteLine(documentPart.Descendants(WordMl.TableRowName).First(tr => tr.Descendants().Any(el => el.Value == "Certificate 1")));
            //Console.WriteLine(documentPart);
            Assert.IsFalse(documentPart.Descendants(WordMl.SdtName).Any());
            Assert.IsFalse(documentPart.Descendants(WordMl.ParagraphName).Descendants().Any(el => el.Name == WordMl.ParagraphName));
            Assert.IsFalse(documentPart.Descendants(WordMl.TableCellName).Elements(WordMl.ParagraphName).Any(el => el.Name.Equals(WordMl.TextRunName)));
        }
        public void TestActualGeneration()
        {
            var input = AssemblyResourceHelper.GetResourceStream(this, "DocxPackageTest.docx");

            using (input)
            {
                var          output      = new MemoryStream();
                var          generator   = new DocxGenerator();
                const string DynamicText = "My anaconda don't";
                generator.GenerateDocx(
                    input,
                    output,
                    new SomeEntityWrapper
                {
                    Test = new SomeEntity
                    {
                        Text = DynamicText
                    }
                });
                var package = new DocxPackage(output);
                package.Load();

                XDocument documentPartXml = package.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml;
                Assert.IsFalse(documentPartXml.Descendants(WordMl.SdtName).Any());
                Assert.IsNotNull(documentPartXml.Descendants(WordMl.TextRunName).Single(e => e.Value == DynamicText));
            }
        }
        public void TestActualGenerationItemHtmlContentInTable()
        {
            var input = AssemblyResourceHelper.GetResourceStream(this, "ItemHtmlContentInTable.docx");

            using (input)
            {
                var       output    = new MemoryStream();
                var       generator = new DocxGenerator();
                XDocument data;
                using (var dataStream = AssemblyResourceHelper.GetResourceStream(this, "data.xml"))
                {
                    data = XDocument.Load(dataStream);
                }
                generator.GenerateDocx(
                    input,
                    output,
                    data, new DocxGeneratorSettings()
                {
                    MissingDataMode = MissingDataMode.Ignore
                });
                var package = new DocxPackage(output);
                package.Load();

                var documentPart = package.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml;
                Assert.IsFalse(documentPart.Descendants(WordMl.SdtName).Any());
                Assert.IsFalse(documentPart.Descendants(WordMl.ParagraphName).Descendants().Any(el => el.Name == WordMl.ParagraphName));
            }
        }
Пример #15
0
 public void Init()
 {
     this.generalParser = new GeneralParser();
     using (var docStream = AssemblyResourceHelper.GetResourceStream(this, "GeneralParserTest.xml"))
     {
         this.doc = XDocument.Load(docStream);
     }
 }
Пример #16
0
 public void Initialize()
 {
     using (var docStream = AssemblyResourceHelper.GetResourceStream(this, "TableProcessorTemplateTest.xml"))
     {
         var doc = XDocument.Load(docStream);
         this.documentRoot = doc.Root.Element(WordMl.BodyName);
     }
 }
 private XmlDocument GetXmlDocument()
 {
     using (var stream = AssemblyResourceHelper.GetResourceStream(this, "DataReaderTest.xml"))
     {
         var xmlDoc = new XmlDocument();
         xmlDoc.Load(stream);
         return(xmlDoc);
     }
 }
        private void InitializeStubbedExecution()
        {
            this.docxPackageMock = new Mock <DocxPackage>();
            this.docxPackageMock.Setup(p => p.Load()).Verifiable();
            this.docxPackageMock.Setup(p => p.Save()).Verifiable();

            this.docxPackagePartMock = new Mock <DocxPackagePart>();
            var document = new XDocument();

            this.root = document.Root;

            this.docxPackagePartMock.SetupGet(p => p.PartXml).Returns(document);
            this.docxPackagePartMock.Setup(p => p.Save()).Verifiable();

            var packageParts = new List <DocxPackagePart> {
                this.docxPackagePartMock.Object
            };

            this.docxPackageMock.SetupGet(x => x.Parts).Returns(packageParts);

            var packageFactoryMock = new Mock <IDocxPackageFactory>();

            packageFactoryMock.Setup(f => f.Create(It.IsAny <Stream>())).Returns(this.docxPackageMock.Object);

            this.processorMock = new Mock <AbstractProcessor>();
            this.processorMock.Setup(p => p.Process()).Verifiable();
            this.processorMock.SetupSet(processor => processor.DataReader = It.IsAny <DataReader>()).Verifiable();
            var processorFactoryMock = new Mock <IProcessorFactory>();

            processorFactoryMock.Setup(f => f.Create()).Returns(this.processorMock.Object);

            this.parserMock = new Mock <ITagParser>();
            var parserFactoryMock = new Mock <IParserFactory>();

            parserFactoryMock.Setup(f => f.Create()).Returns(this.parserMock.Object);

            this.stringDataReaderMock   = new Mock <DataReader>();
            this.entityDataReaderMock   = new Mock <DataReader>();
            this.documentDataReaderMock = new Mock <DataReader>();
            var dataReaderFactoryMock = new Mock <IDataReaderFactory>();

            dataReaderFactoryMock.Setup(f => f.CreateReader(It.IsAny <string>())).Returns(this.stringDataReaderMock.Object);
            dataReaderFactoryMock.Setup(f => f.CreateReader(It.IsAny <A>())).Returns(this.entityDataReaderMock.Object);
            dataReaderFactoryMock.Setup(f => f.CreateReader(It.IsAny <XDocument>())).Returns(this.documentDataReaderMock.Object);

            this.docxGenerator = new DocxGenerator
            {
                DataReaderFactory = dataReaderFactoryMock.Object,
                PackageFactory    = packageFactoryMock.Object,
                ParserFactory     = parserFactoryMock.Object,
                ProcessorFactory  = processorFactoryMock.Object
            };

            this.templateStream = AssemblyResourceHelper.GetResourceStream(this, "DocxPackageTest.docx");
            this.outputStream   = new MemoryStream();
        }
Пример #19
0
        public TextTagInHeaderFooterAndDocumentTest()
        {
            var input     = AssemblyResourceHelper.GetResourceStream(this, "DocxPackageHeaderFooterTest.docx");
            var output    = new MemoryStream();
            var generator = new DocxGenerator();
            var stream    = AssemblyResourceHelper.GetResourceStream(this, "DocxPackageHeaderFooterTest_data.xml");
            var data      = XDocument.Load(stream);

            generator.GenerateDocx(input, output, data);

            _package = new DocxPackage(output).Load();
        }
Пример #20
0
        public void Initialize()
        {
            using (var docStream = AssemblyResourceHelper.GetResourceStream(this, "TableProcessorTemplateTest.xml"))
            {
                var doc = XDocument.Load(docStream);
                this.documentRoot = doc.Root.Element(WordMl.BodyName);

                var dataStream = AssemblyResourceHelper.GetResourceStream(this, "TableProcessorDataTest.xml");
                var xmlDoc     = XDocument.Load(dataStream);
                this.dataReader = DataReaderFactory.CreateReader(xmlDoc);
            }
        }
        public void TestLoadDocument()
        {
            using (var stream = AssemblyResourceHelper.GetResourceStream(this, "DocxPackageHeaderFooterTest.docx"))
            {
                var package = new DocxPackage(stream).Load();

                var expectedDocument = XDocument.Load(AssemblyResourceHelper.GetResourceStream(this, "DocxPackageHeaderFooterTest_document.xml"));

                var documentPart = package.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml;

                Assert.AreEqual(expectedDocument.ToString().Trim(), documentPart.Root.ToString().Trim());
            }
        }
Пример #22
0
 public void TestParseFooter()
 {
     using (var docStream = AssemblyResourceHelper.GetResourceStream(this, "TextParserTest_footer.xml"))
     {
         var doc           = XDocument.Load(docStream);
         var processorMock = new TagProcessorMock <TextProcessor>();
         var parser        = new TextParser();
         parser.Parse(processorMock, doc.Descendants(WordMl.SdtName).First());
         var processor = processorMock.InnerProcessor;
         var tag       = processor.TextTag;
         Assert.IsNotNull(tag);
         Assert.AreEqual("//test/text", tag.Expression);
     }
 }
 public void Initialize()
 {
     using (var docStream = AssemblyResourceHelper.GetResourceStream(this, "TableParserTest.xml"))
     {
         var doc = XDocument.Load(docStream);
         Assert.IsNotNull(doc.Root);
         this.documentRoot = doc.Root.Element(WordMl.BodyName);
     }
     using (var docStream = AssemblyResourceHelper.GetResourceStream(this, "TableParserNestedTest.xml"))
     {
         var doc = XDocument.Load(docStream);
         Assert.IsNotNull(doc.Root);
         this.nestedDocumentRoot = doc.Root.Element(WordMl.BodyName);
     }
 }
Пример #24
0
        public void Initialize()
        {
            Stream    docStream;
            XDocument doc;

            using (docStream = AssemblyResourceHelper.GetResourceStream(this, "Repeater_Ok.xml"))
            {
                doc = XDocument.Load(docStream);
                this.documentRoot = doc.Root.Element(WordMl.WordMlNamespace + "body");
            }
            using (docStream = AssemblyResourceHelper.GetResourceStream(this, "Repeater_Nested.xml"))
            {
                doc = XDocument.Load(docStream);
                this.nestedDocumentRoot = doc.Root.Element(WordMl.WordMlNamespace + "body");
            }
        }
        public void TestActualXDocumentGenerationHTMLLat1Content()
        {
            const string htmlContentText = @"
                &lt;html&gt;
		        &lt;head /&gt;
		        &lt;body&gt;
		        &lt;p&gt; Text is  &laquo;gfdgfdg &lt;/p&gt;
		        &lt;/body&gt;
		        &lt;/html&gt;
            ";
            var          input           = AssemblyResourceHelper.GetResourceStream(this, "HTMLLat1Content.docx");

            using (input)
            {
                var output    = new MemoryStream();
                var generator = new DocxGenerator();

                var data = new XDocument();
                data.Add(
                    new XElement("test", new XElement(
                                     "hypertext", htmlContentText
                                     )
                                 )
                    );

                generator.GenerateDocx(
                    input,
                    output,
                    data, new DocxGeneratorSettings()
                {
                    MissingDataMode = MissingDataMode.ThrowException
                });
                var package = new DocxPackage(output);
                package.Load();

                using (var fileStream = File.Create("C:\\xdochtmlcontent.docx"))
                {
                    output.Seek(0, SeekOrigin.Begin);
                    output.CopyTo(fileStream);
                }

                var documentPart = package.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml;
                Console.WriteLine(documentPart);
                Assert.IsFalse(documentPart.Descendants(WordMl.SdtName).Any());
                Assert.IsFalse(documentPart.Descendants(WordMl.ParagraphName).Descendants().Any(el => el.Name == WordMl.ParagraphName));
            }
        }
        public void TestParseCplx()
        {
            using (var docStream = AssemblyResourceHelper.GetResourceStream(this, "document_dontworking_if.xml"))
            {
                var doc          = XDocument.Load(docStream);
                var documentRoot = doc.Root.Element(WordMl.WordMlNamespace + "body");

                var parser          = new IfParser();
                var startElement    = TraverseUtils.TagElement(documentRoot, "If");
                var endElement      = TraverseUtils.TagElement(documentRoot, "EndIf");
                var parentProcessor = new RootProcessor();
                parser.Parse(parentProcessor, startElement);

                var          ifProcessor = (IfProcessor)parentProcessor.Processors.First();
                var          ifTag       = ifProcessor.Tag;
                const string IfCondition = "//test/condition";

                Assert.IsNotNull(ifProcessor);

                //Assert.IsTrue(IfCondition.Equals(ifTag.Conidition));
                CollectionAssert.AreEqual(Encoding.UTF8.GetBytes(IfCondition), Encoding.UTF8.GetBytes(ifTag.Conidition));

                Assert.AreEqual(startElement, ifTag.StartIf);
                Assert.AreEqual(endElement, ifTag.EndIf);

                var elements = ifTag.IfContent.ToList();

                Assert.AreEqual(1, elements.Count());

                /*
                 * Assert.IsTrue(elements.Take(6).All(e => e.Name.Equals(WordMl.TextRunName)));
                 * Assert.IsTrue(elements.Skip(6).Take(1).All(e => e.Name.Equals(WordMl.ParagraphName)));
                 * Assert.IsTrue(elements.Skip(7).Take(1).All(e => e.Name.Equals(WordMl.ParagraphPropertiesName)));
                 * Assert.IsTrue(elements.Skip(8).Take(1).All(e => e.Name.Equals(WordMl.BookmarkStartName)));
                 * Assert.IsTrue(elements.Skip(9).Take(4).All(e => e.Name.Equals(WordMl.TextRunName)));
                 * Assert.IsTrue(elements.Skip(13).Take(1).All(e => e.Name.Equals(WordMl.ProofingErrorAnchorName)));
                 * Assert.IsTrue(elements.Skip(14).Take(1).All(e => e.Name.Equals(WordMl.TextRunName)));
                 * Assert.IsTrue(elements.Skip(15).Take(1).All(e => e.Name.Equals(WordMl.ProofingErrorAnchorName)));
                 * Assert.IsTrue(elements.Skip(16).Take(1).All(e => e.Name.Equals(WordMl.TextRunName)));
                 */
                Assert.AreEqual(startElement, ifTag.StartIf);
                Assert.AreEqual(endElement, ifTag.EndIf);
            }
        }
        public void TestParse()
        {
            using (var docStream = AssemblyResourceHelper.GetResourceStream(this, "IfParserTest.xml"))
            {
                var doc          = XDocument.Load(docStream);
                var documentRoot = doc.Root.Element(WordMl.WordMlNamespace + "body");

                var parser          = new IfParser();
                var startElement    = TraverseUtils.TagElement(documentRoot, "If");
                var endElement      = TraverseUtils.TagElement(documentRoot, "EndIf");
                var parentProcessor = new RootProcessor();
                parser.Parse(parentProcessor, startElement);

                var          ifProcessor = (IfProcessor)parentProcessor.Processors.First();
                var          ifTag       = ifProcessor.Tag;
                const string IfCondition = "//test/condition";

                Assert.IsNotNull(ifProcessor);
                Assert.AreEqual(IfCondition, ifTag.Conidition);
                var content = ifTag.IfContent.ToList();
                Assert.AreEqual(1, content.Count);
                Assert.AreEqual(WordMl.ParagraphName, content[0].Name);
                var paragraphChildren = content[0].Elements().ToList();
                Assert.AreEqual(6, paragraphChildren.Count());
                Assert.AreEqual(WordMl.ParagraphPropertiesName, paragraphChildren[0].Name);
                Assert.AreEqual(WordMl.ProofingErrorAnchorName, paragraphChildren[1].Name);
                Assert.AreEqual(WordMl.TextRunName, paragraphChildren[2].Name);
                Assert.AreEqual("Hello", paragraphChildren[2].Value);
                Assert.AreEqual(WordMl.ProofingErrorAnchorName, paragraphChildren[3].Name);
                Assert.AreEqual(WordMl.TextRunName, paragraphChildren[4].Name);
                Assert.AreEqual(", World!", paragraphChildren[4].Value);
                Assert.AreEqual(WordMl.SdtName, paragraphChildren[5].Name);
                Assert.IsTrue(paragraphChildren[5].IsTag("text"));
                Assert.AreEqual(startElement, ifTag.StartIf);
                Assert.AreEqual(endElement, ifTag.EndIf);

                var textProcessor = (TextProcessor)ifProcessor.Processors.FirstOrDefault();
                Assert.IsNotNull(textProcessor);
                Assert.IsNotNull(textProcessor.TextTag);
                Assert.AreEqual("//test/text", textProcessor.TextTag.Expression);
            }
        }
        public void TestActualGenerationRepeaterInIf()
        {
            var input = AssemblyResourceHelper.GetResourceStream(this, "RepeaterInIf.docx");

            using (input)
            {
                var       output    = new MemoryStream();
                var       generator = new DocxGenerator();
                XDocument data;
                using (var dataStream = AssemblyResourceHelper.GetResourceStream(this, "dataRepeaterInIf.xml"))
                {
                    data = XDocument.Load(dataStream);
                }
                generator.GenerateDocx(
                    input,
                    output,
                    data);
                var package = new DocxPackage(output);
                package.Load();

                Assert.IsFalse(package.Parts.Single(p => p.PartUri.OriginalString.Contains("document.xml")).PartXml.Descendants(WordMl.SdtName).Any());
            }
        }
Пример #29
0
        public void TestProcessItemIfCells()
        {
            XElement root;

            using (var docStream = AssemblyResourceHelper.GetResourceStream(this, "TableItemIfsTest.xml"))
            {
                var document = XDocument.Load(docStream);
                root = document.Root.Element(WordMl.BodyName);
            }
            var tableTag = this.GetTableTag(root);

            XDocument data;

            using (var docStream = AssemblyResourceHelper.GetResourceStream(this, "TableItemIfsData.xml"))
            {
                data = XDocument.Load(docStream);
            }

            Func <XElement, IEnumerable <TableElement> > MakeTableElementCallback = element =>
            {
                var cells = element.Descendants(WordMl.TableCellName).ToList();
                ICollection <TableElement> tableElements = new Collection <TableElement>();
                foreach (var cell in cells)
                {
                    var cellTags = cell.Descendants(WordMl.SdtName).ToList();
                    var firtsIf  = new TableElement
                    {
                        IsIndex     = false,
                        IsItem      = false,
                        IsItemIf    = true,
                        StartTag    = cellTags[0],
                        EndTag      = cellTags[2],
                        Expression  = cellTags[0].Value,
                        TagElements =
                            new TableElement[]
                        {
                            new TableElement
                            {
                                IsIndex    = false,
                                IsItemIf   = false,
                                IsItem     = true,
                                StartTag   = cellTags[1],
                                Expression = cellTags[1].Value,
                            }
                        }
                    };

                    var secondIf = new TableElement
                    {
                        IsIndex     = false,
                        IsItem      = false,
                        IsItemIf    = true,
                        StartTag    = cellTags[3],
                        EndTag      = cellTags[5],
                        Expression  = cellTags[3].Value,
                        TagElements =
                            new TableElement[]
                        {
                            new TableElement
                            {
                                IsIndex    = false,
                                IsItemIf   = false,
                                IsItem     = true,
                                StartTag   = cellTags[4],
                                Expression = cellTags[4].Value,
                            }
                        }
                    };

                    tableElements.Add(firtsIf);
                    tableElements.Add(secondIf);
                }

                return(tableElements);
            };

            tableTag.MakeTableElementCallback = MakeTableElementCallback;

            var tableProcessor = new TableProcessor
            {
                TableTag   = tableTag,
                DataReader = DataReaderFactory.CreateReader(data)
            };

            tableProcessor.Process();

            var expectedTableStructure = new[]
            {
                new[] { "#", "Certificate", "Date" },
                new[] { string.Empty, string.Empty, "Issue", "Expiration" },
                new[] { "1", "2", "3", "4" },
                new[] { "row1 - value1", "row1 - value2", "row1 - value3", "row1 - value4" },
                new[] { "row2 - value1", "row2 - value2", "row2 - value3", "row2 - value4" },
                new[] { "row3 - value1", "row3 - value2", "row3 - value3", "row3 - value4" },
                new[] { "row4 - value1", "row4 - value2", "row4 - value3", "row4 - value4" },
                new[] { "row5 - value1", "row5 - value2", "row5 - value3", "row5 - value4" },
                new[] { "This", "row", "stays", "untouched" },
            };

            var rows = tableTag.Table.Elements(WordMl.TableRowName).ToList();

            Assert.AreEqual(expectedTableStructure.Count(), rows.Count());
            int rowIndex = 0;

            foreach (var row in rows)
            {
                var expectedRow = expectedTableStructure[rowIndex];

                var cellsInRow = row.Elements(WordMl.TableCellName).ToList();

                Assert.AreEqual(expectedRow.Count(), cellsInRow.Count());

                int cellIndex = 0;
                foreach (var cell in cellsInRow)
                {
                    Assert.AreEqual(expectedRow[cellIndex], cell.Value);
                    cellIndex++;
                }

                rowIndex++;
            }

            /*
             * var tagsBetween =
             *  tableTag.TagTable.ElementsAfterSelf().Where(element => element.IsBefore(tableTag.TagContent));
             * Assert.IsFalse(tagsBetween.Any());
             *
             * tagsBetween =
             *  tableTag.TagEndContent.ElementsAfterSelf().Where(element => element.IsBefore(tableTag.TagEndTable));
             * Assert.IsFalse(tagsBetween.Any());*/
            this.ValidateTagsRemoved(root);
        }
Пример #30
0
        public void TestProcessWithStaticCells()
        {
            XElement root;

            using (var docStream = AssemblyResourceHelper.GetResourceStream(this, "TableProcessorWithStaticCellsTest.xml"))
            {
                var document = XDocument.Load(docStream);
                root = document.Root.Element(WordMl.BodyName);
            }
            var tableTag = this.GetTableTag(root);

            Func <XElement, IEnumerable <TableElement> > MakeTableElementCallback = element =>
            {
                var tags = element.Descendants(WordMl.SdtName).ToList();

                var index =
                    tags.Find(
                        e =>
                        e.Element(WordMl.SdtPrName)
                        .Element(WordMl.TagName)
                        .Attribute(WordMl.ValAttributeName)
                        .Value.ToLower()
                        .Equals("itemindex"));
                var issueDate =
                    tags.Find(
                        e =>
                        e.Element(WordMl.SdtPrName)
                        .Element(WordMl.TagName)
                        .Attribute(WordMl.ValAttributeName)
                        .Value.ToLower()
                        .Equals("itemtext") && e.Element(WordMl.SdtContentName).Value == "./IssueDate");


                IEnumerable <TableElement> tableElements = new TableElement[]
                {
                    new TableElement
                    {
                        IsIndex  = true,
                        IsItem   = false,
                        IsItemIf = false,
                        StartTag = index,
                    },
                    new TableElement
                    {
                        IsItem     = true,
                        IsIndex    = false,
                        IsItemIf   = false,
                        StartTag   = issueDate,
                        Expression = "./IssueDate"
                    },
                };
                return(tableElements);
            };

            tableTag.MakeTableElementCallback = MakeTableElementCallback;

            var tableProcessor = new TableProcessor
            {
                TableTag   = tableTag,
                DataReader = this.dataReader
            };

            tableProcessor.Process();

            var expectedTableStructure = new[]
            {
                new[] { "#", "Certificate", "Date" },
                new[] { string.Empty, string.Empty, "Issue", "Expiration" },
                new[] { "1", "2", "3", "4" },
                new[] { "1", "First static text", "01.04.2014", "Second static text" },
                new[] { "2", "First static text", "01.03.2014", "Second static text" },
                new[] { "3", "First static text", "01.01.2011", "Second static text" },
                new[] { "This", "row", "stays", "untouched" },
            };

            var rows = tableTag.Table.Elements(WordMl.TableRowName).ToList();

            Assert.AreEqual(expectedTableStructure.Count(), rows.Count());
            int rowIndex = 0;

            foreach (var row in rows)
            {
                var expectedRow = expectedTableStructure[rowIndex];

                var cellsInRow = row.Elements(WordMl.TableCellName).ToList();

                Assert.AreEqual(expectedRow.Count(), cellsInRow.Count());

                int cellIndex = 0;
                foreach (var cell in cellsInRow)
                {
                    Assert.AreEqual(expectedRow[cellIndex], cell.Value);
                    cellIndex++;
                }

                rowIndex++;
            }
            this.ValidateTagsRemoved(root);
        }