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