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.

XWPFAbstractFootnoteEndnote.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 java.math.BigInteger;
  17. import java.util.ArrayList;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Spliterator;
  21. import org.apache.poi.ooxml.POIXMLDocumentPart;
  22. import org.apache.poi.util.Internal;
  23. import org.apache.xmlbeans.XmlCursor;
  24. import org.apache.xmlbeans.XmlObject;
  25. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn;
  26. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdnRef;
  27. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
  28. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;
  29. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock;
  30. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
  31. import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
  32. /**
  33. * Base class for both bottom-of-the-page footnotes {@link XWPFFootnote} and end
  34. * notes {@link XWPFEndnote}).
  35. * <p>The only significant difference between footnotes and
  36. * end notes is which part they go on. Footnotes are managed by the Footnotes part
  37. * {@link XWPFFootnotes} and end notes are managed by the Endnotes part {@link XWPFEndnotes}.</p>
  38. * @since 4.0.0
  39. */
  40. public abstract class XWPFAbstractFootnoteEndnote implements Iterable<XWPFParagraph>, IBody {
  41. private final List<XWPFParagraph> paragraphs = new ArrayList<>();
  42. private final List<XWPFTable> tables = new ArrayList<>();
  43. private final List<XWPFPictureData> pictures = new ArrayList<>();
  44. private final List<IBodyElement> bodyElements = new ArrayList<>();
  45. protected CTFtnEdn ctFtnEdn;
  46. protected XWPFAbstractFootnotesEndnotes footnotes;
  47. protected XWPFDocument document;
  48. public XWPFAbstractFootnoteEndnote() {
  49. super();
  50. }
  51. @Internal
  52. protected XWPFAbstractFootnoteEndnote(XWPFDocument document, CTFtnEdn body) {
  53. ctFtnEdn = body;
  54. this.document = document;
  55. init();
  56. }
  57. @Internal
  58. protected XWPFAbstractFootnoteEndnote(CTFtnEdn note, XWPFAbstractFootnotesEndnotes footnotes) {
  59. this.footnotes = footnotes;
  60. ctFtnEdn = note;
  61. document = footnotes.getXWPFDocument();
  62. init();
  63. }
  64. protected void init() {
  65. XmlCursor cursor = ctFtnEdn.newCursor();
  66. //copied from XWPFDocument...should centralize this code
  67. //to avoid duplication
  68. cursor.selectPath("./*");
  69. while (cursor.toNextSelection()) {
  70. XmlObject o = cursor.getObject();
  71. if (o instanceof CTP) {
  72. XWPFParagraph p = new XWPFParagraph((CTP) o, this);
  73. bodyElements.add(p);
  74. paragraphs.add(p);
  75. } else if (o instanceof CTTbl) {
  76. XWPFTable t = new XWPFTable((CTTbl) o, this);
  77. bodyElements.add(t);
  78. tables.add(t);
  79. } else if (o instanceof CTSdtBlock) {
  80. XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
  81. bodyElements.add(c);
  82. }
  83. }
  84. cursor.dispose();
  85. }
  86. /**
  87. * Get the list of {@link XWPFParagraph}s in the footnote.
  88. * @return List of paragraphs
  89. */
  90. @Override
  91. public List<XWPFParagraph> getParagraphs() {
  92. return paragraphs;
  93. }
  94. /**
  95. * Get an iterator over the {@link XWPFParagraph}s in the footnote.
  96. * @return Iterator over the paragraph list.
  97. */
  98. @Override
  99. public Iterator<XWPFParagraph> iterator() {
  100. return paragraphs.iterator();
  101. }
  102. /**
  103. * Get a spliterator over the {@link XWPFParagraph}s in the footnote.
  104. * @return Spliterator over the paragraph list.
  105. *
  106. * @since POI 5.2.0
  107. */
  108. @Override
  109. public Spliterator<XWPFParagraph> spliterator() {
  110. return paragraphs.spliterator();
  111. }
  112. /**
  113. * Get the list of {@link XWPFTable}s in the footnote.
  114. * @return List of tables
  115. */
  116. @Override
  117. public List<XWPFTable> getTables() {
  118. return tables;
  119. }
  120. /**
  121. * Gets the list of {@link XWPFPictureData}s in the footnote.
  122. * @return List of pictures
  123. */
  124. public List<XWPFPictureData> getPictures() {
  125. return pictures;
  126. }
  127. /**
  128. * Gets the body elements ({@link IBodyElement}) of the footnote.
  129. * @return List of body elements.
  130. */
  131. @Override
  132. public List<IBodyElement> getBodyElements() {
  133. return bodyElements;
  134. }
  135. /**
  136. * Gets the underlying CTFtnEdn object for the footnote.
  137. * @return CTFtnEdn object
  138. */
  139. public CTFtnEdn getCTFtnEdn() {
  140. return ctFtnEdn;
  141. }
  142. /**
  143. * Set the underlying CTFtnEdn for the footnote.
  144. * <p>Use {@link XWPFDocument#createFootnote()} to create new footnotes.</p>
  145. * @param footnote The CTFtnEdn object that will underly the footnote.
  146. */
  147. public void setCTFtnEdn(CTFtnEdn footnote) {
  148. ctFtnEdn = footnote;
  149. }
  150. /**
  151. * Gets the {@link XWPFTable} at the specified position from the footnote's table array.
  152. * @param pos in table array
  153. * @return The {@link XWPFTable} at position pos, or null if there is no table at position pos.
  154. */
  155. @Override
  156. public XWPFTable getTableArray(int pos) {
  157. if (pos >= 0 && pos < tables.size()) {
  158. return tables.get(pos);
  159. }
  160. return null;
  161. }
  162. /**
  163. * Inserts an existing XWPFTable into the arrays bodyElements and tables.
  164. *
  165. * @param pos Position, in the bodyElements array, to insert the table
  166. * @param table XWPFTable to be inserted
  167. */
  168. @Override
  169. public void insertTable(int pos, XWPFTable table) {
  170. bodyElements.add(pos, table);
  171. int i = 0;
  172. for (CTTbl tbl : ctFtnEdn.getTblList()) {
  173. if (tbl == table.getCTTbl()) {
  174. break;
  175. }
  176. i++;
  177. }
  178. tables.add(i, table);
  179. }
  180. /**
  181. * if there is a corresponding {@link XWPFTable} of the parameter
  182. * ctTable in the tableList of this header
  183. * the method will return this table, or null if there is no
  184. * corresponding {@link XWPFTable}.
  185. */
  186. @Override
  187. public XWPFTable getTable(CTTbl ctTable) {
  188. for (XWPFTable table : tables) {
  189. if (table == null)
  190. return null;
  191. if (table.getCTTbl().equals(ctTable))
  192. return table;
  193. }
  194. return null;
  195. }
  196. @Override
  197. public XWPFParagraph getParagraph(CTP p) {
  198. for (XWPFParagraph paragraph : paragraphs) {
  199. if (paragraph.getCTP().equals(p))
  200. return paragraph;
  201. }
  202. return null;
  203. }
  204. /**
  205. * Returns the {@link XWPFParagraph} at position pos in footnote's paragraph array.
  206. * @param pos Array position of the paragraph to get.
  207. * @return the {@link XWPFParagraph} at position pos, or null if there is no paragraph at that position.
  208. */
  209. @Override
  210. public XWPFParagraph getParagraphArray(int pos) {
  211. if(pos >=0 && pos < paragraphs.size()) {
  212. return paragraphs.get(pos);
  213. }
  214. return null;
  215. }
  216. /**
  217. * get the {@link XWPFTableCell} that belongs to the CTTc cell.
  218. *
  219. * @return {@link XWPFTableCell} that corresponds to the CTTc cell, if there is one, otherwise null.
  220. */
  221. @Override
  222. public XWPFTableCell getTableCell(CTTc cell) {
  223. XmlCursor cursor = cell.newCursor();
  224. cursor.toParent();
  225. XmlObject o = cursor.getObject();
  226. if (!(o instanceof CTRow)) {
  227. return null;
  228. }
  229. CTRow row = (CTRow) o;
  230. cursor.toParent();
  231. o = cursor.getObject();
  232. cursor.dispose();
  233. if (!(o instanceof CTTbl)) {
  234. return null;
  235. }
  236. CTTbl tbl = (CTTbl) o;
  237. XWPFTable table = getTable(tbl);
  238. if (table == null) {
  239. return null;
  240. }
  241. XWPFTableRow tableRow = table.getRow(row);
  242. if(tableRow == null){
  243. return null;
  244. }
  245. return tableRow.getTableCell(cell);
  246. }
  247. /**
  248. * Verifies that cursor is on the right position.
  249. *
  250. * @return true if the cursor is within a CTFtnEdn element.
  251. */
  252. private boolean isCursorInFtn(XmlCursor cursor) {
  253. XmlCursor verify = cursor.newCursor();
  254. verify.toParent();
  255. boolean result = (verify.getObject() == this.ctFtnEdn);
  256. verify.dispose();
  257. return result;
  258. }
  259. /**
  260. * The owning object for this footnote
  261. *
  262. * @return The {@link XWPFFootnotes} object that contains this footnote.
  263. */
  264. public POIXMLDocumentPart getOwner() {
  265. return footnotes;
  266. }
  267. /**
  268. * Insert a table constructed from OOXML table markup.
  269. * @return the inserted {@link XWPFTable}
  270. */
  271. @Override
  272. public XWPFTable insertNewTbl(XmlCursor cursor) {
  273. if (isCursorInFtn(cursor)) {
  274. String uri = CTTbl.type.getName().getNamespaceURI();
  275. String localPart = "tbl";
  276. cursor.beginElement(localPart, uri);
  277. cursor.toParent();
  278. CTTbl t = (CTTbl) cursor.getObject();
  279. XWPFTable newT = new XWPFTable(t, this);
  280. cursor.removeXmlContents();
  281. XmlObject o = null;
  282. while (!(o instanceof CTTbl) && (cursor.toPrevSibling())) {
  283. o = cursor.getObject();
  284. }
  285. if (!(o instanceof CTTbl)) {
  286. tables.add(0, newT);
  287. } else {
  288. int pos = tables.indexOf(getTable((CTTbl) o)) + 1;
  289. tables.add(pos, newT);
  290. }
  291. int i = 0;
  292. cursor = t.newCursor();
  293. while (cursor.toPrevSibling()) {
  294. o = cursor.getObject();
  295. if (o instanceof CTP || o instanceof CTTbl)
  296. i++;
  297. }
  298. bodyElements.add(i, newT);
  299. XmlCursor c2 = t.newCursor();
  300. cursor.toCursor(c2);
  301. cursor.toEndToken();
  302. c2.dispose();
  303. return newT;
  304. }
  305. return null;
  306. }
  307. /**
  308. * Add a new {@link XWPFParagraph} at position of the cursor.
  309. *
  310. * @return The inserted {@link XWPFParagraph}
  311. */
  312. @Override
  313. public XWPFParagraph insertNewParagraph(final XmlCursor cursor) {
  314. if (isCursorInFtn(cursor)) {
  315. String uri = CTP.type.getName().getNamespaceURI();
  316. String localPart = "p";
  317. cursor.beginElement(localPart, uri);
  318. cursor.toParent();
  319. CTP p = (CTP) cursor.getObject();
  320. XWPFParagraph newP = new XWPFParagraph(p, this);
  321. XmlObject o = null;
  322. while (!(o instanceof CTP) && (cursor.toPrevSibling())) {
  323. o = cursor.getObject();
  324. }
  325. if ((!(o instanceof CTP)) || o == p) {
  326. paragraphs.add(0, newP);
  327. } else {
  328. int pos = paragraphs.indexOf(getParagraph((CTP) o)) + 1;
  329. paragraphs.add(pos, newP);
  330. }
  331. int i = 0;
  332. XmlCursor p2 = p.newCursor();
  333. cursor.toCursor(p2);
  334. p2.dispose();
  335. while (cursor.toPrevSibling()) {
  336. o = cursor.getObject();
  337. if (o instanceof CTP || o instanceof CTTbl)
  338. i++;
  339. }
  340. bodyElements.add(i, newP);
  341. p2 = p.newCursor();
  342. cursor.toCursor(p2);
  343. cursor.toEndToken();
  344. p2.dispose();
  345. return newP;
  346. }
  347. return null;
  348. }
  349. /**
  350. * Add a new {@link XWPFTable} to the end of the footnote.
  351. *
  352. * @param table CTTbl object from which to construct the {@link XWPFTable}
  353. * @return The added {@link XWPFTable}
  354. */
  355. public XWPFTable addNewTbl(CTTbl table) {
  356. CTTbl newTable = ctFtnEdn.addNewTbl();
  357. newTable.set(table);
  358. XWPFTable xTable = new XWPFTable(newTable, this);
  359. tables.add(xTable);
  360. return xTable;
  361. }
  362. /**
  363. * Add a new {@link XWPFParagraph} to the end of the footnote.
  364. *
  365. * @param paragraph CTP paragraph from which to construct the {@link XWPFParagraph}
  366. * @return The added {@link XWPFParagraph}
  367. */
  368. public XWPFParagraph addNewParagraph(CTP paragraph) {
  369. CTP newPara = ctFtnEdn.addNewP();
  370. newPara.set(paragraph);
  371. XWPFParagraph xPara = new XWPFParagraph(newPara, this);
  372. paragraphs.add(xPara);
  373. return xPara;
  374. }
  375. /**
  376. * Get the {@link XWPFDocument} the footnote is part of.
  377. */
  378. @Override
  379. public XWPFDocument getXWPFDocument() {
  380. return document;
  381. }
  382. /**
  383. * Get the Part to which the footnote belongs, which you need for adding relationships to other parts
  384. * @return {@link POIXMLDocumentPart} that contains the footnote.
  385. */
  386. @Override
  387. public POIXMLDocumentPart getPart() {
  388. return footnotes;
  389. }
  390. /**
  391. * Get the part type {@link BodyType} of the footnote.
  392. * @return The {@link BodyType} value.
  393. */
  394. @Override
  395. public BodyType getPartType() {
  396. return BodyType.FOOTNOTE;
  397. }
  398. /**
  399. * Get the ID of the footnote.
  400. * <p>Footnote IDs are unique across all bottom-of-the-page and
  401. * end note footnotes.</p>
  402. *
  403. * @return Footnote ID
  404. * @since 4.0.0
  405. */
  406. public BigInteger getId() {
  407. return this.ctFtnEdn.getId();
  408. }
  409. /**
  410. * Appends a new {@link XWPFParagraph} to this footnote.
  411. *
  412. * @return The new {@link XWPFParagraph}
  413. * @since 4.0.0
  414. */
  415. public XWPFParagraph createParagraph() {
  416. XWPFParagraph p = new XWPFParagraph(this.ctFtnEdn.addNewP(), this);
  417. paragraphs.add(p);
  418. bodyElements.add(p);
  419. // If the paragraph is the first paragraph in the footnote,
  420. // ensure that it has a footnote reference run.
  421. if (p.equals(getParagraphs().get(0))) {
  422. ensureFootnoteRef(p);
  423. }
  424. return p;
  425. }
  426. /**
  427. * Ensure that the specified paragraph has a reference marker for this
  428. * footnote by adding a footnote reference if one is not found.
  429. * <p>This method is for the first paragraph in the footnote, not
  430. * paragraphs that will refer to the footnote. For references to
  431. * the footnote, use {@link XWPFParagraph#addFootnoteReference(XWPFAbstractFootnoteEndnote)}.
  432. * </p>
  433. * <p>The first run of the first paragraph in a footnote should
  434. * contain a {@link CTFtnEdnRef} object.</p>
  435. *
  436. * @param p The {@link XWPFParagraph} to ensure
  437. * @since 4.0.0
  438. */
  439. public abstract void ensureFootnoteRef(XWPFParagraph p);
  440. /**
  441. * Appends a new {@link XWPFTable} to this footnote
  442. *
  443. * @return The new {@link XWPFTable}
  444. * @since 4.0.0
  445. */
  446. public XWPFTable createTable() {
  447. XWPFTable table = new XWPFTable(ctFtnEdn.addNewTbl(), this);
  448. if (bodyElements.size() == 0) {
  449. XWPFParagraph p = createParagraph();
  450. ensureFootnoteRef(p);
  451. }
  452. bodyElements.add(table);
  453. tables.add(table);
  454. return table;
  455. }
  456. /**
  457. * Appends a new {@link XWPFTable} to this footnote
  458. * @param rows Number of rows to initialize the table with
  459. * @param cols Number of columns to initialize the table with
  460. * @return the new {@link XWPFTable} with the specified number of rows and columns
  461. * @since 4.0.0
  462. */
  463. public XWPFTable createTable(int rows, int cols) {
  464. XWPFTable table = new XWPFTable(ctFtnEdn.addNewTbl(), this, rows, cols);
  465. bodyElements.add(table);
  466. tables.add(table);
  467. return table;
  468. }
  469. }