Browse Source

remove use of some calls to deprecated cursor dispose()

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902065 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_5_2_3
PJ Fanning 1 year ago
parent
commit
b24d8ca779

+ 16
- 31
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFAbstractFootnoteEndnote.java View File

@@ -72,8 +72,7 @@ public abstract class XWPFAbstractFootnoteEndnote implements Iterable<XWPFParag
}

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("./*");
@@ -93,8 +92,6 @@ public abstract class XWPFAbstractFootnoteEndnote implements Iterable<XWPFParag
}

}
} finally {
cursor.dispose();
}
}

@@ -251,8 +248,7 @@ public abstract class XWPFAbstractFootnoteEndnote implements Iterable<XWPFParag
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)) {
@@ -261,8 +257,6 @@ public abstract class XWPFAbstractFootnoteEndnote implements Iterable<XWPFParag
row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
} finally {
cursor.dispose();
}
if (!(o instanceof CTTbl)) {
return null;
@@ -285,13 +279,9 @@ public abstract class XWPFAbstractFootnoteEndnote implements Iterable<XWPFParag
* @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();

}
}

@@ -329,19 +319,17 @@ public abstract class XWPFAbstractFootnoteEndnote implements Iterable<XWPFParag
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;
}
@@ -373,11 +361,8 @@ public abstract class XWPFAbstractFootnoteEndnote implements Iterable<XWPFParag
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();
@@ -385,10 +370,10 @@ public abstract class XWPFAbstractFootnoteEndnote implements Iterable<XWPFParag
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;

+ 30
- 41
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java View File

@@ -201,34 +201,33 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {

// 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);
@@ -724,8 +723,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
* 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
@@ -742,8 +740,6 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
cursor.toCursor(newParaPos);
cursor.toEndToken();
return newP;
} finally {
newParaPos.dispose();
}
}
return null;
@@ -769,8 +765,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
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();
@@ -782,8 +777,6 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
cursor.toCursor(tableCursor);
cursor.toEndToken();
return newT;
} finally {
tableCursor.dispose();
}
}
return null;
@@ -793,11 +786,10 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
* 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) {
@@ -1661,8 +1653,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
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)) {
@@ -1671,8 +1662,6 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
row = (CTRow) o;
cursor.toParent();
o = cursor.getObject();
} finally {
cursor.dispose();
}
if (!(o instanceof CTTbl)) {
return null;

+ 19
- 22
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeader.java View File

@@ -55,8 +55,7 @@ public class XWPFHeader extends XWPFHeaderFooter {

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();
@@ -69,8 +68,6 @@ public class XWPFHeader extends XWPFHeaderFooter {
tables.add(t);
}
}
} finally {
cursor.dispose();
}
}

@@ -99,26 +96,26 @@ public class XWPFHeader extends XWPFHeaderFooter {
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);
}

+ 9
- 36
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java View File

@@ -330,11 +330,8 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
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);
@@ -349,11 +346,8 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
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);
@@ -364,11 +358,8 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
* 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();
@@ -400,11 +391,8 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
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();
@@ -412,12 +400,9 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
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;
}
@@ -449,24 +434,18 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
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;
}
@@ -477,13 +456,10 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
* 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();
}
}

@@ -526,8 +502,7 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
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();
@@ -542,8 +517,6 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo
bodyElements.add(t);
}
}
} finally {
cursor.dispose();
}
}


Loading…
Cancel
Save