}
protected void init() {
- XmlCursor cursor = ctFtnEdn.newCursor();
- try {
+ try (XmlCursor cursor = ctFtnEdn.newCursor()) {
//copied from XWPFDocument...should centralize this code
//to avoid duplication
cursor.selectPath("./*");
}
}
- } finally {
- cursor.dispose();
}
}
public XWPFTableCell getTableCell(CTTc cell) {
XmlObject o;
CTRow row;
- final XmlCursor cursor = cell.newCursor();
- try {
+ try (final XmlCursor cursor = cell.newCursor()) {
cursor.toParent();
o = cursor.getObject();
if (!(o instanceof CTRow)) {
row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
- } finally {
- cursor.dispose();
}
if (!(o instanceof CTTbl)) {
return null;
* @return true if the cursor is within a CTFtnEdn element.
*/
private boolean isCursorInFtn(XmlCursor cursor) {
- XmlCursor verify = cursor.newCursor();
- try {
+ try (XmlCursor verify = cursor.newCursor()) {
verify.toParent();
return (verify.getObject() == this.ctFtnEdn);
- } finally {
- verify.dispose();
-
}
}
tables.add(pos, newT);
}
int i = 0;
- cursor = t.newCursor();
- while (cursor.toPrevSibling()) {
- o = cursor.getObject();
- if (o instanceof CTP || o instanceof CTTbl)
- i++;
+ try (XmlCursor cursor1 = t.newCursor()) {
+ while (cursor1.toPrevSibling()) {
+ o = cursor1.getObject();
+ if (o instanceof CTP || o instanceof CTTbl)
+ i++;
+ }
+ bodyElements.add(i, newT);
}
- bodyElements.add(i, newT);
- XmlCursor c2 = t.newCursor();
- try {
+ try (XmlCursor c2 = t.newCursor()) {
cursor.toCursor(c2);
cursor.toEndToken();
- } finally {
- c2.dispose();
}
return newT;
}
paragraphs.add(pos, newP);
}
int i = 0;
- XmlCursor p2 = p.newCursor();
- try {
+ try (XmlCursor p2 = p.newCursor()) {
cursor.toCursor(p2);
- } finally {
- p2.dispose();
}
while (cursor.toPrevSibling()) {
o = cursor.getObject();
i++;
}
bodyElements.add(i, newP);
- p2 = p.newCursor();
- cursor.toCursor(p2);
- cursor.toEndToken();
- p2.dispose();
+ try (XmlCursor p2 = p.newCursor()) {
+ cursor.toCursor(p2);
+ cursor.toEndToken();
+ }
return newP;
}
return null;
// parse the document with cursor and add
// the XmlObject to its lists
- XmlCursor docCursor = ctDocument.newCursor();
- docCursor.selectPath("./*");
- while (docCursor.toNextSelection()) {
- XmlObject o = docCursor.getObject();
- if (o instanceof CTBody) {
- XmlCursor bodyCursor = o.newCursor();
- bodyCursor.selectPath("./*");
- while (bodyCursor.toNextSelection()) {
- XmlObject bodyObj = bodyCursor.getObject();
- if (bodyObj instanceof CTP) {
- XWPFParagraph p = new XWPFParagraph((CTP) bodyObj,
- this);
- bodyElements.add(p);
- paragraphs.add(p);
- } else if (bodyObj instanceof CTTbl) {
- XWPFTable t = new XWPFTable((CTTbl) bodyObj, this);
- bodyElements.add(t);
- tables.add(t);
- } else if (bodyObj instanceof CTSdtBlock) {
- XWPFSDT c = new XWPFSDT((CTSdtBlock) bodyObj, this);
- bodyElements.add(c);
- contentControls.add(c);
+ try (XmlCursor docCursor = ctDocument.newCursor()) {
+ docCursor.selectPath("./*");
+ while (docCursor.toNextSelection()) {
+ XmlObject o = docCursor.getObject();
+ if (o instanceof CTBody) {
+ try (XmlCursor bodyCursor = o.newCursor()) {
+ bodyCursor.selectPath("./*");
+ while (bodyCursor.toNextSelection()) {
+ XmlObject bodyObj = bodyCursor.getObject();
+ if (bodyObj instanceof CTP) {
+ XWPFParagraph p = new XWPFParagraph((CTP) bodyObj, this);
+ bodyElements.add(p);
+ paragraphs.add(p);
+ } else if (bodyObj instanceof CTTbl) {
+ XWPFTable t = new XWPFTable((CTTbl) bodyObj, this);
+ bodyElements.add(t);
+ tables.add(t);
+ } else if (bodyObj instanceof CTSdtBlock) {
+ XWPFSDT c = new XWPFSDT((CTSdtBlock) bodyObj, this);
+ bodyElements.add(c);
+ contentControls.add(c);
+ }
+ }
}
}
- bodyCursor.dispose();
}
}
- docCursor.dispose();
// Sort out headers and footers
if (doc.getDocument().getBody().getSectPr() != null) {
headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
* create a new cursor, that points to the START token of the just
* inserted paragraph
*/
- XmlCursor newParaPos = p.newCursor();
- try {
+ try (XmlCursor newParaPos = p.newCursor()) {
/*
* Calculate the paragraphs index in the list of all body
* elements
cursor.toCursor(newParaPos);
cursor.toEndToken();
return newP;
- } finally {
- newParaPos.dispose();
}
}
return null;
tables.add(pos, newT);
}
int i = 0;
- XmlCursor tableCursor = t.newCursor();
- try {
+ try (XmlCursor tableCursor = t.newCursor()) {
cursor.toCursor(tableCursor);
while (cursor.toPrevSibling()) {
o = cursor.getObject();
cursor.toCursor(tableCursor);
cursor.toEndToken();
return newT;
- } finally {
- tableCursor.dispose();
}
}
return null;
* verifies that cursor is on the right position
*/
private boolean isCursorInBody(XmlCursor cursor) {
- XmlCursor verify = cursor.newCursor();
- verify.toParent();
- boolean result = (verify.getObject() == this.ctDocument.getBody());
- verify.dispose();
- return result;
+ try (XmlCursor verify = cursor.newCursor()) {
+ verify.toParent();
+ return (verify.getObject() == this.ctDocument.getBody());
+ }
}
private int getPosOfBodyElement(IBodyElement needle) {
public XWPFTableCell getTableCell(CTTc cell) {
XmlObject o;
CTRow row;
- final XmlCursor cursor = cell.newCursor();
- try {
+ try (final XmlCursor cursor = cell.newCursor()) {
cursor.toParent();
o = cursor.getObject();
if (!(o instanceof CTRow)) {
row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
- } finally {
- cursor.dispose();
}
if (!(o instanceof CTTbl)) {
return null;
public XWPFHeader(XWPFDocument doc, CTHdrFtr hdrFtr) {
super(doc, hdrFtr);
- XmlCursor cursor = headerFooter.newCursor();
- try {
+ try (XmlCursor cursor = headerFooter.newCursor()) {
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
tables.add(t);
}
}
- } finally {
- cursor.dispose();
}
}
headerFooter = hdrDocument.getHdr();
// parse the document with cursor and add
// the XmlObject to its lists
- XmlCursor cursor = headerFooter.newCursor();
- cursor.selectPath("./*");
- while (cursor.toNextSelection()) {
- XmlObject o = cursor.getObject();
- if (o instanceof CTP) {
- XWPFParagraph p = new XWPFParagraph((CTP) o, this);
- paragraphs.add(p);
- bodyElements.add(p);
- }
- if (o instanceof CTTbl) {
- XWPFTable t = new XWPFTable((CTTbl) o, this);
- tables.add(t);
- bodyElements.add(t);
- }
- if (o instanceof CTSdtBlock) {
- XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
- bodyElements.add(c);
+ try (XmlCursor cursor = headerFooter.newCursor()) {
+ cursor.selectPath("./*");
+ while (cursor.toNextSelection()) {
+ XmlObject o = cursor.getObject();
+ if (o instanceof CTP) {
+ XWPFParagraph p = new XWPFParagraph((CTP) o, this);
+ paragraphs.add(p);
+ bodyElements.add(p);
+ }
+ if (o instanceof CTTbl) {
+ XWPFTable t = new XWPFTable((CTTbl) o, this);
+ tables.add(t);
+ bodyElements.add(t);
+ }
+ if (o instanceof CTSdtBlock) {
+ XWPFSDT c = new XWPFSDT((CTSdtBlock) o, this);
+ bodyElements.add(c);
+ }
}
}
- cursor.dispose();
} catch (XmlException e) {
throw new POIXMLException(e);
}
public void removeParagraph(XWPFParagraph paragraph) {
if (paragraphs.contains(paragraph)) {
CTP ctP = paragraph.getCTP();
- XmlCursor c = ctP.newCursor();
- try {
+ try (XmlCursor c = ctP.newCursor()) {
c.removeXml();
- } finally {
- c.dispose();
}
paragraphs.remove(paragraph);
bodyElements.remove(paragraph);
public void removeTable(XWPFTable table) {
if (tables.contains(table)) {
CTTbl ctTbl = table.getCTTbl();
- XmlCursor c = ctTbl.newCursor();
- try {
+ try (XmlCursor c = ctTbl.newCursor()) {
c.removeXml();
- } finally {
- c.dispose();
}
tables.remove(table);
bodyElements.remove(table);
* Clears all paragraphs and tables from this header / footer
*/
public void clearHeaderFooter() {
- XmlCursor c = headerFooter.newCursor();
- try {
+ try (XmlCursor c = headerFooter.newCursor()) {
c.removeXmlContents();
- } finally {
- c.dispose();
}
paragraphs.clear();
tables.clear();
paragraphs.add(pos, newP);
}
int i = 0;
- final XmlCursor p2 = p.newCursor();
- try {
+ try (final XmlCursor p2 = p.newCursor()) {
cursor.toCursor(p2);
- } finally {
- p2.dispose();
}
while (cursor.toPrevSibling()) {
o = cursor.getObject();
i++;
}
bodyElements.add(i, newP);
- final XmlCursor p3 = p.newCursor();
- try {
+ try(final XmlCursor p3 = p.newCursor()) {
cursor.toCursor(p3);
cursor.toEndToken();
- } finally {
- p3.dispose();
}
return newP;
}
tables.add(pos, newT);
}
int i = 0;
- final XmlCursor cursor2 = t.newCursor();
- try {
+ try (final XmlCursor cursor2 = t.newCursor()) {
while (cursor2.toPrevSibling()) {
o = cursor2.getObject();
if (o instanceof CTP || o instanceof CTTbl) {
i++;
}
}
- } finally {
- cursor2.dispose();
}
bodyElements.add(i, newT);
- final XmlCursor cursor3 = t.newCursor();
- try {
+ try(final XmlCursor cursor3 = t.newCursor()) {
cursor.toCursor(cursor3);
cursor.toEndToken();
- } finally {
- cursor3.dispose();
}
return newT;
}
* verifies that cursor is on the right position
*/
private boolean isCursorInHdrF(XmlCursor cursor) {
- XmlCursor verify = cursor.newCursor();
- try {
+ try (XmlCursor verify = cursor.newCursor()) {
verify.toParent();
boolean result = (verify.getObject() == this.headerFooter);
return result;
- } finally {
- verify.dispose();
}
}
tables = new ArrayList<>();
// parse the document with cursor and add
// the XmlObject to its lists
- XmlCursor cursor = headerFooter.newCursor();
- try {
+ try (XmlCursor cursor = headerFooter.newCursor()) {
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject o = cursor.getObject();
bodyElements.add(t);
}
}
- } finally {
- cursor.dispose();
}
}