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.

XWPFWordExtractor.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 java.io.IOException;
  17. import java.util.List;
  18. import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
  19. import org.apache.poi.openxml4j.opc.OPCPackage;
  20. import org.apache.poi.xwpf.model.XWPFCommentsDecorator;
  21. import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
  22. import org.apache.poi.xwpf.usermodel.IBodyElement;
  23. import org.apache.poi.xwpf.usermodel.ICell;
  24. import org.apache.poi.xwpf.usermodel.IRunElement;
  25. import org.apache.poi.xwpf.usermodel.XWPFDocument;
  26. import org.apache.poi.xwpf.usermodel.XWPFHyperlink;
  27. import org.apache.poi.xwpf.usermodel.XWPFHyperlinkRun;
  28. import org.apache.poi.xwpf.usermodel.XWPFParagraph;
  29. import org.apache.poi.xwpf.usermodel.XWPFRelation;
  30. import org.apache.poi.xwpf.usermodel.XWPFRun;
  31. import org.apache.poi.xwpf.usermodel.XWPFSDT;
  32. import org.apache.poi.xwpf.usermodel.XWPFSDTCell;
  33. import org.apache.poi.xwpf.usermodel.XWPFTable;
  34. import org.apache.poi.xwpf.usermodel.XWPFTableCell;
  35. import org.apache.poi.xwpf.usermodel.XWPFTableRow;
  36. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
  37. /**
  38. * Helper class to extract text from an OOXML Word file
  39. */
  40. public class XWPFWordExtractor implements POIXMLTextExtractor {
  41. public static final XWPFRelation[] SUPPORTED_TYPES = {
  42. XWPFRelation.DOCUMENT, XWPFRelation.TEMPLATE,
  43. XWPFRelation.MACRO_DOCUMENT,
  44. XWPFRelation.MACRO_TEMPLATE_DOCUMENT
  45. };
  46. private final XWPFDocument document;
  47. private boolean fetchHyperlinks;
  48. private boolean concatenatePhoneticRuns = true;
  49. private boolean doCloseFilesystem = true;
  50. public XWPFWordExtractor(OPCPackage container) throws IOException {
  51. this(new XWPFDocument(container));
  52. }
  53. public XWPFWordExtractor(XWPFDocument document) {
  54. this.document = document;
  55. }
  56. /**
  57. * Should we also fetch the hyperlinks, when fetching
  58. * the text content? Default is to only output the
  59. * hyperlink label, and not the contents
  60. */
  61. public void setFetchHyperlinks(boolean fetch) {
  62. fetchHyperlinks = fetch;
  63. }
  64. /**
  65. * Should we concatenate phonetic runs in extraction. Default is <code>true</code>
  66. * @param concatenatePhoneticRuns If phonetic runs should be concatenated
  67. */
  68. public void setConcatenatePhoneticRuns(boolean concatenatePhoneticRuns) {
  69. this.concatenatePhoneticRuns = concatenatePhoneticRuns;
  70. }
  71. public String getText() {
  72. StringBuilder text = new StringBuilder(64);
  73. XWPFHeaderFooterPolicy hfPolicy = document.getHeaderFooterPolicy();
  74. // Start out with all headers
  75. extractHeaders(text, hfPolicy);
  76. // Process all body elements
  77. for (IBodyElement e : document.getBodyElements()) {
  78. appendBodyElementText(text, e);
  79. text.append('\n');
  80. }
  81. // Finish up with all the footers
  82. extractFooters(text, hfPolicy);
  83. return text.toString();
  84. }
  85. public void appendBodyElementText(StringBuilder text, IBodyElement e) {
  86. if (e instanceof XWPFParagraph) {
  87. appendParagraphText(text, (XWPFParagraph) e);
  88. } else if (e instanceof XWPFTable) {
  89. appendTableText(text, (XWPFTable) e);
  90. } else if (e instanceof XWPFSDT) {
  91. text.append(((XWPFSDT) e).getContent().getText());
  92. }
  93. }
  94. public void appendParagraphText(StringBuilder text, XWPFParagraph paragraph) {
  95. CTSectPr ctSectPr = null;
  96. if (paragraph.getCTP().getPPr() != null) {
  97. ctSectPr = paragraph.getCTP().getPPr().getSectPr();
  98. }
  99. XWPFHeaderFooterPolicy headerFooterPolicy = null;
  100. if (ctSectPr != null) {
  101. headerFooterPolicy = new XWPFHeaderFooterPolicy(document, ctSectPr);
  102. extractHeaders(text, headerFooterPolicy);
  103. }
  104. for (IRunElement run : paragraph.getIRuns()) {
  105. if (run instanceof XWPFSDT) {
  106. text.append(((XWPFSDT) run).getContent().getText());
  107. } else if (! concatenatePhoneticRuns && run instanceof XWPFRun) {
  108. text.append(((XWPFRun)run).text());
  109. } else {
  110. text.append(run);
  111. }
  112. if (run instanceof XWPFHyperlinkRun && fetchHyperlinks) {
  113. XWPFHyperlink link = ((XWPFHyperlinkRun) run).getHyperlink(document);
  114. if (link != null)
  115. text.append(" <").append(link.getURL()).append(">");
  116. }
  117. }
  118. // Add comments
  119. XWPFCommentsDecorator decorator = new XWPFCommentsDecorator(paragraph, null);
  120. String commentText = decorator.getCommentText();
  121. if (commentText.length() > 0) {
  122. text.append(commentText).append('\n');
  123. }
  124. // Do endnotes and footnotes
  125. String footnameText = paragraph.getFootnoteText();
  126. if (footnameText != null && footnameText.length() > 0) {
  127. text.append(footnameText).append('\n');
  128. }
  129. if (ctSectPr != null) {
  130. extractFooters(text, headerFooterPolicy);
  131. }
  132. }
  133. private void appendTableText(StringBuilder text, XWPFTable table) {
  134. //this works recursively to pull embedded tables from tables
  135. for (XWPFTableRow row : table.getRows()) {
  136. List<ICell> cells = row.getTableICells();
  137. for (int i = 0; i < cells.size(); i++) {
  138. ICell cell = cells.get(i);
  139. if (cell instanceof XWPFTableCell) {
  140. text.append(((XWPFTableCell) cell).getTextRecursively());
  141. } else if (cell instanceof XWPFSDTCell) {
  142. text.append(((XWPFSDTCell) cell).getContent().getText());
  143. }
  144. if (i < cells.size() - 1) {
  145. text.append("\t");
  146. }
  147. }
  148. text.append('\n');
  149. }
  150. }
  151. private void extractFooters(StringBuilder text, XWPFHeaderFooterPolicy hfPolicy) {
  152. if (hfPolicy == null) return;
  153. if (hfPolicy.getFirstPageFooter() != null) {
  154. text.append(hfPolicy.getFirstPageFooter().getText());
  155. }
  156. if (hfPolicy.getEvenPageFooter() != null) {
  157. text.append(hfPolicy.getEvenPageFooter().getText());
  158. }
  159. if (hfPolicy.getDefaultFooter() != null) {
  160. text.append(hfPolicy.getDefaultFooter().getText());
  161. }
  162. }
  163. private void extractHeaders(StringBuilder text, XWPFHeaderFooterPolicy hfPolicy) {
  164. if (hfPolicy == null) return;
  165. if (hfPolicy.getFirstPageHeader() != null) {
  166. text.append(hfPolicy.getFirstPageHeader().getText());
  167. }
  168. if (hfPolicy.getEvenPageHeader() != null) {
  169. text.append(hfPolicy.getEvenPageHeader().getText());
  170. }
  171. if (hfPolicy.getDefaultHeader() != null) {
  172. text.append(hfPolicy.getDefaultHeader().getText());
  173. }
  174. }
  175. @Override
  176. public XWPFDocument getDocument() {
  177. return document;
  178. }
  179. @Override
  180. public void setCloseFilesystem(boolean doCloseFilesystem) {
  181. this.doCloseFilesystem = doCloseFilesystem;
  182. }
  183. @Override
  184. public boolean isCloseFilesystem() {
  185. return doCloseFilesystem;
  186. }
  187. @Override
  188. public XWPFDocument getFilesystem() {
  189. return document;
  190. }
  191. }