Link Here
|
20 |
import java.util.Iterator; |
20 |
import java.util.Iterator; |
21 |
import java.util.List; |
21 |
import java.util.List; |
22 |
|
22 |
|
|
|
23 |
import org.apache.poi.POIXMLDocumentPart; |
24 |
import org.apache.poi.POIXMLException; |
25 |
import org.apache.xmlbeans.XmlObject; |
26 |
import org.apache.xmlbeans.XmlCursor; |
27 |
|
23 |
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn; |
28 |
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFtnEdn; |
24 |
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; |
29 |
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; |
25 |
|
30 |
|
26 |
public class XWPFFootnote implements Iterable<XWPFParagraph> { |
31 |
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc; |
|
|
32 |
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; |
33 |
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow; |
34 |
|
35 |
public class XWPFFootnote implements Iterable<XWPFParagraph>,IBody { |
27 |
private List<XWPFParagraph> paragraphs = new ArrayList<XWPFParagraph>(); |
36 |
private List<XWPFParagraph> paragraphs = new ArrayList<XWPFParagraph>(); |
|
|
37 |
private List<XWPFTable> tables= new ArrayList<XWPFTable>(); |
38 |
private List<XWPFPictureData> pictures = new ArrayList<XWPFPictureData>(); |
39 |
private List<IBodyElement> bodyElements = new ArrayList<IBodyElement>(); |
28 |
|
40 |
|
|
|
41 |
private CTFtnEdn ctFtnEdn; |
42 |
private XWPFFootnotes footnotes; |
43 |
|
44 |
public XWPFFootnote(CTFtnEdn note, XWPFFootnotes xFootnotes) { |
45 |
footnotes = xFootnotes; |
46 |
ctFtnEdn = note; |
47 |
for (CTP p : ctFtnEdn.getPList()) { |
48 |
paragraphs.add(new XWPFParagraph(p, this)); |
49 |
} |
50 |
} |
51 |
|
29 |
public XWPFFootnote(XWPFDocument document, CTFtnEdn body) { |
52 |
public XWPFFootnote(XWPFDocument document, CTFtnEdn body) { |
30 |
for (CTP p : body.getPList()) { |
53 |
for (CTP p : body.getPList()) { |
31 |
paragraphs.add(new XWPFParagraph(p, document)); |
54 |
paragraphs.add(new XWPFParagraph(p, document)); |
Link Here
|
38 |
|
61 |
|
39 |
public Iterator<XWPFParagraph> iterator(){ |
62 |
public Iterator<XWPFParagraph> iterator(){ |
40 |
return paragraphs.iterator(); |
63 |
return paragraphs.iterator(); |
|
|
64 |
} |
65 |
|
66 |
public List<XWPFTable> getTables() { |
67 |
return tables; |
41 |
} |
68 |
} |
42 |
|
69 |
|
|
|
70 |
public List<XWPFPictureData> getPictures() { |
71 |
return pictures; |
72 |
} |
73 |
|
74 |
public List<IBodyElement> getBodyElements() { |
75 |
return bodyElements; |
76 |
} |
77 |
|
78 |
public CTFtnEdn getCTFtnEdn() { |
79 |
return ctFtnEdn; |
80 |
} |
81 |
|
82 |
public void setCTFtnEdn(CTFtnEdn footnote) { |
83 |
ctFtnEdn = footnote; |
84 |
} |
85 |
|
86 |
/** |
87 |
* @param position in table array |
88 |
* @return The table at position pos |
89 |
* @see org.apache.poi.xwpf.usermodel.IBody#getTableArray(int) |
90 |
*/ |
91 |
public XWPFTable getTableArray(int pos) { |
92 |
|
93 |
if(pos > 0 && pos < tables.size()){ |
94 |
return tables.get(pos); |
95 |
} |
96 |
return null; |
97 |
} |
98 |
|
99 |
/** |
100 |
* inserts an existing XWPFTable to the arrays bodyElements and tables |
101 |
* @param pos |
102 |
* @param table |
103 |
* @see org.apache.poi.xwpf.usermodel.IBody#insertTable(int pos, XWPFTable table) |
104 |
*/ |
105 |
public void insertTable(int pos, XWPFTable table) { |
106 |
bodyElements.add(pos, table); |
107 |
int i; |
108 |
for (i = 0; i < ctFtnEdn.getTblList().size(); i++) { |
109 |
CTTbl tbl = ctFtnEdn.getTblArray(i); |
110 |
if(tbl == table.getCTTbl()){ |
111 |
break; |
112 |
} |
113 |
} |
114 |
tables.add(i, table); |
115 |
|
116 |
} |
117 |
|
118 |
/** |
119 |
* if there is a corresponding {@link XWPFTable} of the parameter ctTable in the tableList of this header |
120 |
* the method will return this table |
121 |
* if there is no corresponding {@link XWPFTable} the method will return null |
122 |
* @param ctTable |
123 |
* @see org.apache.poi.xwpf.usermodel.IBody#getTable(CTTbl ctTable) |
124 |
*/ |
125 |
public XWPFTable getTable(CTTbl ctTable){ |
126 |
for (XWPFTable table : tables) { |
127 |
if(table==null) |
128 |
return null; |
129 |
if(table.getCTTbl().equals(ctTable)) |
130 |
return table; |
131 |
} |
132 |
return null; |
133 |
} |
134 |
|
135 |
/** |
136 |
* if there is a corresponding {@link XWPFParagraph} of the parameter ctTable in the paragraphList of this header or footer |
137 |
* the method will return this paragraph |
138 |
* if there is no corresponding {@link XWPFParagraph} the method will return null |
139 |
* @param p is instance of CTP and is searching for an XWPFParagraph |
140 |
* @return null if there is no XWPFParagraph with an corresponding CTPparagraph in the paragraphList of this header or footer |
141 |
* XWPFParagraph with the correspondig CTP p |
142 |
* @see org.apache.poi.xwpf.usermodel.IBody#getParagraph(CTP p) |
143 |
*/ |
144 |
public XWPFParagraph getParagraph(CTP p){ |
145 |
for (XWPFParagraph paragraph : paragraphs) { |
146 |
if(paragraph.getCTP().equals(p)) |
147 |
return paragraph; |
148 |
} |
149 |
return null; |
150 |
} |
151 |
|
152 |
/** |
153 |
* Returns the paragraph that holds |
154 |
* the text of the header or footer. |
155 |
* @see org.apache.poi.xwpf.usermodel.IBody#getParagraphArray(int pos) |
156 |
*/ |
157 |
public XWPFParagraph getParagraphArray(int pos) { |
158 |
|
159 |
return paragraphs.get(pos); |
160 |
} |
161 |
|
162 |
/** |
163 |
* get the TableCell which belongs to the TableCell |
164 |
* @param cell |
165 |
* @see org.apache.poi.xwpf.usermodel.IBody#getTableCell(CTTc cell) |
166 |
*/ |
167 |
public XWPFTableCell getTableCell(CTTc cell) { |
168 |
XmlCursor cursor = cell.newCursor(); |
169 |
cursor.toParent(); |
170 |
XmlObject o = cursor.getObject(); |
171 |
if(!(o instanceof CTRow)){ |
172 |
return null; |
173 |
} |
174 |
CTRow row = (CTRow)o; |
175 |
cursor.toParent(); |
176 |
o = cursor.getObject(); |
177 |
cursor.dispose(); |
178 |
if(! (o instanceof CTTbl)){ |
179 |
return null; |
180 |
} |
181 |
CTTbl tbl = (CTTbl) o; |
182 |
XWPFTable table = getTable(tbl); |
183 |
if(table == null){ |
184 |
return null; |
185 |
} |
186 |
XWPFTableRow tableRow = table.getRow(row); |
187 |
if(row == null){ |
188 |
return null; |
189 |
} |
190 |
return tableRow.getTableCell(cell); |
191 |
} |
192 |
|
193 |
/** |
194 |
* verifies that cursor is on the right position |
195 |
* @param cursor |
196 |
*/ |
197 |
private boolean isCursorInFtn(XmlCursor cursor) { |
198 |
XmlCursor verify = cursor.newCursor(); |
199 |
verify.toParent(); |
200 |
if(verify.getObject() == this.ctFtnEdn){ |
201 |
return true; |
202 |
} |
203 |
return false; |
204 |
} |
205 |
|
206 |
public POIXMLDocumentPart getOwner(){ |
207 |
return footnotes; |
208 |
} |
209 |
|
210 |
/** |
211 |
* |
212 |
* @param cursor |
213 |
* @return the inserted table |
214 |
* @see org.apache.poi.xwpf.usermodel.IBody#insertNewTbl(XmlCursor cursor) |
215 |
*/ |
216 |
public XWPFTable insertNewTbl(XmlCursor cursor) { |
217 |
if(isCursorInFtn(cursor)){ |
218 |
String uri = CTTbl.type.getName().getNamespaceURI(); |
219 |
String localPart = "tbl"; |
220 |
cursor.beginElement(localPart,uri); |
221 |
cursor.toParent(); |
222 |
CTTbl t = (CTTbl)cursor.getObject(); |
223 |
XWPFTable newT = new XWPFTable(t, this); |
224 |
cursor.removeXmlContents(); |
225 |
XmlObject o = null; |
226 |
while(!(o instanceof CTTbl)&&(cursor.toPrevSibling())){ |
227 |
o = cursor.getObject(); |
228 |
} |
229 |
if(!(o instanceof CTTbl)){ |
230 |
tables.add(0, newT); |
231 |
} |
232 |
else{ |
233 |
int pos = tables.indexOf(getTable((CTTbl)o))+1; |
234 |
tables.add(pos,newT); |
235 |
} |
236 |
int i=0; |
237 |
cursor = t.newCursor(); |
238 |
while(cursor.toPrevSibling()){ |
239 |
o =cursor.getObject(); |
240 |
if(o instanceof CTP || o instanceof CTTbl) |
241 |
i++; |
242 |
} |
243 |
bodyElements.add(i, newT); |
244 |
cursor = t.newCursor(); |
245 |
cursor.toEndToken(); |
246 |
return newT; |
247 |
} |
248 |
return null; |
249 |
} |
250 |
|
251 |
/** |
252 |
* add a new paragraph at position of the cursor |
253 |
* @param cursor |
254 |
* @return the inserted paragraph |
255 |
* @see org.apache.poi.xwpf.usermodel.IBody#insertNewParagraph(XmlCursor cursor) |
256 |
*/ |
257 |
public XWPFParagraph insertNewParagraph(XmlCursor cursor){ |
258 |
if(isCursorInFtn(cursor)){ |
259 |
String uri = CTP.type.getName().getNamespaceURI(); |
260 |
String localPart = "p"; |
261 |
cursor.beginElement(localPart,uri); |
262 |
cursor.toParent(); |
263 |
CTP p = (CTP)cursor.getObject(); |
264 |
XWPFParagraph newP = new XWPFParagraph(p, this); |
265 |
XmlObject o = null; |
266 |
while(!(o instanceof CTP)&&(cursor.toPrevSibling())){ |
267 |
o = cursor.getObject(); |
268 |
} |
269 |
if((!(o instanceof CTP)) || (CTP)o == p){ |
270 |
paragraphs.add(0, newP); |
271 |
} |
272 |
else{ |
273 |
int pos = paragraphs.indexOf(getParagraph((CTP)o))+1; |
274 |
paragraphs.add(pos,newP); |
275 |
} |
276 |
int i=0; |
277 |
cursor.toCursor(p.newCursor()); |
278 |
while(cursor.toPrevSibling()){ |
279 |
o =cursor.getObject(); |
280 |
if(o instanceof CTP || o instanceof CTTbl) |
281 |
i++; |
282 |
} |
283 |
bodyElements.add(i, newP); |
284 |
cursor.toCursor(p.newCursor()); |
285 |
cursor.toEndToken(); |
286 |
return newP; |
287 |
} |
288 |
return null; |
289 |
} |
290 |
|
291 |
/** |
292 |
* add a new table to the end of the footnote |
293 |
* @param table |
294 |
* @return the added XWPFTable |
295 |
*/ |
296 |
public XWPFTable addNewTbl(CTTbl table) { |
297 |
CTTbl newTable = ctFtnEdn.addNewTbl(); |
298 |
newTable.set(table); |
299 |
XWPFTable xTable = new XWPFTable(newTable, this); |
300 |
tables.add(xTable); |
301 |
return xTable; |
302 |
} |
303 |
|
304 |
/** |
305 |
* add a new paragraph to the end of the footnote |
306 |
* @param paragraph |
307 |
* @return the added XWPFParagraph |
308 |
*/ |
309 |
public XWPFParagraph addNewParagraph(CTP paragraph) { |
310 |
CTP newPara = ctFtnEdn.addNewP(); |
311 |
newPara.set(paragraph); |
312 |
XWPFParagraph xPara = new XWPFParagraph(newPara, this); |
313 |
paragraphs.add(xPara); |
314 |
return xPara; |
315 |
} |
316 |
|
317 |
/** |
318 |
* @see org.apache.poi.xwpf.usermodel.IBody#getXWPFDocument() |
319 |
*/ |
320 |
public XWPFDocument getXWPFDocument() { |
321 |
return footnotes.getXWPFDocument(); |
322 |
} |
323 |
|
324 |
/** |
325 |
* returns the Part, to which the body belongs, which you need for adding relationship to other parts |
326 |
* @see org.apache.poi.xwpf.usermodel.IBody#getPart() |
327 |
*/ |
328 |
public POIXMLDocumentPart getPart() { |
329 |
return footnotes; |
330 |
} |
331 |
|
332 |
/** |
333 |
* get the PartType of the body |
334 |
* @see org.apache.poi.xwpf.usermodel.IBody#getPartType() |
335 |
*/ |
336 |
public BodyType getPartType() { |
337 |
return BodyType.FOOTNOTE; |
338 |
} |
43 |
} |
339 |
} |
|
|
340 |
|