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.

XWPFFootnote.java 12KB

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