@Test public void testHeaderByUsingDefaultHeadingStyle() throws Exception { IContext context = new MockContext(); // Add default style (in real context, this DefaultStyle is added by DocxStylesPreprocessor // which search // heading style from the word/styles.xml entry of the docx) DefaultStyle defaultStyle = new DefaultStyle(); defaultStyle.addHeaderStyle(1, "Heading1"); defaultStyle.addHeaderStyle(2, "Heading2"); DocxContextHelper.putDefaultStyle(context, defaultStyle); BufferedElement parent = null; ITextStylingTransformer formatter = HTMLTextStylingTransformer.INSTANCE; IDocumentHandler handler = new DocxDocumentHandler(parent, context, "word/document.xml"); formatter.transform( "xxx<h1>Title1</h1><h2>Title2</h2><h3>Title3</h3><h4>Title4</h4><h5>Title5</h5><h6>Title6</h6>", handler); Assert.assertEquals("", handler.getTextBefore()); Assert.assertEquals("<w:r><w:t xml:space=\"preserve\" >xxx</w:t></w:r>", handler.getTextBody()); Assert.assertEquals( "<w:p><w:pPr><w:pStyle w:val=\"Heading1\" /></w:pPr><w:r><w:t>Title1</w:t></w:r></w:p>" + "<w:p><w:pPr><w:pStyle w:val=\"Heading2\" /></w:pPr><w:r><w:t>Title2</w:t></w:r></w:p>" + "<w:p><w:pPr><w:pStyle w:val=\"XDocReport_Heading_3\" /></w:pPr><w:r><w:t>Title3</w:t></w:r></w:p>" + "<w:p><w:pPr><w:pStyle w:val=\"XDocReport_Heading_4\" /></w:pPr><w:r><w:t>Title4</w:t></w:r></w:p>" + "<w:p><w:pPr><w:pStyle w:val=\"XDocReport_Heading_5\" /></w:pPr><w:r><w:t>Title5</w:t></w:r></w:p>" + "<w:p><w:pPr><w:pStyle w:val=\"XDocReport_Heading_6\" /></w:pPr><w:r><w:t>Title6</w:t></w:r></w:p>", handler.getTextEnd()); }
@Test public void testUnorderedList() throws Exception { IContext context = new MockContext(); // Add default style (in real context, this DefaultStyle is added by DocxNumberingPreprocessor // which search // numbering from the word/numbering.xml entry of the docx) DefaultStyle defaultStyle = new DefaultStyle(); // defaultStyle.setNumIdForUnordererList( 1 ); DocxContextHelper.putDefaultStyle(context, defaultStyle); BufferedElement parent = null; ITextStylingTransformer formatter = HTMLTextStylingTransformer.INSTANCE; IDocumentHandler handler = new DocxDocumentHandler(parent, context, "word/document.xml"); formatter.transform( "<ol><li>item1</li><li>item2</li></ol><ul><li>item1</li><li>item2</li></ul>", handler); Assert.assertEquals("", handler.getTextBefore()); Assert.assertEquals("", handler.getTextBody()); Assert.assertEquals("", handler.getTextBody()); Assert.assertEquals( "<w:p><w:pPr><w:numPr><w:ilvl w:val=\"0\" /><w:numId w:val=\"0\" /></w:numPr></w:pPr><w:r><w:t xml:space=\"preserve\" >item1</w:t></w:r></w:p>" + "<w:p><w:pPr><w:numPr><w:ilvl w:val=\"0\" /><w:numId w:val=\"0\" /></w:numPr></w:pPr><w:r><w:t xml:space=\"preserve\" >item2</w:t></w:r></w:p>" + "<w:p><w:pPr><w:numPr><w:ilvl w:val=\"0\" /><w:numId w:val=\"1\" /></w:numPr></w:pPr><w:r><w:t xml:space=\"preserve\" >item1</w:t></w:r></w:p>" + "<w:p><w:pPr><w:numPr><w:ilvl w:val=\"0\" /><w:numId w:val=\"1\" /></w:numPr></w:pPr><w:r><w:t xml:space=\"preserve\" >item2</w:t></w:r></w:p>", handler.getTextEnd()); }
@Test public void testHyperlinkByUsingDefaultHyperlinkStyle() throws Exception { IContext context = new MockContext(); // Add default style (in real context, this DefaultStyle is added by DocxStylesPreprocessor // which search // hyperlink style from the word/styles.xml entry of the docx) DefaultStyle defaultStyle = new DefaultStyle(); defaultStyle.setHyperLinkStyleId("DefaultHyperlink"); DocxContextHelper.putDefaultStyle(context, defaultStyle); BufferedElement parent = null; ITextStylingTransformer formatter = HTMLTextStylingTransformer.INSTANCE; IDocumentHandler handler = new DocxDocumentHandler(parent, context, "word/document.xml"); formatter.transform( "<a href=\"http://code.google.com/p/xdocreport/\" >XDocReport</a>", handler); Assert.assertEquals("", handler.getTextBefore()); Assert.assertEquals( "<w:hyperlink r:id=\"___rId0\" w:history=\"1\"> <w:proofErr w:type=\"spellStart\" /><w:r w:rsidRPr=\"001D30B5\"><w:rPr><w:rStyle w:val=\"DefaultHyperlink\" /></w:rPr><w:t>XDocReport</w:t></w:r><w:proofErr w:type=\"spellEnd\" /></w:hyperlink>", handler.getTextBody()); Assert.assertEquals("", handler.getTextEnd()); HyperlinkRegistry registry = DocxContextHelper.getHyperlinkRegistry(context, "word/document.xml"); Assert.assertNotNull(registry); Assert.assertEquals(1, registry.getHyperlinks().size()); HyperlinkInfo hyperlinkInfo = registry.getHyperlinks().get(0); Assert.assertEquals("___rId0", hyperlinkInfo.getId()); Assert.assertEquals("http://code.google.com/p/xdocreport/", hyperlinkInfo.getTarget()); Assert.assertEquals("External", hyperlinkInfo.getTargetMode()); }
@Test public void testHyperlinkByUsingXDocReport_HyperlinkStyle() throws Exception { IContext context = new MockContext(); BufferedElement parent = null; ITextStylingTransformer formatter = HTMLTextStylingTransformer.INSTANCE; IDocumentHandler handler = new DocxDocumentHandler(parent, context, "word/document.xml"); formatter.transform( "<a href=\"http://code.google.com/p/xdocreport/\" >XDocReport</a>", handler); Assert.assertEquals("", handler.getTextBefore()); Assert.assertEquals( "<w:hyperlink r:id=\"___rId0\" w:history=\"1\"> <w:proofErr w:type=\"spellStart\" /><w:r w:rsidRPr=\"001D30B5\"><w:rPr><w:rStyle w:val=\"XDocReport_Hyperlink\" /></w:rPr><w:t>XDocReport</w:t></w:r><w:proofErr w:type=\"spellEnd\" /></w:hyperlink>", handler.getTextBody()); Assert.assertEquals("", handler.getTextEnd()); HyperlinkRegistry registry = DocxContextHelper.getHyperlinkRegistry(context, "word/document.xml"); Assert.assertNotNull(registry); Assert.assertEquals(1, registry.getHyperlinks().size()); HyperlinkInfo hyperlinkInfo = registry.getHyperlinks().get(0); Assert.assertEquals("___rId0", hyperlinkInfo.getId()); Assert.assertEquals("http://code.google.com/p/xdocreport/", hyperlinkInfo.getTarget()); Assert.assertEquals("External", hyperlinkInfo.getTargetMode()); }
@Test public void testItalicWithI() throws Exception { IContext context = new MockContext(); BufferedElement parent = null; ITextStylingTransformer formatter = HTMLTextStylingTransformer.INSTANCE; IDocumentHandler handler = new DocxDocumentHandler(parent, context, "word/document.xml"); formatter.transform("<i>text</i>", handler); Assert.assertEquals("", handler.getTextBefore()); Assert.assertEquals( "<w:r><w:rPr><w:i /></w:rPr><w:t xml:space=\"preserve\" >text</w:t></w:r>", handler.getTextBody()); Assert.assertEquals("", handler.getTextEnd()); }
@Test public void testAll() throws Exception { IContext context = new MockContext(); BufferedElement parent = null; context.put("comments_html", "<b>Bold</b> text"); ITextStylingTransformer formatter = HTMLTextStylingTransformer.INSTANCE; IDocumentHandler handler = new DocxDocumentHandler(parent, context, "word/document.xml"); formatter.transform( "<p>\r\n\tHere are severals styles :</p>\r\n<ul>\r\n\t<li>\r\n\t\t<strong>Bold</strong> style.</li>\r\n\t<li>\r\n\t\t<em>Italic</em> style.</li>\r\n\t<li>\r\n\t\t<strong><em>BoldAndItalic</em></strong> style.</li>\r\n</ul>\r\n<p>\r\n\tHere are 3 styles :</p>\r\n<ol>\r\n\t<li>\r\n\t\t<strong>Bold</strong> style.</li>\r\n\t<li>\r\n\t\t<em>Italic</em> style.</li>\r\n\t<li>\r\n\t\t<strong><em>BoldAndItalic</em></strong> style.</li>\r\n</ol>\r\n<p>\r\n\tXDocReport can manage thoses styles.</p>\r\n<h1>\r\n\tsqsq</h1>\r\n<p>\r\n\tzazazaa</p>\r\n ", handler); // System.err.println(handler.getTextEnd()); }
@Test public void testParagraph() throws Exception { IContext context = new MockContext(); BufferedElement parent = null; ITextStylingTransformer formatter = HTMLTextStylingTransformer.INSTANCE; IDocumentHandler handler = new DocxDocumentHandler(parent, context, "word/document.xml"); formatter.transform("some <strong>text</strong><p>paragraph1</p><p>paragraph2</p>", handler); Assert.assertEquals("", handler.getTextBefore()); Assert.assertEquals( "<w:r><w:t xml:space=\"preserve\" >some </w:t></w:r><w:r><w:rPr><w:b /></w:rPr><w:t xml:space=\"preserve\" >text</w:t></w:r>", handler.getTextBody()); Assert.assertEquals( "<w:p><w:r><w:t xml:space=\"preserve\" >paragraph1</w:t></w:r></w:p><w:p><w:r><w:t xml:space=\"preserve\" >paragraph2</w:t></w:r></w:p>", handler.getTextEnd()); }
@Test public void testHeaderAndTextInParagraph() throws Exception { IContext context = new MockContext(); BufferedElement parent = null; ITextStylingTransformer formatter = HTMLTextStylingTransformer.INSTANCE; IDocumentHandler handler = new DocxDocumentHandler(parent, context, "word/document.xml"); formatter.transform("<h1>Title1</h1><p>paragraph1</p>", handler); Assert.assertEquals("", handler.getTextBefore()); Assert.assertEquals("", handler.getTextBody()); Assert.assertEquals( "<w:p><w:pPr><w:pStyle w:val=\"XDocReport_Heading_1\" /></w:pPr><w:r><w:t>Title1</w:t></w:r></w:p>" + "<w:p><w:r><w:t xml:space=\"preserve\" >paragraph1</w:t></w:r></w:p>", handler.getTextEnd()); }
@Test public void testHeaderByUsingXDocReport_HeadingStyle() throws Exception { IContext context = new MockContext(); BufferedElement parent = null; ITextStylingTransformer formatter = HTMLTextStylingTransformer.INSTANCE; IDocumentHandler handler = new DocxDocumentHandler(parent, context, "word/document.xml"); formatter.transform( "xxx<h1>Title1</h1><h2>Title2</h2><h3>Title3</h3><h4>Title4</h4><h5>Title5</h5><h6>Title6</h6>", handler); Assert.assertEquals("", handler.getTextBefore()); Assert.assertEquals("<w:r><w:t xml:space=\"preserve\" >xxx</w:t></w:r>", handler.getTextBody()); Assert.assertEquals( "<w:p><w:pPr><w:pStyle w:val=\"XDocReport_Heading_1\" /></w:pPr><w:r><w:t>Title1</w:t></w:r></w:p>" + "<w:p><w:pPr><w:pStyle w:val=\"XDocReport_Heading_2\" /></w:pPr><w:r><w:t>Title2</w:t></w:r></w:p>" + "<w:p><w:pPr><w:pStyle w:val=\"XDocReport_Heading_3\" /></w:pPr><w:r><w:t>Title3</w:t></w:r></w:p>" + "<w:p><w:pPr><w:pStyle w:val=\"XDocReport_Heading_4\" /></w:pPr><w:r><w:t>Title4</w:t></w:r></w:p>" + "<w:p><w:pPr><w:pStyle w:val=\"XDocReport_Heading_5\" /></w:pPr><w:r><w:t>Title5</w:t></w:r></w:p>" + "<w:p><w:pPr><w:pStyle w:val=\"XDocReport_Heading_6\" /></w:pPr><w:r><w:t>Title6</w:t></w:r></w:p>", handler.getTextEnd()); }