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 19KB

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