You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXWPFSDT.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xwpf.usermodel;
  16. import static org.apache.poi.POITestCase.assertContains;
  17. import static org.junit.jupiter.api.Assertions.assertEquals;
  18. import static org.junit.jupiter.api.Assertions.assertTrue;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import org.apache.poi.xwpf.XWPFTestDataSamples;
  23. import org.junit.jupiter.api.Test;
  24. public final class TestXWPFSDT {
  25. /**
  26. * Test text extraction from nested SDTs
  27. */
  28. @Test
  29. void testNestedSDTs() throws Exception {
  30. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug64561.docx")) {
  31. XWPFAbstractSDT sdt = extractAllSDTs(doc).get(0);
  32. assertEquals("Subject", sdt.getContent().getText(), "extracted text");
  33. }
  34. }
  35. /**
  36. * Test simple tag and title extraction from SDT
  37. */
  38. @Test
  39. void testTagTitle() throws Exception {
  40. try (XWPFDocument doc =XWPFTestDataSamples.openSampleDocument("Bug54849.docx")) {
  41. String tag = null;
  42. String title = null;
  43. List<XWPFAbstractSDT> sdts = extractAllSDTs(doc);
  44. for (XWPFAbstractSDT sdt : sdts) {
  45. if (sdt.getContent().toString().equals("Rich_text")) {
  46. tag = "MyTag";
  47. title = "MyTitle";
  48. break;
  49. }
  50. }
  51. assertEquals(13, sdts.size(), "controls size");
  52. assertEquals("MyTag", tag, "tag");
  53. assertEquals("MyTitle", title, "title");
  54. }
  55. }
  56. @Test
  57. void testGetSDTs() throws Exception {
  58. String[] contents = new String[]{
  59. "header_rich_text",
  60. "Rich_text",
  61. "Rich_text_pre_table\nRich_text_cell1\t\t\t\n\t\t\t\n\t\t\t\n\nRich_text_post_table",
  62. "Plain_text_no_newlines",
  63. "Plain_text_with_newlines1\nplain_text_with_newlines2",
  64. "Watermelon",
  65. "Dirt",
  66. "4/16/2013",
  67. "Rich_text_in_cell",
  68. "rich_text_in_paragraph_in_cell",
  69. "Footer_rich_text",
  70. "Footnote_sdt",
  71. "Endnote_sdt"
  72. };
  73. try (XWPFDocument doc =XWPFTestDataSamples.openSampleDocument("Bug54849.docx")) {
  74. List<XWPFAbstractSDT> sdts = extractAllSDTs(doc);
  75. assertEquals(contents.length, sdts.size(), "number of sdts");
  76. for (int i = 0; i < contents.length; i++) {
  77. XWPFAbstractSDT sdt = sdts.get(i);
  78. assertEquals(contents[i], sdt.getContent().toString(), i + ": " + contents[i]);
  79. }
  80. }
  81. }
  82. /**
  83. * POI-54771 and TIKA-1317
  84. */
  85. @Test
  86. void testSDTAsCell() throws Exception {
  87. //Bug54771a.docx and Bug54771b.docx test slightly
  88. //different recursion patterns. Keep both!
  89. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54771a.docx")) {
  90. List<XWPFAbstractSDT> sdts = extractAllSDTs(doc);
  91. String text = sdts.get(0).getContent().getText();
  92. assertEquals(2, sdts.size());
  93. assertContains(text, "Test");
  94. text = sdts.get(1).getContent().getText();
  95. assertContains(text, "Test Subtitle");
  96. assertContains(text, "Test User");
  97. assertTrue(text.indexOf("Test") < text.indexOf("Test Subtitle"));
  98. }
  99. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54771b.docx")) {
  100. List<XWPFAbstractSDT> sdts = extractAllSDTs(doc);
  101. assertEquals(3, sdts.size());
  102. assertContains(sdts.get(0).getContent().getText(), "Test");
  103. assertContains(sdts.get(1).getContent().getText(), "Test Subtitle");
  104. assertContains(sdts.get(2).getContent().getText(), "Test User");
  105. }
  106. }
  107. /**
  108. * POI-55142 and Tika 1130
  109. */
  110. @Test
  111. void testNewLinesBetweenRuns() throws Exception {
  112. try (XWPFDocument doc =XWPFTestDataSamples.openSampleDocument("Bug55142.docx")) {
  113. List<XWPFAbstractSDT> sdts = extractAllSDTs(doc);
  114. List<String> targs = new ArrayList<>();
  115. //these test newlines and tabs in paragraphs/body elements
  116. targs.add("Rich-text1 abcdefghi");
  117. targs.add("Rich-text2 abcd\t\tefgh");
  118. targs.add("Rich-text3 abcd\nefg");
  119. targs.add("Rich-text4 abcdefg");
  120. targs.add("Rich-text5 abcdefg\nhijk");
  121. targs.add("Plain-text1 abcdefg");
  122. targs.add("Plain-text2 abcdefg\nhijk\nlmnop");
  123. //this tests consecutive runs within a cell (not a paragraph)
  124. //this test case was triggered by Tika-1130
  125. targs.add("sdt_incell2 abcdefg");
  126. for (int i = 0; i < sdts.size(); i++) {
  127. XWPFAbstractSDT sdt = sdts.get(i);
  128. assertEquals(targs.get(i), sdt.getContent().getText());
  129. }
  130. }
  131. }
  132. @Test
  133. void test60341() throws IOException {
  134. //handle sdtbody without an sdtpr
  135. try (XWPFDocument doc =XWPFTestDataSamples.openSampleDocument("Bug60341.docx")) {
  136. List<XWPFAbstractSDT> sdts = extractAllSDTs(doc);
  137. assertEquals(1, sdts.size());
  138. assertEquals("", sdts.get(0).getTag());
  139. assertEquals("", sdts.get(0).getTitle());
  140. }
  141. }
  142. @Test
  143. void test62859() throws IOException {
  144. //this doesn't test the exact code path for this issue, but
  145. //it does test for a related issue, and the fix fixes both.
  146. //We should try to add the actual triggering document
  147. //to our test suite.
  148. try (XWPFDocument doc =XWPFTestDataSamples.openSampleDocument("Bug62859.docx")) {
  149. List<XWPFAbstractSDT> sdts = extractAllSDTs(doc);
  150. assertEquals(1, sdts.size());
  151. assertEquals("", sdts.get(0).getTag());
  152. assertEquals("", sdts.get(0).getTitle());
  153. }
  154. }
  155. private List<XWPFAbstractSDT> extractAllSDTs(XWPFDocument doc) {
  156. List<XWPFAbstractSDT> sdts = new ArrayList<>();
  157. List<XWPFHeader> headers = doc.getHeaderList();
  158. for (XWPFHeader header : headers) {
  159. sdts.addAll(extractSDTsFromBodyElements(header.getBodyElements()));
  160. }
  161. sdts.addAll(extractSDTsFromBodyElements(doc.getBodyElements()));
  162. List<XWPFFooter> footers = doc.getFooterList();
  163. for (XWPFFooter footer : footers) {
  164. sdts.addAll(extractSDTsFromBodyElements(footer.getBodyElements()));
  165. }
  166. for (XWPFFootnote footnote : doc.getFootnotes()) {
  167. sdts.addAll(extractSDTsFromBodyElements(footnote.getBodyElements()));
  168. }
  169. for (XWPFEndnote footnote : doc.getEndnotes()) {
  170. sdts.addAll(extractSDTsFromBodyElements(footnote.getBodyElements()));
  171. }
  172. return sdts;
  173. }
  174. private List<XWPFAbstractSDT> extractSDTsFromBodyElements(List<IBodyElement> elements) {
  175. List<XWPFAbstractSDT> sdts = new ArrayList<>();
  176. for (IBodyElement e : elements) {
  177. if (e instanceof XWPFSDT) {
  178. XWPFSDT sdt = (XWPFSDT) e;
  179. sdts.add(sdt);
  180. } else if (e instanceof XWPFParagraph) {
  181. XWPFParagraph p = (XWPFParagraph) e;
  182. for (IRunElement e2 : p.getIRuns()) {
  183. if (e2 instanceof XWPFSDT) {
  184. XWPFSDT sdt = (XWPFSDT) e2;
  185. sdts.add(sdt);
  186. }
  187. }
  188. } else if (e instanceof XWPFTable) {
  189. XWPFTable table = (XWPFTable) e;
  190. sdts.addAll(extractSDTsFromTable(table));
  191. }
  192. }
  193. return sdts;
  194. }
  195. private List<XWPFAbstractSDT> extractSDTsFromTable(XWPFTable table) {
  196. List<XWPFAbstractSDT> sdts = new ArrayList<>();
  197. for (XWPFTableRow r : table.getRows()) {
  198. for (ICell c : r.getTableICells()) {
  199. if (c instanceof XWPFSDTCell) {
  200. sdts.add((XWPFSDTCell) c);
  201. } else if (c instanceof XWPFTableCell) {
  202. sdts.addAll(extractSDTsFromBodyElements(((XWPFTableCell) c).getBodyElements()));
  203. }
  204. }
  205. }
  206. return sdts;
  207. }
  208. }