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.

TestXWPFWordExtractor.java 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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.extractor;
  16. import static org.apache.poi.POITestCase.assertContains;
  17. import static org.apache.poi.POITestCase.assertEndsWith;
  18. import static org.apache.poi.POITestCase.assertNotContained;
  19. import static org.apache.poi.POITestCase.assertStartsWith;
  20. import static org.junit.jupiter.api.Assertions.assertEquals;
  21. import static org.junit.jupiter.api.Assertions.assertFalse;
  22. import static org.junit.jupiter.api.Assertions.assertNotNull;
  23. import static org.junit.jupiter.api.Assertions.assertTrue;
  24. import java.io.IOException;
  25. import java.util.Locale;
  26. import java.util.regex.Matcher;
  27. import java.util.regex.Pattern;
  28. import org.apache.poi.util.StringUtil;
  29. import org.apache.poi.xwpf.XWPFTestDataSamples;
  30. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  31. import org.junit.jupiter.api.Test;
  32. /**
  33. * Tests for HXFWordExtractor
  34. */
  35. class TestXWPFWordExtractor {
  36. /**
  37. * Get text out of the simple file
  38. */
  39. @Test
  40. void testGetSimpleText() throws IOException {
  41. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("sample.docx");
  42. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  43. String text = extractor.getText();
  44. assertTrue(text.length() > 0);
  45. // Check contents
  46. assertStartsWith(text,
  47. "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nunc at risus vel erat tempus posuere. Aenean non ante. Suspendisse vehicula dolor sit amet odio."
  48. );
  49. assertEndsWith(text,
  50. "Phasellus ultricies mi nec leo. Sed tempus. In sit amet lorem at velit faucibus vestibulum.\n"
  51. );
  52. // Check number of paragraphs by counting number of newlines
  53. int numberOfParagraphs = StringUtil.countMatches(text, '\n');
  54. assertEquals(3, numberOfParagraphs);
  55. }
  56. }
  57. /**
  58. * Tests getting the text out of a complex file
  59. */
  60. @Test
  61. void testGetComplexText() throws IOException {
  62. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("IllustrativeCases.docx");
  63. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  64. String text = extractor.getText();
  65. assertTrue(text.length() > 0);
  66. char euro = '\u20ac';
  67. // Check contents
  68. assertStartsWith(text,
  69. " \n(V) ILLUSTRATIVE CASES\n\n"
  70. );
  71. assertContains(text,
  72. "As well as gaining " + euro + "90 from child benefit increases, he will also receive the early childhood supplement of " + euro + "250 per quarter for Vincent for the full four quarters of the year.\n\n\n\n"// \n\n\n"
  73. );
  74. assertEndsWith(text,
  75. "11.4%\t\t90\t\t\t\t\t250\t\t1,310\t\n\n \n\n\n"
  76. );
  77. // Check number of paragraphs by counting number of newlines
  78. int numberOfParagraphs = StringUtil.countMatches(text, '\n');
  79. assertEquals(134, numberOfParagraphs);
  80. }
  81. }
  82. @Test
  83. void testGetWithHyperlinks() throws IOException {
  84. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx");
  85. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  86. // Now check contents
  87. extractor.setFetchHyperlinks(false);
  88. assertEquals(
  89. "This is a test document.\nThis bit is in bold and italic\n" +
  90. "Back to normal\n" +
  91. "This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.\n" +
  92. "We have a hyperlink here, and another.\n",
  93. extractor.getText()
  94. );
  95. // One hyperlink is a real one, one is just to the top of page
  96. extractor.setFetchHyperlinks(true);
  97. assertEquals(
  98. "This is a test document.\nThis bit is in bold and italic\n" +
  99. "Back to normal\n" +
  100. "This contains BOLD, ITALIC and BOTH, as well as RED and YELLOW text.\n" +
  101. "We have a hyperlink <http://poi.apache.org/> here, and another.\n",
  102. extractor.getText()
  103. );
  104. }
  105. }
  106. @Test
  107. void testHeadersFooters() throws IOException {
  108. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("ThreeColHeadFoot.docx");
  109. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  110. assertEquals(
  111. "First header column!\tMid header\tRight header!\n" +
  112. "This is a sample word document. It has two pages. It has a three column heading, and a three column footer\n" +
  113. "\n" +
  114. "HEADING TEXT\n" +
  115. "\n" +
  116. "More on page one\n" +
  117. "\n\n" +
  118. "End of page 1\n\n\n" +
  119. "This is page two. It also has a three column heading, and a three column footer.\n" +
  120. "Footer Left\tFooter Middle\tFooter Right\n",
  121. extractor.getText()
  122. );
  123. }
  124. // Now another file, expect multiple headers
  125. // and multiple footers
  126. try (XWPFDocument doc2 = XWPFTestDataSamples.openSampleDocument("DiffFirstPageHeadFoot.docx")) {
  127. new XWPFWordExtractor(doc2).close();
  128. try (XWPFWordExtractor extractor = new XWPFWordExtractor(doc2)) {
  129. extractor.getText();
  130. assertEquals(
  131. "I am the header on the first page, and I" + '\u2019' + "m nice and simple\n" +
  132. "First header column!\tMid header\tRight header!\n" +
  133. "This is a sample word document. It has two pages. It has a simple header and footer, which is different to all the other pages.\n" +
  134. "\n" +
  135. "HEADING TEXT\n" +
  136. "\n" +
  137. "More on page one\n" +
  138. "\n\n" +
  139. "End of page 1\n\n\n" +
  140. "This is page two. It also has a three column heading, and a three column footer.\n" +
  141. "The footer of the first page\n" +
  142. "Footer Left\tFooter Middle\tFooter Right\n",
  143. extractor.getText()
  144. );
  145. }
  146. }
  147. }
  148. @Test
  149. void testFootnotes() throws IOException {
  150. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("footnotes.docx");
  151. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  152. String text = extractor.getText();
  153. assertContains(text, "snoska");
  154. assertContains(text, "Eto ochen prostoy[footnoteRef:1] text so snoskoy");
  155. }
  156. }
  157. @Test
  158. void testTableFootnotes() throws IOException {
  159. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("table_footnotes.docx");
  160. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  161. assertContains(extractor.getText(), "snoska");
  162. }
  163. }
  164. @Test
  165. void testFormFootnotes() throws IOException {
  166. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("form_footnotes.docx");
  167. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  168. String text = extractor.getText();
  169. assertContains(text, "testdoc");
  170. assertContains(text, "test phrase");
  171. }
  172. }
  173. @Test
  174. void testEndnotes() throws IOException {
  175. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("endnotes.docx");
  176. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  177. String text = extractor.getText();
  178. assertContains(text, "XXX");
  179. assertContains(text, "tilaka [endnoteRef:2]or 'tika'");
  180. }
  181. }
  182. @Test
  183. void testInsertedDeletedText() throws IOException {
  184. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("delins.docx");
  185. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  186. assertContains(extractor.getText(), "pendant worn");
  187. assertContains(extractor.getText(), "extremely well");
  188. }
  189. }
  190. @Test
  191. void testParagraphHeader() throws IOException {
  192. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Headers.docx");
  193. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  194. assertContains(extractor.getText(), "Section 1");
  195. assertContains(extractor.getText(), "Section 2");
  196. assertContains(extractor.getText(), "Section 3");
  197. }
  198. }
  199. /**
  200. * Test that we can open and process .docm
  201. * (macro enabled) docx files (bug #45690)
  202. */
  203. @Test
  204. void testDOCMFiles() throws IOException {
  205. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("45690.docm");
  206. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  207. assertContains(extractor.getText(), "2004");
  208. assertContains(extractor.getText(), "2008");
  209. assertContains(extractor.getText(), "(120 ");
  210. }
  211. }
  212. /**
  213. * Test that we handle things like tabs and
  214. * carriage returns properly in the text that
  215. * we're extracting (bug #49189)
  216. */
  217. @Test
  218. void testDocTabs() throws IOException {
  219. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("WithTabs.docx");
  220. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  221. // Check bits
  222. assertContains(extractor.getText(), "a");
  223. assertContains(extractor.getText(), "\t");
  224. assertContains(extractor.getText(), "b");
  225. // Now check the first paragraph in total
  226. assertContains(extractor.getText(), "a\tb\n");
  227. }
  228. }
  229. /**
  230. * The output should not contain field codes, e.g. those specified in the
  231. * w:instrText tag (spec sec. 17.16.23)
  232. */
  233. @Test
  234. void testNoFieldCodes() throws IOException {
  235. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("FieldCodes.docx");
  236. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  237. String text = extractor.getText();
  238. assertTrue(text.length() > 0);
  239. assertFalse(text.contains("AUTHOR"));
  240. assertFalse(text.contains("CREATEDATE"));
  241. }
  242. }
  243. /**
  244. * The output should contain the values of simple fields, those specified
  245. * with the fldSimple element (spec sec. 17.16.19)
  246. */
  247. @Test
  248. void testFldSimpleContent() throws IOException {
  249. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("FldSimple.docx");
  250. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  251. String text = extractor.getText();
  252. assertTrue(text.length() > 0);
  253. assertContains(text, "FldSimple.docx");
  254. }
  255. }
  256. /**
  257. * Test for parsing document with drawings to prevent
  258. * NoClassDefFoundError for CTAnchor in XWPFRun
  259. */
  260. @Test
  261. void testDrawings() throws IOException {
  262. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("drawing.docx");
  263. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  264. String text = extractor.getText();
  265. assertTrue(text.length() > 0);
  266. }
  267. }
  268. /**
  269. * Test for basic extraction of SDT content
  270. */
  271. @Test
  272. void testSimpleControlContent() throws IOException {
  273. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("Bug54849.docx");
  274. XWPFWordExtractor ex = new XWPFWordExtractor(doc)) {
  275. String[] targs = new String[]{
  276. "header_rich_text",
  277. "rich_text",
  278. "rich_text_pre_table\nrich_text_cell1\t\t\t\n\t\t\t\n\t\t\t\n\nrich_text_post_table",
  279. "plain_text_no_newlines",
  280. "plain_text_with_newlines1\nplain_text_with_newlines2\n",
  281. "watermelon\n",
  282. "dirt\n",
  283. "4/16/2013\n",
  284. "rich_text_in_cell",
  285. "abc",
  286. "rich_text_in_paragraph_in_cell",
  287. "footer_rich_text",
  288. "footnote_sdt",
  289. "endnote_sdt"
  290. };
  291. String s = ex.getText().toLowerCase(Locale.ROOT);
  292. int hits = 0;
  293. for (String targ : targs) {
  294. boolean hit = false;
  295. if (s.contains(targ)) {
  296. hit = true;
  297. hits++;
  298. }
  299. assertTrue(hit, "controlled content loading-" + targ);
  300. }
  301. assertEquals(targs.length, hits, "controlled content loading hit count");
  302. }
  303. try (XWPFDocument doc2 = XWPFTestDataSamples.openSampleDocument("Bug54771a.docx");
  304. XWPFWordExtractor ex = new XWPFWordExtractor(doc2)) {
  305. String s = ex.getText().toLowerCase(Locale.ROOT);
  306. String[] targs = {
  307. "bb",
  308. "test subtitle\n",
  309. "test user\n",
  310. };
  311. //At one point in development there were three copies of the text.
  312. //This ensures that there is only one copy.
  313. for (String targ : targs) {
  314. Matcher m = Pattern.compile(targ).matcher(s);
  315. int hit = 0;
  316. while (m.find()) {
  317. hit++;
  318. }
  319. assertEquals(1, hit, "controlled content loading-" + targ);
  320. }
  321. //"test\n" appears twice: once as the "title" and once in the text.
  322. //This also happens when you save this document as text from MSWord.
  323. Matcher m = Pattern.compile("test\n").matcher(s);
  324. int hit = 0;
  325. while (m.find()) {
  326. hit++;
  327. }
  328. assertEquals(2, hit, "test<N>");
  329. }
  330. }
  331. /**
  332. * No Header or Footer in document
  333. */
  334. @Test
  335. void testBug55733() throws Exception {
  336. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("55733.docx");
  337. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  338. // Check it gives text without error
  339. assertNotNull(extractor.getText());
  340. }
  341. }
  342. @Test
  343. void testCheckboxes() throws IOException {
  344. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("checkboxes.docx");
  345. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  346. assertEquals("This is a small test for checkboxes \nunchecked: |_| \n" +
  347. "Or checked: |X|\n\n\n\n\n" +
  348. "Test a checkbox within a textbox: |_| -> |X|\n\n\n" +
  349. "In Table:\n|_|\t|X|\n\n\n" +
  350. "In Sequence:\n|X||_||X|\n", extractor.getText());
  351. }
  352. }
  353. @Test
  354. void testMultipleBodyBug() throws IOException {
  355. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("MultipleBodyBug.docx");
  356. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  357. assertEquals("START BODY 1 The quick, brown fox jumps over a lazy dog. END BODY 1.\n"
  358. + "START BODY 2 The quick, brown fox jumps over a lazy dog. END BODY 2.\n"
  359. + "START BODY 3 The quick, brown fox jumps over a lazy dog. END BODY 3.\n",
  360. extractor.getText());
  361. }
  362. }
  363. @Test
  364. void testPhonetic() throws IOException {
  365. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("61470.docx")) {
  366. try (XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  367. //expect: baseText (phoneticText)
  368. assertEquals("\u6771\u4EAC (\u3068\u3046\u304D\u3087\u3046)", extractor.getText().trim());
  369. }
  370. try (XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  371. extractor.setConcatenatePhoneticRuns(false);
  372. assertEquals("\u6771\u4EAC", extractor.getText().trim());
  373. }
  374. }
  375. }
  376. @Test
  377. void testCTPictureBase() throws IOException {
  378. //This forces ctpicturebase to be included in the poi-ooxml-lite jar
  379. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("61991.docx");
  380. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  381. String txt = extractor.getText();
  382. assertContains(txt, "Sequencing data");
  383. }
  384. }
  385. @Test
  386. void testGlossary() throws IOException {
  387. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("60316.dotx")) {
  388. XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
  389. String txt = extractor.getText();
  390. assertContains(txt, "Getting the perfect");
  391. //this content appears only in the glossary document
  392. //once we add processing for this, we can change this to contains
  393. assertNotContained(txt, "table rows");
  394. }
  395. }
  396. @Test
  397. void testPartsInTemplate() throws IOException {
  398. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("60316b.dotx")) {
  399. XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
  400. String txt = extractor.getText();
  401. assertContains(txt, "header 2");
  402. assertContains(txt, "footer 1");
  403. }
  404. }
  405. @Test
  406. void bug55966() throws IOException {
  407. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("55966.docx")) {
  408. String expected = "Content control within a paragraph is here text content from within a paragraph second control with a new\n" +
  409. "line\n" +
  410. "\n" +
  411. "Content control that is the entire paragraph\n";
  412. XWPFWordExtractor extractedDoc = new XWPFWordExtractor(doc);
  413. String actual = extractedDoc.getText();
  414. extractedDoc.close();
  415. assertEquals(expected, actual);
  416. }
  417. }
  418. @Test
  419. void testCapitalizedFlag() throws IOException {
  420. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("capitalized.docx");
  421. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  422. String txt = extractor.getText();
  423. assertEquals( "The following word is: CAPITALIZED.", txt.trim());
  424. }
  425. }
  426. @Test
  427. void testTika2163() throws IOException {
  428. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("ChronologicalResume.dotx");
  429. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  430. String txt = extractor.getText();
  431. assertContains(txt, "but a great-looking résumé doesn’t have to be!");
  432. }
  433. }
  434. @Test
  435. void testTika3816() throws IOException {
  436. try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("tika-3816.docx");
  437. XWPFWordExtractor extractor = new XWPFWordExtractor(doc)) {
  438. String txt = extractor.getText();
  439. assertContains(txt, "Note\tDetails");
  440. }
  441. }
  442. }